From dd371a59de0a8730bab3b4e61f7718a7260fb0ef Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Fri, 4 Jan 2019 00:32:15 +0900 Subject: [PATCH] Move DirectoryHTMLBuilder to sphinx.builders.dirhtml package --- sphinx/application.py | 1 + sphinx/builders/dirhtml.py | 68 ++++++++++++++++++++++++++++++++++++++ sphinx/builders/html.py | 36 ++------------------ 3 files changed, 71 insertions(+), 34 deletions(-) create mode 100644 sphinx/builders/dirhtml.py diff --git a/sphinx/application.py b/sphinx/application.py index 762b7f89c7..cce9926b53 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -64,6 +64,7 @@ builtin_extensions = ( 'sphinx.addnodes', 'sphinx.builders.changes', 'sphinx.builders.epub3', + 'sphinx.builders.dirhtml', 'sphinx.builders.dummy', 'sphinx.builders.gettext', 'sphinx.builders.html', diff --git a/sphinx/builders/dirhtml.py b/sphinx/builders/dirhtml.py new file mode 100644 index 0000000000..7ab6fad98a --- /dev/null +++ b/sphinx/builders/dirhtml.py @@ -0,0 +1,68 @@ +""" + sphinx.builders.dirhtml + ~~~~~~~~~~~~~~~~~~~~~~~ + + Directory HTML builders. + + :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS. + :license: BSD, see LICENSE for details. +""" + +from os import path + +from sphinx.builders.html import StandaloneHTMLBuilder +from sphinx.util import logging +from sphinx.util.osutil import SEP, os_path + +if False: + # For type annotation + from typing import Any, Dict, Set # NOQA + from sphinx.application import Sphinx # NOQA + +logger = logging.getLogger(__name__) + + +class DirectoryHTMLBuilder(StandaloneHTMLBuilder): + """ + A StandaloneHTMLBuilder that creates all HTML pages as "index.html" in + a directory given by their pagename, so that generated URLs don't have + ``.html`` in them. + """ + name = 'dirhtml' + + def get_target_uri(self, docname, typ=None): + # type: (str, str) -> str + if docname == 'index': + return '' + if docname.endswith(SEP + 'index'): + return docname[:-5] # up to sep + return docname + SEP + + def get_outfilename(self, pagename): + # type: (str) -> str + if pagename == 'index' or pagename.endswith(SEP + 'index'): + outfilename = path.join(self.outdir, os_path(pagename) + + self.out_suffix) + else: + outfilename = path.join(self.outdir, os_path(pagename), + 'index' + self.out_suffix) + + return outfilename + + def prepare_writing(self, docnames): + # type: (Set[str]) -> None + super().prepare_writing(docnames) + self.globalcontext['no_search_suffix'] = True + + +def setup(app): + # type: (Sphinx) -> Dict[str, Any] + app.setup_extension('sphinx.builders.html') + + app.add_builder(DirectoryHTMLBuilder) + + return { + 'version': 'builtin', + 'parallel_read_safe': True, + 'parallel_write_safe': True, + } diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 87b3ccac8b..09ba8d6778 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -1169,39 +1169,6 @@ class StandaloneHTMLBuilder(Builder): logger.info(__('done')) -class DirectoryHTMLBuilder(StandaloneHTMLBuilder): - """ - A StandaloneHTMLBuilder that creates all HTML pages as "index.html" in - a directory given by their pagename, so that generated URLs don't have - ``.html`` in them. - """ - name = 'dirhtml' - - def get_target_uri(self, docname, typ=None): - # type: (str, str) -> str - if docname == 'index': - return '' - if docname.endswith(SEP + 'index'): - return docname[:-5] # up to sep - return docname + SEP - - def get_outfilename(self, pagename): - # type: (str) -> str - if pagename == 'index' or pagename.endswith(SEP + 'index'): - outfilename = path.join(self.outdir, os_path(pagename) + - self.out_suffix) - else: - outfilename = path.join(self.outdir, os_path(pagename), - 'index' + self.out_suffix) - - return outfilename - - def prepare_writing(self, docnames): - # type: (Set[str]) -> None - super().prepare_writing(docnames) - self.globalcontext['no_search_suffix'] = True - - class SerializingHTMLBuilder(StandaloneHTMLBuilder): """ An abstract builder that serializes the generated HTML. @@ -1417,10 +1384,12 @@ def validate_math_renderer(app): # for compatibility +from sphinx.builders.dirhtml import DirectoryHTMLBuilder # NOQA from sphinx.builders.singlehtml import SingleFileHTMLBuilder # NOQA deprecated_alias('sphinx.builders.html', { + 'DirectoryHTMLBuilder': DirectoryHTMLBuilder, 'SingleFileHTMLBuilder': SingleFileHTMLBuilder, }, RemovedInSphinx40Warning) @@ -1430,7 +1399,6 @@ def setup(app): # type: (Sphinx) -> Dict[str, Any] # builders app.add_builder(StandaloneHTMLBuilder) - app.add_builder(DirectoryHTMLBuilder) app.add_builder(PickleHTMLBuilder) app.add_builder(JSONHTMLBuilder)