Hi, developers! I ran into a problem when displaying the centuries in the template http://ru.wikipedia.org/w/index.php?title=%D0%A8%D0%B0%D0%B1%D0%BB%D0%BE%D0%BD:%D0%92%D0%B5%D0%BA&action=edit
The problem is that the library is not processed by the flag "xr" for the format strings in #time. http://www.mediawiki.org/wiki/Help:Extension:ParserFunctions#.23time (at the end of table) I added two of the test cases and corrected the mwlib.templ.magic_time. diff --git a/mwlib/templ/magic_time.py b/mwlib/templ/magic_time.py index 69c461c..a7b7d10 100644 --- a/mwlib/templ/magic_time.py +++ b/mwlib/templ/magic_time.py @@ -2,6 +2,7 @@ import sys import datetime import re import calendar +import roman from timelib import strtodatetime as parsedate from mwlib.strftime import strftime @@ -12,7 +13,7 @@ def ampm(date): else: return "pm" -rx = re.compile('"[^"]*"|\\\\.|.') +rx = re.compile('"[^"]*"|x[rnN]|\\\\.|.') codemap = dict( y = '%y', Y = '%Y', @@ -41,11 +42,13 @@ codemap = dict( c = "%Y-%m-%dT%H:%M:%S+00:00", r = "%a, %d %b %Y %H:%M:%S +0000", t = lambda d: str(calendar.monthrange(d.year, d.month)[1]), + xr = ("process_next", lambda n: roman.toRoman(int(n))), ) def formatdate(format, date): split = rx.findall(format) + process_next = None tmp = [] for x in split: @@ -58,11 +61,22 @@ def formatdate(format, date): else: tmp.append(x) else: + if isinstance(f, tuple): + process_next = f[1] + continue + if isinstance(f, basestring): - tmp.append(strftime(date, f)) + res = strftime(date, f) else: - tmp.append(f(date)) + res = f(date) + if process_next: + try: + res = process_next(res) + except: + pass + process_next = None + tmp.append(res) tmp = u"".join(tmp).strip() return tmp @@ -75,7 +89,7 @@ def time(format, datestring=None): date = datetime.datetime.now().replace(hour=int(datestring[:2]), minute=int(datestring[2:]), second=0) except ValueError: pass - + if date is None: try: date = parsedate(datestring) @@ -87,8 +101,9 @@ def time(format, datestring=None): if date is None: return u'<strong class="error">Error: invalid time</ strong>' - + if date is None: date = datetime.datetime.now() return formatdate(format, date) + diff --git a/setup.py b/setup.py index 0f89fbd..9a48283 100755 --- a/setup.py +++ b/setup.py @@ -64,7 +64,7 @@ def main(): if os.path.exists(distutils.util.convert_path('Makefile')): build_deps() # this is a hg checkout. - install_requires=["pyparsing>=1.4.11", "odfpy>=0.9, <0.10", "flup>=1.0", "twisted>=8.2", "lockfile>=0.8", "timelib>=0.2", "WebOb>=0.9", "pyPdf>=1.12"] + install_requires=["pyparsing>=1.4.11", "odfpy>=0.9, <0.10", "flup>=1.0", "twisted>=8.2", "lockfile>=0.8", "timelib>=0.2", "WebOb>=0.9", "pyPdf>=1.12", "roman"] if sys.version_info[:2] < (2,5): install_requires.append("wsgiref>=0.1.2") install_requires.append("elementtree>=1.2.6") diff --git a/tests/test_expander_time.py b/tests/test_expander_time.py index 60d7d9f..635d830 100755 --- a/tests/test_expander_time.py +++ b/tests/test_expander_time.py @@ -72,6 +72,9 @@ def test_codes(): yield e, "r", "Sat, 09 Feb 2008 10:55:17 +0000" + yield e, "xrY", "MMVIII" + yield e, "xrU", "XVI", "1970-1-1 + 16 second" + def test_examples(): -- You received this message because you are subscribed to the Google Groups "mwlib" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/mwlib?hl=en.
