12
12
from dataclasses import dataclass
13
13
14
14
from quartodoc .pandoc .components import Attr
15
- from quartodoc .pandoc .inlines import Inline , Inlines , InlineContent , inlinecontent_to_str
15
+ from quartodoc .pandoc .inlines import (
16
+ Inline ,
17
+ Inlines ,
18
+ InlineContent ,
19
+ inlinecontent_to_str ,
20
+ str_as_list_item ,
21
+ )
16
22
17
23
__all__ = (
18
24
"Block" ,
@@ -56,14 +62,24 @@ def html(self):
56
62
f"html property method not implemented for: { type (self )} "
57
63
)
58
64
65
+ @property
66
+ def as_list_item (self ):
67
+ """
68
+ A block as a list item
69
+
70
+ Some block type need special spacing consideration
71
+ """
72
+ # To balance correctness, compactness and readability,
73
+ # block items get an empty line between them and the next
74
+ # item.
75
+ return f"{ self } \n \n "
76
+
59
77
60
78
# TypeAlias declared here to avoid forward-references which
61
79
# break beartype
62
- BlockContent : TypeAlias = InlineContent | Block | Sequence [Block ]
63
80
ContentItem : TypeAlias = str | Inline | Block
64
- DefinitionItem : TypeAlias = tuple [
65
- InlineContent , ContentItem | Sequence [BlockContent ]
66
- ]
81
+ BlockContent : TypeAlias = ContentItem | Sequence [ContentItem ]
82
+ DefinitionItem : TypeAlias = tuple [InlineContent , BlockContent ]
67
83
68
84
69
85
@dataclass
@@ -79,7 +95,7 @@ def __str__(self):
79
95
Div_TPL = """\
80
96
::: {{{attr}}}
81
97
{content}
82
- :::
98
+ :::\
83
99
"""
84
100
85
101
@dataclass
@@ -105,7 +121,7 @@ def __str__(self):
105
121
# definition with more than one block.
106
122
DefinitionItem_TPL = """\
107
123
{term}
108
- {definitions}
124
+ {definitions}\
109
125
"""
110
126
111
127
Definition_TPL = """
@@ -167,11 +183,15 @@ class Para(Block):
167
183
Paragraph
168
184
"""
169
185
content : Optional [InlineContent ] = None
170
-
171
186
def __str__ (self ):
172
187
content = inlinecontent_to_str (self .content )
173
188
return f"{ SEP } { content } { SEP } "
174
189
190
+ @property
191
+ def as_list_item (self ):
192
+ content = inlinecontent_to_str (self .content )
193
+ return f"{ content } \n \n "
194
+
175
195
176
196
@dataclass
177
197
class Header (Block ):
@@ -244,6 +264,9 @@ def html(self):
244
264
attr = f" { self .attr .html } " if self .attr else ""
245
265
return CodeBlockHTML_TPL .format (content = content , attr = attr )
246
266
267
+ @property
268
+ def as_list_item (self ):
269
+ return f"\n { self } \n \n "
247
270
248
271
249
272
@dataclass
@@ -337,8 +360,16 @@ def fmt(s:str, pfx: str):
337
360
# mnop
338
361
if not s :
339
362
return ""
340
- pad = " " * (len (pfx ) + 1 )
341
- return f"{ pfx } " + indent (s , pad ).lstrip (pad )
363
+
364
+ # We avoid having a space after the item bullet/number if
365
+ # there is no content on that line
366
+ space = ""
367
+ indent_size = len (pfx ) + 1
368
+ s_indented = indent (s , " " * indent_size )
369
+ if s [0 ] != "\n " :
370
+ space = " "
371
+ s_indented = s_indented [indent_size :]
372
+ return f"{ pfx } { space } { s_indented } "
342
373
343
374
if not content :
344
375
return ""
@@ -348,18 +379,16 @@ def fmt(s:str, pfx: str):
348
379
else :
349
380
pfx_it = (f"{ i } ." for i in itertools .count (1 ))
350
381
351
- if isinstance (content , (str , Inline , Block )):
352
- return fmt (str (content ), next (pfx_it ))
382
+ if isinstance (content , str ):
383
+ return fmt (str_as_list_item (content ), next (pfx_it ))
384
+ elif isinstance (content , (Inline , Block )):
385
+ return fmt (content .as_list_item , next (pfx_it ))
353
386
elif isinstance (content , abc .Sequence ):
354
- # To balance correctness, compactness and readability,
355
- # items with content get an empty line between them and
356
- # the next item.
357
- items = []
358
- pad = ""
359
- for item in content :
360
- s = fmt (str (item ), next (pfx_it ))
361
- pad = f"{ SEP } { SEP } " if isinstance (item , Block ) else f"{ SEP } "
362
- items .append (f"{ s } { pad } " )
363
- return "" .join (items )[:- len (pad )]
387
+ it = (
388
+ str_as_list_item (c ) if isinstance (c , str ) else c .as_list_item
389
+ for c in content
390
+ )
391
+ items = (fmt (s , next (pfx_it )) for s in it )
392
+ return "" .join (items ).strip ()
364
393
else :
365
394
raise TypeError (f"Could not process type: { type (content )} " )
0 commit comments