|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + sphinxcontrib.builders.singlerst |
| 4 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 5 | +
|
| 6 | + Single ReST file 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 | +from sphinx.util.nodes import inline_all_toctrees |
| 15 | + |
| 16 | +from .rst import RstBuilder |
| 17 | +from ..writers.rst import RstWriter |
| 18 | + |
| 19 | + |
| 20 | +class SingleRstBuilder(RstBuilder): |
| 21 | + """ |
| 22 | + A builder that combines all documents into a single RST file. |
| 23 | + """ |
| 24 | + name = 'singlerst' |
| 25 | + |
| 26 | + def get_outdated_docs(self): |
| 27 | + """ |
| 28 | + Return an iterable of input files that are outdated. |
| 29 | + """ |
| 30 | + # Since all documents are combined into one, we always rebuild. |
| 31 | + return 'all documents' |
| 32 | + |
| 33 | + def get_target_uri(self, docname, typ=None): |
| 34 | + if docname == self.config.root_doc: |
| 35 | + return '' |
| 36 | + return '#document-' + docname |
| 37 | + |
| 38 | + def assemble_doctree(self): |
| 39 | + """Assemble all documents into a single doctree.""" |
| 40 | + root_doc = self.config.root_doc |
| 41 | + tree = self.env.get_doctree(root_doc) |
| 42 | + # Inline all toctrees to merge all documents into the root doctree. |
| 43 | + tree = inline_all_toctrees( |
| 44 | + self, set(), root_doc, tree, colorfunc=lambda x: x, traversed=[] |
| 45 | + ) |
| 46 | + tree['docname'] = root_doc |
| 47 | + # Resolve all references now that all documents are combined. |
| 48 | + self.env.resolve_references(tree, root_doc, self) |
| 49 | + return tree |
| 50 | + |
| 51 | + def prepare_writing(self, docnames): |
| 52 | + self.writer = RstWriter(self) |
| 53 | + |
| 54 | + def write(self, build_docnames, updated_docnames, method='update'): |
| 55 | + # This method overrides the base builder's write() to combine |
| 56 | + # all documents into a single file instead of writing each separately. |
| 57 | + docnames = self.env.all_docs |
| 58 | + self.prepare_writing(docnames) |
| 59 | + doctree = self.assemble_doctree() |
| 60 | + self.write_doc(self.config.root_doc, doctree) |
| 61 | + |
| 62 | + def finish(self): |
| 63 | + pass |
0 commit comments