Custom command line variables and rst substitution

19 views
Skip to first unread message

DexterMagnific

unread,
Jan 17, 2020, 4:52:18 AM1/17/20
to sphinx-users

Dear all,

I'd like to pass custom variables through the command line with -D. For this I use the following code:


def setup(app): app.add_config_value('myVar', value, True)
 

Now I'd like to reference them in my .rst file using |myVar|, just like for |version| but I cannot make it work.


Can you tell me how can I achieve this ?

Thanks.

Komiya Takeshi

unread,
Jan 20, 2020, 11:58:31 AM1/20/20
to sphinx...@googlegroups.com
Hi,

reST parser does not refers config values. So you need to pass it to the parser.
Sphinx uses a transform component to do that:
https://github.com/sphinx-doc/sphinx/blob/816a06564de5cfdd23df57d1b3291c0ccc8dacda/sphinx/transforms/__init__.py#L100-L118

BTW, we have `rst_prolog` config value to generate reST source. So it
is easy way to pass the values.
How about such hack?

def update_rst_prolog(app, config):
# Update rst_prolog using config values
rst_prolog = config.rst_prolog or ''
if config.myVar:
rst_prolog += '.. |myVar| replace:: %s' % config.myVar

config.rst_prolog = rst_prolog

def setup(app):
# You need to handle config-inited event to obtain determined config values.
app.connect('config-inited', update_rst_prolog)

Thanks,
Takeshi KOMIYA

2020年1月17日(金) 18:52 DexterMagnific <said....@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/2ded30c4-0231-4d0c-8b3b-69318d654a08%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages