-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
source-read event does not modify include'd files source #10678
Comments
Unfortunately, the
You can see the result of the replacement in |
This should at the very least be documented so users don't expect it to work like I did. I understand "wontfix" as "this is working as intended", is there any technical reason behind this choice? Basically, is this something that can be implemented/fixed in future versions or is it an active and deliberate choice that it'll never be supported? |
…content modification Files included with the `include` directive have their content available in the source-read event but modifications will not make it to the output, so let's document it. Closes sphinx-doc#10678 Signed-off-by: Quentin Schulz <[email protected]>
Hello. Is there any workaround to solve this? Maybe hooking the include action as with source-read?? |
I spent the last two days trying to use the Pretty old ticket, it is not clear from the response by @tk0miya if this a bug, or what should be done instead. This seems like a pretty basic use case for the Sphinx API (i.e., search/replace text using the API) |
AFAICT, this is the intended behaviour. As they said:
IIRC, the If you want the file being included via the Instead of using rst_prolog = """
.. |mine| replace:: not yours
""" and then, in the desired document: This document is |mine|. For a more generic way, you'll need to dig up more. Here are some hacky ideas:
|
Here is a solution, that fixes the underlying problem in Sphinx, using an extension: """Extension to fix issues in the built-in include directive."""
import docutils.statemachine
# Provide fixes for Sphinx `include` directive, which doesn't support Sphinx's
# source-read event.
# Fortunately the Include directive ultimately calls StateMachine.insert_input,
# for rst text and this is the only use of that function. So we monkey-patch!
def setup(app):
og_insert_input = docutils.statemachine.StateMachine.insert_input
def my_insert_input(self, include_lines, path):
# first we need to combine the lines back into text so we can send it with the source-read
# event:
text = "\n".join(include_lines)
# emit "source-read" event
arg = [text]
app.env.events.emit("source-read", path, arg)
text = arg[0]
# split into lines again:
include_lines = text.splitlines()
# call the original function:
og_insert_input(self, include_lines, path)
# inject our patched function
docutils.statemachine.StateMachine.insert_input = my_insert_input
return {
"version": "0.0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
} Now, I'm willing to contribute a proper patch to Sphinx (subclassing |
This extension enables me to set conditionals on table rows. Yay! |
Wow! that's a great plugin. Thanks for sharing!! |
One more thing, this issue should be named "source-read event is not emitted for included rst files" - that is truly the issue at play here. What my patch does is inject code into the |
I was worked around the problem by eliminating all include statements in our documentation. I am using TOC solely instead. Probably best practice anyway. |
@halldorfannar please could you convert your patch into a PR? A |
Absolutely, @AA-Turner. I will start that work today. |
Closing since it's now merged (and scheduled for 7.2). |
Describe the bug
In Yocto documentation, we use a custom extension to do some search and replace in literal blocks, see https://git.yoctoproject.org/yocto-docs/tree/documentation/sphinx/yocto-vars.py.
We discovered (https://git.yoctoproject.org/yocto-docs/commit/?id=b7375ea4380e716a02c736e4231aaf7c1d868c6b and https://lore.kernel.org/yocto-docs/CAP71WjwG2PCT=ceuZpBmeF-Xzn9yVQi1PG2+d6+wRjouoAZ0Aw@mail.gmail.com/#r) that this does not work on all files and some are left out of this mechanism. Such is the case for include'd files.
I could reproduce on Sphinx 5.0.2.
How to Reproduce
conf.py:
index.rst:
something-to-include.rst:
Testing ======= &REPLACE_ME;
my-extension.py:
build/index.html
will contain:Note that the dumping docname and source[0] shows that the function actually gets called for something-to-include.rst file and its content is correctly replaced in source[0], it just does not make it to the final HTML file for some reason.
Expected behavior
build/index.html
should contain:Your project
https://git.yoctoproject.org/yocto-docs
Screenshots
No response
OS
Linux
Python version
3.10
Sphinx version
5.0.2
Sphinx extensions
Custom extension using source-read event
Extra tools
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: