diff --git a/renderer/fifty_ohm_html_renderer.py b/renderer/fifty_ohm_html_renderer.py index fc56d42..22c68e1 100644 --- a/renderer/fifty_ohm_html_renderer.py +++ b/renderer/fifty_ohm_html_renderer.py @@ -210,13 +210,16 @@ class FiftyOhmHtmlRenderer(HtmlRenderer): def render_table(self, token): alignment = token.alignment - table = "" - + table = "
\n" + for i, row in enumerate(token.children): content = "" for j, cell in enumerate(row.children): - content += self.render_cell_helper(self.render_inner(cell), alignment[j], ("th" if i==0 else "td")) - table += f"{content}" + content += self.render_cell_helper(self.render_inner(cell), alignment[j], ("th" if i == 0 else "td")) + table += f"\n{content}\n" + + if token.caption != "": + table += f"" table += "
{token.caption}
" - return table \ No newline at end of file + return table diff --git a/renderer/table.py b/renderer/table.py index 14c153b..93b1c23 100644 --- a/renderer/table.py +++ b/renderer/table.py @@ -1,3 +1,4 @@ +import re from mistletoe import span_token from mistletoe.block_token import BlockToken @@ -6,13 +7,13 @@ from mistletoe.block_token import BlockToken class Table(BlockToken): @staticmethod def start(line): - return '|' in line + return "|" in line @classmethod - def parse_header(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 | + line = line.strip().strip("|") # Remove leading/trailing whitespace and pipes + columns = [col.strip() for col in line.split("|")] # Split by pipe | alignment = [] header = "| " @@ -21,32 +22,45 @@ class Table(BlockToken): attr, val = re.match(r"^ ?(?:([lcrX]):)?(.*)", column).group(1, 2) alignment.append(attr) header += val + " | " - + return header, alignment @classmethod def read(cls, lines): header = next(lines) header, alignment = cls.parse_header(header) + name = "" + caption = "" rows = [TableRow(header)] - for line in lines: - if "|" not in line : + # Read table until the end: No more column definitions, caption, or empty line. + line_buffer = [] + next_line = lines.peek() + while next_line is not None and next_line.strip() != "": + maybe_caption = re.match(r"\[table:([^:\]]+):([^:\]]+)\]", next_line) + if maybe_caption is not None: + name, caption = maybe_caption.group(1, 2) + next(lines) break - else : - rows.append(TableRow(line)) + elif "|" not in next_line: + break + else: + line_buffer.append(next(lines)) + next_line = lines.peek() - return alignment, rows + rows.extend([TableRow(line) for line in line_buffer]) + + return alignment, rows, name, caption def __init__(self, match): - self.alignment, self.children = match + self.alignment, self.children, self.name, self.caption = match class TableRow(BlockToken): @classmethod def parse_row(cls, line): - line = line.strip().strip('|') - columns = [col.strip() for col in line.split('|')] + line = line.strip().strip("|") + columns = [col.strip() for col in line.split("|")] data = [] @@ -58,6 +72,7 @@ class TableRow(BlockToken): def __init__(self, match): self.children = self.parse_row(match) -class TableCell(BlockToken) : + +class TableCell(BlockToken): def __init__(self, match): super().__init__(match, span_token.tokenize_inner)