wikitextparser icon indicating copy to clipboard operation
wikitextparser copied to clipboard

Lists with a missing level parsed incorrectly

Open TrueBrain opened this issue 4 years ago • 1 comments

Another corner-case, this time because of a somewhat invalid user input. Given the following example, with wikitextparser==0.46:

import wikitextparser

wtp = wikitextparser.parse("** Item 1\n** Item 2\n")
print(wtp.get_lists()[0].items)
print(wtp.get_lists()[0].sublists(0))
print(wtp.get_lists()[0].sublists())

(There is nothing on level 1, it immediately jumps to level 2)

returns:

['* Item 1']
[]
[WikiList('** Item 1\n** Item 2\n')]

This input seems to confuse wikitextparser enough that it becomes a bit difficult to process the result. item 2 is basically hidden unless I parse the whole list myself.

I have been wondering what I would expect wikitextparser to do, and ideal I would want get_lists() to return a list with items like [], and where sublists(0) would return ["Item 1", "Item 2"]. But as I am only beginning to touch this codebase, that is easy for me to say; no clue if it is wanted behaviour or even possible :D

One can also say: garbage in, garbage out, and just consider this invalid input.

TrueBrain avatar Oct 14 '20 12:10 TrueBrain

Cheap workaround, most likely breaks on other cases:

    for list in wtp.get_lists():
        if list.items[0][0] in (";:*#"):
            list.string = f"{list.fullitems[0][list.level - 1]} \n{list.string}"
        (...)

(I do love that I can modify string and that the node updates immediately :D)

TrueBrain avatar Oct 14 '20 12:10 TrueBrain