Skip to content

Commit 4ca6b0b

Browse files
Merge pull request #215 from adamtheturtle/title-table
Error cleanly when a table is given a title
2 parents 2952d12 + 3692664 commit 4ca6b0b

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

src/sphinx_notion/__init__.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -458,18 +458,31 @@ def _(
458458
"""
459459
del section_level
460460

461+
for child in node.children:
462+
if isinstance(child, nodes.title):
463+
table_no_titles_msg = (
464+
f"Table has a title '{child.astext()}', but Notion tables "
465+
"do not have titles."
466+
)
467+
# Ignore error which is about a type error, but we want to
468+
# raise a value error because the user has not sent anything to
469+
# do with types.
470+
raise ValueError(table_no_titles_msg) # noqa: TRY004
471+
461472
table_structure = _extract_table_structure(node=node)
462473

463474
if len(table_structure.header_rows) > 1:
464-
msg = "Tables with multiple header rows are not supported."
465-
raise ValueError(msg)
475+
table_multiple_header_rows_msg = (
476+
"Tables with multiple header rows are not supported."
477+
)
478+
raise ValueError(table_multiple_header_rows_msg)
466479

467480
if table_structure.num_stub_columns > 1:
468-
msg = (
481+
table_more_than_one_stub_column_msg = (
469482
f"Tables with more than 1 stub column are not supported. "
470483
f"Found {table_structure.num_stub_columns} stub columns."
471484
)
472-
raise ValueError(msg)
485+
raise ValueError(table_more_than_one_stub_column_msg)
473486

474487
rows = [*table_structure.header_rows, *table_structure.body_rows]
475488
table = UnoTable(

tests/test_integration.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1919,3 +1919,35 @@ def test_list_table_stub_columns_two(
19191919
make_app=make_app,
19201920
tmp_path=tmp_path,
19211921
)
1922+
1923+
1924+
def test_list_table_with_title_error(
1925+
*,
1926+
make_app: Callable[..., SphinxTestApp],
1927+
tmp_path: Path,
1928+
) -> None:
1929+
"""
1930+
List table with title raises ValueError since Notion tables do not have
1931+
titles.
1932+
"""
1933+
rst_content = """
1934+
.. list-table:: My Table Title
1935+
:header-rows: 1
1936+
1937+
* - Header 1
1938+
- Header 2
1939+
* - Cell 1
1940+
- Cell 2
1941+
"""
1942+
1943+
expected_message = (
1944+
r"^Table has a title 'My Table Title', but Notion tables do not "
1945+
r"have titles.$"
1946+
)
1947+
with pytest.raises(expected_exception=ValueError, match=expected_message):
1948+
_assert_rst_converts_to_notion_objects(
1949+
rst_content=rst_content,
1950+
expected_objects=[],
1951+
make_app=make_app,
1952+
tmp_path=tmp_path,
1953+
)

0 commit comments

Comments
 (0)