|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + sphinxcontrib.builders.rst |
| 4 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 | +
|
| 6 | + ReST Sphinx builder. |
| 7 | +
|
| 8 | + :copyright: Copyright 2012-2013 by Freek Dijkstra. |
| 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, os_path, SEP |
| 21 | +from ..writers.rst import RstWriter |
| 22 | + |
| 23 | + |
| 24 | +# Clone of relative_uri() sphinx.util.osutil, with bug-fixes |
| 25 | +# since the original code had a few errors. |
| 26 | +# This was fixed in Sphinx 1.2b. |
| 27 | +def relative_uri(base, to): |
| 28 | + """Return a relative URL from ``base`` to ``to``.""" |
| 29 | + if to.startswith(SEP): |
| 30 | + return to |
| 31 | + b2 = base.split(SEP) |
| 32 | + t2 = to.split(SEP) |
| 33 | + # remove common segments (except the last segment) |
| 34 | + for x, y in zip(b2[:-1], t2[:-1]): |
| 35 | + if x != y: |
| 36 | + break |
| 37 | + b2.pop(0) |
| 38 | + t2.pop(0) |
| 39 | + if b2 == t2: |
| 40 | + # Special case: relative_uri('f/index.html','f/index.html') |
| 41 | + # returns '', not 'index.html' |
| 42 | + return '' |
| 43 | + if len(b2) == 1 and t2 == ['']: |
| 44 | + # Special case: relative_uri('f/index.html','f/') should |
| 45 | + # return './', not '' |
| 46 | + return '.' + SEP |
| 47 | + return ('..' + SEP) * (len(b2)-1) + SEP.join(t2) |
| 48 | + |
| 49 | + |
| 50 | +class RstBuilder(Builder): |
| 51 | + name = 'rst' |
| 52 | + format = 'rst' |
| 53 | + file_suffix = '.rst' |
| 54 | + link_suffix = None # defaults to file_suffix |
| 55 | + |
| 56 | + def init(self): |
| 57 | + """Load necessary templates and perform initialization.""" |
| 58 | + if self.config.rst_file_suffix is not None: |
| 59 | + self.file_suffix = self.config.rst_file_suffix |
| 60 | + if self.config.rst_link_suffix is not None: |
| 61 | + self.link_suffix = self.config.rst_link_suffix |
| 62 | + elif self.link_suffix == None: |
| 63 | + self.link_suffix = self.file_suffix |
| 64 | + |
| 65 | + # Function to convert the docname to a reST file name. |
| 66 | + def file_transform(docname): |
| 67 | + return docname + self.file_suffix |
| 68 | + # Function to convert the docname to a relative URI. |
| 69 | + def link_transform(docname): |
| 70 | + return docname + self.link_suffix |
| 71 | + |
| 72 | + if self.config.rst_file_transform != None: |
| 73 | + self.file_transform = self.config.rst_file_transform |
| 74 | + else: |
| 75 | + self.file_transform = file_transform |
| 76 | + if self.config.rst_link_transform != None: |
| 77 | + self.link_transform = self.config.rst_link_transform |
| 78 | + else: |
| 79 | + self.link_transform = link_transform |
| 80 | + |
| 81 | + def get_outdated_docs(self): |
| 82 | + """ |
| 83 | + Return an iterable of input files that are outdated. |
| 84 | + """ |
| 85 | + # This method is taken from TextBuilder.get_outdated_docs() |
| 86 | + # with minor changes to support :confval:`rst_file_transform`. |
| 87 | + for docname in self.env.found_docs: |
| 88 | + if docname not in self.env.all_docs: |
| 89 | + yield docname |
| 90 | + continue |
| 91 | + sourcename = path.join(self.env.srcdir, docname + |
| 92 | + self.env.source_suffix) |
| 93 | + targetname = path.join(self.outdir, self.file_transform(docname)) |
| 94 | + print (sourcename, targetname) |
| 95 | + |
| 96 | + try: |
| 97 | + targetmtime = path.getmtime(targetname) |
| 98 | + except Exception: |
| 99 | + targetmtime = 0 |
| 100 | + try: |
| 101 | + srcmtime = path.getmtime(sourcename) |
| 102 | + if srcmtime > targetmtime: |
| 103 | + yield docname |
| 104 | + except EnvironmentError: |
| 105 | + # source doesn't exist anymore |
| 106 | + pass |
| 107 | + |
| 108 | + def get_target_uri(self, docname, typ=None): |
| 109 | + return self.link_transform(docname) |
| 110 | + |
| 111 | + def get_relative_uri(self, from_, to, typ=None): |
| 112 | + """ |
| 113 | + Return a relative URI between two source filenames. |
| 114 | + """ |
| 115 | + # This is slightly different from Builder.get_relative_uri, |
| 116 | + # as it contains a small bug (which was fixed in Sphinx 1.2). |
| 117 | + return relative_uri(self.get_target_uri(from_), |
| 118 | + self.get_target_uri(to, typ)) |
| 119 | + |
| 120 | + def prepare_writing(self, docnames): |
| 121 | + self.writer = RstWriter(self) |
| 122 | + |
| 123 | + def write_doc(self, docname, doctree): |
| 124 | + # This method is taken from TextBuilder.write_doc() |
| 125 | + # with minor changes to support :confval:`rst_file_transform`. |
| 126 | + destination = StringOutput(encoding='utf-8') |
| 127 | + # print "write(%s,%s)" % (type(doctree), type(destination)) |
| 128 | + |
| 129 | + self.writer.write(doctree, destination) |
| 130 | + outfilename = path.join(self.outdir, self.file_transform(docname)) |
| 131 | + # print "write(%s,%s) -> %s" % (type(doctree), type(destination), outfilename) |
| 132 | + ensuredir(path.dirname(outfilename)) |
| 133 | + try: |
| 134 | + f = codecs.open(outfilename, 'w', 'utf-8') |
| 135 | + try: |
| 136 | + f.write(self.writer.output) |
| 137 | + finally: |
| 138 | + f.close() |
| 139 | + except (IOError, OSError), err: |
| 140 | + self.warn("error writing file %s: %s" % (outfilename, err)) |
| 141 | + |
| 142 | + def finish(self): |
| 143 | + pass |
0 commit comments