diff --git a/doc/config.rst b/doc/config.rst index 891f080b5..9654e7856 100644 --- a/doc/config.rst +++ b/doc/config.rst @@ -870,6 +870,13 @@ that use Sphinx's HTMLWriter class. .. versionadded:: 0.6 +.. confval:: html_sourcelink_txt + + If true, ``.txt`` is appended to source links (see + :confval:`html_show_sourcelink`). Default is ``True``. + + .. versionadded:: 1.5 + .. confval:: html_use_opensearch If nonempty, an `OpenSearch `_ description file will be diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index 63ccdd66c..01a634bdd 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -402,15 +402,21 @@ class StandaloneHTMLBuilder(Builder): # title rendered as HTML title = self.env.longtitles.get(docname) title = title and self.render_partial(title)['title'] or '' + + # Suffix for the document + source_suffix = path.splitext(self.env.doc2path(docname))[1] + # the name for the copied source - sourcename = self.config.html_copy_source and docname + '.txt' or '' + if self.config.html_copy_source: + sourcename = docname + source_suffix + if self.config.html_sourcelink_txt and source_suffix != '.txt': + sourcename += '.txt' + else: + sourcename = '' # metadata for the document meta = self.env.metadata.get(docname) - # Suffix for the document - source_suffix = '.' + self.env.doc2path(docname).split('.')[-1] - # local TOC and global TOC tree self_toc = self.env.get_toc_for(docname, self) toc = self.render_partial(self_toc)['fragment'] diff --git a/sphinx/config.py b/sphinx/config.py index ef57334fe..cd05db45e 100644 --- a/sphinx/config.py +++ b/sphinx/config.py @@ -121,6 +121,7 @@ class Config(object): html_split_index = (False, 'html'), html_copy_source = (True, 'html'), html_show_sourcelink = (True, 'html'), + html_sourcelink_txt = (True, 'html'), html_use_opensearch = ('', 'html'), html_file_suffix = (None, 'html', string_classes), html_link_suffix = (None, 'html', string_classes), diff --git a/tests/test_build_html.py b/tests/test_build_html.py index 177fa71b5..df10c458c 100644 --- a/tests/test_build_html.py +++ b/tests/test_build_html.py @@ -319,7 +319,7 @@ HTML_XPATH = { ], 'otherext.html': [ (".//h1", "Generated section"), - (".//a[@href='_sources/otherext.txt']", ''), + (".//a[@href='_sources/otherext.foo.txt']", ''), ] } @@ -987,3 +987,15 @@ def test_html_extra_path(app, status, warning): assert (app.outdir / 'rimg.png').exists() assert not (app.outdir / '_build/index.html').exists() assert (app.outdir / 'background.png').exists() + + +@with_app(buildername='html', confoverrides={'html_sourcelink_txt': False}) +def test_html_sourcelink_txt(app, status, warning): + app.builder.build_all() + content_otherext = (app.outdir / 'otherext.html').text() + content_images = (app.outdir / 'images.html').text() + + assert '