diff --git a/src/pelican/plugins/pybtex/style.py b/src/pelican/plugins/pybtex/style.py index 5c6e3bb..305323f 100644 --- a/src/pelican/plugins/pybtex/style.py +++ b/src/pelican/plugins/pybtex/style.py @@ -6,39 +6,8 @@ from pybtex.style.formatting import toplevel import pybtex.style.formatting.unsrt -from pybtex.style.formatting.unsrt import date from pybtex.style.template import field, node, optional, sentence, tag, words - -def _monkeypatch_method(cls): - def decorator(func): - setattr(cls, func.__name__, func) - return func - - return decorator - - -@_monkeypatch_method(pybtex.style.formatting.unsrt.Style) -def get_patent_template(self, e): - """Format patent bibtex entry. - - Parameters - ---------- - e - The entry to be formatted. - - Returns - ------- - The formatted entry object. - """ - return toplevel[ - sentence[self.format_names("author")], - self.format_title(e, "title"), - sentence(capfirst=False)[tag("em")[field("number")], date], - optional[self.format_url(e), optional[" (visited on ", field("urldate"), ")"]], - ] - - # format month by converting integers to month name _MONTH_NAMES = { 1: "January", @@ -74,3 +43,34 @@ def _month_field(children, data): # Ensures we always have the month correctly formatted pybtex.style.formatting.unsrt.date = words[_month_field(), field("year")] + + +def _monkeypatch_method(cls): + def decorator(func): + setattr(cls, func.__name__, func) + return func + + return decorator + + +@_monkeypatch_method(pybtex.style.formatting.unsrt.Style) +def get_patent_template(self, e): + """Format patent bibtex entry. + + Parameters + ---------- + e + The entry to be formatted. + + Returns + ------- + The formatted entry object. + """ + return toplevel[ + sentence[self.format_names("author")], + self.format_title(e, "title"), + sentence(capfirst=False)[ + tag("em")[field("number")], pybtex.style.formatting.unsrt.date + ], + optional[self.format_url(e), optional[" (visited on ", field("urldate"), ")"]], + ] diff --git a/src/pelican/plugins/pybtex/utils.py b/src/pelican/plugins/pybtex/utils.py index f7634e8..94a7f16 100644 --- a/src/pelican/plugins/pybtex/utils.py +++ b/src/pelican/plugins/pybtex/utils.py @@ -140,6 +140,18 @@ def format_bibtex_pygments( "oct": 10, "nov": 11, "dec": 12, + "1": 1, + "2": 2, + "3": 3, + "4": 4, + "5": 5, + "6": 6, + "7": 7, + "8": 8, + "9": 9, + "10": 10, + "11": 11, + "12": 12, } @@ -155,7 +167,7 @@ def _get_month_number(m: str) -> int: ------- An integer, representing the month number. """ - return _MONTH_NUMBERS[m.lower()[:3]] + return _MONTH_NUMBERS[m.lower()[:3].strip()] def generate_context( diff --git a/tests/data/biblio-patent/content/article.bib b/tests/data/biblio-patent/content/article.bib index a13cab5..fc21ec6 100644 --- a/tests/data/biblio-patent/content/article.bib +++ b/tests/data/biblio-patent/content/article.bib @@ -11,3 +11,14 @@ @patent{pat year = "1876", url = "https://www.google.com/patents/US174465", } + +@patent{pat2, + author = "A. G. Bell", + title = "Improvement in telegraphy", + nationality = "United States", + number = "174465", + day = "7", + month = "3", + year = "1876", + url = "https://www.google.com/patents/US174465", +} diff --git a/tests/data/biblio-patent/content/article.rst b/tests/data/biblio-patent/content/article.rst index 8f8fa1b..27a1d92 100644 --- a/tests/data/biblio-patent/content/article.rst +++ b/tests/data/biblio-patent/content/article.rst @@ -13,4 +13,6 @@ This is an article :summary: Short version for index and feeds :pybtex_sources: article.bib -This will be turned into a citation [@@pat]. +This will be turned into a citation [@@pat2]. + +This will be turned into another citation [@@pat]. diff --git a/tests/test_pybtex.py b/tests/test_pybtex.py index 315e6af..7da6ad9 100644 --- a/tests/test_pybtex.py +++ b/tests/test_pybtex.py @@ -339,12 +339,16 @@ def test_biblio_patent(setup_pelican: tuple[list[logging.LogRecord], pathlib.Pat soup = BeautifulSoup(f, "html.parser") publication_keys = [ + "pat2", "pat", ] para = soup.find_all("p") - text_to_be_checked = ((publication_keys[0], "1", para[0]),) + text_to_be_checked = ( + (publication_keys[0], "1", para[0]), + (publication_keys[1], "2", para[1]), + ) for key, label, paragraph in text_to_be_checked: a = paragraph.find_all("a") @@ -369,6 +373,11 @@ def test_biblio_patent(setup_pelican: tuple[list[logging.LogRecord], pathlib.Pat # prefixed by "pybtex-" assert details[0].attrs["id"].endswith(publication_keys[0]) + assert details[1].attrs["id"].endswith(publication_keys[1]) + + # assert that the month number was correctly translated in both entries + assert "March 1876" in details[0].find_all("summary")[0].text + assert "March 1876" in details[1].find_all("summary")[0].text _assert_log_no_errors(records) _assert_log_contains(