Support tables

This commit is contained in:
Kujiu 2021-02-01 01:54:20 +01:00
parent 6371dd0f8d
commit dba8119eba
Signed by: kujiu
GPG key ID: ABBB2CAC6855599F

View file

@ -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,6 +583,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def depart_paragraph(self, node: Element) -> None:
if not self.table:
self.end_block()
self.end_block()
@ -593,6 +607,12 @@ class GeminiTranslator(SphinxTranslator):
def visit_reference(self, node: Element) -> None:
if 'refuri' in node:
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()
@ -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', ' ')