From 43f8096d2b8170e087f2c38a6ee1bd2448ef80b6 Mon Sep 17 00:00:00 2001 From: Andre Anjos Date: Wed, 6 Nov 2024 21:57:33 +0100 Subject: [PATCH] [style] Improve nationality support on patent entries --- src/pelican/plugins/pybtex/style.py | 13 +++++++++++-- tests/data/biblio-patent/content/article.bib | 1 - tests/test_pybtex.py | 2 ++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/pelican/plugins/pybtex/style.py b/src/pelican/plugins/pybtex/style.py index 305323f..2dcfc13 100644 --- a/src/pelican/plugins/pybtex/style.py +++ b/src/pelican/plugins/pybtex/style.py @@ -4,6 +4,7 @@ types. """ +import pybtex.database from pybtex.style.formatting import toplevel import pybtex.style.formatting.unsrt from pybtex.style.template import field, node, optional, sentence, tag, words @@ -54,7 +55,7 @@ def decorator(func): @_monkeypatch_method(pybtex.style.formatting.unsrt.Style) -def get_patent_template(self, e): +def get_patent_template(self, e: pybtex.database.Entry): """Format patent bibtex entry. Parameters @@ -66,11 +67,19 @@ def get_patent_template(self, e): ------- The formatted entry object. """ + assert e.fields is not None + + nationality = e.fields.get("nationality", "") + + if nationality.lower() == "united states": + nationality = "U.S." + return toplevel[ sentence[self.format_names("author")], self.format_title(e, "title"), sentence(capfirst=False)[ - tag("em")[field("number")], pybtex.style.formatting.unsrt.date + tag("em")[optional[nationality, " "], "Patent ", field("number")], + pybtex.style.formatting.unsrt.date, ], optional[self.format_url(e), optional[" (visited on ", field("urldate"), ")"]], ] diff --git a/tests/data/biblio-patent/content/article.bib b/tests/data/biblio-patent/content/article.bib index fc21ec6..b8716ba 100644 --- a/tests/data/biblio-patent/content/article.bib +++ b/tests/data/biblio-patent/content/article.bib @@ -15,7 +15,6 @@ @patent{pat @patent{pat2, author = "A. G. Bell", title = "Improvement in telegraphy", - nationality = "United States", number = "174465", day = "7", month = "3", diff --git a/tests/test_pybtex.py b/tests/test_pybtex.py index 7da6ad9..7de174e 100644 --- a/tests/test_pybtex.py +++ b/tests/test_pybtex.py @@ -377,7 +377,9 @@ def test_biblio_patent(setup_pelican: tuple[list[logging.LogRecord], pathlib.Pat # assert that the month number was correctly translated in both entries assert "March 1876" in details[0].find_all("summary")[0].text + assert "Patent" in details[0].find_all("summary")[0].text assert "March 1876" in details[1].find_all("summary")[0].text + assert "U.S. Patent" in details[1].find_all("summary")[0].text _assert_log_no_errors(records) _assert_log_contains(