From 29fa7be25481930ea5cfee7293e31653d04c009f Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Thu, 14 Dec 2023 11:14:08 +0300 Subject: [PATCH 1/2] Define pandoc InlineContent type to be recursive InlineContent is already processed recursively, this change makes the type definition recursive too. And it also defines a clearer relationship between InlineContent and BlockContent. --- quartodoc/pandoc/blocks.py | 5 +++-- quartodoc/pandoc/inlines.py | 3 ++- quartodoc/tests/pandoc/test_inlines.py | 3 +++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/quartodoc/pandoc/blocks.py b/quartodoc/pandoc/blocks.py index 865345a..135a9eb 100644 --- a/quartodoc/pandoc/blocks.py +++ b/quartodoc/pandoc/blocks.py @@ -20,6 +20,7 @@ from quartodoc.pandoc.inlines import ( Inline, InlineContent, + InlineContentItem, inlinecontent_to_str, str_as_list_item, ) @@ -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] diff --git a/quartodoc/pandoc/inlines.py b/quartodoc/pandoc/inlines.py index a3a5f07..6d06943 100644 --- a/quartodoc/pandoc/inlines.py +++ b/quartodoc/pandoc/inlines.py @@ -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] +InlineContent: TypeAlias = Union[InlineContentItem, Sequence[InlineContentItem]] @dataclass diff --git a/quartodoc/tests/pandoc/test_inlines.py b/quartodoc/tests/pandoc/test_inlines.py index bd0c23b..287de2c 100644 --- a/quartodoc/tests/pandoc/test_inlines.py +++ b/quartodoc/tests/pandoc/test_inlines.py @@ -87,6 +87,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") From 5b098ecd88998c43c022e1ec4e77d646cca63070 Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Thu, 21 Dec 2023 21:25:53 +0300 Subject: [PATCH 2/2] Add None to pandoc.(Inline|Block)Content types It is a noop equivalent to "". This reduces conditional expressions when generating markdown. --- quartodoc/pandoc/blocks.py | 5 ++++- quartodoc/pandoc/inlines.py | 2 +- quartodoc/tests/pandoc/test_blocks.py | 26 ++++++++++++++++++++++++++ quartodoc/tests/pandoc/test_inlines.py | 6 ++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/quartodoc/pandoc/blocks.py b/quartodoc/pandoc/blocks.py index 135a9eb..e70e629 100644 --- a/quartodoc/pandoc/blocks.py +++ b/quartodoc/pandoc/blocks.py @@ -157,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 @@ -398,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() diff --git a/quartodoc/pandoc/inlines.py b/quartodoc/pandoc/inlines.py index 6d06943..93a2ebf 100644 --- a/quartodoc/pandoc/inlines.py +++ b/quartodoc/pandoc/inlines.py @@ -65,7 +65,7 @@ def as_list_item(self): # TypeAlias declared here to avoid forward-references which # break beartype -InlineContentItem = Union[str, Inline] +InlineContentItem = Union[str, Inline, None] InlineContent: TypeAlias = Union[InlineContentItem, Sequence[InlineContentItem]] diff --git a/quartodoc/tests/pandoc/test_blocks.py b/quartodoc/tests/pandoc/test_blocks.py index 613fbbf..b831066 100644 --- a/quartodoc/tests/pandoc/test_blocks.py +++ b/quartodoc/tests/pandoc/test_blocks.py @@ -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"]) @@ -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") diff --git a/quartodoc/tests/pandoc/test_inlines.py b/quartodoc/tests/pandoc/test_inlines.py index 287de2c..e5c6bf9 100644 --- a/quartodoc/tests/pandoc/test_inlines.py +++ b/quartodoc/tests/pandoc/test_inlines.py @@ -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**"