33import sys
44from pathlib import Path
55
6+ import docutils
67from pygments .lexers import JsonLexer , XmlLexer
78from sphinx .util import logging
89import sphinx
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
5763extension_dir = Path ('extensions' )
5864sys .path .insert (0 , str (extension_dir .absolute ()))
7581 )
7682else :
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
266274# If true, show URL addresses after external links.
267275latex_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
270294def 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
284330def _generate_alternate_urls (app , pagename , templatename , context , doctree ):
285331 """ Add keys of required alternate URLs for the current document in the rendering context.
0 commit comments