EvenThis
EvenThis

Reputation: 333

Sphinx documentation variables

I'm creating documentation using Sphinx Doc for one of my projects and I'm using many times some words in whole doc, for example - IP address, port numbers and many other things that may change in time. If, for some reason, one of it will be changed, I want to change it simply in one place, not manually in every file. What I'm looking for, is some simple way to create some kind of resource file/variables, which can be easily accessible from all my .rst files.

I found this one:

my_config_value = 'test'
rst_epilog = '.. |my_conf_val| replace:: %s' % my_config_value

It's almost perfect, but I want to have many variables. Using method above, I'm able to use only one - that's not enough.

Also I have found method using sphinx.ext.extlinks. It's working, but not exactly in the way I wanted to. I'm using this in my conf.py:

extlinks = {'test': ('bla_bla_bla', None)}

And then, in some .rst:

:test:`1`

with result bla_bla_bla1

First, this is hyperlink to nowhere. Second, I can't write

:test:` `

or even just :test:, because it doesn't work.

And there come my question: is there any simple way to create variables, which can be used in whole doc?

Upvotes: 5

Views: 2655

Answers (1)

EvenThis
EvenThis

Reputation: 333

Thanks to mzjn I have found exact answer for my question. If somebody will search for it in future, I left answer below.

In my conf.py I added this:

address = '192.168.1.1'
port = 'port 3333'

rst_prolog = """
.. |address| replace:: {0}
.. |port| replace:: {1}
""".format(
address, 
port
)

And then in my .rst just use:

|address| - |port|

with result 192.168.1.1 - port 3333. This is exactly what I needed. Thank you very much mzjn.

Upvotes: 17

Related Questions