# -*- coding: utf-8 -*- """ sphinxcontrib.builders.rst ~~~~~~~~~~~~~~~~~~~~~~~~~~ ReST Sphinx builder. :copyright: Copyright 2012-2021 by Freek Dijkstra and contributors. :license: BSD, see LICENSE.txt for details. """ from __future__ import (print_function, unicode_literals, absolute_import) import codecs from os import path from docutils.io import StringOutput from sphinx.builders import Builder from sphinx.util.osutil import ensuredir, SEP from ..writers.rst import RstWriter class RstBuilder(Builder): name = 'rst' format = 'rst' file_suffix = '.rst' link_suffix = None # defaults to file_suffix def init(self): """Load necessary templates and perform initialization.""" if self.config.rst_file_suffix is not None: self.file_suffix = self.config.rst_file_suffix if self.config.rst_link_suffix is not None: self.link_suffix = self.config.rst_link_suffix elif self.link_suffix is None: self.link_suffix = self.file_suffix # Function to convert the docname to a reST file name. def file_transform(docname): return docname + self.file_suffix # Function to convert the docname to a relative URI. def link_transform(docname): return docname + self.link_suffix if self.config.rst_file_transform is not None: self.file_transform = self.config.rst_file_transform else: self.file_transform = file_transform if self.config.rst_link_transform is not None: self.link_transform = self.config.rst_link_transform else: self.link_transform = link_transform def get_outdated_docs(self): """ Return an iterable of input files that are outdated. """ # This method is taken from TextBuilder.get_outdated_docs() # with minor changes to support :confval:`rst_file_transform`. for docname in self.env.found_docs: if docname not in self.env.all_docs: yield docname continue sourcename = path.join(self.env.srcdir, docname + self.file_suffix) targetname = path.join(self.outdir, self.file_transform(docname)) # print (sourcename, targetname) try: targetmtime = path.getmtime(targetname) except Exception: targetmtime = 0 try: srcmtime = path.getmtime(sourcename) if srcmtime > targetmtime: yield docname except EnvironmentError: # source doesn't exist anymore pass def get_target_uri(self, docname, typ=None): return self.link_transform(docname) def prepare_writing(self, docnames): self.writer = RstWriter(self) def write_doc(self, docname, doctree): # This method is taken from TextBuilder.write_doc() # with minor changes to support :confval:`rst_file_transform`. destination = StringOutput(encoding='utf-8') # print "write(%s,%s)" % (type(doctree), type(destination)) self.writer.write(doctree, destination) outfilename = path.join(self.outdir, self.file_transform(docname)) # print "write(%s,%s) -> %s" % (type(doctree), type(destination), outfilename) ensuredir(path.dirname(outfilename)) try: f = codecs.open(outfilename, 'w', 'utf-8') try: f.write(self.writer.output) finally: f.close() except (IOError, OSError) as err: self.warn("error writing file %s: %s" % (outfilename, err)) def finish(self): pass