From a5ed94980c327e08e5ca7fc6bb07519ed3a5df68 Mon Sep 17 00:00:00 2001 From: daylin <47667941+daylinmorgan@users.noreply.github.com> Date: Mon, 5 Jun 2023 23:36:08 -0500 Subject: [PATCH] =?UTF-8?q?fix(#173):=20add=20config=20option=20to=20preve?= =?UTF-8?q?nt=20clobbering=20user's=20numbered=20fo=E2=80=A6=20(#192)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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> --- src/mkdocs_bibtex/plugin.py | 34 ++++++++++++++++++++++++++++++---- test_files/test_features.py | 15 +++++++++++++++ test_files/test_plugin.py | 15 +++++++++++++++ 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/mkdocs_bibtex/plugin.py b/src/mkdocs_bibtex/plugin.py index b0cf826..36f777c 100644 --- a/src/mkdocs_bibtex/plugin.py +++ b/src/mkdocs_bibtex/plugin.py @@ -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): @@ -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): @@ -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 @@ -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 @@ -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 ] @@ -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) diff --git a/test_files/test_features.py b/test_files/test_features.py index 9dd69d4..9a04c8d 100644 --- a/test_files/test_features.py +++ b/test_files/test_features.py @@ -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] diff --git a/test_files/test_plugin.py b/test_files/test_plugin.py index 58e374e..682c1b5 100644 --- a/test_files/test_plugin.py +++ b/test_files/test_plugin.py @@ -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)