feature: html table implemented

as envisioned by mistltoe developers
This commit is contained in:
Prof. Dr. Matthias Jung DL9MJ
2025-05-14 15:55:51 +02:00
parent 7485915114
commit 9a747dc8bf
3 changed files with 44 additions and 40 deletions
+3 -6
View File
@@ -215,17 +215,14 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
return f"<{type}>{cell}</{type}>" return f"<{type}>{cell}</{type}>"
def render_table(self, token): def render_table(self, token):
data = token.table
alignment = token.alignment alignment = token.alignment
table = "<table>" table = "<table>"
for i, row in enumerate(data): for i, row in enumerate(token.children):
content = "" content = ""
for j, cell in enumerate(row[0]): for j, cell in enumerate(row.children):
content += self.render_cell_helper(cell, alignment[j], ("th" if i==0 else "td")) content += self.render_cell_helper(self.render_inner(cell), alignment[j], ("th" if i==0 else "td"))
table += self.render_row_helper(content) table += self.render_row_helper(content)
table += "</table>" table += "</table>"
return table return table
+31 -26
View File
@@ -1,4 +1,5 @@
from mistletoe import span_token
from mistletoe.block_token import BlockToken from mistletoe.block_token import BlockToken
@@ -8,20 +9,40 @@ class Table(BlockToken):
return '|' in line return '|' in line
@classmethod @classmethod
def parseHeader(self, line): def parseHeader(cls, line):
# Parse header, extract alignment and make an ordinary row out of it
line = line.strip().strip('|') # Remove leading/trailing whitespace and pipes line = line.strip().strip('|') # Remove leading/trailing whitespace and pipes
columns = [col.strip() for col in line.split('|')] # Split by pipe | columns = [col.strip() for col in line.split('|')] # Split by pipe |
alignment = [] alignment = []
data = [] header = "| "
for column in columns: for column in columns:
attr, val = column.split(':') attr, val = column.split(':')
alignment.append(attr) alignment.append(attr)
data.append(val) header += val + " | "
return data, alignment return header, alignment
@classmethod
def read(cls, lines):
header = next(lines)
header, alignment = cls.parseHeader(header)
rows = [TableRow(header)]
for line in lines:
if "|" not in line :
break
else :
rows.append(TableRow(line))
return alignment, rows
def __init__(self, match):
self.alignment, self.children = match
class TableRow(BlockToken):
@classmethod @classmethod
def parseRow(self, line): def parseRow(self, line):
line = line.strip().strip('|') line = line.strip().strip('|')
@@ -30,29 +51,13 @@ class Table(BlockToken):
data = [] data = []
for column in columns: for column in columns:
data.append(column) data.append(TableCell(column))
return data return data
@classmethod
def read(self, lines):
table = []
header = next(lines)
data, alignment = self.parseHeader(header)
table.append([data])
for line in lines:
if "|" not in line:
break
data = self.parseRow(line)
table.append([data])
return [table, alignment]
def __init__(self, match): def __init__(self, match):
self.table = match[0] self.children = self.parseRow(match)
self.alignment = match[1]
print("init") class TableCell(BlockToken) :
def __init__(self, match):
super().__init__(match, span_token.tokenize_inner)
+9 -7
View File
@@ -5,15 +5,17 @@ from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
@pytest.mark.html @pytest.mark.html
def test_question_html(): def test_table_html():
assertions = { assertions = {
"| l: abc | r: abc | X: abc |\n| 123 | 456 | 789 | \n | 987 | 654 | 321 | ": "<table><tr><th style='text-align: left;'> abc</th><th style='text-align: right;'> abc</th><th> abc</th></tr><tr><td style='text-align: left;'>123</td><td style='text-align: right;'>456</td><td>789</td></tr><tr><td style='text-align: left;'>987</td><td style='text-align: right;'>654</td><td>321</td></tr></table>\n", "| l: a |" : "<table><tr><th style='text-align: left;'>a</th></tr></table>\n",
"text\n| l: abc | r: abc | X: abc |\n| 123 | 456 | 789 | \n | 987 | 654 | 321 |\ntext": "text\n<table><tr><th style='text-align: left;'> abc</th><th style='text-align: right;'> abc</th><th> abc</th></tr><tr><td style='text-align: left;'>123</td><td style='text-align: right;'>456</td><td>789</td></tr><tr><td style='text-align: left;'>987</td><td style='text-align: right;'>654</td><td>321</td></tr></table>\ntext\n", "| r: a |" : "<table><tr><th style='text-align: right;'>a</th></tr></table>\n",
"| l: a |\n| *b* |" : "<table><tr><th style='text-align: left;'>a</th></tr><tr><td style='text-align: left;'><em>b</em></td></tr></table>\n",
"| l: *a* |" : "<table><tr><th style='text-align: left;'><em>a</em></th></tr></table>\n",
"| l: a |\n| [morse:a] |" : "<table><tr><th style=\'text-align: left;\'>a</th></tr><tr><td style=\'text-align: left;\'><span class=\"morse\"><span class=\"morse_char\">\n<span class=\"morse_char\">\n▄</span>\n<span class=\"morse_char\">\n▄▄▄</span>\n</span>\n</span></td></tr></table>\n",
"| l: a |\n| b |" : "<table><tr><th style='text-align: left;'>a</th></tr><tr><td style='text-align: left;'>b</td></tr></table>\n",
"| l: abc | r: abc | X: abc |\n| 123 | 456 | 789 | \n | 987 | 654 | 321 | ": "<table><tr><th style='text-align: left;'>abc</th><th style='text-align: right;'>abc</th><th>abc</th></tr><tr><td style='text-align: left;'>123</td><td style='text-align: right;'>456</td><td>789</td></tr><tr><td style='text-align: left;'>987</td><td style='text-align: right;'>654</td><td>321</td></tr></table>\n",
} }
def test_function(input): with FiftyOhmHtmlRenderer() as renderer:
return f"{input}"
with FiftyOhmHtmlRenderer(test_function) as renderer:
for assertion in assertions: for assertion in assertions:
assert renderer.render(Document(assertion)) == assertions[assertion] assert renderer.render(Document(assertion)) == assertions[assertion]