5j9 / wikitextparser

A Python library to parse MediaWiki WikiText
GNU General Public License v3.0
289 stars 22 forks source link

IndexError: string index out of range in Template.normal_name() #105

Closed winstontsai closed 2 years ago

winstontsai commented 2 years ago

I encountered an IndexError in the Template.normal_name function while running my bot. I don't have the actual example that caused this, but I have reproduced an example of what I think happened:

>>> x = wtp.Template('{{Template: | hi=bye}}')
>>> x.normal_name(capitalize=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/winston/projects/IndentBot/venv/lib/python3.9/site-packages/wikitextparser/_template.py", line 95, in normal_name
    n0 = name[0]
IndexError: string index out of range

One fix would be to change this code:

if capitalize:
    # Use uppercase for the first letter
    n0 = name[0]
    if n0.islower():
        name = n0.upper() + name[1:]

to

if capitalize:
    # Use uppercase for the first letter
    name = name[:1].upper() + name[1:]
5j9 commented 2 years ago

Thank you for the debugging the issue. Ideally, wikitextparser should not detect '{{Template: }} as a template in the first place, but since template namespace can be localized and wikitextparser does not currently support localized namespaces, I think your solution is best for now. Tnx again!