Skip to content

Commit 6c4a4ab

Browse files
authored
Merge pull request #299 from MiWeiss/master
This merges all the minor changes I added to my fork into the main repo. Much relies on other PRs (to which, at the time, I did not have access yet).
2 parents bfb7502 + 11340f3 commit 6c4a4ab

22 files changed

Lines changed: 87 additions & 113 deletions

.github/workflows/tests.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
name: Tests
3+
4+
on: [push, pull_request]
5+
6+
jobs:
7+
test:
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
python-version: [3.6, 3.7, 3.8, 3.9, "3.10"]
12+
13+
steps:
14+
- uses: actions/checkout@v2
15+
- uses: actions/setup-node@v2-beta
16+
with:
17+
node-version: '12'
18+
check-latest: true
19+
20+
- name: Set up Python ${{ matrix.python-version }}
21+
uses: actions/setup-python@v2
22+
with:
23+
python-version: ${{ matrix.python-version }}
24+
25+
- name: Install dependencies (tf ${{ matrix.tf-version }} )
26+
run: |
27+
pip install -r requirements.txt
28+
29+
- name: Run Tests
30+
run: |
31+
python -m unittest discover -s ./bibtexparser/tests

.travis.yml

Lines changed: 0 additions & 55 deletions
This file was deleted.

