mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
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:
commit
c9f9df0eb0
@ -179,6 +179,8 @@ class BIBTEX(CatalogPlugin):
|
|||||||
#\n removal
|
#\n removal
|
||||||
item = item.replace(u'\r\n',u' ')
|
item = item.replace(u'\r\n',u' ')
|
||||||
item = item.replace(u'\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
|
#html to text
|
||||||
try:
|
try:
|
||||||
item = html2text(item)
|
item = html2text(item)
|
||||||
|
@ -2863,7 +2863,7 @@ class BibTeX:
|
|||||||
return self.invalid_cit.sub(u'', text)
|
return self.invalid_cit.sub(u'', text)
|
||||||
|
|
||||||
def braceUppercase(self, text):
|
def braceUppercase(self, text):
|
||||||
"""
|
"""
|
||||||
Convert uppercase letters to bibtex encoded uppercase
|
Convert uppercase letters to bibtex encoded uppercase
|
||||||
"""
|
"""
|
||||||
return self.upper.sub(lambda m: u'{%s}' % m.group(), text)
|
return self.upper.sub(lambda m: u'{%s}' % m.group(), text)
|
||||||
@ -2902,3 +2902,29 @@ class BibTeX:
|
|||||||
"""
|
"""
|
||||||
return self.utf8ToBibtex(u' and '.join([author for author in item]))
|
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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user