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)
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))
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)
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)
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)
def test_pformat_pf_directly(): assert ( '{{#iferror:\n' ' <t a="">\n' ' | yes\n' ' | no\n' '}}') == ParserFunction('{{#iferror:<t a="">|yes|no}}').pformat()
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'
def test_pformat_pf_directly(self): self.assertEqual( '{{#iferror:\n' ' <t a="">\n' ' | yes\n' ' | no\n' '}}', ParserFunction('{{#iferror:<t a="">|yes|no}}').pformat())
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)
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
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)
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
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 = ""
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
def test_parser_function(): assert repr(ParserFunction('{{#if:a|{{#if:b|c}}}}').parser_functions[0]) \ == "ParserFunction('{{#if:b|c}}')"
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)
def test_args_containing_braces(self): self.assertEqual( 4, len(ParserFunction('{{#pf:\n{|2\n|3\n|}\n}}').arguments))
def test_repr(self): pf = ParserFunction('{{#if:a|b}}') self.assertEqual(repr(pf), "ParserFunction('{{#if:a|b}}')")
def test_repr(self): self.assertEqual(repr(ParserFunction('{{#if:a|b}}')), "ParserFunction('{{#if:a|b}}')")
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])
def test_args_containing_braces(): assert 4 == len(ParserFunction('{{#pf:\n{|2\n|3\n|}\n}}').arguments)
def test_repr(): assert repr(ParserFunction('{{#if:a|b}}')) ==\ "ParserFunction('{{#if:a|b}}')"
def test_set_name(): pf = ParserFunction('{{ #if: test | true | false }}') pf.name = pf.name.strip(WS) assert '{{#if: test | true | false }}' == pf.string
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)