def test_pipes_inside_params_or_templates():
    pf = ParserFunction('{{ #if: test | {{ text | aaa }} }}')
    assert [] == pf.parameters
    assert 2 == len(pf.arguments)
    pf = ParserFunction('{{ #if: test | {{{ text | aaa }}} }}')
    assert 1 == len(pf.parameters)
    assert 2 == len(pf.arguments)
Ejemplo n.º 2
0
 def test_pipes_inside_params_or_templates(self):
     pf = ParserFunction('{{ #if: test | {{ text | aaa }} }}')
     self.assertEqual([], pf.parameters)
     self.assertEqual(2, len(pf.arguments))
     pf = ParserFunction('{{ #if: test | {{{ text | aaa }}} }}')
     self.assertEqual(1, len(pf.parameters))
     self.assertEqual(2, len(pf.arguments))
Ejemplo n.º 3
0
def if_(instance: WikiTextHtml,
        parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    condition = get_argument(parser_function, 0)

    if condition.strip():
        parser_function.string = get_argument(parser_function, 1)
    else:
        parser_function.string = get_argument(parser_function, 2)
Ejemplo n.º 4
0
def ifexist(instance: WikiTextHtml,
            parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    page_title = get_argument(parser_function, 0)
    page_exists = instance.page_exists(page_title)

    if page_exists:
        parser_function.string = get_argument(parser_function, 1)
    else:
        parser_function.string = get_argument(parser_function, 2)
Ejemplo n.º 5
0
def ifeq(
    instance: WikiTextHtml,
    parser_function: wikitextparser.ParserFunction,
):
    if len(parser_function.arguments) < 2:
        raise ParserFunctionWrongArgumentCount

    left = get_argument(parser_function, 0)
    right = get_argument(parser_function, 1)

    if left.strip() == right.strip():
        parser_function.string = get_argument(parser_function, 2)
    else:
        parser_function.string = get_argument(parser_function, 3)
Ejemplo n.º 6
0
def test_pformat_pf_directly():
    assert (
        '{{#iferror:\n'
        '    <t a="">\n'
        '    | yes\n'
        '    | no\n'
        '}}') == ParserFunction('{{#iferror:<t a="">|yes|no}}').pformat()
Ejemplo n.º 7
0
 def test_name_and_args(self):
     pf = ParserFunction('{{ #if: test | true | false }}')
     self.assertEqual(' #if', pf.name)
     self.assertEqual(
         [': test ', '| true ', '| false '],
         [a.string for a in pf.arguments]
     )
def test_name_and_args():
    f = ParserFunction('{{ #if: test | true | false }}')
    assert ' #if' == f.name
    args = f.arguments
    assert [': test ', '| true ', '| false '] ==\
           [a.string for a in args]
    assert args[0].name == '1'
    assert args[2].name == '3'
Ejemplo n.º 9
0
 def test_pformat_pf_directly(self):
     self.assertEqual(
         '{{#iferror:\n'
         '    <t a="">\n'
         '    | yes\n'
         '    | no\n'
         '}}',
         ParserFunction('{{#iferror:<t a="">|yes|no}}').pformat())
Ejemplo n.º 10
0
def ifexpr(instance: WikiTextHtml,
           parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    expr = get_argument(parser_function, 0)

    try:
        result = evaluate(expr)
    except EvaluationError as e:
        parser_function.string = f"Expression error: {e.args[0]}"
        return

    if result:
        parser_function.string = get_argument(parser_function, 1)
    else:
        parser_function.string = get_argument(parser_function, 2)
Ejemplo n.º 11
0
def encode(instance: WikiTextHtml, parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) != 1:
        raise ParserFunctionWrongArgumentCount

    url = get_argument(parser_function, 0).strip()

    # All variables should already be HTML escaped, so unescape first,
    # as otherwise we are double encoding them.
    url = urllib.parse.quote(html.unescape(url))

    parser_function.string = url
Ejemplo n.º 12
0
def test_get_lists_deprecation():
    with warns(DeprecationWarning):
        list_ = ParserFunction('{{#if:|*a\n*b}}').get_lists(None)[0]
    with warns(DeprecationWarning):
        list_.get_lists(None)
    with warns(DeprecationWarning):
        list_.sublists(pattern=None)
Ejemplo n.º 13
0
def expr(instance: WikiTextHtml,
         parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    expr = get_argument(parser_function, 0)

    try:
        result = evaluate(expr, as_str=True)
    except EvaluationError as e:
        result = f"Expression error: {e.args[0]}"

    parser_function.string = result
Ejemplo n.º 14
0
def variable(instance: WikiTextHtml,
             parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) != 0:
        raise ParserFunctionWrongArgumentCount

    name = parser_function.name.lower()
    if name == "currentyear":
        parser_function.string = str(datetime.datetime.now().year)
    elif name in ("pagename", "fullpagename", "basepagename"):
        parser_function.string = html.escape(instance.page)
    elif name == "subpagename":
        try:
            parser_function.string = html.escape(
                instance.clean_title(instance.page))
        except InvalidWikiLink as e:
            # Errors always end with a dot, hence the [:-1].
            instance.add_error(
                f'{e.args[0][:-1]} (parserfunction "{parser_function.string}").'
            )
            parser_function.string = ""
    elif name == "namespace":
        parser_function.string = ""
Ejemplo n.º 15
0
def switch(instance: WikiTextHtml, parser_function: wikitextparser.ParserFunction):
    if len(parser_function.arguments) < 1:
        raise ParserFunctionWrongArgumentCount

    branch = get_argument(parser_function, 0).strip()

    explicit_default = None
    positional_default = None
    fallthrough = False

    for argument in parser_function.arguments[1:]:
        # Fallthough cases are positional from our perspective
        if argument.positional:
            name = argument.value.strip()
        else:
            name = argument.name.strip()

        if name == "#default":
            explicit_default = argument.value
        # The last positional argument is the positional default, which is
        # used if no explicit default is set.
        if argument.positional:
            positional_default = argument.value.strip()

        # Find the fallthrough value
        if fallthrough and not argument.positional:
            value = argument.value
            break

        if name == branch:
            if argument.positional:
                fallthrough = True
            else:
                value = argument.value
                break
    else:
        if explicit_default is not None:
            value = explicit_default
        elif positional_default is not None:
            value = positional_default
        else:
            value = ""

    # Only strip if this was not a multiline value
    if value.count("\n") <= 1:
        value = value.strip()
    parser_function.string = value
Ejemplo n.º 16
0
def test_parser_function():
    assert repr(ParserFunction('{{#if:a|{{#if:b|c}}}}').parser_functions[0]) \
        == "ParserFunction('{{#if:b|c}}')"
Ejemplo n.º 17
0
 def test_set_name(self):
     pf = ParserFunction('{{   #if: test | true | false }}')
     pf.name = pf.name.strip(WS)
     self.assertEqual('{{#if: test | true | false }}', pf.string)
Ejemplo n.º 18
0
 def test_args_containing_braces(self):
     self.assertEqual(
         4, len(ParserFunction('{{#pf:\n{|2\n|3\n|}\n}}').arguments))
Ejemplo n.º 19
0
 def test_repr(self):
     pf = ParserFunction('{{#if:a|b}}')
     self.assertEqual(repr(pf), "ParserFunction('{{#if:a|b}}')")
Ejemplo n.º 20
0
 def test_repr(self):
     self.assertEqual(repr(ParserFunction('{{#if:a|b}}')),
                      "ParserFunction('{{#if:a|b}}')")
Ejemplo n.º 21
0
 def test_name_and_args(self):
     ae = self.assertEqual
     f = ParserFunction('{{ #if: test | true | false }}')
     ae(' #if', f.name)
     ae([': test ', '| true ', '| false '], [a.string for a in f.arguments])
Ejemplo n.º 22
0
def test_args_containing_braces():
    assert 4 == len(ParserFunction('{{#pf:\n{|2\n|3\n|}\n}}').arguments)
Ejemplo n.º 23
0
def test_repr():
    assert repr(ParserFunction('{{#if:a|b}}')) ==\
            "ParserFunction('{{#if:a|b}}')"
Ejemplo n.º 24
0
def test_set_name():
    pf = ParserFunction('{{   #if: test | true | false }}')
    pf.name = pf.name.strip(WS)
    assert '{{#if: test | true | false }}' == pf.string
Ejemplo n.º 25
0
def test_tag_containing_pipe():
    assert len(ParserFunction("{{text|a<s |>b</s>c}}").arguments) == 1
 def test_tag_containing_pipe(self):
     self.assertEqual(len(
         ParserFunction("{{text|a<s |>b</s>c}}").arguments), 1)