Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip building bib_data if files are older than config time (for mkdocs serve) #240

Merged
merged 6 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/mkdocs_bibtex/plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import time
import validators
from collections import OrderedDict
from pathlib import Path
Expand Down Expand Up @@ -51,6 +52,11 @@
self.bib_data = None
self.all_references = OrderedDict()
self.unescape_for_arithmatex = False
self.configured = False

def on_startup(self, *, command, dirty):
""" Having on_startup() tells mkdocs to keep the plugin object upon rebuilds"""
pass

Check warning on line 59 in src/mkdocs_bibtex/plugin.py

View check run for this annotation

Codecov / codecov/patch

src/mkdocs_bibtex/plugin.py#L59

Added line #L59 was not covered by tests

def on_config(self, config):
"""
Expand Down Expand Up @@ -80,6 +86,15 @@
bibdata = parse_file(bibfile)
refs.update(bibdata.entries)

if hasattr(self,"last_configured"):
# Skip rebuilding bib data if all files are older than the initial config
if all(Path(bibfile).stat().st_mtime < self.last_configured for bibfile in bibfiles):
log.info("BibTexPlugin: No changes in bibfiles.")
return config

Check warning on line 93 in src/mkdocs_bibtex/plugin.py

View check run for this annotation

Codecov / codecov/patch

src/mkdocs_bibtex/plugin.py#L91-L93

Added lines #L91 - L93 were not covered by tests

# Clear references on reconfig
self.all_references = OrderedDict()

self.bib_data = BibliographyData(entries=refs)

# Set CSL from either url or path (or empty)
Expand All @@ -99,6 +114,7 @@

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

self.last_configured = time.time()
return config

def on_page_markdown(self, markdown, page, config, files):
Expand Down
2 changes: 2 additions & 0 deletions test_files/test_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def plugin():
return plugin



@pytest.fixture
def plugin_advanced_pandoc(plugin):
"""
Expand All @@ -52,6 +53,7 @@ def plugin_advanced_pandoc(plugin):
)
plugin.config["cite_inline"] = True

delattr(plugin,"last_configured")
plugin.on_config(plugin.config)

return plugin
Expand Down
Loading