Fix: Table generation with literals, image copy with glob

This commit is contained in:
Kujiu 2021-02-01 20:39:16 +01:00
parent d60c947335
commit e80603940d
Signed by: kujiu
GPG key ID: ABBB2CAC6855599F
4 changed files with 32 additions and 12 deletions

View file

@ -2,6 +2,12 @@
Changes Changes
======= =======
1.1.0 (*2021-02-??*)
====================
- Fix table generation with literal blocks
- Fix image copy when using glob
1.0.0 (*2021-02-01*) 1.0.0 (*2021-02-01*)
==================== ====================

View file

@ -28,7 +28,7 @@ with open("README.rst", "r") as fh:
setup( setup(
name="sphinx_gemini_builder", name="sphinx_gemini_builder",
version="1.0.0", version="1.1.0",
url="https://procrastinator.nerv-project.eu/nerv-project/sphinx_gemini_builder", url="https://procrastinator.nerv-project.eu/nerv-project/sphinx_gemini_builder",
license="EUPL 1.2", license="EUPL 1.2",
author="Kujiu", author="Kujiu",

View file

@ -5,11 +5,12 @@
Build Gemini blog from Sphinx. Build Gemini blog from Sphinx.
""" """
__version_info__ = (1, 0, 0) __version_info__ = (1, 1, 0)
__version__ = '.'.join([str(val) for val in __version_info__]) __version__ = '.'.join([str(val) for val in __version_info__])
from os import path from os import path
from pathlib import Path
from typing import Set, Dict, Any from typing import Set, Dict, Any
from urllib.parse import quote from urllib.parse import quote
@ -63,8 +64,12 @@ class GeminiBuilder(TextBuilder):
try: try:
ensuredir(destdir) ensuredir(destdir)
copyfile(path.join(self.srcdir, src), srcpaths = Path(self.srcdir).glob(src)
path.join(self.outdir, self.imagedir, src)) for srcpath in srcpaths:
destpath = path.relpath(srcpath, self.srcdir)
destpath = path.join(self.outdir, self.imagedir, destpath)
copyfile(srcpath, destpath)
except Exception as err: except Exception as err:
logger.warning(__('cannot copy image file %r: %s'), logger.warning(__('cannot copy image file %r: %s'),
path.join(self.srcdir, src), err) path.join(self.srcdir, src), err)

View file

@ -59,7 +59,13 @@ class GeminiTranslator(SphinxTranslator):
self.body = '' self.body = ''
def add_text(self, text: str) -> None: def add_text(self, text: str) -> None:
self.body += text if self.table:
if len(self.cell_text):
self.cell_text[-1] += text
else:
self.cell_text.append(text)
else:
self.body += text
def add_link(self, url: str, label: str = '') -> None: def add_link(self, url: str, label: str = '') -> None:
self.body += '=> %s %s' % (url, label) self.body += '=> %s %s' % (url, label)
@ -143,6 +149,7 @@ class GeminiTranslator(SphinxTranslator):
pass pass
def visit_attribution(self, node: Element) -> None: def visit_attribution(self, node: Element) -> None:
self.end_block()
self.add_text('-- ') self.add_text('-- ')
def depart_attribution(self, node: Element) -> None: def depart_attribution(self, node: Element) -> None:
@ -196,6 +203,7 @@ class GeminiTranslator(SphinxTranslator):
def depart_desc_parameterlist(self, node: Element) -> None: def depart_desc_parameterlist(self, node: Element) -> None:
self.add_text(')') self.add_text(')')
self.end_block()
def visit_desc_parameter(self, node: Element) -> None: def visit_desc_parameter(self, node: Element) -> None:
if not self.first_param: if not self.first_param:
@ -206,6 +214,7 @@ class GeminiTranslator(SphinxTranslator):
raise nodes.SkipNode raise nodes.SkipNode
def visit_desc_optional(self, node: Element) -> None: def visit_desc_optional(self, node: Element) -> None:
self.end_block()
self.add_text('[') self.add_text('[')
def depart_desc_optional(self, node: Element) -> None: def depart_desc_optional(self, node: Element) -> None:
@ -373,26 +382,26 @@ class GeminiTranslator(SphinxTranslator):
def depart_table(self, node: Element) -> None: def depart_table(self, node: Element) -> None:
self.enable_pre() self.enable_pre()
self.add_text(str(self.table)) text = str(self.table)
self.table = None
self.cell_text = []
self.add_text(text)
self.disable_pre() self.disable_pre()
for line in self.after_table: for line in self.after_table:
self.add_text(line) self.add_text(line)
self.end_block() self.end_block()
self.table = None
self.cell_text = []
self.after_table = [] self.after_table = []
def visit_acks(self, node: Element) -> None: def visit_acks(self, node: Element) -> None:
bullet_list = cast(nodes.bullet_list, node[0]) bullet_list = cast(nodes.bullet_list, node[0])
list_items = cast(Iterable[nodes.list_item], bullet_list) list_items = cast(Iterable[nodes.list_item], bullet_list)
self.new_state(0)
self.add_text(', '.join(n.astext() for n in list_items) + '.') self.add_text(', '.join(n.astext() for n in list_items) + '.')
raise nodes.SkipNode raise nodes.SkipNode
def visit_image(self, node: Element) -> None: def visit_image(self, node: Element) -> None:
self.end_block() self.end_block()
uri = relative_uri(self.builder.current_docname, node['uri'])
if 'alt' in node.attributes: if 'alt' in node.attributes:
uri = relative_uri(self.builder.current_docname, node['uri'])
self.add_link(uri, __('[image: %s]') % node['alt']) self.add_link(uri, __('[image: %s]') % node['alt'])
else: else:
self.add_link(uri) self.add_link(uri)
@ -613,7 +622,7 @@ class GeminiTranslator(SphinxTranslator):
self.after_table.append( self.after_table.append(
'=> %s %s' % '=> %s %s' %
(node['refuri'], node.astext())) (node['refuri'], node.astext()))
self.cell_text.append(node.astext() or node['refuri']) self.add_text(node.astext() or node['refuri'])
else: else:
self.end_block() self.end_block()
self.add_link(node['refuri'], node.astext()) self.add_link(node['refuri'], node.astext())
@ -706,7 +715,7 @@ class GeminiTranslator(SphinxTranslator):
def visit_Text(self, node: Text) -> None: def visit_Text(self, node: Text) -> None:
text = node.astext() text = node.astext()
if self.table: if self.table:
self.cell_text.append(text) self.add_text(text)
raise nodes.SkipNode raise nodes.SkipNode
if isinstance(node.parent, nodes.paragraph): if isinstance(node.parent, nodes.paragraph):