Refactor sphinx.environment: Reimplement filter_messages() as a transform

This commit is contained in:
Takeshi KOMIYA 2016-09-10 17:38:10 +09:00
parent cd515d568d
commit d281a32b3b
6 changed files with 42 additions and 12 deletions

View File

@ -728,7 +728,6 @@ class BuildEnvironment(object):
doctree = pub.document
# post-processing
self.filter_messages(doctree)
self.process_dependencies(docname, doctree)
self.process_images(docname, doctree)
self.process_downloads(docname, doctree)
@ -851,14 +850,6 @@ class BuildEnvironment(object):
# post-processing of read doctrees
def filter_messages(self, doctree):
"""Filter system messages from a doctree."""
filterlevel = self.config.keep_warnings and 2 or 5
for node in doctree.traverse(nodes.system_message):
if node['level'] < filterlevel:
self.app.debug('%s [filtered system message]', node.astext())
node.parent.remove(node)
def process_dependencies(self, docname, doctree):
"""Process docutils-generated dependency info."""
cwd = getcwd()

View File

@ -16,7 +16,8 @@ from six import string_types, text_type
from sphinx.transforms import (
ApplySourceWorkaround, ExtraTranslatableNodes, PreserveTranslatableMessages, Locale,
CitationReferences, DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks,
AutoNumbering, AutoIndexUpgrader, SortIds, RemoveTranslatableInline
AutoNumbering, AutoIndexUpgrader, SortIds, RemoveTranslatableInline,
FilterSystemMessages
)
from sphinx.util import import_object, split_docinfo
@ -62,7 +63,7 @@ class SphinxStandaloneReader(SphinxBaseReader):
transforms = [ApplySourceWorkaround, ExtraTranslatableNodes, PreserveTranslatableMessages,
Locale, CitationReferences, DefaultSubstitutions, MoveModuleTargets,
HandleCodeBlocks, AutoNumbering, AutoIndexUpgrader, SortIds,
RemoveTranslatableInline, PreserveTranslatableMessages]
RemoveTranslatableInline, PreserveTranslatableMessages, FilterSystemMessages]
class SphinxI18nReader(SphinxBaseReader):
@ -75,7 +76,8 @@ class SphinxI18nReader(SphinxBaseReader):
transforms = [ApplySourceWorkaround, ExtraTranslatableNodes, CitationReferences,
DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks,
AutoNumbering, SortIds, RemoveTranslatableInline]
AutoNumbering, SortIds, RemoveTranslatableInline,
FilterSystemMessages]
def __init__(self, *args, **kwargs):
SphinxBaseReader.__init__(self, *args, **kwargs)

View File

@ -249,6 +249,19 @@ class PreserveTranslatableMessages(Transform):
node.preserve_original_messages()
class FilterSystemMessages(Transform):
"""Filter system messages from a doctree."""
default_priority = 999
def apply(self):
env = self.document.settings.env
filterlevel = env.config.keep_warnings and 2 or 5
for node in self.document.traverse(nodes.system_message):
if node['level'] < filterlevel:
env.app.debug('%s [filtered system message]', node.astext())
node.parent.remove(node)
class Locale(Transform):
"""
Replace translatable nodes with their translated doctree.

View File

@ -0,0 +1,4 @@
# -*- coding: utf-8 -*-
master_doc = 'index'
keep_warnings = True

View File

@ -0,0 +1,2 @@
keep_warnings
=====

View File

@ -170,3 +170,21 @@ def test_rst_prolog(app, status, warning):
# rst_prolog & rst_epilog on exlucding reST parser
assert not md.rawsource.startswith('*Hello world*.')
assert not md.rawsource.endswith('*Good-bye world*.\n')
@with_app(buildername='dummy', testroot='keep_warnings')
def test_keep_warnings_is_True(app, status, warning):
app.builder.build_all()
doctree = pickle.loads((app.doctreedir / 'index.doctree').bytes())
assert_node(doctree[0], nodes.section)
assert len(doctree[0]) == 2
assert_node(doctree[0][1], nodes.system_message)
@with_app(buildername='dummy', testroot='keep_warnings',
confoverrides={'keep_warnings': False})
def test_keep_warnings_is_False(app, status, warning):
app.builder.build_all()
doctree = pickle.loads((app.doctreedir / 'index.doctree').bytes())
assert_node(doctree[0], nodes.section)
assert len(doctree[0]) == 1