コード例 #1
0
def _list_to_html(instance: WikiTextHtml, list: wikitextparser.WikiList):
    # We skipped a level; add the missing level (wikitextparser will reload
    # the node for us).
    if not list.items[0] or list.items[0][0] in LIST_TYPES:
        list.string = f"{list.fullitems[0][list.level - 1] * list.level} \n{list.string}"

    current_list_tag = None
    content = ""

    for i in range(len(list.items)):
        # Soecial case for definition lists. Here you can have:
        # :;Item:Definition
        # This results in an unbalanced list, where one item is a level 2, and
        # the next item is a level 1.
        # This only happens if:
        # - This is not the first item
        # - This is level 2 or higher
        # - There isn't a list marker on the expected position
        # - The item before us indicates it is a definition list (;)
        # - We start with definition marker (:)
        if (
            i > 0
            and list.level > 1
            and (len(list.fullitems[i]) < 2 or list.fullitems[i][list.level - 1] not in LIST_TYPES)
            and list.fullitems[i - 1][list.level - 1] == ";"
            and list.fullitems[i][0] == ":"
        ):
            list_tag, item_tag = get_list_item_tag(":")
        else:
            list_tag, item_tag = get_list_item_tag(list.fullitems[i][list.level - 1])

        if current_list_tag != list_tag:
            if current_list_tag:
                content += f"</{current_list_tag}>"
            content += f"<{list_tag}>"
            current_list_tag = list_tag

        item = list.items[i]
        if item.count("\n") < 2:
            item = item.strip()
        elif item.endswith("\n"):
            item = item[:-1]

        content += f"<{item_tag}>\n"
        if item:
            content += f"{item}\n"

        for sublist in list.sublists(i):
            content += _list_to_html(instance, sublist)

        content += f"</{item_tag}>"

    content += f"</{current_list_tag}>\n"
    return content
コード例 #2
0
def test_convert():
    wl = WikiList(':*A1\n'
                  ':*#B1\n'
                  ':*#B2\n'
                  ':*:continuing A1\n'
                  ':*A2',
                  pattern=r':\*')
    assert wl.level == 2
    wl.convert('#')
    assert wl.string == ('#A1\n' '##B1\n' '##B2\n' '#:continuing A1\n' '#A2')
    assert wl.pattern == r'\#'
    assert wl.level == 1
コード例 #3
0
 def test_convert(self):
     ae = self.assertEqual
     wl = WikiList(':*A1\n'
                   ':*#B1\n'
                   ':*#B2\n'
                   ':*:continuing A1\n'
                   ':*A2',
                   pattern=r':\*')
     ae(wl.level, 2)
     wl.convert('#')
     ae(wl.string, '#A1\n' '##B1\n' '##B2\n' '#:continuing A1\n' '#A2')
     ae(wl.pattern, r'\#')
     ae(wl.level, 1)
コード例 #4
0
def test_subitems_for_the_first_item():
    wl = WikiList('# 0\n'
                  '## 0.0\n'
                  '## 0.1\n'
                  '#* 0.0\n'
                  '# 2\n',
                  pattern=r'\#')
    items = wl.items[0]
    assert items == ' 0'
    subitems0 = wl.sublists(0, r'\#')[0]
    assert subitems0.items == [' 0.0', ' 0.1']
    # Test to see that arguments are optional.
    sublists = wl.sublists()
    assert sublists[1].fullitems == ['#* 0.0\n']
    assert sublists[0].items == [' 0.0', ' 0.1']
コード例 #5
0
 def test_subitems_for_the_first_item(self):
     ae = self.assertEqual
     wl = WikiList('# 0\n'
                   '## 0.0\n'
                   '## 0.1\n'
                   '#* 0.0\n'
                   '# 2\n',
                   pattern=r'\#')
     items = wl.items[0]
     ae(items, ' 0')
     subitems0 = wl.sublists(0, r'\#')[0]
     ae(subitems0.items, [' 0.0', ' 0.1'])
     # Test to see that arguments are optional.
     sublists = wl.sublists()
     ae(sublists[1].fullitems, ['#* 0.0\n'])
     ae(sublists[0].items, [' 0.0', ' 0.1'])
コード例 #6
0
def test_commented_list_item():
    """One of the list items is commented through the wikitext."""
    wl = WikiList('#1\n'
                  '##1-1\n'
                  '##1-2<!-- \n'
                  '##1-3\n'
                  ' -->\n'
                  '#2\n',
                  pattern=r'\#')
    assert wl.items[1] == '2'
コード例 #7
0
def test_travese_mixed_list_completely():
    wl = WikiList(
        '* Or create mixed lists\n'
        '*# and nest them\n'
        '*#* like this\n'
        '*#*; definitions\n'
        '*#*: work:\n'
        '*#*; apple\n'
        '*#*; banana\n'
        '*#*: fruits',
        pattern=r'\*')
    assert wl.items == [' Or create mixed lists']
    swl = wl.sublists(0, r'\#')[0]
    assert swl.items == [' and nest them']
    sswl = swl.sublists(0, r'\*')[0]
    assert sswl.items == [' like this']
    ssswl = sswl.sublists(0, '[;:]')[0]
    assert ssswl.items == [
        ' definitions', ' work:', ' apple', ' banana', ' fruits'
    ]
