diff --git a/examples/html/html_all_elements.html b/examples/html/html_all_elements.html index 7536ce3..25bcc7d 100644 --- a/examples/html/html_all_elements.html +++ b/examples/html/html_all_elements.html @@ -445,14 +445,7 @@

Tables

This is a caption for a table - - - ID - Name - Date - Address - - + Table footer info @@ -472,6 +465,14 @@

Tables

999 Spruce Lane, Somewhere, CA 94101 + + + ID + Name + Date + Address + +
diff --git a/examples/html/html_just_table.html b/examples/html/html_just_table.html new file mode 100644 index 0000000..4317e80 --- /dev/null +++ b/examples/html/html_just_table.html @@ -0,0 +1,94 @@ + + + + + + + + HTML Patterns + + + + + + + + + +
+

HTML

+

Every html element in one place. Just waiting to be styled.

+
+ +
+

Tables

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ This is a caption for a table +
Table footer info
#999-32acFirst Name13 May, 2013999 Spruce Lane, Somewhere, CA 94101
#888-32ddSample Name17 May, 1984999 Spruce Lane, Somewhere, CA 94101
IDNameDateAddress
+
+
+

Footer

+ +
+ + diff --git a/jsondoc/convert/html.py b/jsondoc/convert/html.py index cb238ab..f87678b 100644 --- a/jsondoc/convert/html.py +++ b/jsondoc/convert/html.py @@ -834,7 +834,15 @@ def convert_tr(self, el, convert_as_inline): """ Table row """ - return ConvertOutput(main_object=create_table_row_block()) + # Check if this is a header row + is_header = False + is_footer = False + if el.find_parent("thead") or el.find_parent("th"): + is_header = True + # Check if this is a footer row + if el.find_parent("tfoot"): + is_footer = True + return ConvertOutput(main_object=create_table_row_block(isHeader=is_header, isFooter=is_footer)) def html_to_jsondoc(html: str | bytes, **options) -> Page | BlockBase | List[BlockBase]: diff --git a/jsondoc/convert/markdown.py b/jsondoc/convert/markdown.py index 73d8155..364ad1b 100644 --- a/jsondoc/convert/markdown.py +++ b/jsondoc/convert/markdown.py @@ -349,7 +349,7 @@ def convert_quote_block(self, block: QuoteBlock, convert_as_inline: bool) -> str ) def _convert_table_row_block( - self, block: TableRowBlock, convert_as_inline: bool, is_headrow: bool + self, block: TableRowBlock, convert_as_inline: bool, is_headrow: bool, is_footrow: bool ) -> str: # cells = block.table_row.cells @@ -383,18 +383,37 @@ def _convert_table_row_block( return overline + "|" + text + "\n" + underline def convert_table_block(self, block: TableBlock, convert_as_inline: bool) -> str: + # TODO: Caption is not at the very bottom after the footer. Fix it. text = "" - for n, row in enumerate(block.children): - is_headrow = block.table.has_column_header and n == 0 - - text += self._convert_table_row_block( + is_headrow = block.table.has_column_header and row.isHeader + is_footrow = row.isFooter + + textHeader = "\n\n" + textFooter = "" + + if is_headrow: + textHeader = self._convert_table_row_block( row, convert_as_inline, is_headrow=is_headrow, + is_footrow=is_footrow, ) - - return "\n\n" + text + "\n" + elif is_footrow: + textFooter = self._convert_table_row_block( + row, + convert_as_inline, + is_headrow=is_headrow, + is_footrow=is_footrow, + ) + else: + text += self._convert_table_row_block( + row, + convert_as_inline, + is_headrow=is_headrow, + is_footrow=is_footrow, + ) + return textHeader + text + textFooter def jsondoc_to_markdown(jsondoc, **options): diff --git a/jsondoc/convert/utils.py b/jsondoc/convert/utils.py index 811efac..7a1a99b 100644 --- a/jsondoc/convert/utils.py +++ b/jsondoc/convert/utils.py @@ -408,6 +408,8 @@ def create_table_row_block( cells: List[List[RichTextBase]] = [], id: str | None = None, created_time=None, + isHeader: bool = False, + isFooter: bool = False, ) -> TableRowBlock: if id is None: id = generate_id() @@ -420,6 +422,8 @@ def create_table_row_block( created_time=created_time, table_row=TableRow(cells=cells), has_children=False, + isHeader=isHeader, + isFooter=isFooter, ) @@ -430,6 +434,7 @@ def create_table_block( table_width: int | None = None, has_column_header: bool = False, has_row_header: bool = False, + caption: str | None = None, ) -> TableBlock: if id is None: id = generate_id() diff --git a/jsondoc/models/block/types/table_row/__init__.py b/jsondoc/models/block/types/table_row/__init__.py index bbded9f..7a59ea0 100644 --- a/jsondoc/models/block/types/table_row/__init__.py +++ b/jsondoc/models/block/types/table_row/__init__.py @@ -23,3 +23,5 @@ class TableRow(BaseModel): class TableRowBlock(BlockBase): type: Literal['table_row'] = 'table_row' table_row: TableRow + isHeader: bool + isFooter: bool diff --git a/schema/block/types/table_row/table_row_schema.json b/schema/block/types/table_row/table_row_schema.json index c299af4..32d84e3 100644 --- a/schema/block/types/table_row/table_row_schema.json +++ b/schema/block/types/table_row/table_row_schema.json @@ -26,6 +26,12 @@ }, "required": ["cells"], "additionalProperties": false + }, + "isHeader": { + "type": "boolean" + }, + "isFooter": { + "type": "boolean" } }, "required": ["type", "table_row"], diff --git a/tests/html_jsondoc_pairs/test_table_basic.json b/tests/html_jsondoc_pairs/test_table_basic.json index 0a0054f..0d44001 100644 --- a/tests/html_jsondoc_pairs/test_table_basic.json +++ b/tests/html_jsondoc_pairs/test_table_basic.json @@ -49,7 +49,9 @@ } ] ] - } + }, + "isHeader": true, + "isFooter": false }, { "object": "block", @@ -90,7 +92,9 @@ } ] ] - } + }, + "isHeader": false, + "isFooter": false }, { "object": "block", @@ -131,7 +135,9 @@ } ] ] - } + }, + "isHeader": false, + "isFooter": false }, { "object": "block", @@ -172,7 +178,9 @@ } ] ] - } + }, + "isHeader": false, + "isFooter": false } ] } diff --git a/tests/html_jsondoc_pairs/test_table_with_caption.json b/tests/html_jsondoc_pairs/test_table_with_caption.json index 1252349..9d9298a 100644 --- a/tests/html_jsondoc_pairs/test_table_with_caption.json +++ b/tests/html_jsondoc_pairs/test_table_with_caption.json @@ -61,7 +61,9 @@ } ] ] - } + }, + "isHeader": true, + "isFooter": false }, { "object": "block", @@ -112,7 +114,9 @@ } ] ] - } + }, + "isHeader": false, + "isFooter": false }, { "object": "block", @@ -163,7 +167,9 @@ } ] ] - } + }, + "isHeader": false, + "isFooter": false }, { "object": "block", @@ -187,7 +193,9 @@ [], [] ] - } + }, + "isHeader": false, + "isFooter": true } ] }, diff --git a/tests/test_html_to_jsondoc.py b/tests/test_html_to_jsondoc.py index 2c63fcf..9986328 100644 --- a/tests/test_html_to_jsondoc.py +++ b/tests/test_html_to_jsondoc.py @@ -87,6 +87,18 @@ def test_examples(): print("PASS") +def test(): + path = "examples/html/html_just_table.html" + content = open(path, "r").read() + ret = html_to_jsondoc(content) + # print(jsondoc_dump_json(ret, indent=2)) + + # print("\n\nConverted to markdown:\n\n") + # print(jsondoc_to_markdown(ret[0])) + print(jsondoc_to_markdown(ret)) + if __name__ == "__main__": test_examples() test_convert_html_all_elements() + # Test for working specifically on tables with caption + # test() \ No newline at end of file