diff --git a/renderer/fifty_ohm_latex_renderer.py b/renderer/fifty_ohm_latex_renderer.py index 83891d1..0ce85c6 100644 --- a/renderer/fifty_ohm_latex_renderer.py +++ b/renderer/fifty_ohm_latex_renderer.py @@ -8,6 +8,7 @@ from .photo import Photo from .picture import Picture from .question import Question from .quote import Quote +from .table import Table from .tag import Tag from .underline import Underline @@ -25,7 +26,8 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer): NonbreakingSpacesDots, Question, Picture, - Photo + Photo, + Table ) self.question_renderer = question_renderer @@ -113,4 +115,23 @@ class FiftyOhmLaTeXRenderer(LaTeXRenderer): \label{{{ref}}}""" def render_photo(self, token) : - return self.render_picture_helper(token.id, token.ref, token.text, token.number) \ No newline at end of file + 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 diff --git a/renderer/table.py b/renderer/table.py index fae2fb4..a827569 100644 --- a/renderer/table.py +++ b/renderer/table.py @@ -9,7 +9,7 @@ class Table(BlockToken): return '|' in line @classmethod - def parseHeader(cls, line): + def parse_header(cls, line): # Parse header, extract alignment and make an ordinary row out of it line = line.strip().strip('|') # Remove leading/trailing whitespace and pipes columns = [col.strip() for col in line.split('|')] # Split by pipe | @@ -27,7 +27,7 @@ class Table(BlockToken): @classmethod def read(cls, lines): header = next(lines) - header, alignment = cls.parseHeader(header) + header, alignment = cls.parse_header(header) rows = [TableRow(header)] for line in lines: @@ -44,7 +44,7 @@ class Table(BlockToken): class TableRow(BlockToken): @classmethod - def parseRow(self, line): + def parse_row(cls, line): line = line.strip().strip('|') columns = [col.strip() for col in line.split('|')] @@ -56,7 +56,7 @@ class TableRow(BlockToken): return data def __init__(self, match): - self.children = self.parseRow(match) + self.children = self.parse_row(match) class TableCell(BlockToken) : def __init__(self, match): diff --git a/test/test_table.py b/test/test_table.py index 46b96d2..009905c 100644 --- a/test/test_table.py +++ b/test/test_table.py @@ -2,6 +2,7 @@ import pytest from mistletoe import Document from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer +from renderer.fifty_ohm_latex_renderer import FiftyOhmLaTeXRenderer @pytest.mark.html @@ -19,3 +20,16 @@ def test_table_html(): with FiftyOhmHtmlRenderer() as renderer: for assertion in assertions: 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]