Skip to content

Commit

Permalink
FEAT: use bookfinder.com for ISBN links (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
redeboer authored Dec 7, 2023
1 parent ff737f6 commit 7a06a85
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
],
"ignoreWords": [
"PyPI",
"bookfinder",
"commitlint",
"etal",
"isbnsearch",
"prereleased",
"rtfd",
"sphinxcontrib",
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ Alternatively, you can use the style for one bibliography only by specifying it
.. bibliography:: /references.bib
:style: unsrt_et_al
```

Book entries that have an ISBN get a link to the book entry on [bookfinder.com](https://bookfinder.com/search). If you want to use [isbnsearch.org](https://isbnsearch.org) instead, add the following to your `conf.py`:

```python
unsrt_etal_isbn_resolver = "isbnsearch"
```
43 changes: 31 additions & 12 deletions src/sphinx_pybtex_etal_style/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# pyright: reportMissingTypeStubs=false
from __future__ import annotations

from typing import TYPE_CHECKING, Any
import sys
from typing import TYPE_CHECKING, Any, ClassVar

from pybtex.plugin import register_plugin
from pybtex.richtext import Tag, Text
Expand All @@ -22,17 +23,27 @@
from pybtex.database import Entry
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
if sys.version_info < (3, 8):
from typing_extensions import Literal
else:
from typing import Literal

ISBNResolvers = Literal["bookfinder", "isbnsearch"]


def setup(app: Sphinx) -> dict[str, Any]:
app.add_config_value(
"unsrt_etal_isbn_resolver", default="bookfinder", rebuild="env"
)
app.connect("config-inited", register_style)
return {
"parallel_read_safe": True,
"parallel_write_safe": True,
}


def register_style(_: Sphinx, __: BuildEnvironment) -> None:
def register_style(app: Sphinx, __: BuildEnvironment) -> None:
MyStyle.isbn_resolver = app.config.unsrt_etal_isbn_resolver
register_plugin("pybtex.style.formatting", "unsrt_et_al", MyStyle)


Expand Down Expand Up @@ -74,6 +85,8 @@ def names(children, context, role, **kwargs): # type: ignore[no-untyped-def]


class MyStyle(UnsrtStyle):
isbn_resolver: ClassVar[ISBNResolvers] = "bookfinder"

def __init__(self) -> None:
super().__init__(abbreviate_names=True)

Expand All @@ -99,16 +112,22 @@ def format_url(self, e: Entry) -> Node:
]

def format_isbn(self, e: Entry) -> Node:
return href[
join[
"https://isbnsearch.org/isbn/",
field("isbn", raw=True, apply_func=remove_dashes_and_spaces),
],
join[
"ISBN:",
field("isbn", raw=True),
],
]
raw_isbn = field("isbn", raw=True, apply_func=remove_dashes_and_spaces)
if self.isbn_resolver == "bookfinder":
url = join[
"https://www.bookfinder.com/search/?isbn=",
raw_isbn,
"&mode=isbn&st=sr&ac=qr",
]
elif self.isbn_resolver == "isbnsearch":
url = join["https://isbnsearch.org/isbn/", raw_isbn]
else:
msg = (
f"Unknown unsrt_etal_isbn_resolver: {self.isbn_resolver}. Valid options"
f" are {', '.join(ISBNResolvers.__args__)}."
)
raise NotImplementedError(msg)
return href[url, join["ISBN:", field("isbn", raw=True)]]


def remove_dashes_and_spaces(isbn: str) -> str:
Expand Down

0 comments on commit 7a06a85

Please sign in to comment.