Skip to content

Commit 65fba43

Browse files
authored
Merge pull request #316 from has2k1/fix-inlinecontent-type
Define pandoc InlineContent type to be recursive
2 parents e99b86a + 5b098ec commit 65fba43

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

quartodoc/pandoc/blocks.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from quartodoc.pandoc.inlines import (
2121
Inline,
2222
InlineContent,
23+
InlineContentItem,
2324
inlinecontent_to_str,
2425
str_as_list_item,
2526
)
@@ -79,8 +80,8 @@ def as_list_item(self):
7980

8081
# TypeAlias declared here to avoid forward-references which
8182
# break beartype
82-
ContentItem: TypeAlias = Union[str, Inline, Block]
83-
BlockContent: TypeAlias = Union[ContentItem, Sequence[ContentItem]]
83+
BlockContentItem: TypeAlias = Union[InlineContentItem, Block]
84+
BlockContent: TypeAlias = Union[BlockContentItem, Sequence[BlockContentItem]]
8485
DefinitionItem: TypeAlias = tuple[InlineContent, BlockContent]
8586

8687

@@ -156,8 +157,10 @@ def __str__(self):
156157
# Single Definition
157158
if isinstance(definitions, (str, Inline, Block)):
158159
definitions = [definitions]
160+
elif definitions is None:
161+
definitions = [""]
159162

160-
# Multiple defnitions
163+
# Multiple definitions
161164
for definition in definitions:
162165
s = blockcontent_to_str(definition)
163166
# strip away the indentation on the first line as it
@@ -397,6 +400,7 @@ def fmt(s: str, pfx: str):
397400
it = (
398401
str_as_list_item(c) if isinstance(c, str) else c.as_list_item
399402
for c in content
403+
if c
400404
)
401405
items = (fmt(s, next(pfx_it)) for s in it)
402406
return "".join(items).strip()

quartodoc/pandoc/inlines.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def as_list_item(self):
6565

6666
# TypeAlias declared here to avoid forward-references which
6767
# break beartype
68-
InlineContent: TypeAlias = Union[str, Inline, Union[Sequence[str], Inline]]
68+
InlineContentItem = Union[str, Inline, None]
69+
InlineContent: TypeAlias = Union[InlineContentItem, Sequence[InlineContentItem]]
6970

7071

7172
@dataclass

quartodoc/tests/pandoc/test_blocks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,25 @@ def test_blocks():
8686
""".strip()
8787
)
8888

89+
b = Blocks([Div("a"), None, Div("c")])
90+
assert (
91+
str(b)
92+
== """
93+
::: {}
94+
a
95+
:::
96+
97+
::: {}
98+
c
99+
:::
100+
""".strip()
101+
)
102+
103+
b1 = Blocks([None, None])
104+
b2 = Blocks(["", "", ""])
105+
assert str(b1) == ""
106+
assert str(b2) == ""
107+
89108

90109
def test_bulletlist():
91110
b = BulletList(["a", "b", "c"])
@@ -282,6 +301,13 @@ def test_definitionlist():
282301
""".strip()
283302
)
284303

304+
# Empty definitions are valid, but require trailling spaces after
305+
# the colon
306+
d1 = DefinitionList([("Term 1", ""), ("Term 2", "Definition 2")])
307+
d2 = DefinitionList([("Term 1", None), ("Term 2", "Definition 2")])
308+
assert ": \n" in str(d1)
309+
assert str(d1) == str(d2)
310+
285311

286312
def test_div():
287313
d = Div("a")

quartodoc/tests/pandoc/test_inlines.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ def test_inlines():
6565
i = Inlines(["a", Span("b"), Emph("c")])
6666
assert str(i) == "a [b]{} *c*"
6767

68+
i = Inlines(["a", None, Span("b"), Emph("c"), None])
69+
assert str(i) == "a [b]{} *c*"
70+
71+
i = Inlines([None, None, None])
72+
assert str(i) == ""
73+
6874
i = Inlines(["a", Span("b"), Emph("c"), ["d", Strong("e")]])
6975
assert str(i) == "a [b]{} *c* d **e**"
7076

@@ -87,6 +93,9 @@ def test_span():
8793
s = Span("a", Attr("span-id", classes=["c1", "c2"], attributes={"data-value": "1"}))
8894
assert str(s) == '[a]{#span-id .c1 .c2 data-value="1"}'
8995

96+
s = Span([Span("a"), Span("b"), "c"])
97+
assert str(s) == "[[a]{} [b]{} c]{}"
98+
9099

91100
def test_str():
92101
s = Str("a")

0 commit comments

Comments
 (0)