Skip to content

Commit 1befe8f

Browse files
authored
Merge pull request #46 from python-project-templates/tkp/ext
Add some hooks for extending, fixes #16
2 parents f77348d + 756ff04 commit 1befe8f

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,3 @@ docs/index.md
141141

142142
# Rust
143143
target
144-

yardang/build.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from contextlib import contextmanager
33
from pathlib import Path
44
from tempfile import TemporaryDirectory
5-
from typing import List, Optional
5+
from typing import Callable, List, Optional
66

77
from 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)

yardang/cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ def build(
2929
use_autoapi: Optional[bool] = None,
3030
custom_css: Optional[Path] = None,
3131
custom_js: Optional[Path] = None,
32+
config_base: Optional[str] = "tool.yardang",
33+
previous_versions: Optional[bool] = False,
3234
):
3335
with generate_docs_configuration(
3436
project=project,
@@ -46,6 +48,8 @@ def build(
4648
use_autoapi=use_autoapi,
4749
custom_css=custom_css,
4850
custom_js=custom_js,
51+
config_base=config_base,
52+
previous_versions=previous_versions,
4953
) as file:
5054
build_cmd = [
5155
executable,

yardang/conf.py.j2

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ def run_convert_github_admonitions_to_rst(app, filename, lines):
189189

190190

191191
def setup(app):
192-
{# app.connect("builder-inited", run_create_previous_version_markdown) #}
192+
if {{previous_versions}}:
193+
app.connect("builder-inited", run_create_previous_version_markdown)
193194
app.connect("builder-inited", run_copyreadme)
194195
app.connect("builder-inited", run_copycname)
195196
app.connect("source-read", run_convert_github_admonitions_to_rst)

0 commit comments

Comments
 (0)