Skip to content

Commit

Permalink
Merge pull request #316 from has2k1/fix-inlinecontent-type
Browse files Browse the repository at this point in the history
Define pandoc InlineContent type to be recursive
  • Loading branch information
machow authored Jan 19, 2024
2 parents e99b86a + 5b098ec commit 65fba43
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
10 changes: 7 additions & 3 deletions quartodoc/pandoc/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from quartodoc.pandoc.inlines import (
Inline,
InlineContent,
InlineContentItem,
inlinecontent_to_str,
str_as_list_item,
)
Expand Down Expand Up @@ -79,8 +80,8 @@ def as_list_item(self):

# TypeAlias declared here to avoid forward-references which
# break beartype
ContentItem: TypeAlias = Union[str, Inline, Block]
BlockContent: TypeAlias = Union[ContentItem, Sequence[ContentItem]]
BlockContentItem: TypeAlias = Union[InlineContentItem, Block]
BlockContent: TypeAlias = Union[BlockContentItem, Sequence[BlockContentItem]]
DefinitionItem: TypeAlias = tuple[InlineContent, BlockContent]


Expand Down Expand Up @@ -156,8 +157,10 @@ def __str__(self):
# Single Definition
if isinstance(definitions, (str, Inline, Block)):
definitions = [definitions]
elif definitions is None:
definitions = [""]

# Multiple defnitions
# Multiple definitions
for definition in definitions:
s = blockcontent_to_str(definition)
# strip away the indentation on the first line as it
Expand Down Expand Up @@ -397,6 +400,7 @@ def fmt(s: str, pfx: str):
it = (
str_as_list_item(c) if isinstance(c, str) else c.as_list_item
for c in content
if c
)
items = (fmt(s, next(pfx_it)) for s in it)
return "".join(items).strip()
Expand Down
3 changes: 2 additions & 1 deletion quartodoc/pandoc/inlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def as_list_item(self):

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


@dataclass
Expand Down
26 changes: 26 additions & 0 deletions quartodoc/tests/pandoc/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ def test_blocks():
""".strip()
)

b = Blocks([Div("a"), None, Div("c")])
assert (
str(b)
== """
::: {}
a
:::
::: {}
c
:::
""".strip()
)

b1 = Blocks([None, None])
b2 = Blocks(["", "", ""])
assert str(b1) == ""
assert str(b2) == ""


def test_bulletlist():
b = BulletList(["a", "b", "c"])
Expand Down Expand Up @@ -282,6 +301,13 @@ def test_definitionlist():
""".strip()
)

# Empty definitions are valid, but require trailling spaces after
# the colon
d1 = DefinitionList([("Term 1", ""), ("Term 2", "Definition 2")])
d2 = DefinitionList([("Term 1", None), ("Term 2", "Definition 2")])
assert ": \n" in str(d1)
assert str(d1) == str(d2)


def test_div():
d = Div("a")
Expand Down
9 changes: 9 additions & 0 deletions quartodoc/tests/pandoc/test_inlines.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def test_inlines():
i = Inlines(["a", Span("b"), Emph("c")])
assert str(i) == "a [b]{} *c*"

i = Inlines(["a", None, Span("b"), Emph("c"), None])
assert str(i) == "a [b]{} *c*"

i = Inlines([None, None, None])
assert str(i) == ""

i = Inlines(["a", Span("b"), Emph("c"), ["d", Strong("e")]])
assert str(i) == "a [b]{} *c* d **e**"

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

s = Span([Span("a"), Span("b"), "c"])
assert str(s) == "[[a]{} [b]{} c]{}"


def test_str():
s = Str("a")
Expand Down

0 comments on commit 65fba43

Please sign in to comment.