Skip to content

Commit 9cd5ba9

Browse files
committed
Fix #2445: rst_prolog and rst_epilog affect to non reST sources
1 parent 89360dd commit 9cd5ba9

File tree

8 files changed

+65
-14
lines changed

8 files changed

+65
-14
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Bugs fixed
6464
* #2561: Info builder crashes when a footnote contains a link
6565
* #2565: The descriptions of objects generated by ``sphinx.ext.autosummary`` overflow lines at LaTeX writer
6666
* Extend pdflatex config in sphinx.sty to subparagraphs (ref: #2551)
67+
* #2445: `rst_prolog` and `rst_epilog` affect to non reST sources
6768

6869

6970
Release 1.4.1 (released Apr 12, 2016)

sphinx/io.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,26 @@ def decode(self, data):
112112
return data.decode(self.encoding, 'sphinx') # py2: decoding
113113

114114
def read(self):
115+
def get_parser_type(docname):
116+
path = self.env.doc2path(docname)
117+
for suffix in self.env.config.source_parsers:
118+
if path.endswith(suffix):
119+
parser_class = self.env.config.source_parsers[suffix]
120+
if isinstance(parser_class, string_types):
121+
parser_class = import_object(parser_class, 'source parser')
122+
return parser_class.supported
123+
else:
124+
return ('restructuredtext',)
125+
115126
data = FileInput.read(self)
116127
if self.app:
117128
arg = [data]
118129
self.app.emit('source-read', self.env.docname, arg)
119130
data = arg[0]
120131
docinfo, data = split_docinfo(data)
121-
if self.env.config.rst_epilog:
122-
data = data + '\n' + self.env.config.rst_epilog + '\n'
123-
if self.env.config.rst_prolog:
124-
data = self.env.config.rst_prolog + '\n' + data
132+
if 'restructuredtext' in get_parser_type(self.env.docname):
133+
if self.env.config.rst_epilog:
134+
data = data + '\n' + self.env.config.rst_epilog + '\n'
135+
if self.env.config.rst_prolog:
136+
data = self.env.config.rst_prolog + '\n' + data
125137
return docinfo + data

tests/roots/test-prolog/conf.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import os
4+
import sys
5+
sys.path.insert(0, os.path.abspath('.'))
6+
7+
18
master_doc = 'index'
9+
extensions = ['prolog_markdown_parser']
10+
211
rst_prolog = '*Hello world*.\n\n'
312
rst_epilog = '\n\n*Good-bye world*.'

tests/roots/test-prolog/index.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
prolog and epilog
22
=================
3+
4+
.. toctree::
5+
6+
restructuredtext
7+
markdown
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# sample document
2+
3+
This is a sample document in markdown
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from docutils.parsers import Parser
4+
5+
6+
class DummyMarkdownParser(Parser):
7+
def parse(self, inputstring, document):
8+
document.rawsource = inputstring
9+
10+
11+
def setup(app):
12+
app.add_source_parser('.md', DummyMarkdownParser)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
sample document
2+
===============
3+
4+
This is a sample document in reST

tests/test_markup.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,22 @@ def test_latex_escaping():
148148
@with_app(buildername='dummy', testroot='prolog')
149149
def test_rst_prolog(app, status, warning):
150150
app.builder.build_all()
151-
doctree = pickle.loads((app.doctreedir / 'index.doctree').bytes())
151+
rst = pickle.loads((app.doctreedir / 'restructuredtext.doctree').bytes())
152+
md = pickle.loads((app.doctreedir / 'markdown.doctree').bytes())
152153

153154
# rst_prolog
154-
assert_node(doctree[0], nodes.paragraph)
155-
assert_node(doctree[0][0], nodes.emphasis)
156-
assert_node(doctree[0][0][0], nodes.Text)
157-
assert doctree[0][0][0] == 'Hello world'
155+
assert_node(rst[0], nodes.paragraph)
156+
assert_node(rst[0][0], nodes.emphasis)
157+
assert_node(rst[0][0][0], nodes.Text)
158+
assert rst[0][0][0] == 'Hello world'
158159

159160
# rst_epilog
160-
assert_node(doctree[-1], nodes.section)
161-
assert_node(doctree[-1][-1], nodes.paragraph)
162-
assert_node(doctree[-1][-1][0], nodes.emphasis)
163-
assert_node(doctree[-1][-1][0][0], nodes.Text)
164-
assert doctree[-1][-1][0][0] == 'Good-bye world'
161+
assert_node(rst[-1], nodes.section)
162+
assert_node(rst[-1][-1], nodes.paragraph)
163+
assert_node(rst[-1][-1][0], nodes.emphasis)
164+
assert_node(rst[-1][-1][0][0], nodes.Text)
165+
assert rst[-1][-1][0][0] == 'Good-bye world'
166+
167+
# rst_prolog & rst_epilog on exlucding reST parser
168+
assert not md.rawsource.startswith('*Hello world*.')
169+
assert not md.rawsource.endswith('*Good-bye world*.\n')

0 commit comments

Comments
 (0)