Fix #2445: rst_prolog and rst_epilog affect to non reST sources

This commit is contained in:
Takeshi KOMIYA
2016-05-25 19:58:15 +09:00
parent 89360dd3f1
commit 9cd5ba994c
8 changed files with 65 additions and 14 deletions

View File

@@ -64,6 +64,7 @@ Bugs fixed
* #2561: Info builder crashes when a footnote contains a link
* #2565: The descriptions of objects generated by ``sphinx.ext.autosummary`` overflow lines at LaTeX writer
* Extend pdflatex config in sphinx.sty to subparagraphs (ref: #2551)
* #2445: `rst_prolog` and `rst_epilog` affect to non reST sources
Release 1.4.1 (released Apr 12, 2016)

View File

@@ -112,14 +112,26 @@ class SphinxFileInput(FileInput):
return data.decode(self.encoding, 'sphinx') # py2: decoding
def read(self):
def get_parser_type(docname):
path = self.env.doc2path(docname)
for suffix in self.env.config.source_parsers:
if path.endswith(suffix):
parser_class = self.env.config.source_parsers[suffix]
if isinstance(parser_class, string_types):
parser_class = import_object(parser_class, 'source parser')
return parser_class.supported
else:
return ('restructuredtext',)
data = FileInput.read(self)
if self.app:
arg = [data]
self.app.emit('source-read', self.env.docname, arg)
data = arg[0]
docinfo, data = split_docinfo(data)
if self.env.config.rst_epilog:
data = data + '\n' + self.env.config.rst_epilog + '\n'
if self.env.config.rst_prolog:
data = self.env.config.rst_prolog + '\n' + data
if 'restructuredtext' in get_parser_type(self.env.docname):
if self.env.config.rst_epilog:
data = data + '\n' + self.env.config.rst_epilog + '\n'
if self.env.config.rst_prolog:
data = self.env.config.rst_prolog + '\n' + data
return docinfo + data

View File

@@ -1,3 +1,12 @@
# -*- coding: utf-8 -*-
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
master_doc = 'index'
extensions = ['prolog_markdown_parser']
rst_prolog = '*Hello world*.\n\n'
rst_epilog = '\n\n*Good-bye world*.'

View File

@@ -1,2 +1,7 @@
prolog and epilog
=================
.. toctree::
restructuredtext
markdown

View File

@@ -0,0 +1,3 @@
# sample document
This is a sample document in markdown

View File

@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-
from docutils.parsers import Parser
class DummyMarkdownParser(Parser):
def parse(self, inputstring, document):
document.rawsource = inputstring
def setup(app):
app.add_source_parser('.md', DummyMarkdownParser)

View File

@@ -0,0 +1,4 @@
sample document
===============
This is a sample document in reST

View File

@@ -148,17 +148,22 @@ def test_latex_escaping():
@with_app(buildername='dummy', testroot='prolog')
def test_rst_prolog(app, status, warning):
app.builder.build_all()
doctree = pickle.loads((app.doctreedir / 'index.doctree').bytes())
rst = pickle.loads((app.doctreedir / 'restructuredtext.doctree').bytes())
md = pickle.loads((app.doctreedir / 'markdown.doctree').bytes())
# rst_prolog
assert_node(doctree[0], nodes.paragraph)
assert_node(doctree[0][0], nodes.emphasis)
assert_node(doctree[0][0][0], nodes.Text)
assert doctree[0][0][0] == 'Hello world'
assert_node(rst[0], nodes.paragraph)
assert_node(rst[0][0], nodes.emphasis)
assert_node(rst[0][0][0], nodes.Text)
assert rst[0][0][0] == 'Hello world'
# rst_epilog
assert_node(doctree[-1], nodes.section)
assert_node(doctree[-1][-1], nodes.paragraph)
assert_node(doctree[-1][-1][0], nodes.emphasis)
assert_node(doctree[-1][-1][0][0], nodes.Text)
assert doctree[-1][-1][0][0] == 'Good-bye world'
assert_node(rst[-1], nodes.section)
assert_node(rst[-1][-1], nodes.paragraph)
assert_node(rst[-1][-1][0], nodes.emphasis)
assert_node(rst[-1][-1][0][0], nodes.Text)
assert rst[-1][-1][0][0] == 'Good-bye world'
# rst_prolog & rst_epilog on exlucding reST parser
assert not md.rawsource.startswith('*Hello world*.')
assert not md.rawsource.endswith('*Good-bye world*.\n')