feat: Rename fallback option and option section

Options now reside under the 'plugins.bbt' section (instead of
'plugins.bbt-formatter'). The 'fallback' option has been renamed
'default-formatter' to signify its purpose a little better - even if it
will be rarely changed in all likelihood.
This commit is contained in:
Marty Oehme 2025-06-09 12:05:06 +02:00
parent a076880913
commit 39ba0f5508
Signed by: Marty
GPG key ID: 4E535BC19C61886E
2 changed files with 28 additions and 28 deletions

View file

@ -1,7 +1,8 @@
# papis-bbt-formatter # papis-bbt-formatter
Formats reference keys in papis similarly to the (zotero plugin) `better-bibtex` keys, in the format `Name2008TitleShort`. Formats reference keys in papis similarly to the (zotero plugin) `better-bibtex` keys, in the format `Name2008TitleShort`.
## Installation:
## Installation
<!-- TODO set up pypi repository / explain git install path --> <!-- TODO set up pypi repository / explain git install path -->
You can install from pypi with `pip install git+https://git.martyoeh.me/Marty/papis-bbt-formatter.git`. You can install from pypi with `pip install git+https://git.martyoeh.me/Marty/papis-bbt-formatter.git`.
@ -16,7 +17,7 @@ In your papis configuration file (usually `~/.config/papis/config`), add the fol
```cfg ```cfg
[settings] [settings]
formater = bbt formater = bbt
ref-format = bbt ref-format = bbt:
``` ```
For now, the ref-format also *has* to start with `bbt`. For now, the ref-format also *has* to start with `bbt`.
@ -38,8 +39,8 @@ Listed below are all options with their defaults:
formater = bbt formater = bbt
ref-format = bbt ref-format = bbt
[plugins.bbt-formatter] [plugins.bbt]
fallback = python default-formatter = python
full-year = False full-year = False
title-words = 4 title-words = 4
title-chars = -1 title-chars = -1
@ -50,7 +51,7 @@ title-chars = -1
You can specifiy whether the reference should contain the full 4-digit year representation (i.e. `1997`, `2018`) or just a shortened 2-digit version (`97`, `18`): You can specifiy whether the reference should contain the full 4-digit year representation (i.e. `1997`, `2018`) or just a shortened 2-digit version (`97`, `18`):
```cfg ```cfg
[plugins.bbt-formatter] [plugins.bbt]
full-year = True full-year = True
``` ```
@ -59,12 +60,12 @@ This will insert the full 4-digit publication year instead of the (default) shor
### Title length ### Title length
You can change the length that the `TitleShort` in `Name2008TitleShort` will be cut down to by setting You can change the length that the `TitleShort` in `Name2008TitleShort` will be cut down to by setting
the maximum length in words `title-words=4` or in characters `title-chars=20` under the `[plugins.bbt-formatter]` section in your papis configuration file (usually located at `~/.config/papis/config`). the maximum length in words `title-words=4` or in characters `title-chars=20` under the `[plugins.bbt]` section in your papis configuration file (usually located at `~/.config/papis/config`).
To set a maximum word length, do: To set a maximum word length, do:
```cfg ```cfg
[plugins.bbt-formatter] [plugins.bbt]
title-words = 4 title-words = 4
``` ```
@ -73,7 +74,7 @@ change the number to shorten/lengthen to your preference.
Same idea for maximum character length: Same idea for maximum character length:
```cfg ```cfg
[plugins.bbt-formatter] [plugins.bbt]
title-chars = 10 title-chars = 10
``` ```
@ -81,7 +82,7 @@ This will allow a maximum of 10 characters for the title.
Using both: Using both:
```cfg ```cfg
[plugins.bbt-formatter] [plugins.bbt]
title-words = 4 title-words = 4
title-chars = 20 title-chars = 20
``` ```
@ -90,7 +91,7 @@ This will ensure a maximum of 4 words, however if they go more than 20 character
You can set either option to `-1` to turn it off: You can set either option to `-1` to turn it off:
```cfg ```cfg
[plugins.bbt-formatter] [plugins.bbt]
title-words = 4 title-words = 4
title-chars = -1 title-chars = -1
``` ```
@ -104,8 +105,8 @@ For anything that is not a reference, use this formatter.
Basically, put the formatter you had before switching to bbt here: Basically, put the formatter you had before switching to bbt here:
```cfg ```cfg
[plugins.bbt-formatter] [plugins.bbt]
fallback = jinja2 default-formatter = jinja2
``` ```
Can be any of the installed papis formatters, including custom ones Can be any of the installed papis formatters, including custom ones

View file

@ -8,9 +8,10 @@ import papis.logging
logger = papis.logging.get_logger(__name__) logger = papis.logging.get_logger(__name__)
OPTIONS_SECTION = "plugins.bbt"
DEFAULT_OPTIONS = { DEFAULT_OPTIONS = {
"plugins.bbt-formatter": { OPTIONS_SECTION: {
"fallback": "python", "default-formatter": "python",
"title-words": 3, "title-words": 3,
"title-chars": -1, "title-chars": -1,
} }
@ -30,13 +31,8 @@ class BBTFormatter(papis.format.Formatter):
additional: dict[str, Any] | None = None, additional: dict[str, Any] | None = None,
default: str | None = None, default: str | None = None,
) -> str: ) -> str:
if fmt.startswith("bbt"): if not fmt.startswith("bbt:"):
formatted = self.use_bbt(doc) fallback_formatter = papis.config.getstring("default-formatter", OPTIONS_SECTION)
return formatted
else:
fallback_formatter = papis.config.getstring(
"fallback", "plugins.bbt-formatter"
)
# NOTE sure would be nice to have a less hacky way of calling another formatter # NOTE sure would be nice to have a less hacky way of calling another formatter
_saved = papis.format.FORMATTER _saved = papis.format.FORMATTER
@ -49,6 +45,9 @@ class BBTFormatter(papis.format.Formatter):
papis.format.FORMATTER = _saved papis.format.FORMATTER = _saved
return fallback_formatted return fallback_formatted
formatted = self.use_bbt(doc)
return formatted
def use_bbt(self, doc: papis.document.DocumentLike) -> str: def use_bbt(self, doc: papis.document.DocumentLike) -> str:
author_unfmt = ( author_unfmt = (
doc["author_list"][0]["family"] doc["author_list"][0]["family"]
@ -67,7 +66,7 @@ class BBTFormatter(papis.format.Formatter):
Returns either the full 4-digit year or a shortened 2-digit Returns either the full 4-digit year or a shortened 2-digit
version depending on the plugin year options.""" version depending on the plugin year options."""
if papis.config.getboolean("full-year", "plugins.bbt-formatter"): if papis.config.getboolean("full-year", OPTIONS_SECTION):
return str(year) return str(year)
return str(year)[-2:] return str(year)[-2:]
@ -83,8 +82,8 @@ class BBTFormatter(papis.format.Formatter):
filter(lambda word: word and word not in SKIP_WORDS, title.split()), filter(lambda word: word and word not in SKIP_WORDS, title.split()),
) )
) )
wlen = papis.config.getint("title-words", "plugins.bbt-formatter") wlen = papis.config.getint("title-words", OPTIONS_SECTION)
clen = papis.config.getint("title-chars", "plugins.bbt-formatter") clen = papis.config.getint("title-chars", OPTIONS_SECTION)
wlen = None if wlen == -1 else wlen wlen = None if wlen == -1 else wlen
clen = None if clen == -1 else clen clen = None if clen == -1 else clen
title = "".join(title_words[:wlen])[:clen] title = "".join(title_words[:wlen])[:clen]