def test_monthnumber(): wikidb = DictDB( MONTHNUMBER= "{{#if:{{{1|}}}|{{#switch:{{lc:{{{1}}}}}|january|jan=1|february|feb=2|march|mar=3|apr|april=4|may=5|june|jun=6|july|jul=7|august|aug=8|september|sep=9|october|oct=10|november|nov=11|december|dec=12|{{#ifexpr:{{{1}}}<0|{{#ifexpr:(({{{1}}})round 0)!=({{{1}}})|{{#expr:12-(((0.5-({{{1}}}))round 0)mod 12)}}|{{#expr:12-(((11.5-({{{1}}}))round 0)mod 12)}}}}|{{#expr:(((10.5+{{{1}}})round 0)mod 12)+1}}}}}}|Missing required parameter 1=''month''!}}" ) expandstr("{{MONTHNUMBER|12}}", "12", wikidb=wikidb)
def test_parse_comment(): ex = """foo <!-- comment ---> bar""" expanded = expander.expandstr(ex) print "EXPANDED:", expanded assert "\n\n" not in expanded
def test_comment_inside_nowiki(): comment = 'this is a comment' s=expander.expandstr('<pre><!-- this is a comment --></pre>') assert comment in s r=parse(s) txt = r.asText() assert 'this is a comment' in txt
def test_comment_inside_nowiki(): comment = "this is a comment" s = expander.expandstr("<pre><!-- this is a comment --></pre>") assert comment in s r = parse(s) txt = r.asText() assert "this is a comment" in txt
def test_comment_inside_nowiki(): comment = 'this is a comment' s = expander.expandstr('<pre><!-- this is a comment --></pre>') assert comment in s r = parse(s) txt = r.asText() assert 'this is a comment' in txt
def ee(s, expected=None): s = expandstr("{{#expr:%s}}" % (s, )) if isinstance(expected, (float, int, long)): assert math.fabs(float(s) - expected) < 1e-5 elif expected is not None: assert s == expected, "expected %r, got %r" % (expected, s) return s
def ee(s, expected=None): s = expandstr("{{#expr:%s}}" % (s,)) if isinstance(expected, (float, int, long)): assert math.fabs(float(s) - expected) < 1e-5 elif expected is not None: assert s == expected, "expected %r, got %r" % (expected, s) return s
def test_parmpart(): parmpart = """{{#ifeq:/{{{2|}}} |{{#titleparts:/{{{2|}}}|1|{{#expr:1+{{{1|1}}}}}}} | |{{#titleparts:/{{{2|}}}|1|{{#expr:1+{{{1|1}}}}}}} }}""" expandstr("{{ParmPart|0|a/b}}", "", wikidb=DictDB(ParmPart=parmpart)) expandstr("{{ParmPart|1|a/b}}", "a", wikidb=DictDB(ParmPart=parmpart)) expandstr("{{ParmPart|2|a/b}}", "b", wikidb=DictDB(ParmPart=parmpart)) expandstr("{{ParmPart|3|a/b}}", "", wikidb=DictDB(ParmPart=parmpart))
def get_article(self, topic): global usejsoncache global threaded cacheDir = "./json_cache/{}".format(self.wiki_version) try: os.mkdir(cacheDir) except: pass filename = "{}/{}".format(cacheDir, topic) if usejsoncache is True and os.path.isfile(filename): print "cache hit for " + topic js = open(filename).read() else: markup = self.get_markup_for_topic(topic) templParser = mwlib.templ.parser.Parser(markup) templates = templParser.parse() print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" markup = "" for t in templates: # print "==>{} {}<==".format(type(t), t) if isinstance(t, unicode): markup+= t elif isinstance(t, mwlib.templ.nodes.Template): print "==>{}<==".format(t[0]) if t[0] == "Wide image": print " -->{}<--".format(t[1][0]) markup+= " [[File:{}]] ".format(t[1][0]) # print article print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" markup = expandstr(markup) # print markup article = compat.parse_txt(markup) self.reset() if threaded: self.build_related_media(article) else: print "Not performing threaded processing" self.depth_first(article) obj = {"content":self.content, "title":topic, "url":"https://{}.wikipedia.org/wiki/{}".format(self.wiki_version, topic), "url":"https://{}.wikipedia.org/wiki/{}".format(self.wiki_version, topic)} js = json.dumps(obj, indent=2) open(filename, "w").write(js) return js
def test_pipe_inside_imagemap(): """pipes inside image maps should not separate template arguments well, they do not separate arguments with the version running on en.wikipedia.org. they do separate arguments with the version running on pediapress.com:8080. (which is hopefully a newer version) """ db = DictDB(sp="""{{#ifeq: {{{1}}} | 1 | <imagemap> Image:Padlock-silver-medium.svg|20px rect 0 0 1000 1000 [[Wikipedia:Protection policy|This page has been semi-protected from editing]] desc none </imagemap> |bla }} """) result = expandstr("{{sp|1}}", wikidb=db) assert "</imagemap>" in result
def test_birth_date_and_age(): db = DictDB({ "birth date and age": '[[ {{{3|{{{day|{{{3}}}}}}}}}]] [[{{{1|{{{year|{{{1}}}}}}}}}]]<font class="noprint"> (age {{age | {{{1|{{{year|{{{1}}}}}}}}} | {{{2|{{{month|{{{2}}}}}}}}} | {{{3|{{{day|{{{3}}}}}}}}} }})</font>', "age": '<includeonly>{{#expr:({{{4|{{CURRENTYEAR}}}}})-({{{1}}})-(({{{5|{{CURRENTMONTH}}}}})<({{{2}}})or({{{5|{{CURRENTMONTH}}}}})=({{{2}}})and({{{6|{{CURRENTDAY}}}}})<({{{3}}}))}}</includeonly>', }) res = expandstr('{{birth date and age|1960|02|8}}', wikidb=db) print "EXPANDED:", repr(res) import datetime now = datetime.datetime.now() b = datetime.datetime(1960, 2, 8) age = now.year - b.year if now.month * 32 + now.day < b.month * 32 + b.day: age -= 1 expected = u"age %s" % age assert expected in res
def test_pipe_inside_imagemap(): """pipes inside image maps should not separate template arguments well, they do not separate arguments with the version running on en.wikipedia.org. they do separate arguments with the version running on pediapress.com:8080. (which is hopefully a newer version) """ db = DictDB( sp="""{{#ifeq: {{{1}}} | 1 | <imagemap> Image:Padlock-silver-medium.svg|20px rect 0 0 1000 1000 [[Wikipedia:Protection policy|This page has been semi-protected from editing]] desc none </imagemap> |bla }} """) result = expandstr("{{sp|1}}", wikidb=db) assert "</imagemap>" in result
def test_noinclude_end(): expandstr("{{foo}}", "foo", wikidb=DictDB(foo="foo<noinclude>bar should not be in expansion"))
def e(a, b): return expandstr(a, b, pagename=u'L\xe9onie s')
def test_convert_ft_in_m_int(): expandstr("{{convert|12|ft|m}}", "12 feet (3.7 m)\n", wikidb=getdb())
def test_round_plus_2(): expandstr("{{rnd/+|1.056|2|5}}", "5", wikidb=getdb())
def test_precision_plus_1(): expandstr("{{precision/+|0.77}}", "2", wikidb=getdb())
def test_switch_numeric_comparison(): expandstr("{{ #switch: +07 | 7 = Yes | 007 = Bond | No }}", "Yes")
def test_round(): expandstr("{{rnd|2.0004|3}}", "2.000", wikidb=getdb()) expandstr("{{rnd|0.000020004|8}}", "2.0E-5000", wikidb=getdb()) expandstr("{{rnd|0|8}}", "0.00000000", wikidb=getdb())
def expand_talk(tpl, expected): return expandstr('{{%s:%s}}' % (tpl, 'Benutzer Diskussion:Anonymous user!/sandbox/my page'), expected, pagename='Help:Irrelevant')
def expand_page(tpl, expected): return expandstr('{{%s}}' % tpl, expected, pagename='Benutzer:Anonymous user!/sandbox/my page')
def test_equal_inside_link(): db = DictDB(t1="{{{1}}}") expandstr("{{t1|[[abc|foo=5]]}}", "[[abc|foo=5]]", wikidb=db)
def test_preserve_space_in_tag(): expandstr("{{#tag:imagemap|cd }}", "<imagemap>cd </imagemap>")
def test_monthnumber(): wikidb = DictDB(MONTHNUMBER="{{#if:{{{1|}}}|{{#switch:{{lc:{{{1}}}}}|january|jan=1|february|feb=2|march|mar=3|apr|april=4|may=5|june|jun=6|july|jul=7|august|aug=8|september|sep=9|october|oct=10|november|nov=11|december|dec=12|{{#ifexpr:{{{1}}}<0|{{#ifexpr:(({{{1}}})round 0)!=({{{1}}})|{{#expr:12-(((0.5-({{{1}}}))round 0)mod 12)}}|{{#expr:12-(((11.5-({{{1}}}))round 0)mod 12)}}}}|{{#expr:(((10.5+{{{1}}})round 0)mod 12)+1}}}}}}|Missing required parameter 1=''month''!}}") expandstr("{{MONTHNUMBER|12}}", "12", wikidb=wikidb)
def test_switch_default_template(): expandstr("{{#switch:1|{{#if:1|5|12}}}}", "5")
def test_urlencode_non_ascii(): expandstr(u'{{urlencode:L\xe9onie}}', 'L%C3%A9onie')
def test_localurle_umlaut(): """http://code.pediapress.com/wiki/ticket/473""" r = expandstr(u"{{LOCALURLE:F\xfcbar}}") assert r.endswith('/F%C3%BCbar')
def test_anchorencode(): """http://code.pediapress.com/wiki/ticket/213""" expandstr('{{anchorencode:x #y @}}', 'x_.23y_.40')
def test_five(): txt = "text of the tnext template" db = DictDB( t1="{{{{{1}}}}}", tnext=txt) expandstr("{{t1|tnext}}", expected=txt, wikidb=db)
def test_anchorencode_non_ascii(): expandstr(u"{{anchorencode:\u0107}}", ".C4.87")
def expand_talk(tpl, expected): return expandstr('{{%s}}' % tpl, expected, pagename='Benutzer Diskussion:Anonymous user!/sandbox/my page')
def test_fullurl(): expandstr('{{fullurl:x y @}}', 'http://en.wikipedia.org/wiki/X_y_%40')
def test_switch_empty_fallback(): expandstr("{{#switch:||foo=good}}", "good")
def test_fullurl_nonascii(): expandstr(u'{{fullurl:L\xe9onie}}', 'http://en.wikipedia.org/wiki/L%C3%A9onie')
def test_switch_case_sensitive2(): expandstr("{{ #switch: A | a=lower | UPPER }}", "UPPER")
def test_server(): expandstr('{{server}}', 'http://en.wikipedia.org')
def test_max_2(): expandstr("{{max/2|-1|1}}", "1", wikidb=getdb()) expandstr("{{max/2|1|-1}}", "1", wikidb=getdb())
def test_servername(): expandstr('{{servername}}', 'en.wikipedia.org')
def test_round_plus_3(): expandstr("{{rnd/+|1.056|2|abc}}", "1.06", wikidb=getdb())
def e(a, b): return expandstr(a, b, wikidb=wikidb)
def test_convert_ft_in_m_float(): expandstr("{{convert|2.5|ft|m}}", "2.5 feet (0.76 m)\n", wikidb=getdb())
def test_variable_alternative(): wikidb = DictDB(t1='{{{var|undefined}}}') expandstr('{{t1|var=}}', '', wikidb=wikidb)
def test_round_minus(): expandstr("{{rnd/-|0.00002|8}}", "2.0E-5000", wikidb=getdb())
def test_implicit_newline_after_expand(): wikidb = DictDB(tone='{{{1}}}{{{2}}}') expandstr('foo {{tone||:}} bar', 'foo \n: bar', wikidb=wikidb)