Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions neoteroi/mkdocs/markdown/tables/spantable.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Cell:
skip: bool = False
col_span: int = 1
row_span: int = 1
col_span_defined: bool = False
row_span_defined: bool = False
props: Optional[Dict[str, str]] = None

@property
Expand Down Expand Up @@ -72,6 +74,9 @@ def get_matrix(table: Table) -> Matrix:
raw_cols_span = match.group(1)
raw_rows_span = match.group(2)

col_span_defined = False
row_span_defined = False

if raw_cols_span is None and raw_rows_span is None:
# Automatic mode: increase the span until empty cells are found
# columns span takes precedence over rows span
Expand All @@ -81,10 +86,21 @@ def get_matrix(table: Table) -> Matrix:
row_index + 1,
slice(column_index, column_index + cols_span),
)

if cols_span > 1:
col_span_defined = True
if rows_span > 1:
row_span_defined = True

else:
cols_span = max(int(raw_cols_span or 1), 1)
rows_span = max(int(raw_rows_span or 1), 1)

if int(raw_cols_span) > 0:
col_span_defined = True
if int(raw_rows_span) > 0:
row_span_defined = True

if cols_span > 0 or rows_span > 0:
for coords in _iter_coords(
column_index, row_index, cols_span, rows_span
Expand All @@ -100,6 +116,8 @@ def get_matrix(table: Table) -> Matrix:
skip=not current,
col_span=cols_span,
row_span=rows_span,
col_span_defined=col_span_defined,
row_span_defined=row_span_defined,
props=props,
)
else:
Expand Down
4 changes: 2 additions & 2 deletions neoteroi/mkdocs/spantable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ def _set_table(self, span_table: SpanTable, table_element: etree.Element):
if cell.html_class:
props["class"] = cell.html_class

if cell.col_span > 1:
if cell.col_span_defined:
props["colspan"] = str(cell.col_span)

if cell.row_span > 1:
if cell.row_span_defined:
props["rowspan"] = str(cell.row_span)

td = etree.SubElement(tr, "td", props)
Expand Down
75 changes: 75 additions & 0 deletions tests/test_spantable.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,81 @@
<td>Warlock</td>
</tr>
</table>
</div>
""",
],
[
"""
::spantable::"

| Class | Power Source |
| ----------------- | ----------------- |
| Warlock @span=0:0 | 1 |

::end-spantable::
""",
"""
<div class="span-table-wrapper">
<table class="span-table">
<tr>
<td>Class</td>
<td>Power Source</td>
</tr>
<tr>
<td>Warlock</td>
<td>1</td>
</tr>
</table>
</div>
""",
],
[
"""
::spantable::"

| Class | Power Source |
| ----------------- | ----------------- |
| Warlock @span=0:1 | 2 |

::end-spantable::
""",
"""
<div class="span-table-wrapper">
<table class="span-table">
<tr>
<td>Class</td>
<td>Power Source</td>
</tr>
<tr>
<td rowspan="1">Warlock</td>
<td>2</td>
</tr>
</table>
</div>
""",
],
[
"""
::spantable::"

| Class | Power Source |
| ----------------- | ----------------- |
| Warlock @span=1:0 | 3 |

::end-spantable::
""",
"""
<div class="span-table-wrapper">
<table class="span-table">
<tr>
<td>Class</td>
<td>Power Source</td>
</tr>
<tr>
<td colspan="1">Warlock</td>
<td>3</td>
</tr>
</table>
</div>
""",
],
Expand Down