Skip to content

Commit

Permalink
✨ Added option to select encoding of .bib file in parse_file (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
himcraft authored Sep 1, 2023
1 parent e8257ec commit c9190cf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ Issues are labelled `v1` and `v2`, correspondingly.
To install the dev dependencies, run `pip install -e .[test,lint,docs]` from within the cloned repository. Then:

- To test your code, run `pytest .`
- To lint your code (required for CI/CD to pass), run: `black bibtexparser tests docs && isort bibtexparser tests docs --profile black
- To lint your code (required for CI/CD to pass), run: `black bibtexparser tests docs && isort bibtexparser tests docs --profile black`
- To build and preview the docs, navigate into `docs` and run `make html`. Then open the `index.html` file in the `docs/build/html` folder.
4 changes: 3 additions & 1 deletion bibtexparser/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,20 @@ def parse_file(
path: str,
parse_stack: Optional[Iterable[Middleware]] = None,
append_middleware: Optional[Iterable[Middleware]] = None,
encoding: str = "UTF-8",
) -> Library:
"""Parse a BibTeX file
Args:
path (str): Path to BibTeX file
parse_stack (Optional[Iterable[Middleware]], optional): List of middleware to apply to the database after splitting. If None (default), a default stack will be used providing simple standard functionality will be used.
append_middleware (Optional[Iterable[Middleware]], optional): List of middleware to append to the default stack (ignored if a not-None parse_stack is passed).
encoding: Encoding of the .bib file. Default encoding is "UTF-8".
Returns:
Library: Parsed BibTeX library
"""
with open(path) as f:
with open(path, encoding=encoding) as f:
bibtex_str = f.read()
return parse_string(
bibtex_str, parse_stack=parse_stack, append_middleware=append_middleware
Expand Down
6 changes: 6 additions & 0 deletions tests/resources/gbk_test.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@ARTICLE{¿­Èö2013,
author = {¿­Èö},
title = {Test Title},
year = {2013},
journal = {²âÊÔÆÚ¿¯}
}
13 changes: 13 additions & 0 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Testing the parse_file function."""

import pytest

from bibtexparser import parse_file, writer


def test_gbk():
library = parse_file("tests/resources/gbk_test.bib", encoding="gbk")
assert library.entries[0]["author"] == "凯撒"
assert library.entries[0]["title"] == "Test Title"
assert library.entries[0]["year"] == "2013"
assert library.entries[0]["journal"] == "测试期刊"

0 comments on commit c9190cf

Please sign in to comment.