Compare commits

..

No commits in common. "f2d8306f1fffa5fa9eef1f5a5f3b02b6669ad29b" and "f8d9ae4d4b2451e53b10280b95c79bec27041c2b" have entirely different histories.

3 changed files with 41 additions and 69 deletions

2
.gitignore vendored
View file

@ -1,6 +1,4 @@
/temp/ /temp/
.papis.config
test.bib
# Created by https://www.toptal.com/developers/gitignore/api/-f,python,linux,vim,quarto,markdown,jupyternotebooks # Created by https://www.toptal.com/developers/gitignore/api/-f,python,linux,vim,quarto,markdown,jupyternotebooks
# Edit at https://www.toptal.com/developers/gitignore?templates=-f,python,linux,vim,quarto,markdown,jupyternotebooks # Edit at https://www.toptal.com/developers/gitignore?templates=-f,python,linux,vim,quarto,markdown,jupyternotebooks

View file

@ -9,87 +9,61 @@ You can install from pypi with `pip install git+https://git.martyoeh.me/Marty/pa
That's it! If you have papis and papis-bbt-formatter installed in the same environment (whether virtual or global), That's it! If you have papis and papis-bbt-formatter installed in the same environment (whether virtual or global),
everything should now be installed. everything should now be installed.
## Usage ## Usage:
In your papis configuration file (usually `~/.config/papis/config`), add the following under the main settings header: In your papis configuration file (usually `~/.config/papis/config`), add the following under the main settings header:
```cfg ```toml
[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 *has* to start with `bbt`.
Formatted reference keys by default will look like:
`Harvey05briefhistoryneoliberalism` (for Harvey, D. (2005). A brief history of neoliberalism. Oxford New York: Oxford University Press.)
or `Harvey22reflectionsacademiclife` (for Harvey, D. (2022). Reflections on an academic life. Human Geography, 15, 1424. doi:10.1177/19427786211046291)
## Configuration
No configuration except for the above setup is required for the formatting to work,
but you can set a couple additional ones to customize the bevhavior to your liking:
### Title length ### Title length
Currently, you can change the length that the `TitleShort` in `Name2008TitleShort` will be cut down to by setting Currently, 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 or in characters.
To set a maximum word length, do: To set a maximum word length, do:
```cfg ```toml
[settings] [settings]
formater = bbt formater = bbt
ref-format = bbt ref-format = bbt[title-words=4]
[plugins.bbt-formatter]
title-words = 4
``` ```
In this case, the title will be shortened to 4 words maximum (the default), In this case, the title will be shortened to 4 words maximum (the default),
change the number to shorten/lengthen to your preference. change the number to shorten/lengthen to your preference.
Same idea for maximum character length: Same idea for maximum character length:
```cfg ```toml
[plugins.bbt-formatter] [settings]
title-chars = 10 formater = bbt
ref-format = bbt[title-chars=10]
``` ```
This will allow a maximum of 10 characters for the title. This will allow a maximum of 10 characters for the title.
Using both: Using both:
```cfg ```toml
[plugins.bbt-formatter] [settings]
title-words = 4 formater = bbt
title-chars = 20 ref-format = bbt[title-words=4][title-chars=15]
``` ```
This will ensure a maximum of 4 words, however if they go more than 20 characters they will be cut off mid-word. This will ensure a maximum of 4 words, however if they go more than 20 characters they will be cut off mid-word.
You can set either option to `-1` to turn it off: You can set either option to `-1` to turn it off:
```cfg ```toml
[plugins.bbt-formatter] [settings]
title-words = 4 formater = bbt
title-chars = -1 ref-format = bbt[title-words=4][title-chars=-1]
``` ```
This will ensure that a maximum of 4 words will be placed in the ref, but they do not have a maximum character length, This will ensure that a maximum of 4 words will be placed in the ref, but they do not have a maximum character length,
so will always be fully written out (the default behavior if no title length options are provided). so will always be fully written out (the default behavior without title length options provided).
### Fallback formatter
For anything that is not a reference, use this formatter.
Basically, put the formatter you had before switching to bbt here:
```cfg
[plugins.bbt-formatter]
fallback = jinja2
```
Can be any of the installed papis formatters, including custom ones
(though usually it will be `python`, which is also the default setting).
--- ---

View file

@ -8,14 +8,8 @@ import papis.logging
logger = papis.logging.get_logger(__name__) logger = papis.logging.get_logger(__name__)
DEFAULT_OPTIONS = { DEFAULT_TITLE_LENGTH_WORDS = 3
"plugins.bbt-formatter": { DEFAULT_TITLE_LENGTH_CHARS = -1
"fallback": "python",
"title-words": 3,
"title-chars": -1,
}
}
papis.config.register_default_settings(DEFAULT_OPTIONS)
class BBTFormatter(papis.format.Formater): class BBTFormatter(papis.format.Formater):
@ -38,23 +32,15 @@ class BBTFormatter(papis.format.Formater):
) )
title_unfmt = doc["title"] if "title" in doc else "NO TITLE" title_unfmt = doc["title"] if "title" in doc else "NO TITLE"
year_unfmt = str(doc["year"]) if "year" in doc else "0000" year_unfmt = str(doc["year"]) if "year" in doc else "0000"
author = re.sub("[^a-z]+", "", author_unfmt.lower()) author = re.sub("[^a-z]+", "", author_unfmt.lower())
year = year_unfmt[-2:] year = year_unfmt[-2:]
title = self.get_title(title_unfmt) title = self.get_title(title_unfmt, fmt)
return f"{author}{year}{title}" return f"{author}{year}{title}"
else: else:
# TODO find less hacky way of calling another formatter? return papis.format.PythonFormater().format(fmt, doc, doc_key, additional)
papis.format._FORMATER = None
fallback_formatter = papis.config.getstring(
"fallback", "plugins.bbt-formatter"
)
formatter = papis.format.get_formater(fallback_formatter).format(
fmt, doc, doc_key=doc_key, additional=additional
)
papis.format._FORMATER = None
return formatter
def get_title(self, title: str) -> str: def get_title(self, title: str, fmt: str) -> str:
title = re.sub("[^0-9a-z ]+", "", title.lower()) title = re.sub("[^0-9a-z ]+", "", title.lower())
title_words = list( title_words = list(
map( map(
@ -62,13 +48,27 @@ class BBTFormatter(papis.format.Formater):
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 = self._title_length_words(fmt)
clen = papis.config.getint("title-chars", "plugins.bbt-formatter") clen = self._title_length_chars(fmt)
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]
return title return title
def _title_length_words(self, fmt: str) -> int:
"""Returns the length (in words) the title should be shortened to."""
if match := re.search(r"\[title-words=(-?\d+)\]", fmt):
logger.debug(f"Found title length: {match.group(1)} words.")
return int(match.group(1))
return DEFAULT_TITLE_LENGTH_WORDS
def _title_length_chars(self, fmt: str) -> int:
"""Returns the length (in characters) the title should be shortened to."""
if match := re.search(r"\[title-chars=(-?\d+)\]", fmt):
logger.debug(f"Found title length: {match.group(1)} chars.")
return int(match.group(1))
return DEFAULT_TITLE_LENGTH_CHARS
SKIP_WORDS = set( SKIP_WORDS = set(
[ [