Skip to content

Commit

Permalink
[style] Correctly format month from patents
Browse files Browse the repository at this point in the history
  • Loading branch information
anjos committed Nov 6, 2024
1 parent 1807be8 commit 275b978
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 34 deletions.
62 changes: 31 additions & 31 deletions src/pelican/plugins/pybtex/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"), ")"]],
]
14 changes: 13 additions & 1 deletion src/pelican/plugins/pybtex/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}


Expand All @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions tests/data/biblio-patent/content/article.bib
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
4 changes: 3 additions & 1 deletion tests/data/biblio-patent/content/article.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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].
11 changes: 10 additions & 1 deletion tests/test_pybtex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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(
Expand Down

0 comments on commit 275b978

Please sign in to comment.