8
8
from os .path import getsize
9
9
from subprocess import run
10
10
from pathlib import Path
11
- from sys import argv
12
11
13
12
# external
14
13
from yaml import safe_load , safe_dump
15
14
15
+ __all__ = ("generate_documentation" ,)
16
+
16
17
17
18
def _write_ref_content (source : Path , module_name : str , func_name : str ):
18
19
"""Write content."""
@@ -23,7 +24,7 @@ def _write_ref_content(source: Path, module_name: str, func_name: str):
23
24
)
24
25
25
26
26
- def generate_reference (source : Path , destination : Path ):
27
+ def _generate_reference (source : Path , destination : Path ):
27
28
"""Generate reference."""
28
29
nav_items : Dict [str , List [str ]] = {"Code Reference" : []}
29
30
# clean destination
@@ -43,7 +44,7 @@ def generate_reference(source: Path, destination: Path):
43
44
return nav_items
44
45
45
46
46
- def update_mkdocs_config (source : Path , destination : Path , nav_items : Dict [str , List [str ]]):
47
+ def _update_mkdocs_config (source : Path , destination : Path , nav_items : Dict [str , List [str ]]):
47
48
"""Temporary update to mkdocs config."""
48
49
copy (source , destination )
49
50
with open (source , "rt" ) as mkf :
@@ -53,25 +54,27 @@ def update_mkdocs_config(source: Path, destination: Path, nav_items: Dict[str, L
53
54
safe_dump (mkdocs_conf , mkf , sort_keys = False )
54
55
55
56
56
- def generate_documentation (source : Path ):
57
+ def generate_documentation (source : Path , discard_refs : bool = True ):
57
58
"""Generate documentation."""
58
59
# copy readme as docs index file
59
60
copy (source / "README.md" , source / "docs/index.md" )
60
61
# generate reference documentation
61
- nav_items = generate_reference (source / "validators/__init__.py" , source / "docs/reference" )
62
+ nav_items = _generate_reference (source / "validators/__init__.py" , source / "docs/reference" )
62
63
# backup mkdocs config
63
- update_mkdocs_config (source / "mkdocs.yml" , source / "mkdocs.bak.yml" , nav_items )
64
+ _update_mkdocs_config (source / "mkdocs.yml" , source / "mkdocs.bak.yml" , nav_items )
64
65
# build docs as subprocess
65
66
print (run (("mkdocs" , "build" ), capture_output = True ).stderr .decode ())
66
67
# restore mkdocs config
67
68
move (str (source / "mkdocs.bak.yml" ), source / "mkdocs.yml" )
69
+ # optionally discard reference folder
70
+ if discard_refs :
71
+ rmtree (source / "docs/reference" )
68
72
69
73
70
74
if __name__ == "__main__" :
71
- project_dir = Path (__file__ ).parent .parent
72
- generate_documentation (project_dir )
73
- # use this option before building package
74
- # with `poetry build` to include refs
75
- if len (argv ) > 1 and argv [1 ] == "--keep" :
76
- quit ()
77
- rmtree (project_dir / "docs/reference" )
75
+ project_root = Path (__file__ ).parent .parent
76
+ generate_documentation (project_root )
77
+ # NOTE: use following lines only for testing/debugging
78
+ # generate_documentation(project_root, discard_refs=False)
79
+ # from sys import argv
80
+ # generate_documentation(project_root, len(argv) > 1 and argv[1] == "--keep")
0 commit comments