summaryrefslogtreecommitdiff
path: root/sphinx/io.py
diff options
context:
space:
mode:
authorTakeshi KOMIYA <i.tkomiya@gmail.com>2017-10-31 23:04:25 +0900
committerTakeshi KOMIYA <i.tkomiya@gmail.com>2017-10-31 23:05:10 +0900
commit1200ab48e1f24fcc8cf3232ae21c056ed6bca0f8 (patch)
tree6604f981620480cc2097fe3ad0d8df16f3956b24 /sphinx/io.py
parentd23f29f301648ba9f84641fb44ebab43b07deb48 (diff)
downloadsphinx-git-1200ab48e1f24fcc8cf3232ae21c056ed6bca0f8.tar.gz
Refactor parsing by docutils
Diffstat (limited to 'sphinx/io.py')
-rw-r--r--sphinx/io.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/sphinx/io.py b/sphinx/io.py
index 9b27e04fd..ab8c9d068 100644
--- a/sphinx/io.py
+++ b/sphinx/io.py
@@ -8,7 +8,8 @@
:copyright: Copyright 2007-2017 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
-from docutils.io import FileInput
+from docutils.io import FileInput, NullOutput
+from docutils.core import Publisher
from docutils.readers import standalone
from docutils.writers import UnfilteredWriter
from six import string_types, text_type, iteritems
@@ -185,3 +186,21 @@ class SphinxFileInput(FileInput):
if self.env.config.rst_prolog:
data = self.env.config.rst_prolog + '\n' + data
return docinfo + data
+
+
+def read_doc(app, env, filename):
+ """Parse a document and convert to doctree."""
+ # type: (Sphinx, BuildEnvironment, unicode) -> nodes.document
+ reader = SphinxStandaloneReader(app, parsers=app.registry.get_source_parsers())
+ source = SphinxFileInput(app, env, source=None, source_path=filename,
+ encoding=env.config.source_encoding)
+
+ pub = Publisher(reader=reader,
+ writer=SphinxDummyWriter(),
+ source_class=SphinxDummySourceClass,
+ destination=NullOutput())
+ pub.set_components(None, 'restructuredtext', None)
+ pub.process_programmatic_settings(None, env.settings, None)
+ pub.set_source(source, filename)
+ pub.publish()
+ return pub.document