Add: baseurl. Fix a lot of things.

This commit is contained in:
Kujiu 2021-01-31 03:21:33 +01:00
parent 87dbd2f641
commit 450bfe2d05
Signed by: kujiu
GPG key ID: ABBB2CAC6855599F
3 changed files with 41 additions and 29 deletions

View file

@ -5,3 +5,8 @@ Build gemini blog from Sphinx.
Install with `python setup.py install` and do `make gemini` in
your project.
You can add a `gemini_footer` in config, formatted under the
Gemini specification. You need to set `gemini_baseurl` for
good url in links.

View file

@ -10,6 +10,7 @@ __version__ = '.'.join([str(val) for val in __version_info__])
from typing import Set, Dict, Any
from urllib.parse import quote
from sphinx.builders.text import TextBuilder
from sphinx.util import logging
@ -35,12 +36,21 @@ class GeminiBuilder(TextBuilder):
current_docname = None
def __init__(self, app) -> None:
super().__init__(app)
self.baseurl = self.config.gemini_baseurl
def prepare_writing(self, docnames: Set[str]) -> None:
self.writer = GeminiWriter(self)
def get_target_uri(self, docname: str, typ: str = None) -> str:
return self.baseurl + quote(docname) + self.out_suffix
def setup(app: Sphinx) -> Dict[str, Any]:
app.add_builder(GeminiBuilder)
app.add_config_value('gemini_footer', '', 'env')
app.add_config_value('gemini_baseurl', '', 'env')
return {
'version': __version__,

View file

@ -11,6 +11,7 @@ from docutils import writers, nodes
from docutils.nodes import Element, Text, Node
from sphinx.util import logging
from sphinx.util.docutils import SphinxTranslator
from sphinx.util.osutil import relative_uri
from sphinx import addnodes
from sphinx.writers.text import Table, Cell
from sphinx.locale import __, admonitionlabels
@ -71,19 +72,19 @@ class GeminiTranslator(SphinxTranslator):
def disable_pre(self) -> None:
if self.pre:
self.body += '```\n'
self.pre = True
self.body += '\n```\n'
self.pre = False
def enable_pre(self) -> None:
if not self.pre:
self.body += '```\n'
self.body += '\n```\n'
self.pre = True
def visit_document(self, node: Element) -> None:
pass
def depart_document(self, node: Element) -> None:
pass
self.add_text(self.config.gemini_footer)
def visit_section(self, node: Element) -> None:
self.sectionlevel += 1
@ -120,14 +121,14 @@ class GeminiTranslator(SphinxTranslator):
pass
def visit_title(self, node: Element) -> None:
if isinstance(node.parent, nodes.Admonition):
self.add_text(node.astext() + ' : ')
else:
if not isinstance(node.parent, nodes.Admonition):
self.add_title(node.astext())
self.end_block()
self.end_block()
raise nodes.SkipNode
def depart_title(self, node: Element) -> None:
pass
if isinstance(node.parent, nodes.Admonition):
self.add_text(':')
def visit_subtitle(self, node: Element) -> None:
pass
@ -139,7 +140,7 @@ class GeminiTranslator(SphinxTranslator):
self.add_text('-- ')
def depart_attribution(self, node: Element) -> None:
self.end_block()
pass
def visit_desc(self, node: Element) -> None:
pass
@ -157,7 +158,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def depart_desc_signature_line(self, node: Element) -> None:
self.end_block()
pass
def visit_desc_name(self, node: Element) -> None:
pass
@ -181,7 +182,7 @@ class GeminiTranslator(SphinxTranslator):
self.add_text(' -> ')
def depart_desc_returns(self, node: Element) -> None:
self.end_block()
pass
def visit_desc_parameterlist(self, node: Element) -> None:
self.add_text('(')
@ -189,7 +190,6 @@ class GeminiTranslator(SphinxTranslator):
def depart_desc_parameterlist(self, node: Element) -> None:
self.add_text(')')
self.end_block()
def visit_desc_parameter(self, node: Element) -> None:
if not self.first_param:
@ -204,7 +204,6 @@ class GeminiTranslator(SphinxTranslator):
def depart_desc_optional(self, node: Element) -> None:
self.add_text(']')
self.end_block()
def visit_desc_annotation(self, node: Element) -> None:
pass
@ -213,7 +212,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def visit_desc_content(self, node: Element) -> None:
self.end_block()
pass
def depart_desc_content(self, node: Element) -> None:
pass
@ -260,7 +259,6 @@ class GeminiTranslator(SphinxTranslator):
self.add_quote(node[0].astext())
else:
self.add_quote()
self.end_block()
def depart_citation(self, node: Element) -> None:
pass
@ -375,13 +373,15 @@ class GeminiTranslator(SphinxTranslator):
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.end_block()
raise nodes.SkipNode
def visit_image(self, node: Element) -> None:
self.end_block()
if 'alt' in node.attributes:
self.add_text(__('[image: %s]') % node['alt'])
self.add_text(__('[image]'))
uri = relative_uri(self.builder.current_docname, node['uri'])
self.add_link(uri, __('[image: %s]') % node['alt'])
else:
self.add_link(uri)
self.end_block()
raise nodes.SkipNode
@ -429,7 +429,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def depart_term(self, node: Element) -> None:
self.end_block()
pass
def visit_classifier(self, node: Element) -> None:
self.add_text(' : ')
@ -465,7 +465,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def depart_field_body(self, node: Element) -> None:
self.end_block()
pass
def visit_centered(self, node: Element) -> None:
pass
@ -489,7 +489,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def depart_admonition(self, node: Element) -> None:
self.end_block()
pass
def _visit_admonition(self, node: Element) -> None:
pass
@ -523,7 +523,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def depart_versionmodified(self, node: Element) -> None:
self.end_block()
pass
def visit_literal_block(self, node: Element) -> None:
self.enable_pre()
@ -545,13 +545,12 @@ class GeminiTranslator(SphinxTranslator):
def depart_line_block(self, node: Element) -> None:
self.disable_pre()
self.end_block()
def visit_line(self, node: Element) -> None:
pass
def depart_line(self, node: Element) -> None:
self.end_block()
pass
def visit_block_quote(self, node: Element) -> None:
pass
@ -572,6 +571,7 @@ class GeminiTranslator(SphinxTranslator):
def depart_paragraph(self, node: Element) -> None:
self.end_block()
self.end_block()
def visit_target(self, node: Element) -> None:
raise nodes.SkipNode
@ -690,10 +690,7 @@ class GeminiTranslator(SphinxTranslator):
pass
def visit_inline(self, node: Element) -> None:
if 'xref' in node['classes'] or 'term' in node['classes']:
self.end_block()
self.add_link(node['classes']['xref'], node.astext())
self.end_block()
pass
def depart_inline(self, node: Element) -> None:
pass