Skip to content

Commit 5b098ec

Browse files
committed
Add None to pandoc.(Inline|Block)Content types
It is a noop equivalent to "". This reduces conditional expressions when generating markdown.
1 parent 29fa7be commit 5b098ec

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

quartodoc/pandoc/blocks.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,10 @@ def __str__(self):
157157
# Single Definition
158158
if isinstance(definitions, (str, Inline, Block)):
159159
definitions = [definitions]
160+
elif definitions is None:
161+
definitions = [""]
160162

161-
# Multiple defnitions
163+
# Multiple definitions
162164
for definition in definitions:
163165
s = blockcontent_to_str(definition)
164166
# strip away the indentation on the first line as it
@@ -398,6 +400,7 @@ def fmt(s: str, pfx: str):
398400
it = (
399401
str_as_list_item(c) if isinstance(c, str) else c.as_list_item
400402
for c in content
403+
if c
401404
)
402405
items = (fmt(s, next(pfx_it)) for s in it)
403406
return "".join(items).strip()

quartodoc/pandoc/inlines.py

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

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

7171

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: 6 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

0 commit comments

Comments
 (0)