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
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
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)
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']
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'])
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'
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' ]
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')
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'])
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
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' ]
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']
def test_cache_update(): wl = WikiList('*a {{t}}', pattern=r'\*') wl.templates[0].name = 'ttt' assert wl.string == '*a {{ttt}}'
def test_cache_update(self): wl = WikiList('*a {{t}}', pattern=r'\*') wl.templates[0].name = 'ttt' self.assertEqual(wl.string, '*a {{ttt}}')
def _render_list(instance: WikiTextHtml, list: wikitextparser.WikiList): list.string = _list_to_html(instance, list)
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"]
def test_link_in_definition_list(): wl = WikiList("; https://github.com : definition", pattern=r'[:;]\s*') assert wl.items == ["https://github.com ", " definition"]
def test_dont_return_shadow(): wl = WikiList( '#1 {{t}}', pattern=r'\#') assert wl.items[0] == '1 {{t}}'
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')]"
def test_dont_return_shadow(self): wl = WikiList('#1 {{t}}', pattern=r'\#') self.assertEqual(wl.items[0], '1 {{t}}')