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 _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 _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_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_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'])