BibTeX catalogs: Strip unmatched braces

BibTeX catalogs: Strip unmatched braces to ensure to revent generation
of invalid .bib file. Fixes #1228905 [Calibre does not create a valid .bib file](https://bugs.launchpad.net/calibre/+bug/1228905)

Merge branch 'bibtex_fix' of https://github.com/alexbiddle/calibre
This commit is contained in:
Kovid Goyal 2013-10-31 15:52:27 +05:30
commit c9f9df0eb0
2 changed files with 29 additions and 1 deletions

View File

@ -179,6 +179,8 @@ class BIBTEX(CatalogPlugin):
#\n removal
item = item.replace(u'\r\n',u' ')
item = item.replace(u'\n',u' ')
# unmatched brace removal (users should use \leftbrace or \rightbrace for single braces)
item = bibtexdict.stripUnmatchedSyntax(item, u'{', u'}')
#html to text
try:
item = html2text(item)

View File

@ -2902,3 +2902,29 @@ class BibTeX:
"""
return self.utf8ToBibtex(u' and '.join([author for author in item]))
def stripUnmatchedSyntax(text, open_character, close_character):
"""
Strips unmatched BibTeX syntax
"""
stack = []
assert len(open_character) == 1 and len(close_character) == 1
remove = []
for i, ch in enumerate(text):
if ch == open_character:
stack.append(i)
elif ch == close_character:
try:
stack.pop()
except IndexError:
# Remove unmatched closing char
remove.append(i)
# Remove unmatched opening chars
remove.extend(stack)
if remove:
text = list(text)
for i in sorted(remove, reverse=True):
text.pop(i)
text = ''.join(text)
return text