コード例 #8
0
 def test_commented_list_item(self):
     """One of the list items is commented through the wikitext."""
     wl = WikiList(
         '#1\n'
         '##1-1\n'
         '##1-2<!-- \n'
         '##1-3\n'
         ' -->\n'
         '#2\n',
         pattern=r'\#')
     self.assertEqual(wl.items[1], '2')
コード例 #9
0
 def test_travese_mixed_list_completely(self):
     ae = self.assertEqual
     wl = WikiList(
         '* Or create mixed lists\n'
         '*# and nest them\n'
         '*#* like this\n'
         '*#*; definitions\n'
         '*#*: work:\n'
         '*#*; apple\n'
         '*#*; banana\n'
         '*#*: fruits',
         pattern=r'\*')
     ae(wl.items, [' Or create mixed lists'])
     swl = wl.sublists(0, r'\#')[0]
     ae(swl.items, [' and nest them'])
     sswl = swl.sublists(0, r'\*')[0]
     ae(sswl.items, [' like this'])
     ssswl = sswl.sublists(0, '[;:]')[0]
     ae(ssswl.items,
        [' definitions', ' work:', ' apple', ' banana', ' fruits'])
コード例 #10
0
ファイル: article_parser.py プロジェクト: nheist/CaLiGraph
def _extract_enum(l: wtp.WikiList) -> list:
    entries = []
    for item_idx, item_text in enumerate(l.items):
        plaintext, entities = _convert_markup(item_text)
        sublists = l.sublists(item_idx)
        entries.append({
            'text': plaintext,
            'depth': l.level,
            'leaf': len(sublists) == 0,
            'entities': entities
        })
        for sl in sublists:
            entries.extend(_extract_enum(sl))
    return entries
コード例 #11
0
def test_mixed_definition_lists():
    wl = WikiList(
        '; Mixed definition lists\n'
        '; item 1 : definition\n'
        ':; sub-item 1 plus term\n'
        ':: two colons plus definition\n'
        ':; sub-item 2 : colon plus definition\n'
        '; item 2 \n'
        ': back to the main list\n',
        pattern=r'[:;]\s*')
    assert wl.items == [
        'Mixed definition lists', 'item 1 ', ' definition', 'item 2 ',
        'back to the main list'
    ]
コード例 #12
0
def test_subitem_are_part_of_item():
    """A few basic examples from [[mw:Help:Lists]]."""
    ul = WikiList(
        '* Lists are easy to do:\n'
        '** start every line\n'
        '* with a star\n'
        '** more stars mean\n'
        '*** deeper levels',
        pattern=r'\*')
    items = ul.items
    assert items == [' Lists are easy to do:', ' with a star']
    fullitems = ul.fullitems
    assert fullitems == [
        '* Lists are easy to do:\n** start every line\n',
        '* with a star\n** more stars mean\n*** deeper levels']
コード例 #13
0
def test_cache_update():
    wl = WikiList('*a {{t}}', pattern=r'\*')
    wl.templates[0].name = 'ttt'
    assert wl.string == '*a {{ttt}}'
コード例 #14
0
 def test_cache_update(self):
     wl = WikiList('*a {{t}}', pattern=r'\*')
     wl.templates[0].name = 'ttt'
     self.assertEqual(wl.string, '*a {{ttt}}')
コード例 #15
0
def _render_list(instance: WikiTextHtml, list: wikitextparser.WikiList):
    list.string = _list_to_html(instance, list)
コード例 #16
0
def test_order_definition_lists():
    wl = WikiList("; Item 1 : definition 1\n", pattern=r'[:;]\s*')
    assert wl.items == ["Item 1 ", " definition 1"]
    assert wl.fullitems == ["; Item 1 : definition 1\n", ": definition 1"]
コード例 #17
0
def test_link_in_definition_list():
    wl = WikiList("; https://github.com : definition", pattern=r'[:;]\s*')
    assert wl.items == ["https://github.com ", " definition"]
コード例 #18
0
def test_dont_return_shadow():
    wl = WikiList(
        '#1 {{t}}',
        pattern=r'\#')
    assert wl.items[0] == '1 {{t}}'
コード例 #19
0
def test_lists():
    assert repr(WikiList('# a\n## b', '#').get_lists()) == "[WikiList('## b')]"
    with warns(DeprecationWarning):
        # noinspection PyDeprecation
        assert repr(WikiList('# a\n## b', '#').lists()) == "[WikiList('## b')]"
コード例 #20
0
 def test_dont_return_shadow(self):
     wl = WikiList('#1 {{t}}', pattern=r'\#')
     self.assertEqual(wl.items[0], '1 {{t}}')