22from contextlib import contextmanager
33from pathlib import Path
44from tempfile import TemporaryDirectory
5- from typing import List , Optional
5+ from typing import Callable , List , Optional
66
77from jinja2 import Environment , FileSystemLoader
88
@@ -29,6 +29,10 @@ def generate_docs_configuration(
2929 use_autoapi : Optional [bool ] = None ,
3030 custom_css : Optional [Path ] = None ,
3131 custom_js : Optional [Path ] = None ,
32+ config_base : str = "tool.yardang" ,
33+ previous_versions : Optional [bool ] = False ,
34+ adjust_arguments : Callable = None ,
35+ adjust_template : Callable = None ,
3236):
3337 if os .path .exists ("conf.py" ):
3438 # yield folder path to sphinx build
@@ -37,8 +41,8 @@ def generate_docs_configuration(
3741 # load configuration
3842 default_data = os .path .split (os .getcwd ())[- 1 ]
3943 project = project or get_config (section = "name" , base = "project" ) or default_data .replace ("_" , "-" )
40- title = title or get_config (section = "title" ) or default_data .replace ("_" , "-" )
41- module = module or get_config (section = "module" ) or project .replace ("-" , "_" ) or default_data .replace ("-" , "_" )
44+ title = title or get_config (section = "title" , base = config_base ) or default_data .replace ("_" , "-" )
45+ module = module or get_config (section = "module" , base = config_base ) or project .replace ("-" , "_" ) or default_data .replace ("-" , "_" )
4246 description = description or get_config (section = "name" , base = "description" ) or default_data .replace ("_" , " " ).replace ("-" , " " )
4347 author = author or get_config (section = "authors" , base = "project" )
4448 if isinstance (author , list ) and len (author ) > 0 :
@@ -48,11 +52,11 @@ def generate_docs_configuration(
4852 if isinstance (author , dict ):
4953 author = author ["name" ]
5054 copyright = copyright or author
51- theme = theme or get_config (section = "theme" )
55+ theme = theme or get_config (section = "theme" , base = config_base )
5256 version = version or get_config (section = "version" , base = "project" )
5357 docs_root = (
5458 docs_root
55- or get_config (section = "docs-host" )
59+ or get_config (section = "docs-host" , base = config_base )
5660 or get_config (section = "urls.Homepage" , base = "project" )
5761 or get_config (section = "urls.homepage" , base = "project" )
5862 or get_config (section = "urls.Documentation" , base = "project" )
@@ -61,13 +65,13 @@ def generate_docs_configuration(
6165 or get_config (section = "urls.source" , base = "project" )
6266 or ""
6367 )
64- root = root or get_config (section = "root" )
65- cname = cname or get_config (section = "cname" )
66- pages = pages or get_config (section = "pages" ) or []
67- use_autoapi = use_autoapi or get_config (section = "use-autoapi" )
68+ root = root or get_config (section = "root" , base = config_base )
69+ cname = cname or get_config (section = "cname" , base = config_base )
70+ pages = pages or get_config (section = "pages" , base = config_base ) or []
71+ use_autoapi = use_autoapi or get_config (section = "use-autoapi" , base = config_base )
6872
69- custom_css = custom_css or get_config (section = "custom-css" ) or (Path (__file__ ).parent / "custom.css" )
70- custom_js = custom_js or get_config (section = "custom-js" ) or (Path (__file__ ).parent / "custom.js" )
73+ custom_css = custom_css or get_config (section = "custom-css" , base = config_base ) or (Path (__file__ ).parent / "custom.css" )
74+ custom_js = custom_js or get_config (section = "custom-js" , base = config_base ) or (Path (__file__ ).parent / "custom.js" )
7175
7276 source_dir = os .path .curdir
7377
@@ -113,12 +117,13 @@ def generate_docs_configuration(
113117 "autodoc_pydantic_settings_show_json" : None ,
114118 "autodoc_pydantic_model_show_field_summary" : None ,
115119 }.items ():
116- configuration_args [config_option ] = get_config (section = config_option ) or default
120+ configuration_args [config_option ] = get_config (section = config_option , base = config_base ) or default
121+
117122 # create a temporary directory to store the conf.py file in
118123 with TemporaryDirectory () as td :
119124 templateEnv = Environment (loader = FileSystemLoader (searchpath = str (Path (__file__ ).parent .resolve ())))
120- # load the templatized conf.py file
121- template = templateEnv . get_template ( "conf.py.j2" ). render (
125+
126+ args = dict (
122127 project = project ,
123128 title = title ,
124129 module = module ,
@@ -133,8 +138,24 @@ def generate_docs_configuration(
133138 pages = pages ,
134139 use_autoapi = use_autoapi ,
135140 source_dir = source_dir ,
141+ previous_versions = previous_versions ,
136142 ** configuration_args ,
137143 )
144+
145+ # adjust arguments if a callable is provided
146+ if adjust_arguments :
147+ args = adjust_arguments (args )
148+
149+ # load the templatized conf.py file
150+ template = templateEnv .get_template ("conf.py.j2" )
151+
152+ # adjust the template if a callable is provided
153+ if adjust_template :
154+ template = adjust_template (template )
155+
156+ # Render
157+ template = template .render (** args )
158+
138159 # dump to file
139160 template_file = Path (td ) / "conf.py"
140161 template_file .write_text (template )
0 commit comments