コード例 #1
0
 def test_overwriting_template_args(self):
     t = Template('{{t|a|b|c}}')
     c = t.arguments[-1]
     self.assertEqual('|c', c.string)
     t.string = '{{t|0|a|b|c}}'
     self.assertEqual('', c.string)
     self.assertEqual('0', t.get_arg('1').value)
     self.assertEqual('c', t.get_arg('4').value)
コード例 #2
0
def test_overwriting_template_args():
    t = Template('{{t|a|b|c}}')
    c = t.arguments[-1]
    assert '|c' == c.string
    t.string = '{{t|0|a|b|c}}'
    assert '' == c.string
    assert '0' == t.get_arg('1').value
    assert 'c' == t.get_arg('4').value
コード例 #3
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_has_arg(self):
     t = Template('{{t|a|b=c}}')
     self.assertEqual(True, t.has_arg('1'))
     self.assertEqual(True, t.has_arg('1', 'a'))
     self.assertEqual(True, t.has_arg('b'))
     self.assertEqual(True, t.has_arg('b', 'c'))
     self.assertEqual(False, t.has_arg('2'))
     self.assertEqual(False, t.has_arg('1', 'b'))
     self.assertEqual(False, t.has_arg('c'))
     self.assertEqual(False, t.has_arg('b', 'd'))
コード例 #4
0
 def test_ancestors_and_parent(self):
     parsed = parse('{{a|{{#if:{{b{{c<!---->}}}}}}}}')
     self.assertEqual(parsed.parent(), None)
     self.assertEqual(parsed.ancestors(), [])
     c = parsed.comments[0]
     c_parent = c.parent()
     self.assertEqual(c_parent.string, '{{c<!---->}}')
     self.assertEqual(c_parent.parent().string, '{{b{{c<!---->}}}}')
     self.assertEqual(len(c.ancestors()), 4)
     self.assertEqual(len(c.ancestors(type_='Template')), 3)
     self.assertEqual(len(c.ancestors(type_='ParserFunction')), 1)
     t = Template('{{a}}')
     self.assertEqual(t.ancestors(), [])
     self.assertIsNone(t.parent())
コード例 #5
0
def test_ancestors_and_parent():
    parsed = parse('{{a|{{#if:{{b{{c<!---->}}}}}}}}')
    assert parsed.parent() is None
    assert parsed.ancestors() == []
    c = parsed.comments[0]
    c_parent = c.parent()
    assert c_parent.string == '{{c<!---->}}'
    assert c_parent.parent().string == '{{b{{c<!---->}}}}'
    assert len(c.ancestors()) == 4
    assert len(c.ancestors(type_='Template')) == 3
    assert len(c.ancestors(type_='ParserFunction')) == 1
    t = Template('{{a}}')
    assert t.ancestors() == []
    assert t.parent() is None
コード例 #6
0
 def test_multiline_arg(self):
     t = Template('{{text|\na=\nb\nc\n}}')
     t.set_arg('d', 'e')
     self.assertEqual('{{text|\na=\nb\nc|\nd=\ne\n}}', t.string)
     t = Template('{{text\n\n | a = b\n\n}}')
     t.set_arg('c', 'd')
     self.assertEqual('{{text\n\n | a = b\n\n | c = d\n\n}}', t.string)
コード例 #7
0
def test_multiline_arg():
    t = Template('{{text|\na=\nb\nc\n}}')
    t.set_arg('d', 'e')
    assert '{{text|\na=\nb\nc|\nd=\ne\n}}' == t.string
    t = Template('{{text\n\n | a = b\n\n}}')
    t.set_arg('c', 'd')
    assert '{{text\n\n | a = b\n\n | c = d\n\n}}' == t.string
コード例 #8
0
def test_normal_name():
    normal_name = Template('{{ u |a}}').normal_name
    assert 'u' == normal_name()
    assert 'U' == normal_name(capitalize=True)
    warns(DeprecationWarning, normal_name, capital_links=True)
    assert 'u' == Template('{{ template: u |a}}').normal_name()
    assert 'u' == Template('{{ الگو:u |a}}').normal_name(['الگو'])
    assert 'a b' == Template('{{a_b}}').normal_name()
    assert 't' == Template('{{t#a|a}}').normal_name()
    t = Template('{{ : fa : Template : t  # A }}')
    assert 't' == t.normal_name(code='fa')
    warns(DeprecationWarning, t.normal_name, _code='fa')
    assert 't' == Template('{{ : t |a}}').normal_name()
    assert 'A B' == Template('{{A___B}}').normal_name()
コード例 #9
0
 def test_pformat_tl_directly(self):
     self.assertEqual(
         '{{t\n'
         '    | 1 = a\n'
         '}}',
         Template('{{t|a}}').pformat(),
     )
