From dba8119eba776791048d799eaa6ef0eb1875b955 Mon Sep 17 00:00:00 2001 From: "kujiu (@rincevent)" Date: Mon, 1 Feb 2021 01:54:20 +0100 Subject: [PATCH] Support tables --- sphinx_gemini_builder/writer.py | 44 +++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 10 deletions(-) diff --git a/sphinx_gemini_builder/writer.py b/sphinx_gemini_builder/writer.py index 9fcf2b6..73e97da 100644 --- a/sphinx_gemini_builder/writer.py +++ b/sphinx_gemini_builder/writer.py @@ -41,6 +41,9 @@ class GeminiWriter(writers.Writer): class GeminiTranslator(SphinxTranslator): + """ + Gemini Translator based on native TextTranslator + """ builder = None def __init__(self, document: nodes.document, builder: "GeminiBuilder") -> None: @@ -49,6 +52,8 @@ class GeminiTranslator(SphinxTranslator): self.first_param = 0 self.list_counter = [] self.table = None + self.cell_text = [] + self.after_table = [] self.pre = False self.body = '' @@ -350,23 +355,31 @@ class GeminiTranslator(SphinxTranslator): pass def visit_entry(self, node: Element) -> None: - self.entry = Cell( - rowspan=node.get("morerows", 0) + 1, colspan=node.get("morecols", 0) + 1 - ) + pass def depart_entry(self, node: Element) -> None: - pass + entry = Cell( + rowspan=node.get("morerows", 0) + 1, colspan=node.get("morecols", 0) + 1 + ) + entry.text = '\n'.join(self.cell_text) + self.table.add_cell(entry) + self.cell_text = [] def visit_table(self, node: Element) -> None: if self.table: raise NotImplementedError('Nested tables are not supported.') - self.enable_pre() self.table = Table() def depart_table(self, node: Element) -> None: + self.enable_pre() self.add_text(str(self.table)) self.disable_pre() + for line in self.after_table: + self.add_text(line) + self.end_block() self.table = None + self.cell_text = [] + self.after_table = [] def visit_acks(self, node: Element) -> None: bullet_list = cast(nodes.bullet_list, node[0]) @@ -570,8 +583,9 @@ class GeminiTranslator(SphinxTranslator): pass def depart_paragraph(self, node: Element) -> None: - self.end_block() - self.end_block() + if not self.table: + self.end_block() + self.end_block() def visit_target(self, node: Element) -> None: raise nodes.SkipNode @@ -593,9 +607,15 @@ class GeminiTranslator(SphinxTranslator): def visit_reference(self, node: Element) -> None: if 'refuri' in node: - self.end_block() - self.add_link(node['refuri'], node.astext()) - self.end_block() + if self.table: + self.after_table.append( + '=> %s %s' % + (node['refuri'], node.astext())) + self.cell_text.append(node.astext() or node['refuri']) + else: + self.end_block() + self.add_link(node['refuri'], node.astext()) + self.end_block() raise nodes.SkipNode def depart_reference(self, node: Element) -> None: @@ -683,6 +703,10 @@ class GeminiTranslator(SphinxTranslator): def visit_Text(self, node: Text) -> None: text = node.astext() + if self.table: + self.cell_text.append(text) + raise nodes.SkipNode + if isinstance(node.parent, nodes.paragraph): text = text.replace('\r', '') text = text.replace('\n', ' ')