Exceptions aren't ignored but it looks like a bad idea to assume templates or 
links with that snippet:
if item[n] == '{': inTemplate.append(True) elif item[n] == '[': 
inLink.append(True)
One idea would be using Page.templatesWithParam() and recombine the template 
source.
Best
Xqt

> Am 10.05.2016 um 11:25 schrieb Jan Dudík <[email protected]>:
> 
> Hello, 
> 
> [[cs:User:Dvorapa]] wrote script for cosmetic changes in infoboxes.
> https://cs.wikipedia.org/wiki/Wikipedie:WikiProjekt_Strojové_zpracování/Skripty
> 
>  This is great tool, but there is very annoying bug: script sometimes 
> destroys table 
> https://cs.wikipedia.org/w/index.php?title=Petr_Bříza&diff=prev&oldid=13643587
> or external link
> https://cs.wikipedia.org/w/index.php?title=Jaroslav_Maxmilián_Kašparů&diff=next&oldid=13643059
> 
> It looks like script ignores 'Exceptions'
> 
> 
> 
> 
> Can somebody repair it?
> 
> JAnD
> -----------
> 
> 
> 
> 
> 
>    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)
> _______________________________________________
> pywikibot mailing list
> [email protected]
> https://lists.wikimedia.org/mailman/listinfo/pywikibot
_______________________________________________
pywikibot mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/pywikibot

Reply via email to