コード例 #10
0
ファイル: template.py プロジェクト: Xaroth/wikitexthtml
def _render_template(instance: WikiTextHtml, template: wikitextparser.Template):
    name = template.name.strip()

    try:
        if not instance.template_exists(name):
            instance.add_error(f'Template "{name}" does not exist.')
    except InvalidTemplate as e:
        # Errors always end with a dot, hence the [:-1].
        instance.add_error(f'{e.args[0][:-1]} (template "{{{{{name}}}}}").')
        return

    instance._templates.add(name)
    if instance._template_path.count(name) >= 10:
        instance.add_error(
            f'Template "{name}" was transcluded itself more than ten times (recursion depth limit reached).'
        )
        return
    instance._template_path.append(name)

    body = instance.template_load(name)
    body = preprocess.begin(instance, body, is_transcluding=True)
    body = preprocess.hr_line(instance, body)

    wtp = wikitextparser.parse(body)

    parameter.replace(instance, wtp, template.arguments)
    parser_function.replace(instance, wtp)
    replace(instance, wtp)

    template.string = wtp.string

    instance._template_path.remove(name)
コード例 #11
0
 def test_normal_name(self):
     ae = self.assertEqual
     aw = self.assertWarns
     normal_name = Template('{{ u |a}}').normal_name
     ae('u', normal_name())
     ae('U', normal_name(capitalize=True))
     aw(DeprecationWarning, normal_name, capital_links=True)
     ae('u', Template('{{ template: u |a}}').normal_name())
     ae('u', Template('{{ الگو:u |a}}').normal_name(['الگو']))
     ae('a b', Template('{{a_b}}').normal_name())
     ae('t', Template('{{t#a|a}}').normal_name())
     t = Template('{{ : fa : Template : t  # A }}')
     ae('t', t.normal_name(code='fa'))
     aw(DeprecationWarning, t.normal_name, _code='fa')
     ae('t', Template('{{ : t |a}}').normal_name())
     ae('A B', Template('{{A___B}}').normal_name())
コード例 #12
0
def test_has_arg():
    has_arg = Template('{{t|a|b=c}}').has_arg
    assert has_arg('1') is True
    assert has_arg('1', 'a') is True
    assert has_arg('b') is True
    assert has_arg('b', 'c') is True
    assert has_arg('2') is False
    assert has_arg('1', 'b') is False
    assert has_arg('c') is False
    assert has_arg('b', 'd') is False
コード例 #13
0
 def test_has_arg(self):
     ae = self.assertEqual
     has_arg = Template('{{t|a|b=c}}').has_arg
     ae(True, has_arg('1'))
     ae(True, has_arg('1', 'a'))
     ae(True, has_arg('b'))
     ae(True, has_arg('b', 'c'))
     ae(False, has_arg('2'))
     ae(False, has_arg('1', 'b'))
     ae(False, has_arg('c'))
     ae(False, has_arg('b', 'd'))
コード例 #14
0
ファイル: category_parser.py プロジェクト: nheist/CaLiGraph
def _get_template_content(template: Template, template_definitions: dict,
                          visited_templates: set) -> Tuple[Optional[str], set]:
    if not template or not template.string.startswith('{{'):
        return None, visited_templates
    try:
        name = template.normal_name(capitalize=True)
    except IndexError:
        return '', visited_templates
    if name in visited_templates:
        return '', visited_templates
    visited_templates.add(name)

    content = wtp.parse(template_definitions[name])
    content = _apply_parameters(content, _get_template_arguments(template))
    for it in content.templates:
        it_content, _ = _get_template_content(it, template_definitions,
                                              visited_templates)
        if it_content is not None:
            content[slice(*it.span)] = it_content
    return content.string, visited_templates
コード例 #15
0
ファイル: wikiFile.py プロジェクト: tholzheim/wikirender
    def update_arguments(template: Template, args: dict, overwrite=False):
        """
        Updates the arguments of the given template with the values of the given dict (args)

        Args:
            template: Template that should be updated
            args(dict): Dict containing the arguments that should be set. key=argument name, value=argument value
            overwrite(bool): If True existing values will be overwritten

        Returns:
            Nothing
        """
        for key, value in args.items():
            if template.has_arg(key):
                # update argument
                if overwrite:
                    template.del_arg(key)
                    template.set_arg(key, value)
                else:
                    pass
            else:
                if value is not None:
                    template.set_arg(key, value, preserve_spacing=False)
