Skip to content

Commit f047eb1

Browse files
committed
Handle caption block children
1 parent 81c79c4 commit f047eb1

File tree

4 files changed

+75
-34
lines changed

4 files changed

+75
-34
lines changed

jsondoc/convert/html.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
from bs4 import BeautifulSoup, Comment, Doctype, NavigableString
66
from pydantic import BaseModel
77

8-
from jsondoc.convert.utils import (
8+
from jsondoc.convert.placeholder import (
99
BreakElementPlaceholderBlock,
1010
CaptionPlaceholderBlock,
1111
CellPlaceholderBlock,
1212
FigurePlaceholderBlock,
13+
)
14+
from jsondoc.convert.utils import (
1315
append_rich_text_to_block,
1416
append_to_parent_block,
1517
append_to_rich_text,
@@ -35,6 +37,7 @@
3537
)
3638
from jsondoc.models.block.base import BlockBase
3739
from jsondoc.models.block.types.image import ImageBlock
40+
from jsondoc.models.block.types.paragraph import ParagraphBlock
3841
from jsondoc.models.block.types.rich_text.base import RichTextBase
3942
from jsondoc.models.block.types.rich_text.equation import RichTextEquation
4043
from jsondoc.models.block.types.rich_text.text import Link, RichTextText
@@ -232,6 +235,30 @@ def override_reconcile_to_figure_placeholder_block(
232235
return ret
233236

234237

238+
def override_reconcile_to_caption_placeholder_block(
239+
parent: CaptionPlaceholderBlock, children: List[CHILDREN_TYPE]
240+
):
241+
"""
242+
Given a caption placeholder block and a list of children,
243+
this function will get the rich text from the children
244+
and set it as the rich text of the caption placeholder block
245+
Finally, it will return the caption placeholder block
246+
"""
247+
final_rich_text = []
248+
for child in children:
249+
if isinstance(child, RichTextBase):
250+
final_rich_text.append(child)
251+
elif isinstance(child, BlockBase):
252+
final_rich_text.extend(get_rich_text_from_block(child))
253+
else:
254+
pass
255+
# raise ValueError(f"Unsupported type: {type(child)}")
256+
257+
parent.rich_text = final_rich_text
258+
259+
return [parent]
260+
261+
235262
# Override append functions
236263
# Maps pairs of (parent_block_type, child_block_type) to a function
237264
# that appends the child block to the parent block
@@ -242,6 +269,7 @@ def override_reconcile_to_figure_placeholder_block(
242269

243270
OVERRIDE_RECONCILE_FUNCTIONS = {
244271
FigurePlaceholderBlock: override_reconcile_to_figure_placeholder_block,
272+
CaptionPlaceholderBlock: override_reconcile_to_caption_placeholder_block,
245273
}
246274

247275

jsondoc/convert/placeholder.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List, Literal, Optional, Type
2+
3+
from jsondoc.models.block.base import BlockBase
4+
from jsondoc.models.block.types.rich_text.base import RichTextBase
5+
6+
7+
class PlaceholderBlockBase(BlockBase):
8+
"""
9+
Base class for all placeholder blocks.
10+
"""
11+
12+
pass
13+
14+
15+
class BreakElementPlaceholderBlock(PlaceholderBlockBase):
16+
type: Literal["break_element_placeholder"] = "break_element_placeholder"
17+
18+
19+
class CaptionPlaceholderBlock(PlaceholderBlockBase):
20+
type: Literal["caption_placeholder"] = "caption_placeholder"
21+
rich_text: Optional[List[RichTextBase]] = None
22+
# children: Optional[List[BlockBase]] = None
23+
24+
25+
class CellPlaceholderBlock(PlaceholderBlockBase):
26+
type: Literal["cell_placeholder"] = "cell_placeholder"
27+
rich_text: Optional[List[RichTextBase]] = None
28+
29+
30+
class FigurePlaceholderBlock(PlaceholderBlockBase):
31+
type: Literal["figure_placeholder"] = "figure_placeholder"
32+
33+
34+
PLACEHOLDER_BLOCKS: List[Type[PlaceholderBlockBase]] = [
35+
BreakElementPlaceholderBlock,
36+
CaptionPlaceholderBlock,
37+
CellPlaceholderBlock,
38+
FigurePlaceholderBlock,
39+
]

jsondoc/convert/utils.py

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
from bs4 import Tag
77
from pydantic import validate_call
88

9+
from jsondoc.convert.placeholder import (
10+
CaptionPlaceholderBlock,
11+
CellPlaceholderBlock,
12+
PlaceholderBlockBase,
13+
)
914
from jsondoc.models.block.base import BlockBase
1015
from jsondoc.models.block.types.bulleted_list_item import (
1116
BulletedListItem,
@@ -45,39 +50,6 @@
4550
all_whitespace_re = re.compile(r"[\s]+")
4651

4752

48-
class PlaceholderBlockBase(BlockBase):
49-
"""
50-
Base class for all placeholder blocks.
51-
"""
52-
53-
pass
54-
55-
56-
class BreakElementPlaceholderBlock(PlaceholderBlockBase):
57-
type: Literal["break_element_placeholder"] = "break_element_placeholder"
58-
59-
60-
class CaptionPlaceholderBlock(PlaceholderBlockBase):
61-
type: Literal["caption_placeholder"] = "caption_placeholder"
62-
rich_text: Optional[List[RichTextBase]] = None
63-
64-
65-
class CellPlaceholderBlock(PlaceholderBlockBase):
66-
type: Literal["cell_placeholder"] = "cell_placeholder"
67-
rich_text: Optional[List[RichTextBase]] = None
68-
69-
70-
class FigurePlaceholderBlock(PlaceholderBlockBase):
71-
type: Literal["figure_placeholder"] = "figure_placeholder"
72-
73-
74-
PLACEHOLDER_BLOCKS: List[Type[PlaceholderBlockBase]] = [
75-
BreakElementPlaceholderBlock,
76-
CaptionPlaceholderBlock,
77-
CellPlaceholderBlock,
78-
FigurePlaceholderBlock,
79-
]
80-
8153
BLOCKS_WITH_RICH_TEXT: List[Type[BlockBase]] = [
8254
ParagraphBlock,
8355
CodeBlock,

jsondoc/rules.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from jsondoc.convert.placeholder import CaptionPlaceholderBlock
12
from jsondoc.models.block.base import BlockBase
23
from jsondoc.models.block.types.bulleted_list_item import BulletedListItemBlock
34
from jsondoc.models.block.types.code import CodeBlock
@@ -58,6 +59,7 @@
5859
TableRowBlock: [],
5960
ToDoBlock: ALL_BLOCK_TYPES,
6061
ToggleBlock: ALL_BLOCK_TYPES,
62+
CaptionPlaceholderBlock: [ParagraphBlock],
6163
}
6264

6365

0 commit comments

Comments
 (0)