Skip to content

Commit

Permalink
bugfix braces
Browse files Browse the repository at this point in the history
  • Loading branch information
François Boulogne committed Aug 25, 2014
1 parent 79334bb commit b6fac69
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* FIX: if title contained multiples words in braces (#34)
* ENH: Removed comma after last key-value pair by faph (#28)
* ENH: optional keys sanitising by faph (#29)
* FIX: missing coma at the end of a record (#24)
Expand Down
5 changes: 4 additions & 1 deletion CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
String replacement

- faph
coma fixes, optional keys sanitising and other improvements
coma fixes, optional keys sanitising, refactoring and other improvements

- Steven M. Bellovin
Fix braces detection
20 changes: 17 additions & 3 deletions bibtexparser/bparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,10 +329,24 @@ def _strip_braces(self, val):
"""
logger.debug('Strip braces')
val = val.strip()
if val.startswith('{') and val.endswith('}'):
if val.startswith('{') and val.endswith('}') and self._full_span(val):
return val[1:-1]
return val

def _full_span(self, val):
cnt = 0
for i in range(0, len(val)):
if val[i] == '{':
cnt += 1
elif val[i] == '}':
cnt -= 1
if cnt == 0:
break
if i == len(val) - 1:
return True
else:
return False

def _string_subst(self, val):
""" Substitute string definitions
Expand Down Expand Up @@ -368,10 +382,10 @@ def repl(m):
logger.debug('Substitute string definitions inside larger expressions')
if '#' not in val:
return val

# TODO?: Does not match two subsequent variables or strings, such as "start" # foo # bar # "end" or "start" # "end".
# TODO: Does not support braces instead of quotes, e.g.: {start} # foo # {bar}
# TODO: Does not support strings like: "te#s#t"
# TODO: Does not support strings like: "te#s#t"
return self.replace_all_re.sub(repl, val)

def _add_val(self, val):
Expand Down
2 changes: 1 addition & 1 deletion bibtexparser/tests/data/traps.bib
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@ARTICLE{Laide2013,
author = {Jean Laid{\'e},
Ben Loaeb},
title = {An amazing title},
title = {{An} amazing {title}},
year = {2013},
month = jan,
volume = {12},
Expand Down
2 changes: 1 addition & 1 deletion bibtexparser/tests/test_bparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def test_traps(self):
'journal': 'Nice Journal',
'id': 'Laide2013',
'pages': '12-23',
'title': 'An amazing title',
'title': '{An} amazing {title}',
'comments': 'A comment',
'author': 'Jean Laid{\\\'e}, Ben Loaeb',
'volume': '12',
Expand Down

0 comments on commit b6fac69

Please sign in to comment.