feature: render latex table

This commit is contained in:
Prof. Dr. Matthias Jung DL9MJ
2025-05-14 18:49:21 +02:00
parent 9a747dc8bf
commit 8397ded59c
3 changed files with 41 additions and 6 deletions
+22 -1
View File
@@ -8,6 +8,7 @@ from .photo import Photo
from .picture import Picture from .picture import Picture
from .question import Question from .question import Question
from .quote import Quote from .quote import Quote
from .table import Table
from .tag import Tag from .tag import Tag
from .underline import Underline from .underline import Underline
@@ -25,7 +26,8 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
NonbreakingSpacesDots, NonbreakingSpacesDots,
Question, Question,
Picture, Picture,
Photo Photo,
Table
) )
self.question_renderer = question_renderer self.question_renderer = question_renderer
@@ -114,3 +116,22 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer):
def render_photo(self, token) : def render_photo(self, token) :
return self.render_picture_helper(token.id, token.ref, token.text, token.number) return self.render_picture_helper(token.id, token.ref, token.text, token.number)
def render_table(self, token):
alignments = token.alignment
align = ""
for alignment in alignments :
align += alignment
table = f"\\begin{{DARCtabular}}{{{align}}}\n"
for row in token.children:
for j, cell in enumerate(row.children):
table += self.render_inner(cell)
if j < len(row.children) - 1:
table += " & "
table += r"\\"+ "\n"
table += "\\end{DARCtabular}"
return table
+4 -4
View File
@@ -9,7 +9,7 @@ class Table(BlockToken):
return '|' in line return '|' in line
@classmethod @classmethod
def parseHeader(cls, line): def parse_header(cls, line):
# Parse header, extract alignment and make an ordinary row out of it # 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 |
@@ -27,7 +27,7 @@ class Table(BlockToken):
@classmethod @classmethod
def read(cls, lines): def read(cls, lines):
header = next(lines) header = next(lines)
header, alignment = cls.parseHeader(header) header, alignment = cls.parse_header(header)
rows = [TableRow(header)] rows = [TableRow(header)]
for line in lines: for line in lines:
@@ -44,7 +44,7 @@ class Table(BlockToken):
class TableRow(BlockToken): class TableRow(BlockToken):
@classmethod @classmethod
def parseRow(self, line): def parse_row(cls, line):
line = line.strip().strip('|') line = line.strip().strip('|')
columns = [col.strip() for col in line.split('|')] columns = [col.strip() for col in line.split('|')]
@@ -56,7 +56,7 @@ class TableRow(BlockToken):
return data return data
def __init__(self, match): def __init__(self, match):
self.children = self.parseRow(match) self.children = self.parse_row(match)
class TableCell(BlockToken) : class TableCell(BlockToken) :
def __init__(self, match): def __init__(self, match):
+14
View File
@@ -2,6 +2,7 @@ import pytest
from mistletoe import Document from mistletoe import Document
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
from renderer.fifty_ohm_latex_renderer import FiftyOhmLaTeXRenderer
@pytest.mark.html @pytest.mark.html
@@ -19,3 +20,16 @@ def test_table_html():
with FiftyOhmHtmlRenderer() as renderer: with FiftyOhmHtmlRenderer() as renderer:
for assertion in assertions: for assertion in assertions:
assert renderer.render(Document(assertion)) == assertions[assertion] assert renderer.render(Document(assertion)) == assertions[assertion]
@pytest.mark.latex
def test_thematic_break_latex():
assertions = {
"| l: a |" : "\\begin{DARCtabular}{l}\na\\\\\n\\end{DARCtabular}",
"| l: a |\n| *b* |" : "\\begin{DARCtabular}{l}\na\\\\\n\\emph{b}\\\\\n\\end{DARCtabular}",
"| l: a | X: b |\n| c | d |" : "\\begin{DARCtabular}{lX}\na & b\\\\\nc & d\\\\\n\\end{DARCtabular}",
}
with FiftyOhmLaTeXRenderer() as renderer:
for assertion in assertions:
assert renderer.render(Document(assertion)) == assertions[assertion]