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
-
13
- # external
14
- from yaml import safe_load , safe_dump
15
11
16
12
__all__ = ("generate_documentation" ,)
17
13
@@ -56,6 +52,9 @@ def _generate_reference(source: Path, destination: Path, ext: str):
56
52
57
53
def _update_mkdocs_config (source : Path , destination : Path , nav_items : Dict [str , List [str ]]):
58
54
"""Temporary update to mkdocs config."""
55
+ # external
56
+ from yaml import safe_load , safe_dump
57
+
59
58
copy (source , destination )
60
59
with open (source , "rt" ) as mkf :
61
60
mkdocs_conf = safe_load (mkf )
@@ -70,17 +69,18 @@ def _gen_md_docs(source: Path, refs_path: Path):
70
69
# backup mkdocs config
71
70
_update_mkdocs_config (source / "mkdocs.yaml" , source / "mkdocs.bak.yaml" , nav_items )
72
71
# build mkdocs as subprocess
73
- print (run (("mkdocs" , "build" ), capture_output = True ).stderr .decode ())
72
+ mkdocs_build = run (("mkdocs" , "build" ), capture_output = True )
73
+ print (mkdocs_build .stderr .decode () + mkdocs_build .stdout .decode ())
74
74
# restore mkdocs config
75
75
move (str (source / "mkdocs.bak.yaml" ), source / "mkdocs.yaml" )
76
+ return mkdocs_build .returncode
76
77
77
78
78
- def _gen_rst_docs (source : Path , refs_path : Path ):
79
+ def _gen_rst_docs (source : Path , refs_path : Path , only_web : bool = False , only_man : bool = False ):
79
80
"""Generate reStructuredText docs."""
80
81
# external
81
82
from pypandoc import convert_file # type: ignore
82
83
83
- # generate index.rst
84
84
with open (source / "docs/index.rst" , "wt" ) as idx_f :
85
85
idx_f .write (
86
86
convert_file (source_file = source / "docs/index.md" , format = "md" , to = "rst" )
@@ -93,19 +93,33 @@ def _gen_rst_docs(source: Path, refs_path: Path):
93
93
)
94
94
# generate RST reference documentation
95
95
_generate_reference (source / "validators/__init__.py" , refs_path , "rst" )
96
- # build sphinx web pages as subprocess
97
- web_build = run (("sphinx-build" , "docs" , "docs/_build/web" ), capture_output = True )
98
- print (web_build .stderr .decode (), "\n " , web_build .stdout .decode (), sep = "" )
99
- # build sphinx man pages as subprocess
100
- man_build = run (("sphinx-build" , "-b" , "man" , "docs" , "docs/_build/man" ), capture_output = True )
101
- print (man_build .stderr .decode (), "\n " , man_build .stdout .decode (), sep = "" )
96
+ rc = 0
97
+ if not only_man :
98
+ # build sphinx web pages as subprocess
99
+ web_build = run (("sphinx-build" , "docs" , "docs/_build/web" ), capture_output = True )
100
+ print (web_build .stderr .decode () + web_build .stdout .decode ())
101
+ rc = web_build .returncode
102
+ if not only_web :
103
+ # build sphinx man pages as subprocess
104
+ man_build = run (
105
+ ("sphinx-build" , "-b" , "man" , "docs" , "docs/_build/man" ), capture_output = True
106
+ )
107
+ print (man_build .stderr .decode () + man_build .stdout .decode ())
108
+ copy (source / "docs/_build/man/validators.1" , source / "docs/validators.1" )
109
+ print (f"Man page copied to: { source / 'docs/validators.1' } " )
110
+ rc = man_build .returncode if rc == 0 else rc
111
+ return rc
102
112
103
113
104
114
def generate_documentation (
105
- source : Path , only_md : bool = False , only_rst : bool = False , discard_refs : bool = True
115
+ source : Path ,
116
+ only_md : bool = False ,
117
+ only_rst_web : bool = False ,
118
+ only_rst_man : bool = False ,
119
+ discard_refs : bool = True ,
106
120
):
107
121
"""Generate documentation."""
108
- if only_md and only_rst :
122
+ if only_md and only_rst_web and only_rst_man :
109
123
return
110
124
# copy readme as docs index file
111
125
copy (source / "README.md" , source / "docs/index.md" )
@@ -114,21 +128,30 @@ def generate_documentation(
114
128
if refs_path .exists () and refs_path .is_dir ():
115
129
rmtree (refs_path )
116
130
refs_path .mkdir (exist_ok = True )
117
- # documentation for each kind
118
- if not only_rst :
119
- _gen_md_docs (source , refs_path )
131
+ rc = 0 if (only_rst_web or only_rst_man ) else _gen_md_docs (source , refs_path )
120
132
if not only_md :
121
- _gen_rst_docs (source , refs_path )
133
+ rc = _gen_rst_docs (source , refs_path , only_rst_web , only_rst_man ) if rc == 0 else rc
122
134
# optionally discard reference folder
123
135
if discard_refs :
124
136
rmtree (source / "docs/reference" )
137
+ return rc
125
138
126
139
127
140
if __name__ == "__main__" :
128
141
project_root = Path (__file__ ).parent .parent
129
- generate_documentation (
142
+
143
+ # # standard
144
+ # from sys import argv
145
+
146
+ rc = generate_documentation (
130
147
project_root ,
131
148
only_md = True ,
132
- only_rst = False ,
133
- discard_refs = len (argv ) <= 1 or argv [1 ] != "--keep" ,
149
+ only_rst_web = False ,
150
+ only_rst_man = False ,
151
+ # # NOTE: use
152
+ # discard_refs=len(argv) <= 1 or argv[1] != "--keep",
153
+ # # instead of
154
+ discard_refs = True ,
155
+ # # for debugging
134
156
)
157
+ quit (rc )
0 commit comments