diff --git a/renderer/fifty_ohm_html_renderer.py b/renderer/fifty_ohm_html_renderer.py
index 9051e3c..cbc1a56 100644
--- a/renderer/fifty_ohm_html_renderer.py
+++ b/renderer/fifty_ohm_html_renderer.py
@@ -12,6 +12,7 @@ from .picture import Picture
from .question import Question
from .quote import Quote
from .references import References
+from .table import Table
from .tag import Tag
from .underline import Underline
from .unit import Unit
@@ -38,7 +39,8 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
References,
Question,
Picture,
- Photo
+ Photo,
+ Table
)
self.question_renderer = question_renderer
@@ -195,4 +197,35 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
"""
def render_photo(self, token) :
- return self.render_photo_helper(token.id, token.ref, token.text, token.number)
\ No newline at end of file
+ return self.render_photo_helper(token.id, token.ref, token.text, token.number)
+
+ @staticmethod
+ def render_row_helper(row):
+ return f"""
{row}
"""
+
+ @staticmethod
+ def render_cell_helper(cell, alignment, type):
+ if alignment == "l":
+ return f"<{type} style='text-align: left;'>{cell}{type}>"
+ elif alignment == "r":
+ return f"<{type} style='text-align: right;'>{cell}{type}>"
+ elif alignment == "c":
+ return f"<{type} style='text-align: center;'>{cell}{type}>"
+ else:
+ return f"<{type}>{cell}{type}>"
+
+ def render_table(self, token):
+ data = token.table
+ alignment = token.alignment
+
+ table = ""
+
+ for i, row in enumerate(data):
+ content = ""
+ for j, cell in enumerate(row[0]):
+ content += self.render_cell_helper(cell, alignment[j], ("th" if i==0 else "td"))
+ table += self.render_row_helper(content)
+
+ table += "
"
+
+ return table
\ No newline at end of file
diff --git a/renderer/table.py b/renderer/table.py
new file mode 100644
index 0000000..c95ff06
--- /dev/null
+++ b/renderer/table.py
@@ -0,0 +1,58 @@
+
+from mistletoe.block_token import BlockToken
+
+
+class Table(BlockToken):
+ @staticmethod
+ def start(line):
+ return '|' in line
+
+ @classmethod
+ def parseHeader(self, line):
+ line = line.strip().strip('|') # Remove leading/trailing whitespace and pipes
+ columns = [col.strip() for col in line.split('|')] # Split by pipe |
+
+ alignment = []
+ data = []
+
+ for column in columns:
+ attr, val = column.split(':')
+ alignment.append(attr)
+ data.append(val)
+
+ return data, alignment
+
+ @classmethod
+ def parseRow(self, line):
+ line = line.strip().strip('|')
+ columns = [col.strip() for col in line.split('|')]
+
+ data = []
+
+ for column in columns:
+ data.append(column)
+
+ 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):
+ self.table = match[0]
+ self.alignment = match[1]
+ print("init")
diff --git a/test/acceptanceTest.md b/test/acceptanceTest.md
index 5148735..3d5c06c 100644
--- a/test/acceptanceTest.md
+++ b/test/acceptanceTest.md
@@ -79,7 +79,7 @@ CQ CQ hier ist DL1PZ mit einem allgemeinen Anruf, hier ist DL1PZ und hört.
-| c | l | c | l | c | l |
+| c: | l: | c: | l: | c: | l: |
| | | | | | |
| A | [morse:a] | K | [morse:k] | U | [morse:u] |
| B | [morse:b] | L | [morse:l] | V | [morse:v] |
diff --git a/test/test_question.py b/test/test_question.py
index 027c446..0a4d84e 100644
--- a/test/test_question.py
+++ b/test/test_question.py
@@ -7,7 +7,7 @@ from test.util import paragraph
@pytest.mark.html
-def test_comment_html():
+def test_question_html():
assertions = {
"[question:123]": "123\n",
"\n[question:123]\n": "123\n",
@@ -24,7 +24,7 @@ def test_comment_html():
@pytest.mark.latex
-def test_comment_latex():
+def test_question_latex():
assertions = {
"[question:123]": "123",
"\n[question:123]\n": "123",
diff --git a/test/test_table.py b/test/test_table.py
new file mode 100644
index 0000000..6293444
--- /dev/null
+++ b/test/test_table.py
@@ -0,0 +1,19 @@
+import pytest
+from mistletoe import Document
+
+from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
+
+
+@pytest.mark.html
+def test_question_html():
+ assertions = {
+ "| l: abc | r: abc | X: abc |\n| 123 | 456 | 789 | \n | 987 | 654 | 321 | ": "| abc | abc | abc |
|---|
| 123 | 456 | 789 |
| 987 | 654 | 321 |
\n",
+ "text\n| l: abc | r: abc | X: abc |\n| 123 | 456 | 789 | \n | 987 | 654 | 321 |\ntext": "text\n| abc | abc | abc |
|---|
| 123 | 456 | 789 |
| 987 | 654 | 321 |
\ntext\n",
+ }
+
+ def test_function(input):
+ return f"{input}"
+
+ with FiftyOhmHtmlRenderer(test_function) as renderer:
+ for assertion in assertions:
+ assert renderer.render(Document(assertion)) == assertions[assertion]