Skip to content

MkHtmlTable

Show source on GitHub

Class representing a html table.

Description

Compared to MkTable, this will end up with a more verbose output, but it can contain more complex Markdown in cells.

Example: Regular

Jinja

{% set code_col = [_mk.MkCode("print('hello world')
sys.exit()"), _mk.MkCode("print('hello world')
sys.exit()")] %}
{% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %}
{% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %}
{% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %}
{{ data | MkHtmlTable }}

Python

MkHtmlTable(
    data={
        "Admonitions": [
            MkAdmonition("Admonition inside cell"),
            MkAdmonition("Admonition inside cell"),
        ],
        "Code": [
            MkCode("print('hello world')\nsys.exit()"),
            MkCode("print('hello world')\nsys.exit()"),
        ],
        "Tabs": [MkTabbed(tabs=[...]), MkTabbed(tabs=[...])],
    }
)

Code Admonitions Tabs
print('hello world')
sys.exit()

Info

Admonition inside cell

Tab a

Tab b

print('hello world')
sys.exit()

Info

Admonition inside cell

Tab a

Tab b

<table markdown="1">
<tr>
<td>
Code
</td>
<td>
Admonitions
</td>
<td>
Tabs
</td>
</tr>
<tr>
<td>
```` {.python }
print('hello world')
sys.exit()
````
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
<tr>
<td>
```` {.python }
print('hello world')
sys.exit()
````
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
</table>
<table>
<tr>
<td>
Code
</td>
<td>
Admonitions
</td>
<td>
Tabs
</td>
</tr>
<tr>
<td>
<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">)</span>
</span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
</span></code></pre></div>
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
<tr>
<td>
<div class="language-python highlight"><pre><span></span><code><span id="__span-1-1"><a id="__codelineno-1-1" name="__codelineno-1-1" href="#__codelineno-1-1"></a><span class="nb">print</span><span class="p">(</span><span class="s1">&#39;hello world&#39;</span><span class="p">)</span>
</span><span id="__span-1-2"><a id="__codelineno-1-2" name="__codelineno-1-2" href="#__codelineno-1-2"></a><span class="n">sys</span><span class="o">.</span><span class="n">exit</span><span class="p">()</span>
</span></code></pre></div>
</td>
<td>
!!! info
    Admonition inside cell

</td>
<td>
===! "A"
    Tab a

=== "B"
    Tab b

</td>
</tr>
</table>
MkHtmlTable
├── MkCode("print('hello world')\nsys.exit()")
│   ╰── MkText("print('hello world')\nsys.exit()")
├── MkCode("print('hello world')\nsys.exit()")
│   ╰── MkText("print('hello world')\nsys.exit()")
├── MkAdmonition('Admonition inside cell')
│   ╰── MkText('Admonition inside cell')
├── MkAdmonition('Admonition inside cell')
│   ╰── MkText('Admonition inside cell')
├── MkTabbed(tabs=[...])
│   ├── MkTab(title='A', content='Tab a', new=True)
│   │   ╰── MkText('Tab a')
│   ╰── MkTab(title='B', content='Tab b')
│       ╰── MkText('Tab b')
├── MkTabbed(tabs=[...])
│   ├── MkTab(title='A', content='Tab a', new=True)
│   │   ╰── MkText('Tab a')
│   ╰── MkTab(title='B', content='Tab b')
│       ╰── MkText('Tab b')

Bases: MkBaseTable

Name Children Inherits
MkBaseTable
mknodes.basenodes.mkbasetable
Base Class for MkTables. Only deals with managing the data.
graph TD
  94854583012912["mkhtmltable.MkHtmlTable"]
  94854583040896["mkbasetable.MkBaseTable"]
  94854582919984["mkcontainer.MkContainer"]
  94854582916880["mknode.MkNode"]
  94854582838576["node.Node"]
  140544995341632["builtins.object"]
  94854583040896 --> 94854583012912
  94854582919984 --> 94854583040896
  94854582916880 --> 94854582919984
  94854582838576 --> 94854582916880
  140544995341632 --> 94854582838576
/home/runner/work/mknodes/mknodes/mknodes/basenodes/mkhtmltable/metadata.toml
[metadata]
icon = "octicon:table-24"
status = "new"
name = "MkHtmlTable"

[examples.regular]
title = "Regular"
jinja = """
{% set code_col = [_mk.MkCode("print('hello world')\nsys.exit()"), _mk.MkCode("print('hello world')\nsys.exit()")] %}
{% set admonitions = [_mk.MkAdmonition("Admonition inside cell"), _mk.MkAdmonition("Admonition inside cell")] %}
{% set tabs = [_mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"])), _mk.MkTabbed(dict(A=["Tab a"], B=["Tab b"]))] %}
{% set data = {"Code": code_col, "Admonitions": admonitions, "Tabs": tabs} %}
{{ data | MkHtmlTable }}
"""

[output.markdown]
template = """
{% set table = node.data %}
<table markdown="1">
<tr>
{% for header in table.keys() %}
<td>
{{ header }}
</td>
{% endfor %}
</tr>
{% for row in node.iter_rows() %}
<tr>
{% for item in row %}
<td>
{{ item }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
"""
mknodes.basenodes.mkhtmltable.MkHtmlTable
class MkHtmlTable(mkbasetable.MkBaseTable):
    """Class representing a html table.

    Compared to MkTable, this will end up with a more verbose output,
    but it can contain more complex Markdown in cells.
    """

    STATUS = "new"

    def get_element(self) -> xml.Table | None:
        table_data = self.data  # property
        if not any(table_data[k] for k in table_data):
            return None
        root = xml.Table(markdown=True)
        data = [[str(k) for k in row] for row in self.iter_rows()]
        headers = list(table_data.keys())
        data.insert(0, headers)
        for items in data:
            tr = xml.Tr(parent=root)
            for item in items:
                td = xml.Td(parent=tr)
                td.text = "\n" + item + "\n"
        return root

    def _to_markdown(self) -> str:
        root = self.get_element()
        return root.to_string(space="") if root is not None else ""