Skip to content

Commit

Permalink
Improve warning messages during including (refs: #4818)
Browse files Browse the repository at this point in the history
  • Loading branch information
tk0miya committed Apr 8, 2018
1 parent 6030e73 commit 8f5a7fe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -58,6 +58,7 @@ Features added
* LaTeX: new key ``'fvset'`` for :confval:`latex_elements`. For
XeLaTeX/LuaLaTeX its default sets ``fanvyvrb`` to use normal, not small,
fontsize in code-blocks (refs: #4793)
* Improve warning messages during including (refs: #4818)

Bugs fixed
----------
Expand Down
25 changes: 24 additions & 1 deletion sphinx/directives/other.py
Expand Up @@ -7,6 +7,8 @@
:license: BSD, see LICENSE for details.
"""

from contextlib import contextmanager

from docutils import nodes
from docutils.parsers.rst import Directive, directives
from docutils.parsers.rst.directives.admonitions import BaseAdmonition
Expand Down Expand Up @@ -430,14 +432,35 @@ class Include(BaseInclude):
def run(self):
# type: () -> List[nodes.Node]
env = self.state.document.settings.env
current_filename = env.doc2path(env.docname)
if self.arguments[0].startswith('<') and \
self.arguments[0].endswith('>'):
# docutils "standard" includes, do not do path processing
return BaseInclude.run(self)
rel_filename, filename = env.relfn2path(self.arguments[0])
self.arguments[0] = filename
env.note_included(filename)
return BaseInclude.run(self)
with patched_warnings(self, current_filename):
return BaseInclude.run(self)


@contextmanager
def patched_warnings(directive, parent_filename):
# type: (BaseInclude, unicode) -> Generator[None, None, None]
"""Add includee filename to the warnings during inclusion."""
try:
original = directive.state_machine.insert_input

def insert_input(input_lines, source):
# type: (Any, unicode) -> None
source += ' <included from %s>' % parent_filename
original(input_lines, source)

# patch insert_input() temporarily
directive.state_machine.insert_input = insert_input
yield
finally:
directive.state_machine.insert_input = original


def setup(app):
Expand Down

0 comments on commit 8f5a7fe

Please sign in to comment.