Is it possible to use some kind of variable mechanism in code blocks?
I.e. i have such line in conf.py:
my_config_value = 42
rst_epilog = '.. |my_conf_val| replace:: %d' % my_config_value
And when i use it in some regular text like so:
My config value is |my_conf_val|!
Sphinx nicely replace the var and output as follows:
My config value is 42!
But when i try the same using code block:
.. code-block:: javascript
// |my_conf_val|
I get unreplaced |my_conf_val| string in the output.
Is it possible to achieve something similar (variables) inside code block?
Not sure if its related but it seems that other directives also does not handle this properly, i.e. http directive from sphinxcontrib.httpdomain package:
.. http:get:: |my_conf_va||/users
Is also not replacing.
Please send the question to users list: https://groups.google.com/group/sphinx-users
This is example to do that
My config value is |my_conf_val|!
.. code-block:: javascript
// |my_conf_val|
.. parsed-literal::
// |my_conf_val|
var a = 1;
However you cannot use highlighting on parsed-literal.

see also: http://docutils.sourceforge.net/docs/ref/rst/directives.html#parsed-literal-block
I found some solution for this to work - adding a source-read event handler that replaces strings on reading so it replaces them also in directives like http or code-block.
config.py:
def ultimateReplace(app, docname, source):
result = source[0]
for key in app.config.ultimate_replacements:
result = result.replace(key, app.config.ultimate_replacements[key])
source[0] = result
ultimate_replacements = {
"{TEST}" : "replaced"
}
def setup(app):
app.add_config_value('ultimate_replacements', {}, True)
app.connect('source-read', ultimateReplace)
And this kind of markup:
.. http:get:: testing/replacement/{TEST}
Properly generates like:
testing/replacement/replaced
I plan to create simple sphinx extension working like that but i dont have time right now.
create simple sphinx extension
sounds nice!
Now I close issue. Good luck ;)
This works fine for "normal" processing, but not for files added via .. include:: directive. For these files not source-read event seems to be sent, the file is processed by the directive itself.
If i want also to parse / replace in these files, what would be the recommended way?
@kreuzberger +1; Could you file a new issue please? Then I'll work for it later.
Hi! I'm not sure what to write in the issue( Bug, Improvement)?
Topic "Source-Read events for include directive missing" This seems to be already discussed and closed on issue #837
So can you give me a more detailled hint about what issue to open? E.g. a general
Topic "Using variables inside directives?"
This seems also to be closed on #2793 as duplicate of this one here.
Take a look at https://github.com/adamtheturtle/sphinx-substitution-extensions for variables in code blocks.
Most helpful comment
I found some solution for this to work - adding a
source-readevent handler that replaces strings on reading so it replaces them also in directives likehttporcode-block.config.py:And this kind of markup:
Properly generates like:
I plan to create simple sphinx extension working like that but i dont have time right now.