nixos-render-docs: add support for tables
This commit is contained in:
@@ -257,6 +257,47 @@ class HTMLRenderer(Renderer):
|
|||||||
'</p>'
|
'</p>'
|
||||||
'<div class="figure-contents">'
|
'<div class="figure-contents">'
|
||||||
)
|
)
|
||||||
|
def table_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return (
|
||||||
|
'<div class="informaltable">'
|
||||||
|
'<table class="informaltable" border="1">'
|
||||||
|
)
|
||||||
|
def table_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return (
|
||||||
|
'</table>'
|
||||||
|
'</div>'
|
||||||
|
)
|
||||||
|
def thead_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
cols = []
|
||||||
|
for j in range(i + 1, len(tokens)):
|
||||||
|
if tokens[j].type == 'thead_close':
|
||||||
|
break
|
||||||
|
elif tokens[j].type == 'th_open':
|
||||||
|
cols.append(cast(str, tokens[j].attrs.get('style', 'left')).removeprefix('text-align:'))
|
||||||
|
return "".join([
|
||||||
|
"<colgroup>",
|
||||||
|
"".join([ f'<col align="{col}" />' for col in cols ]),
|
||||||
|
"</colgroup>",
|
||||||
|
"<thead>",
|
||||||
|
])
|
||||||
|
def thead_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return "</thead>"
|
||||||
|
def tr_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return "<tr>"
|
||||||
|
def tr_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return "</tr>"
|
||||||
|
def th_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return f'<th align="{cast(str, token.attrs.get("style", "left")).removeprefix("text-align:")}">'
|
||||||
|
def th_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return "</th>"
|
||||||
|
def tbody_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return "<tbody>"
|
||||||
|
def tbody_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return "</tbody>"
|
||||||
|
def td_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return f'<td align="{cast(str, token.attrs.get("style", "left")).removeprefix("text-align:")}">'
|
||||||
|
def td_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
return "</td>"
|
||||||
|
|
||||||
def _make_hN(self, level: int) -> tuple[str, str]:
|
def _make_hN(self, level: int) -> tuple[str, str]:
|
||||||
return f"h{min(6, max(1, level + self._hlevel_offset))}", ""
|
return f"h{min(6, max(1, level + self._hlevel_offset))}", ""
|
||||||
|
|||||||
@@ -95,6 +95,18 @@ class Renderer:
|
|||||||
"figure_close": self.figure_close,
|
"figure_close": self.figure_close,
|
||||||
"figure_title_open": self.figure_title_open,
|
"figure_title_open": self.figure_title_open,
|
||||||
"figure_title_close": self.figure_title_close,
|
"figure_title_close": self.figure_title_close,
|
||||||
|
"table_open": self.table_open,
|
||||||
|
"table_close": self.table_close,
|
||||||
|
"thead_open": self.thead_open,
|
||||||
|
"thead_close": self.thead_close,
|
||||||
|
"tr_open": self.tr_open,
|
||||||
|
"tr_close": self.tr_close,
|
||||||
|
"th_open": self.th_open,
|
||||||
|
"th_close": self.th_close,
|
||||||
|
"tbody_open": self.tbody_open,
|
||||||
|
"tbody_close": self.tbody_close,
|
||||||
|
"td_open": self.td_open,
|
||||||
|
"td_close": self.td_close,
|
||||||
}
|
}
|
||||||
|
|
||||||
self._admonitions = {
|
self._admonitions = {
|
||||||
@@ -240,6 +252,30 @@ class Renderer:
|
|||||||
raise RuntimeError("md token not supported", token)
|
raise RuntimeError("md token not supported", token)
|
||||||
def figure_title_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
def figure_title_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
raise RuntimeError("md token not supported", token)
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def table_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def table_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def thead_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def thead_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def tr_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def tr_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def th_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def th_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def tbody_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def tbody_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def td_open(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
def td_close(self, token: Token, tokens: Sequence[Token], i: int) -> str:
|
||||||
|
raise RuntimeError("md token not supported", token)
|
||||||
|
|
||||||
def _is_escaped(src: str, pos: int) -> bool:
|
def _is_escaped(src: str, pos: int) -> bool:
|
||||||
found = 0
|
found = 0
|
||||||
@@ -506,6 +542,7 @@ class Converter(ABC, Generic[TR]):
|
|||||||
},
|
},
|
||||||
renderer_cls=self.ForbiddenRenderer
|
renderer_cls=self.ForbiddenRenderer
|
||||||
)
|
)
|
||||||
|
self._md.enable('table')
|
||||||
self._md.use(
|
self._md.use(
|
||||||
container_plugin,
|
container_plugin,
|
||||||
name="blockattr",
|
name="blockattr",
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import nixos_render_docs as nrd
|
import nixos_render_docs as nrd
|
||||||
import pytest
|
import pytest
|
||||||
|
import textwrap
|
||||||
|
|
||||||
from sample_md import sample1
|
from sample_md import sample1
|
||||||
|
|
||||||
@@ -83,6 +84,41 @@ def test_images() -> None:
|
|||||||
</p>
|
</p>
|
||||||
""")
|
""")
|
||||||
|
|
||||||
|
def test_tables() -> None:
|
||||||
|
c = Converter({}, {})
|
||||||
|
assert c._render(textwrap.dedent("""
|
||||||
|
| d | l | m | r |
|
||||||
|
|---|:--|:-:|--:|
|
||||||
|
| a | b | c | d |
|
||||||
|
""")) == unpretty("""
|
||||||
|
<div class="informaltable">
|
||||||
|
<table class="informaltable" border="1">
|
||||||
|
<colgroup>
|
||||||
|
<col align="left" />
|
||||||
|
<col align="left" />
|
||||||
|
<col align="center" />
|
||||||
|
<col align="right" />
|
||||||
|
</colgroup>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th align="left">d</th>
|
||||||
|
<th align="left">l</th>
|
||||||
|
<th align="center">m</th>
|
||||||
|
<th align="right">r</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td align="left">a</td>
|
||||||
|
<td align="left">b</td>
|
||||||
|
<td align="center">c</td>
|
||||||
|
<td align="right">d</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
""")
|
||||||
|
|
||||||
def test_full() -> None:
|
def test_full() -> None:
|
||||||
c = Converter({ 'man(1)': 'http://example.org' }, {})
|
c = Converter({ 'man(1)': 'http://example.org' }, {})
|
||||||
assert c._render(sample1) == unpretty("""
|
assert c._render(sample1) == unpretty("""
|
||||||
|
|||||||
Reference in New Issue
Block a user