Skip to content

Commit

Permalink
Fix blocks not starting on new lines (#416)
Browse files Browse the repository at this point in the history
closes #411 

This PR replaced an in-part identical PR #412 by @benlogan. He is thus added as co-author.

Co-authored-by: Ben Logan <1737958+benlogan@users.noreply.github.com>
  • Loading branch information
zepinglee and benlogan authored Nov 4, 2023
1 parent 770eb00 commit 92d38d8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bibtexparser/splitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def split(self, library: Optional[Library] = None) -> Library:
The library with the added blocks.
"""
self._markiter = re.finditer(
r"(?<!\\)[\{\}\",=\n]|(?<=\n)@[\w]*( |\t)*(?={)", self.bibstr, re.MULTILINE
r"(?<!\\)[\{\}\",=\n]|@[\w]*( |\t)*(?={)", self.bibstr, re.MULTILINE
)

if library is None:
Expand Down Expand Up @@ -395,7 +395,7 @@ def _handle_entry(self, m, m_val) -> Union[Entry, ParsingFailedBlock]:
entry_type=entry_type,
key=key,
fields=fields,
raw=self.bibstr[m.start() : end_index + 1],
raw=self.bibstr[m.start() : end_index],
)

# If there were duplicate field keys, we return a DuplicateFieldKeyBlock wrapping
Expand Down
76 changes: 76 additions & 0 deletions tests/splitter_tests/test_splitter_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,79 @@ def test_handles_duplicate_strings():
assert isinstance(
lib.failed_blocks[0].ignore_error_block, bibtexparser.model.String
)


blocks_not_starting_on_new_lines = """\
@article{KUEH2023S98,
title = {Myocardial Characterisation Using Delayed Dual-Energy Cardiac Computed Tomography},
author = {S.-H. Kueh and J. Benatar and R. Stewart}
}@INPROCEEDINGS{9837531,
author={Hassan, Mona Bakri and Saeed, Rashid A. and Khalifa, Othman and Ali, Elmustafa Sayed and Mokhtar, Rania A. and Hashim, Aisha A.},
title={Green Machine Learning for Green Cloud Energy Efficiency},
doi={10.1109/MI-STA54861.2022.9837531}}@ARTICLE{9372936,
author={Hu, Ning and Tian, Zhihong and Du, Xiaojiang and Guizani, Nadra and Zhu, Zhihan},
title={Deep-Green: A Dispersed Energy-Efficiency Computing Paradigm for Green Industrial IoT},
doi={10.1109/TGCN.2021.3064683}}@ARTICLE{5445167,
author={Kumar, Karthik and Lu, Yung-Hsiang},
title={Cloud Computing for Mobile Users: Can Offloading Computation Save Energy?},
doi={10.1109/MC.2010.98}}
"""


@pytest.mark.parametrize(
"expected",
[
{
"key": "KUEH2023S98",
"type": "article",
"raw": "@article{KUEH2023S98,\n"
"title = {Myocardial Characterisation Using Delayed Dual-Energy Cardiac Computed Tomography},\n"
"author = {S.-H. Kueh and J. Benatar and R. Stewart}\n}",
"start_line": 0,
},
{
"key": "9837531",
"type": "inproceedings",
"raw": "@INPROCEEDINGS{9837531,\n"
" author={Hassan, Mona Bakri and Saeed, Rashid A. and Khalifa, Othman and Ali, Elmustafa Sayed and Mokhtar, Rania A. and Hashim, Aisha A.},\n"
" title={Green Machine Learning for Green Cloud Energy Efficiency},\n"
" doi={10.1109/MI-STA54861.2022.9837531}}",
"start_line": 3,
},
{
"key": "9372936",
"type": "article",
"raw": "@ARTICLE{9372936,\n"
" author={Hu, Ning and Tian, Zhihong and Du, Xiaojiang and Guizani, Nadra and Zhu, Zhihan},\n"
" title={Deep-Green: A Dispersed Energy-Efficiency Computing Paradigm for Green Industrial IoT},\n"
" doi={10.1109/TGCN.2021.3064683}}",
"start_line": 6,
},
{
"key": "5445167",
"type": "article",
"raw": "@ARTICLE{5445167,\n"
" author={Kumar, Karthik and Lu, Yung-Hsiang},\n"
" title={Cloud Computing for Mobile Users: Can Offloading Computation Save Energy?},\n"
" doi={10.1109/MC.2010.98}}",
"start_line": 9,
},
],
)
def test_blocks_not_starting_on_new_lines(expected: Dict[str, Any]):
"""Test the new blocks that are not on new lines.
Discussed at https://github.com/sciunto-org/python-bibtexparser/issues/411.
"""
import bibtexparser

lib = bibtexparser.parse_string(blocks_not_starting_on_new_lines)
assert len(lib.entries) == 4
entry_by_key = lib.entries_dict
assert len(entry_by_key) == 4

entry = entry_by_key[expected["key"]]
assert entry.key == expected["key"]
assert entry.entry_type == expected["type"]
assert entry.raw == expected["raw"]
assert entry.start_line == expected["start_line"]

0 comments on commit 92d38d8

Please sign in to comment.