Fix Bug #1228905 Calibre does not create a valid .bib file

This commit is contained in:
Alex Biddle 2013-10-31 09:57:26 +00:00
parent ab4f53331e
commit 45aec8c2e1
2 changed files with 30 additions and 0 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,31 @@ class BibTeX:
"""
return self.utf8ToBibtex(u' and '.join([author for author in item]))
def stripUnmatchedSyntax(self, text, open_character, close_character):
"""
Strips unmatched BibTeX syntax
"""
stack = list()
if len(open_character) > 1 or len(close_character) > 1:
raise ValueError("Only single characters accepted")
for i in text:
if i == open_character:
stack.append(open_character)
# if there is already an orphan closing character strip everything
elif i == close_character and not stack:
text = text.replace(open_character, u'')
text = text.replace(close_character, u'')
return text
elif i == close_character and stack:
stack.pop()
# if there are still open_tokens on the stack they have not been closed strip everything
if stack:
text = text.replace(open_character, u'')
text = text.replace(close_character, u'')
return text
# if nothing left on stack all characters matched and do nothing
else:
return text