bibtexparser/bparser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __init__(self, data=None,
7777
common_strings=False,
7878
add_missing_from_crossref=False):
7979
"""
80-
Creates a parser for rading BibTeX files
80+
Creates a parser for reading BibTeX files
8181
8282
:return: parser
8383
:rtype: `BibTexParser`
@@ -309,7 +309,7 @@ def _add_string(self, string_key, string):
309309
:type string: string
310310
"""
311311
if string_key in self.bib_database.strings:
312-
logger.warning('Overwritting existing string for key: %s.',
312+
logger.warning('Overwriting existing string for key: %s.',
313313
string_key)
314314
logger.debug(u'Store string: {} -> {}'.format(string_key, string))
315315
self.bib_database.strings[string_key] = self._clean_val(string)

bibtexparser/bwriter.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ def write(self, bib_database):
9797
return bibtex
9898

9999
def _entries_to_bibtex(self, bib_database):
100-
bibtex = ''
101100
if self.order_entries_by:
102101
# TODO: allow sort field does not exist for entry
103102
entries = sorted(bib_database.entries, key=lambda x: BibDatabase.entry_sort_key(x, self.order_entries_by))
@@ -109,9 +108,7 @@ def _entries_to_bibtex(self, bib_database):
109108
widths = [max(map(len, entry.keys())) for entry in entries]
110109
self._max_field_width = max(widths)
111110

112-
for entry in entries:
113-
bibtex += self._entry_to_bibtex(entry)
114-
return bibtex
111+
return self.entry_separator.join(self._entry_to_bibtex(entry) for entry in entries)
115112

116113
def _entry_to_bibtex(self, entry):
117114
bibtex = ''
@@ -143,7 +140,7 @@ def _entry_to_bibtex(self, entry):
143140
bibtex += '\n'+self.indent+','
144141
else:
145142
bibtex += ','
146-
bibtex += "\n}\n" + self.entry_separator
143+
bibtex += "\n}\n"
147144
return bibtex
148145

149146
def _comments_to_bibtex(self, bib_database):

bibtexparser/customization.py

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
Each of them takes a record and return the modified record.
77
"""
88

9-
import re
109
import logging
11-
10+
import re
11+
import warnings
1212
from builtins import str
1313

1414
from bibtexparser.latexenc import latex_to_unicode, string_to_latex, protect_uppercase
@@ -72,14 +72,14 @@ def splitname(name, strict_mode=True):
7272
# We'll iterate over the input once, dividing it into a list of words for
7373
# each comma-separated section. We'll also calculate the case of each word
7474
# as we work.
75-
sections = [[]] # Sections of the name.
76-
cases = [[]] # 1 = uppercase, 0 = lowercase, -1 = caseless.
77-
word = [] # Current word.
78-
case = -1 # Case of the current word.
79-
level = 0 # Current brace level.
80-
bracestart = False # Will the next character be the first within a brace?
81-
controlseq = True # Are we currently processing a control sequence?
82-
specialchar = None # Are we currently processing a special character?
75+
sections = [[]] # Sections of the name.
76+
cases = [[]] # 1 = uppercase, 0 = lowercase, -1 = caseless.
77+
word = [] # Current word.
78+
case = -1 # Case of the current word.
79+
level = 0 # Current brace level.
80+
bracestart = False # Will the next character be the first within a brace?
81+
controlseq = True # Are we currently processing a control sequence?
82+
specialchar = None # Are we currently processing a special character?
8383

8484
# Using an iterator allows us to deal with escapes in a simple manner.
8585
nameiter = iter(name)
@@ -246,12 +246,12 @@ def splitname(name, strict_mode=True):
246246
firstl = cases.index(0) - len(cases)
247247
lastl = -cases[::-1].index(0) - 1
248248
if lastl == -1:
249-
lastl -= 1 # Cannot consume the rest of the string.
249+
lastl -= 1 # Cannot consume the rest of the string.
250250

251251
# Pull the parts out.
252252
parts['first'] = p0[:firstl]
253-
parts['von'] = p0[firstl:lastl+1]
254-
parts['last'] = p0[lastl+1:]
253+
parts['von'] = p0[firstl:lastl + 1]
254+
parts['last'] = p0[lastl + 1:]
255255

256256
# No lowercase: last is the last word, first is everything else.
257257
else:
@@ -287,7 +287,7 @@ def splitname(name, strict_mode=True):
287287
if 0 in lcases:
288288
split = len(lcases) - lcases[::-1].index(0)
289289
if split == len(lcases):
290-
split = 0 # Last cannot be empty.
290+
split = 0 # Last cannot be empty.
291291
parts['von'] = sections[0][:split]
292292
parts['last'] = sections[0][split:]
293293

@@ -345,7 +345,8 @@ def author(record):
345345
"""
346346
if "author" in record:
347347
if record["author"]:
348-
record["author"] = getnames([i.strip() for i in record["author"].replace('\n', ' ').split(" and ")])
348+
record["author"] = getnames([i.strip() for i in re.split(r"\ and\ ", record["author"].replace('\n', ' '),
349+
flags=re.IGNORECASE)])
349350
else:
350351
del record["author"]
351352
return record
@@ -365,7 +366,8 @@ def editor(record):
365366
if record["editor"]:
366367
record["editor"] = getnames([i.strip() for i in record["editor"].replace('\n', ' ').split(" and ")])
367368
# convert editor to object
368-
record["editor"] = [{"name": i, "ID": i.replace(',', '').replace(' ', '').replace('.', '')} for i in record["editor"]]
369+
record["editor"] = [{"name": i, "ID": i.replace(',', '').replace(' ', '').replace('.', '')} for i in
370+
record["editor"]]
369371
else:
370372
del record["editor"]
371373
return record
@@ -417,7 +419,8 @@ def journal(record):
417419
if "journal" in record:
418420
# switch journal to object
419421
if record["journal"]:
420-
record["journal"] = {"name": record["journal"], "ID": record["journal"].replace(',', '').replace(' ', '').replace('.', '')}
422+
record["journal"] = {"name": record["journal"],
423+
"ID": record["journal"].replace(',', '').replace(' ', '').replace('.', '')}
421424

422425
return record
423426

@@ -518,13 +521,21 @@ def homogenize_latex_encoding(record):
518521
:type record: dict
519522
:returns: dict -- the modified record.
520523
"""
521-
# First, we convert everything to unicode
524+
#  First, we convert everything to unicode
522525
record = convert_to_unicode(record)
523526
# And then, we fall back
524527
for val in record:
525528
if val not in ('ID',):
526529
logger.debug('Apply string_to_latex to: %s', val)
527-
record[val] = string_to_latex(record[val])
530+
if isinstance(record[val], list):
531+
record[val] = [
532+
string_to_latex(x) for x in record[val]
533+
]
534+
elif isinstance(record[val], str):
535+
record[val] = string_to_latex(record[val])
536+
else:
537+
warnings.warn('Unable to homogenize latex encoding for %s: Expected string or list,' % val,
538+
RuntimeWarning)
528539
if val == 'title':
529540
logger.debug('Protect uppercase in title')
530541
logger.debug('Before: %s', record[val])
@@ -543,6 +554,7 @@ def add_plaintext_fields(record):
543554
:type record: dict
544555
:returns: dict -- the modified record.
545556
"""
557+
546558
def _strip_string(string):
547559
for stripped in ['{', '}']:
548560
string = string.replace(stripped, "")

bibtexparser/tests/data/article_comma_first.bib

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ @ARTICLE{ Baltazar2013
1616
, journal = {Nice Journal}
1717
, comments = {A comment}
1818
, keyword = {keyword1, keyword2}}
19+

bibtexparser/tests/data/article_comma_first_and_trailing_comma_output.bib

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ @article{Cesar2013
1212
, year = {2013}
1313
,
1414
}
15-

bibtexparser/tests/data/article_output.bib

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ @article{Cesar2013
1111
volume = {12},
1212
year = {2013}
1313
}
14-

bibtexparser/tests/data/article_trailing_comma_output.bib

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,3 @@ @article{Cesar2013
1111
volume = {12},
1212
year = {2013},
1313
}
14-

bibtexparser/tests/data/article_with_annotation_output.bib

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ @article{Cesar2013
1212
volume = {12},
1313
year = {2013}
1414
}
15-

0 commit comments

Comments
 (0)