Skip to content

Commit a514c90

Browse files
william-andreAntoineVDV
authored andcommitted
[ADD] conf: give relative and absolute path to odoo repo
Also add a condition on some directives to ignore them when we have no relative/absolute path. Part-of: #1956
1 parent 496a284 commit a514c90

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

conf.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
from pathlib import Path
55

6+
import docutils
67
from pygments.lexers import JsonLexer, XmlLexer
78
from sphinx.util import logging
89
import sphinx
@@ -53,6 +54,11 @@
5354

5455
#=== Extensions configuration ===#
5556

57+
source_read_replace_vals = {
58+
'GITHUB_PATH': f'https://github.com/odoo/odoo/blob/{version}',
59+
'GITHUB_ENT_PATH': f'https://github.com/odoo/enterprise/blob/{version}',
60+
}
61+
5662
# Add extensions directory to PYTHONPATH
5763
extension_dir = Path('extensions')
5864
sys.path.insert(0, str(extension_dir.absolute()))
@@ -75,6 +81,8 @@
7581
)
7682
else:
7783
odoo_dir = odoo_sources_dirs[0].resolve()
84+
source_read_replace_vals['ODOO_RELPATH'] = '/../' + str(odoo_sources_dirs[0])
85+
source_read_replace_vals['ODOO_ABSPATH'] = str(odoo_dir)
7886
sys.path.insert(0, str(odoo_dir))
7987
if (3, 6) < sys.version_info < (3, 7):
8088
# Running odoo needs python 3.7 min but monkey patch version_info to be compatible with 3.6
@@ -266,6 +274,22 @@
266274
# If true, show URL addresses after external links.
267275
latex_show_urls = 'True'
268276

277+
# https://github.com/sphinx-doc/sphinx/issues/4054#issuecomment-329097229
278+
def source_read_replace(app, docname, source):
279+
"""Substitute parts of strings with computed values.
280+
281+
Since the RST substitution is not working everywhere, i.e. in directives'
282+
options, we need to be able to input those values when reading the sources.
283+
This is using the config `source_read_replace_vals`, mapping a name to its
284+
replacement. This will look for the name surrounded by curly braces in the source.
285+
286+
Meant to be connected to the `source-read` event.
287+
"""
288+
result = source[0]
289+
for key in app.config.source_read_replace_vals:
290+
result = result.replace(f"{{{key}}}", app.config.source_read_replace_vals[key])
291+
source[0] = result
292+
269293

270294
def setup(app):
271295
# Generate all alternate URLs for each document
@@ -274,12 +298,34 @@ def setup(app):
274298
app.add_config_value('versions', None, 'env')
275299
app.add_config_value('languages', None, 'env')
276300
app.add_config_value('is_remote_build', None, 'env') # Whether the build is remotely deployed
301+
app.add_config_value('source_read_replace_vals', {}, 'env')
302+
app.connect('source-read', source_read_replace)
277303

278304
app.add_lexer('json', JsonLexer)
279305
app.add_lexer('xml', XmlLexer)
280306

281307
app.connect('html-page-context', _generate_alternate_urls)
282308

309+
# Add a `condition` option on directives to ignore them based on config values
310+
app.add_config_value('odoo_dir_in_path', None, 'env')
311+
def context_eval(expr):
312+
return eval(expr, {confval.name: confval.value for confval in app.config})
313+
314+
def patch(to_patch):
315+
to_patch.option_spec['condition'] = context_eval
316+
original_run = to_patch.run
317+
def new_run(self):
318+
if not self.options.get('condition', True):
319+
return []
320+
return original_run(self)
321+
to_patch.run = new_run
322+
323+
for to_patch in (
324+
sphinx.directives.code.LiteralInclude,
325+
docutils.parsers.rst.directives.tables.CSVTable,
326+
):
327+
patch(to_patch)
328+
283329

284330
def _generate_alternate_urls(app, pagename, templatename, context, doctree):
285331
""" Add keys of required alternate URLs for the current document in the rendering context.

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
docutils>=0.14,<0.19
12
libsass==0.18.0
23
pygments~=2.6.1
34
pygments-csv-lexer~=0.1

0 commit comments

Comments
 (0)