Skip to content

Commit

Permalink
fix(#173): add config option to prevent clobbering user's numbered fo… (
Browse files Browse the repository at this point in the history
#192)

* fix(#173): add config option to prevent clobbering user's numbered footnotes

* Add test cases for basic functionality

* ensure config becomes local arg

---------

Co-authored-by: shyamd <16827130+shyamd@users.noreply.github.com>
  • Loading branch information
daylinmorgan and shyamd authored Jun 6, 2023
1 parent c4bd007 commit a5ed949
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 4 deletions.
34 changes: 30 additions & 4 deletions src/mkdocs_bibtex/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class BibTexPlugin(BasePlugin):
("full_bib_command", config_options.Type(str, default="\\full_bibliography")),
("csl_file", config_options.Type(str, default="")),
("cite_inline", config_options.Type(bool, default=False)),
("footnote_format", config_options.Type(str, default="{number}")),
]

def __init__(self):
Expand Down Expand Up @@ -90,6 +91,11 @@ def on_config(self, config):
if self.cite_inline and not self.csl_file: # pragma: no cover
raise Exception("Must supply a CSL file in order to use cite_inline")

if "{number}" not in self.config.get("footnote_format"):
raise Exception("Must include `{number}` placeholder in footnote_format")

self.footnote_format = self.config.get("footnote_format")

return config

def on_page_markdown(self, markdown, page, config, files):
Expand All @@ -100,9 +106,9 @@ def on_page_markdown(self, markdown, page, config, files):
1. Finds all cite keys (may include multiple citation references)
2. Convert all cite keys to citation quads:
(full cite key,
induvidual cite key,
individual cite key,
citation key in corresponding style,
citation for induvidual cite key)
citation for individual cite key)
3. Insert formatted cite keys into text
4. Insert the bibliography into the markdown
5. Insert the full bibliograph into the markdown
Expand Down Expand Up @@ -150,6 +156,18 @@ def on_page_markdown(self, markdown, page, config, files):

return markdown

def format_footnote_key(self, number):
"""
Create footnote key based on footnote_format
Args:
number (int): citation number
Returns:
formatted footnote
"""
return self.footnote_format.format(number=number)

def format_citations(self, cite_keys):
"""
Formats references into citation quads and adds them to the global registry
Expand Down Expand Up @@ -189,7 +207,12 @@ def format_citations(self, cite_keys):

# 4. Construct quads
quads = [
(cite_block, key, numbers[key], self.all_references[key])
(
cite_block,
key,
self.format_footnote_key(numbers[key]),
self.all_references[key],
)
for cite_block, key in pairs
]

Expand All @@ -204,7 +227,10 @@ def full_bibliography(self):

bibliography = []
for number, (key, citation) in enumerate(self.all_references.items()):
bibliography_text = "[^{}]: {}".format(number, citation)
bibliography_text = "[^{}]: {}".format(
number,
citation,
)
bibliography.append(bibliography_text)

return "\n".join(bibliography)
15 changes: 15 additions & 0 deletions test_files/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,18 @@ def test_multi_reference(plugin_advanced_pandoc):
assert result == insert_citation_keys(
quads, test_markdown, plugin.csl_file, plugin.bib_data.to_string("bibtex")
)


def test_custom_footnote_formatting(plugin):

assert plugin.format_footnote_key(1) == "1"
plugin.footnote_format = "Test Format {number}"
assert plugin.format_footnote_key(1) == "Test Format 1"

plugin.csl_file = os.path.join(test_files_dir, "nature.csl")
assert (
"[@test]",
"test",
"Test Format 1",
"Author, F. & Author, S. Test title. *Testing Journal* **1**, (2019).",
) == plugin.format_citations(["[@test]"])[0]
15 changes: 15 additions & 0 deletions test_files/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,18 @@ def test_on_page_markdown(plugin):
test_markdown = "This is a citation. [@test; @test2] This is another citation [@test]\n\n \\bibliography"

assert "[^3]" not in plugin.on_page_markdown(test_markdown, None, None, None)


def test_footnote_formatting_config(plugin):
"""
This function tests to ensure footnote formatting configuration is working properly
"""
# Test to make sure the config enforces {number} in the format
bad_plugin = BibTexPlugin()
bad_plugin.load_config(
options={"footnote_format": ""},
config_file_path=test_files_dir,
)

with pytest.raises(Exception):
bad_plugin.on_config(bad_plugin.config)

0 comments on commit a5ed949

Please sign in to comment.