Skip to content

Commit

Permalink
Merge pull request #62 from entangled/55-exceptions-on-malformed-quar…
Browse files Browse the repository at this point in the history
…to-attributes

fix #55
  • Loading branch information
jhidding authored Mar 4, 2025
2 parents 9a8f7df + 359c664 commit 5064e52
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 287 deletions.
13 changes: 9 additions & 4 deletions entangled/hooks/quarto_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,18 @@ def on_read(self, code: CodeBlock):
attrs = yaml.safe_load(header)
if attrs is None:
return
if not isinstance(attrs, dict):
log.warning("quarto attributes do not evaluate to dictionary; skipping")
log.warning(f"tried to parse: {header}")
return

if "id" in attrs:
if "id" in attrs.keys():
code.properties.append(Id(attrs["id"]))

if "classes" in attrs:
if "classes" in attrs.keys():
code.properties.extend(Class(c) for c in attrs["classes"])

code.properties.extend(Attribute(k, v) for k, v in attrs.items())

code.properties.extend(Attribute(k, v) for k, v in attrs.items()
if k not in ("id", "classes"))

log.debug("quarto attributes: %s", attrs)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ dev = [
"mkdocs>=1.4.3,<2",
"mkdocs-material>=9.1.13,<10",
"mkdocstrings[python]>=0.21.2,<0.22",
"twine>=4.0.2,<5",
"pytest-asyncio>=0.21.1,<0.22",
"ruff>=0.4.4,<0.5",
"types-pyyaml>=6.0.12.20240311,<7",
Expand Down
27 changes: 27 additions & 0 deletions test/test_quarto_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from entangled.hooks import get_hooks
from entangled.markdown_reader import read_markdown_string
from entangled.tangle import tangle_ref
from entangled.properties import Attribute


md_in = """
Expand Down Expand Up @@ -40,3 +41,29 @@ def test_quarto_attributes():
refs, _ = read_markdown_string(md_in, hooks=hooks)
result, _ = tangle_ref(refs, "hello.scm", AnnotationMethod.NAKED)
assert result.strip() == scm_out.strip()


malformed_attrs = """
Test after issue 55
``` {.scheme #test}
;|blah:nospace
42
```
"""


def test_malformed_quarto_attributes():
with config(
hooks=["quarto_attributes"]):
# it is currently not possible to change markers at run-time,
# because the config is passed to MarkdownLexer at module load time
# markers=Markers(
# open=r"^(?P<indent>\s*)```(?P<properties>.*)$",
# close=r"^(?P<indent>\s*)```\s*$")):
hooks = get_hooks()

refs, _ = read_markdown_string(malformed_attrs, hooks=hooks)
assert "test" in refs
codeblock = next(refs["test"])
assert not any(isinstance(p, Attribute) for p in codeblock.properties)
Loading

0 comments on commit 5064e52

Please sign in to comment.