Пример #1
0
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)
Пример #2
0
def test_parse_comment():
    ex = """foo
<!-- comment --->
bar"""
    expanded = expander.expandstr(ex)
    print "EXPANDED:", expanded
    assert "\n\n" not in expanded
Пример #3
0
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
Пример #4
0
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
Пример #5
0
def test_parse_comment():
    ex = """foo
<!-- comment --->
bar"""
    expanded = expander.expandstr(ex)
    print "EXPANDED:", expanded
    assert "\n\n" not in expanded
Пример #6
0
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
Пример #7
0
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
Пример #8
0
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
Пример #9
0
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))
Пример #10
0
    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
Пример #11
0
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
Пример #12
0
def test_birth_date_and_age():
    db = DictDB({
        "birth date and age": '[[ {{{3|{{{day|{{{3}}}}}}}}}]] [[{{{1|{{{year|{{{1}}}}}}}}}]]<font class="noprint"> (age&nbsp;{{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&nbsp;%s" % age
    assert expected in res
Пример #13
0
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
Пример #14
0
def test_noinclude_end():
    expandstr("{{foo}}", "foo", wikidb=DictDB(foo="foo<noinclude>bar should not be in expansion"))
Пример #15
0
 def e(a, b):
     return expandstr(a, b, pagename=u'L\xe9onie s')
Пример #16
0
def test_convert_ft_in_m_int():
    expandstr("{{convert|12|ft|m}}", "12&nbsp;feet (3.7&nbsp;m)\n", wikidb=getdb())
Пример #17
0
def test_round_plus_2():
    expandstr("{{rnd/+|1.056|2|5}}", "5", wikidb=getdb())
Пример #18
0
def test_precision_plus_1():
    expandstr("{{precision/+|0.77}}", "2", wikidb=getdb())
Пример #19
0
def test_switch_numeric_comparison():
    expandstr("{{ #switch: +07 | 7 = Yes | 007 = Bond | No }}", "Yes")
Пример #20
0
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())
Пример #21
0
 def expand_talk(tpl, expected):
     return expandstr('{{%s:%s}}' % (tpl, 'Benutzer Diskussion:Anonymous user!/sandbox/my page'),
                      expected, pagename='Help:Irrelevant')
Пример #22
0
 def expand_page(tpl, expected):
     return expandstr('{{%s}}' % tpl, expected,
                      pagename='Benutzer:Anonymous user!/sandbox/my page')
Пример #23
0
def test_equal_inside_link():
    db = DictDB(t1="{{{1}}}")
    expandstr("{{t1|[[abc|foo=5]]}}", "[[abc|foo=5]]", wikidb=db)
Пример #24
0
def test_preserve_space_in_tag():
    expandstr("{{#tag:imagemap|cd }}", "<imagemap>cd </imagemap>")
Пример #25
0
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)
Пример #26
0
def test_switch_default_template():
    expandstr("{{#switch:1|{{#if:1|5|12}}}}", "5")
Пример #27
0
def test_urlencode_non_ascii():
    expandstr(u'{{urlencode:L\xe9onie}}', 'L%C3%A9onie')
Пример #28
0
def test_localurle_umlaut():
    """http://code.pediapress.com/wiki/ticket/473"""
    r = expandstr(u"{{LOCALURLE:F\xfcbar}}")
    assert r.endswith('/F%C3%BCbar')
Пример #29
0
def test_anchorencode():
    """http://code.pediapress.com/wiki/ticket/213"""
    expandstr('{{anchorencode:x #y @}}', 'x_.23y_.40')
Пример #30
0
def test_five():
    txt = "text of the tnext template"
    db = DictDB(
        t1="{{{{{1}}}}}",
        tnext=txt)
    expandstr("{{t1|tnext}}", expected=txt, wikidb=db)
Пример #31
0
def test_anchorencode_non_ascii():
    expandstr(u"{{anchorencode:\u0107}}", ".C4.87")
Пример #32
0
 def expand_talk(tpl, expected):
     return expandstr('{{%s}}' % tpl, expected,
                      pagename='Benutzer Diskussion:Anonymous user!/sandbox/my page')
Пример #33
0
def test_fullurl():
    expandstr('{{fullurl:x y @}}', 'http://en.wikipedia.org/wiki/X_y_%40')
Пример #34
0
def test_switch_empty_fallback():
    expandstr("{{#switch:||foo=good}}", "good")
Пример #35
0
def test_fullurl_nonascii():
    expandstr(u'{{fullurl:L\xe9onie}}', 'http://en.wikipedia.org/wiki/L%C3%A9onie')
Пример #36
0
def test_switch_case_sensitive2():
    expandstr("{{ #switch: A | a=lower | UPPER }}", "UPPER")
Пример #37
0
def test_server():
    expandstr('{{server}}', 'http://en.wikipedia.org')
Пример #38
0
def test_max_2():
    expandstr("{{max/2|-1|1}}", "1", wikidb=getdb())
    expandstr("{{max/2|1|-1}}", "1", wikidb=getdb())
Пример #39
0
def test_servername():
    expandstr('{{servername}}', 'en.wikipedia.org')
Пример #40
0
def test_round_plus_3():
    expandstr("{{rnd/+|1.056|2|abc}}", "1.06", wikidb=getdb())
Пример #41
0
 def e(a, b):
     return expandstr(a, b, wikidb=wikidb)
Пример #42
0
def test_convert_ft_in_m_float():
    expandstr("{{convert|2.5|ft|m}}", "2.5&nbsp;feet (0.76&nbsp;m)\n", wikidb=getdb())
Пример #43
0
def test_variable_alternative():
    wikidb = DictDB(t1='{{{var|undefined}}}')
    expandstr('{{t1|var=}}', '', wikidb=wikidb)
Пример #44
0
def test_round_minus():
    expandstr("{{rnd/-|0.00002|8}}", "2.0E-5000", wikidb=getdb())
Пример #45
0
def test_implicit_newline_after_expand():
    wikidb = DictDB(tone='{{{1}}}{{{2}}}')
    expandstr('foo {{tone||:}} bar', 'foo \n: bar', wikidb=wikidb)