[[cs:User:Dvorapa]] wrote script for cosmetic changes in infoboxes.
This is great tool, but there is very annoying bug: script sometimes destroys table
def beautifyInfoboxes(self, text):
"""Cleanup multiple or trailing spaces."""
exceptions = ['comment', 'math', 'nowiki', 'pre', 'table', 'link']
items = [i for i in text.split('|')]
newitems = []
inInfobox = [False]
inTemplate = [False]
inLink = [False]
for item in items:
if inInfobox[-1] and not inLink[-1] and not inTemplate[-1]:
item = textlib.replaceExcept(item.lstrip(), r'^', r' ', exceptions, site=self.site)
# TODO: nahradit re.sub za textlib.replaceExcept po vyřešení T125307
item = re.sub(r' *= *', r' = ', item, count=1)
if re.search(r'(?i)\{{2}(Infobox|NFPA 704)[^\}]*$', item) != None:
inInfobox.append(True)
elif inInfobox[-1]:
brackets = [m.start() for m in re.finditer("}}",item)]
brackets += [m.start() for m in re.finditer("{{",item)]
brackets += [m.start() for m in re.finditer("]]",item)]
brackets += [m.start() for m in re.finditer(r"\[{2}",item)]
brackets.sort(key=int)
if not brackets == []:
endInfobox = []
for n in brackets:
if item[n] == '{':
inTemplate.append(True)
elif item[n] == '[':
inLink.append(True)
elif item[n] == ']':
inLink.pop()
else:
if inTemplate[-1]:
inTemplate.pop()
elif inInfobox[-1]:
endInfobox.append(n)
inInfobox.pop()
if not inInfobox[-1]:
break
for n in reversed(endInfobox):
before = item[:n]
before = textlib.replaceExcept(before.rstrip(), r'$', r'\n', exceptions, site=self.site)
after = item[n:]
if not inInfobox[-1]:
after = textlib.replaceExcept(after, r'^}}\s*', r'}}\n', exceptions, site=self.site)
item = before + after
if inInfobox[-1] and not inLink[-1] and not inTemplate[-1]:
item = textlib.replaceExcept(item.rstrip(), r'$', r'\n ', exceptions, site=self.site)
newitems.append(item)
return '|'.join(newitems)