| 1 |
# -*- coding: utf-8 -*- |
| 2 |
""" |
| 3 |
sphinxcontrib.builders.rst |
| 4 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 |
|
| 6 |
ReST Sphinx builder. |
| 7 |
|
| 8 |
:copyright: Copyright 2012-2021 by Freek Dijkstra and contributors. |
| 9 |
:license: BSD, see LICENSE.txt for details. |
| 10 |
""" |
| 11 |
|
| 12 |
from __future__ import (print_function, unicode_literals, absolute_import) |
| 13 |
|
| 14 |
import codecs |
| 15 |
from os import path |
| 16 |
|
| 17 |
from docutils.io import StringOutput |
| 18 |
|
| 19 |
from sphinx.builders import Builder |
| 20 |
from sphinx.util.osutil import ensuredir, SEP |
| 21 |
from ..writers.rst import RstWriter |
| 22 |
|
| 23 |
|
| 24 |
class RstBuilder(Builder): |
| 25 |
name = 'rst' |
| 26 |
format = 'rst' |
| 27 |
file_suffix = '.rst' |
| 28 |
link_suffix = None # defaults to file_suffix |
| 29 |
|
| 30 |
def init(self): |
| 31 |
"""Load necessary templates and perform initialization.""" |
| 32 |
if self.config.rst_file_suffix is not None: |
| 33 |
self.file_suffix = self.config.rst_file_suffix |
| 34 |
if self.config.rst_link_suffix is not None: |
| 35 |
self.link_suffix = self.config.rst_link_suffix |
| 36 |
elif self.link_suffix is None: |
| 37 |
self.link_suffix = self.file_suffix |
| 38 |
|
| 39 |
# Function to convert the docname to a reST file name. |
| 40 |
def file_transform(docname): |
| 41 |
return docname + self.file_suffix |
| 42 |
|
| 43 |
# Function to convert the docname to a relative URI. |
| 44 |
def link_transform(docname): |
| 45 |
return docname + self.link_suffix |
| 46 |
|
| 47 |
if self.config.rst_file_transform is not None: |
| 48 |
self.file_transform = self.config.rst_file_transform |
| 49 |
else: |
| 50 |
self.file_transform = file_transform |
| 51 |
if self.config.rst_link_transform is not None: |
| 52 |
self.link_transform = self.config.rst_link_transform |
| 53 |
else: |
| 54 |
self.link_transform = link_transform |
| 55 |
|
| 56 |
def get_outdated_docs(self): |
| 57 |
""" |
| 58 |
Return an iterable of input files that are outdated. |
| 59 |
""" |
| 60 |
# This method is taken from TextBuilder.get_outdated_docs() |
| 61 |
# with minor changes to support :confval:`rst_file_transform`. |
| 62 |
for docname in self.env.found_docs: |
| 63 |
if docname not in self.env.all_docs: |
| 64 |
yield docname |
| 65 |
continue |
| 66 |
sourcename = path.join(self.env.srcdir, docname + |
| 67 |
self.file_suffix) |
| 68 |
targetname = path.join(self.outdir, self.file_transform(docname)) |
| 69 |
# print (sourcename, targetname) |
| 70 |
|
| 71 |
try: |
| 72 |
targetmtime = path.getmtime(targetname) |
| 73 |
except Exception: |
| 74 |
targetmtime = 0 |
| 75 |
try: |
| 76 |
srcmtime = path.getmtime(sourcename) |
| 77 |
if srcmtime > targetmtime: |
| 78 |
yield docname |
| 79 |
except EnvironmentError: |
| 80 |
# source doesn't exist anymore |
| 81 |
pass |
| 82 |
|
| 83 |
def get_target_uri(self, docname, typ=None): |
| 84 |
return self.link_transform(docname) |
| 85 |
|
| 86 |
def prepare_writing(self, docnames): |
| 87 |
self.writer = RstWriter(self) |
| 88 |
|
| 89 |
def write_doc(self, docname, doctree): |
| 90 |
# This method is taken from TextBuilder.write_doc() |
| 91 |
# with minor changes to support :confval:`rst_file_transform`. |
| 92 |
destination = StringOutput(encoding='utf-8') |
| 93 |
# print "write(%s,%s)" % (type(doctree), type(destination)) |
| 94 |
|
| 95 |
self.writer.write(doctree, destination) |
| 96 |
outfilename = path.join(self.outdir, self.file_transform(docname)) |
| 97 |
# print "write(%s,%s) -> %s" % (type(doctree), type(destination), outfilename) |
| 98 |
ensuredir(path.dirname(outfilename)) |
| 99 |
try: |
| 100 |
f = codecs.open(outfilename, 'w', 'utf-8') |
| 101 |
try: |
| 102 |
f.write(self.writer.output) |
| 103 |
finally: |
| 104 |
f.close() |
| 105 |
except (IOError, OSError) as err: |
| 106 |
self.warn("error writing file %s: %s" % (outfilename, err)) |
| 107 |
|
| 108 |
def finish(self): |
| 109 |
pass |