Skip to content

Commit 80c3591

Browse files
committed
Add possibility to enforce row or col span
For instance, this is helpful to unify the appearance of tables using a row span for some rows and no row span for others. Without an explicitly defined row span of 1, CSS will not apply the corresponding style. Signed-off-by: Benny Fuhry <benny.fuhry@intel.com>
1 parent a01f62c commit 80c3591

File tree

3 files changed

+95
-2
lines changed

3 files changed

+95
-2
lines changed

neoteroi/mkdocs/markdown/tables/spantable.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ class Cell:
1414
skip: bool = False
1515
col_span: int = 1
1616
row_span: int = 1
17+
col_span_defined: bool = False
18+
row_span_defined: bool = False
1719
props: Optional[Dict[str, str]] = None
1820

1921
@property
@@ -72,6 +74,9 @@ def get_matrix(table: Table) -> Matrix:
7274
raw_cols_span = match.group(1)
7375
raw_rows_span = match.group(2)
7476

77+
col_span_defined = False
78+
row_span_defined = False
79+
7580
if raw_cols_span is None and raw_rows_span is None:
7681
# Automatic mode: increase the span until empty cells are found
7782
# columns span takes precedence over rows span
@@ -81,10 +86,21 @@ def get_matrix(table: Table) -> Matrix:
8186
row_index + 1,
8287
slice(column_index, column_index + cols_span),
8388
)
89+
90+
if cols_span > 1:
91+
col_span_defined = True
92+
if rows_span > 1:
93+
row_span_defined = True
94+
8495
else:
8596
cols_span = max(int(raw_cols_span or 1), 1)
8697
rows_span = max(int(raw_rows_span or 1), 1)
8798

99+
if int(raw_cols_span) > 0:
100+
col_span_defined = True
101+
if int(raw_rows_span) > 0:
102+
row_span_defined = True
103+
88104
if cols_span > 0 or rows_span > 0:
89105
for coords in _iter_coords(
90106
column_index, row_index, cols_span, rows_span
@@ -100,6 +116,8 @@ def get_matrix(table: Table) -> Matrix:
100116
skip=not current,
101117
col_span=cols_span,
102118
row_span=rows_span,
119+
col_span_defined=col_span_defined,
120+
row_span_defined=row_span_defined,
103121
props=props,
104122
)
105123
else:

neoteroi/mkdocs/spantable/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ def _set_table(self, span_table: SpanTable, table_element: etree.Element):
119119
if cell.html_class:
120120
props["class"] = cell.html_class
121121

122-
if cell.col_span > 1:
122+
if cell.col_span_defined:
123123
props["colspan"] = str(cell.col_span)
124124

125-
if cell.row_span > 1:
125+
if cell.row_span_defined:
126126
props["rowspan"] = str(cell.row_span)
127127

128128
td = etree.SubElement(tr, "td", props)

tests/test_spantable.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,81 @@
324324
<td>Warlock</td>
325325
</tr>
326326
</table>
327+
</div>
328+
""",
329+
],
330+
[
331+
"""
332+
::spantable::"
333+
334+
| Class | Power Source |
335+
| ----------------- | ----------------- |
336+
| Warlock @span=0:0 | 1 |
337+
338+
::end-spantable::
339+
""",
340+
"""
341+
<div class="span-table-wrapper">
342+
<table class="span-table">
343+
<tr>
344+
<td>Class</td>
345+
<td>Power Source</td>
346+
</tr>
347+
<tr>
348+
<td>Warlock</td>
349+
<td>1</td>
350+
</tr>
351+
</table>
352+
</div>
353+
""",
354+
],
355+
[
356+
"""
357+
::spantable::"
358+
359+
| Class | Power Source |
360+
| ----------------- | ----------------- |
361+
| Warlock @span=0:1 | 2 |
362+
363+
::end-spantable::
364+
""",
365+
"""
366+
<div class="span-table-wrapper">
367+
<table class="span-table">
368+
<tr>
369+
<td>Class</td>
370+
<td>Power Source</td>
371+
</tr>
372+
<tr>
373+
<td rowspan="1">Warlock</td>
374+
<td>2</td>
375+
</tr>
376+
</table>
377+
</div>
378+
""",
379+
],
380+
[
381+
"""
382+
::spantable::"
383+
384+
| Class | Power Source |
385+
| ----------------- | ----------------- |
386+
| Warlock @span=1:0 | 3 |
387+
388+
::end-spantable::
389+
""",
390+
"""
391+
<div class="span-table-wrapper">
392+
<table class="span-table">
393+
<tr>
394+
<td>Class</td>
395+
<td>Power Source</td>
396+
</tr>
397+
<tr>
398+
<td colspan="1">Warlock</td>
399+
<td>3</td>
400+
</tr>
401+
</table>
327402
</div>
328403
""",
329404
],

0 commit comments

Comments
 (0)