2016-06-23 14:08:59 +00:00
|
|
|
import sphinx.builders.manpage
|
|
|
|
import sphinx.writers.manpage
|
|
|
|
from docutils.frontend import OptionParser
|
|
|
|
from sphinx.util.console import bold, darkgreen
|
|
|
|
from six import string_types
|
|
|
|
from docutils.io import FileOutput
|
|
|
|
from os import path
|
|
|
|
from sphinx.util.nodes import inline_all_toctrees
|
|
|
|
from sphinx import addnodes
|
2020-06-01 18:22:40 +00:00
|
|
|
from sphinx.util import logging
|
2016-06-23 14:08:59 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Extension based on sphinx builtin manpage.
|
|
|
|
It does not write its own .SH NAME based on config,
|
|
|
|
but leaves everything to actual reStructuredText file content.
|
|
|
|
"""
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2020-06-01 18:22:40 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2016-06-23 14:08:59 +00:00
|
|
|
class ManualPageTranslator(sphinx.writers.manpage.ManualPageTranslator):
|
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
def header(self):
|
|
|
|
tmpl = (".TH \"%(title_upper)s\" \"%(manual_section)s\""
|
|
|
|
" \"%(date)s\" \"%(version)s\" \"%(manual_group)s\"\n")
|
|
|
|
return tmpl % self._docinfo
|
2016-06-23 14:08:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManualPageWriter(sphinx.writers.manpage.ManualPageWriter):
|
|
|
|
|
|
|
|
def __init__(self, builder):
|
|
|
|
super().__init__(builder)
|
2018-03-15 12:49:22 +00:00
|
|
|
self.translator_class = (
|
2020-06-01 18:22:40 +00:00
|
|
|
self.builder.get_translator_class() or ManualPageTranslator)
|
2016-06-23 14:08:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManualPageBuilder(sphinx.builders.manpage.ManualPageBuilder):
|
|
|
|
|
|
|
|
name = 'cman'
|
2018-03-13 14:09:13 +00:00
|
|
|
default_translator_class = ManualPageTranslator
|
2016-06-23 14:08:59 +00:00
|
|
|
|
|
|
|
def write(self, *ignored):
|
|
|
|
docwriter = ManualPageWriter(self)
|
|
|
|
docsettings = OptionParser(
|
|
|
|
defaults=self.env.settings,
|
|
|
|
components=(docwriter,),
|
|
|
|
read_config_files=True).get_default_values()
|
|
|
|
|
2020-06-01 18:22:40 +00:00
|
|
|
logger.info(bold('writing... '), nonl=True)
|
2016-06-23 14:08:59 +00:00
|
|
|
|
|
|
|
for info in self.config.man_pages:
|
|
|
|
docname, name, description, authors, section = info
|
|
|
|
if isinstance(authors, string_types):
|
|
|
|
if authors:
|
|
|
|
authors = [authors]
|
|
|
|
else:
|
|
|
|
authors = []
|
|
|
|
|
|
|
|
targetname = '%s.%s' % (name, section)
|
2020-06-01 18:22:40 +00:00
|
|
|
logger.info(darkgreen(targetname) + ' { ', nonl=True)
|
2016-06-23 14:08:59 +00:00
|
|
|
destination = FileOutput(
|
|
|
|
destination_path=path.join(self.outdir, targetname),
|
|
|
|
encoding='utf-8')
|
|
|
|
|
|
|
|
tree = self.env.get_doctree(docname)
|
|
|
|
docnames = set()
|
|
|
|
largetree = inline_all_toctrees(self, docnames, docname, tree,
|
|
|
|
darkgreen, [docname])
|
2020-06-01 18:22:40 +00:00
|
|
|
logger.info('} ', nonl=True)
|
2016-06-23 14:08:59 +00:00
|
|
|
self.env.resolve_references(largetree, docname, self)
|
|
|
|
# remove pending_xref nodes
|
|
|
|
for pendingnode in largetree.traverse(addnodes.pending_xref):
|
|
|
|
pendingnode.replace_self(pendingnode.children)
|
|
|
|
|
|
|
|
largetree.settings = docsettings
|
|
|
|
largetree.settings.title = name
|
|
|
|
largetree.settings.subtitle = description
|
|
|
|
largetree.settings.authors = authors
|
|
|
|
largetree.settings.section = section
|
|
|
|
|
|
|
|
docwriter.write(largetree, destination)
|
2020-06-01 18:22:40 +00:00
|
|
|
logger.info("")
|
2016-06-23 14:08:59 +00:00
|
|
|
|
2016-07-05 18:44:24 +00:00
|
|
|
|
2016-06-23 14:08:59 +00:00
|
|
|
def setup(app):
|
|
|
|
app.add_builder(ManualPageBuilder)
|