コード例 #16
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_rm_first_of_dup_args(self):
     # Remove first of duplicates, keep last
     t = Template('{{template|year=9999|year=2000}}')
     t.rm_first_of_dup_args()
     self.assertEqual('{{template|year=2000}}', str(t))
     # Don't remove duplicate positional args in different positions
     s = """{{cite|{{t1}}|{{t1}}}}"""
     t = Template(s)
     t.rm_first_of_dup_args()
     self.assertEqual(s, str(t))
     # Don't remove duplicate subargs
     s1 = "{{i| c = {{g}} |p={{t|h={{g}}}} |q={{t|h={{g}}}}}}"
     t = Template(s1)
     t.rm_first_of_dup_args()
     self.assertEqual(s1, str(t))
     # test_dont_touch_empty_strings
     s1 = '{{template|url=||work=|accessdate=}}'
     s2 = '{{template|url=||work=|accessdate=}}'
     t = Template(s1)
     t.rm_first_of_dup_args()
     self.assertEqual(s2, str(t))
     # Positional args
     t = Template('{{t|1=v|v}}')
     t.rm_first_of_dup_args()
     self.assertEqual('{{t|v}}', str(t))
     # Triple duplicates:
     t = Template('{{t|1=v|v|1=v}}')
     t.rm_first_of_dup_args()
     self.assertEqual('{{t|1=v}}', str(t))
コード例 #17
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_keyword_and_positional_args(self):
     t = Template("{{t|kw=a|1=|pa|kw2=a|pa2}}")
     self.assertEqual('1', t.arguments[2].name)
コード例 #18
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_normal_name(self):
     t = Template('{{ u |a}}')
     self.assertEqual('u', t.normal_name())
     self.assertEqual('U', t.normal_name(capital_links=True))
     t = Template('{{ template: u |a}}')
     self.assertEqual('u', t.normal_name())
     t = Template('{{ الگو:u |a}}')
     self.assertEqual('u', t.normal_name(['الگو']))
     t = Template('{{a_b}}')
     self.assertEqual('a b', t.normal_name())
     t = Template('{{t#a|a}}')
     self.assertEqual('t', t.normal_name())
     t = Template('{{ : fa : Template : t  # A }}')
     self.assertEqual('t', t.normal_name(code='fa'))
     t = Template('{{ : t |a}}')
     self.assertEqual('t', t.normal_name())
     t = Template('{{A___B}}')
     self.assertEqual('A B', t.normal_name())
コード例 #19
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_set_name(self):
     t = Template("{{t|a|a}}")
     t.name = ' u '
     self.assertEqual("{{ u |a|a}}", t.string)
コード例 #20
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_dont_remove_nonkeyword_argument(self):
     t = Template("{{t|a|a}}")
     self.assertEqual("{{t|a|a}}", str(t))
コード例 #21
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_name(self):
     s1 = "{{ wrapper | p1 | {{ cite | sp1 | dateformat = ymd}} }}"
     t = Template(s1)
     self.assertEqual(' wrapper ', t.name)
コード例 #22
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_contains_newlines(self):
     s = '{{template\n|s=2}}'
     t = Template(s)
     self.assertEqual(s, str(t))
コード例 #23
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_not_name_and_positional_is_none(self):
     t = Template('{{t}}')
     t.set_arg(None, 'v')
     self.assertEqual('{{t|v}}', t.string)
コード例 #24
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_nowiki_makes_equal_ineffective(self):
     t = Template('{{text|1<nowiki>=</nowiki>g}}')
     a = t.arguments[0]
     self.assertEqual(a.value, '1<nowiki>=</nowiki>g')
     self.assertEqual(a.name, '1')
コード例 #25
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_no_parameters(self):
     s = '{{template}}'
     t = Template(s)
     self.assertEqual("Template('{{template}}')", repr(t))
コード例 #26
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_force_new_to_positional_when_old_is_keyword(self):
     t = Template('{{t|1=v}}')
     t.set_arg('1', 'v', positional=True)
     self.assertEqual('{{t|v}}', t.string)
コード例 #27
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_invalid_position(self):
     t = Template('{{t}}')
     t.set_arg('2', 'a', positional=True)
     self.assertEqual('{{t|2=a}}', t.string)
コード例 #28
0
def test_equal_sign_in_val():
    a, c = Template('{{t|a==b|c}}').arguments
    assert a.value == '=b'
    assert c.name == '1'
コード例 #29
0
def test_removing_last_arg_should_not_effect_the_others():
    a, b, c = Template('{{t|1=v|v|1=v}}').arguments
    del c[:]
    assert '|1=v' == a.string
    assert '|v' == b.string
コード例 #30
0
ファイル: template_test.py プロジェクト: sgml/wikitextparser
 def test_multi_set_positional_args(self):
     t = Template('{{t}}')
     t.set_arg('1', 'p', positional=True)
     t.set_arg('2', 'q', positional=True)
     self.assertEqual('{{t|p|q}}', t.string)