From aa559bcd7b28d45e7d2495a05319231c4264ea83 Mon Sep 17 00:00:00 2001 From: shyamd <16827130+shyamd@users.noreply.github.com> Date: Tue, 14 Jan 2025 21:28:14 -0800 Subject: [PATCH] fix full bibliography --- src/mkdocs_bibtex/plugin.py | 23 ++++++++++------------- test_files/test_integration.py | 12 ++++++++++++ 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/mkdocs_bibtex/plugin.py b/src/mkdocs_bibtex/plugin.py index dc9937c..4879e75 100644 --- a/src/mkdocs_bibtex/plugin.py +++ b/src/mkdocs_bibtex/plugin.py @@ -1,4 +1,3 @@ -import re import time import validators from collections import OrderedDict @@ -129,21 +128,19 @@ def on_page_markdown(self, markdown, page, config, files): except Exception as e: log.warning(f"Error formatting citation {citation.key}: {e}") bibliography = "\n".join(bibliography) - markdown = re.sub( - re.escape(bib_command), - bibliography, - markdown, - ) + markdown = markdown.replace(bib_command, bibliography) + # 5. Build the full Bibliography and insert into the text full_bib_command = self.config.full_bib_command - all_citations = [Citation(key=key) for key in self.registry.bib_data.entries] - full_bibliography = [] - for citation in all_citations: - full_bibliography.append("[^{}]: {}".format(citation.key, self.registry.reference_text(citation))) - full_bibliography = "\n".join(full_bibliography) + if markdown.count(full_bib_command) > 0: + all_citations = [Citation(key=key) for key in self.registry.bib_data.entries] + full_bibliography = [] + for citation in all_citations: + full_bibliography.append("[^{}]: {}".format(citation.key, self.registry.reference_text(citation))) + full_bibliography = "\n".join(full_bibliography) + markdown = markdown.replace(full_bib_command, full_bibliography) + - markdown = markdown.replace(bib_command, bibliography) - markdown = markdown.replace(full_bib_command, full_bibliography) return markdown diff --git a/test_files/test_integration.py b/test_files/test_integration.py index 6a39042..529f5f4 100644 --- a/test_files/test_integration.py +++ b/test_files/test_integration.py @@ -145,3 +145,15 @@ def test_invalid_citations(plugin): result = plugin.on_page_markdown(markdown, None, None, None) # assert "[@nonexistent]" in result # Invalid citation should remain unchanged assert "[^nonexistent]" not in result + + +def test_full_bib_command(plugin): + """Test full bibliography command""" + markdown = "Full bibliography [@test]\n\n\\full_bibliography" + result = plugin.on_page_markdown(markdown, None, None, None) + print(result) + assert "Full bibliography [^test]" in result + assert "[^test]:" in result + assert "[^test2]:" in result + assert "[^Bivort2016]:" in result + assert "[^test_citavi]:" in result