Skip to content
Open
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
31 changes: 30 additions & 1 deletion mkquartodocs/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,14 @@ class HTMLContext:


class AdmotionCellDataPreprocessor(Preprocessor):
def __init__(self, md: Markdown | None = None, extension: QuartoCellDataExtension | None = None):
super().__init__(md)
self.extension = extension

def run(self, lines):
if self.extension and self.extension.should_ignore_current_page():
log.debug("Skipping admonition preprocessor for ignored page")
return lines
log.info(f"Running {self}")
file_content = FileContent(lines)
context = []
Expand Down Expand Up @@ -444,6 +451,28 @@ def run(self, lines):


class QuartoCellDataExtension(Extension):
def __init__(self):
super().__init__()
self._ignore_patterns: list[re.Pattern[str]] = []
self._current_page_candidates: tuple[str, ...] = ()

def update_ignore_patterns(self, patterns: list[re.Pattern[str]]) -> None:
self._ignore_patterns = patterns

def set_current_page(self, *candidates: str) -> None:
self._current_page_candidates = tuple(filter(None, candidates))

def should_ignore_current_page(self) -> bool:
if not self._ignore_patterns or not self._current_page_candidates:
return False

for candidate in self._current_page_candidates:
for pattern in self._ignore_patterns:
if pattern.fullmatch(candidate):
log.debug("Ignoring page %s due to pattern %s", candidate, pattern)
return True
return False

def extendMarkdown(
self,
md: Markdown,
Expand All @@ -456,7 +485,7 @@ def extendMarkdown(

md.registerExtension(self)
md.preprocessors.register(
AdmotionCellDataPreprocessor(),
AdmotionCellDataPreprocessor(md, extension=self),
name="QuartoCellData",
priority=106, # Right before Admonition
)
Expand Down
11 changes: 11 additions & 0 deletions mkquartodocs/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def on_config(self, config, **kwargs):
self.exit_action = _delete_file if not self.config["keep_output"] else None

self.extension = QuartoCellDataExtension()
self.extension.update_ignore_patterns(self.ignores)
config["markdown_extensions"].append(self.extension)
return config

Expand Down Expand Up @@ -165,3 +166,13 @@ def on_pre_build(self, config):
def on_post_build(self, config):
log.info("Cleaning up:")
self.dir_context.exit(update=False)

def on_page_markdown(self, markdown, page, config, files):
if hasattr(self, "extension"):
src_path = page.file.src_path.replace("\\", "/")
candidates = [src_path]
candidates.append(f"docs/{src_path}")
if page.file.abs_src_path:
candidates.append(str(page.file.abs_src_path))
self.extension.set_current_page(*candidates)
return markdown
15 changes: 14 additions & 1 deletion tests/test_preprocessor.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from mkquartodocs.extension import AdmotionCellDataPreprocessor
import re

from mkquartodocs.extension import AdmotionCellDataPreprocessor, QuartoCellDataExtension
import os
from pathlib import Path
import pytest
Expand Down Expand Up @@ -115,3 +117,14 @@ def test_conversion_file(document):
else:
msg += ", For extra information set MKQUARTODOCS_TEST_DEBUG_OUT_DIR=1"
raise AssertionError(msg)


def test_preprocessor_respects_ignore_patterns():
extension = QuartoCellDataExtension()
extension.update_ignore_patterns([re.compile(r"docs/index\.md")])
extension.set_current_page("index.md", "docs/index.md")
preprocessor = AdmotionCellDataPreprocessor(extension=extension)
lines = ["::: foo.main.hello"]

out = preprocessor.run(lines)
assert out == lines