88from os .path import getsize
99from subprocess import run
1010from pathlib import Path
11- from sys import argv
12-
13- # external
14- from yaml import safe_load , safe_dump
1511
1612__all__ = ("generate_documentation" ,)
1713
@@ -56,6 +52,9 @@ def _generate_reference(source: Path, destination: Path, ext: str):
5652
5753def _update_mkdocs_config (source : Path , destination : Path , nav_items : Dict [str , List [str ]]):
5854 """Temporary update to mkdocs config."""
55+ # external
56+ from yaml import safe_load , safe_dump
57+
5958 copy (source , destination )
6059 with open (source , "rt" ) as mkf :
6160 mkdocs_conf = safe_load (mkf )
@@ -70,17 +69,18 @@ def _gen_md_docs(source: Path, refs_path: Path):
7069 # backup mkdocs config
7170 _update_mkdocs_config (source / "mkdocs.yaml" , source / "mkdocs.bak.yaml" , nav_items )
7271 # 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 ())
7474 # restore mkdocs config
7575 move (str (source / "mkdocs.bak.yaml" ), source / "mkdocs.yaml" )
76+ return mkdocs_build .returncode
7677
7778
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 ):
7980 """Generate reStructuredText docs."""
8081 # external
8182 from pypandoc import convert_file # type: ignore
8283
83- # generate index.rst
8484 with open (source / "docs/index.rst" , "wt" ) as idx_f :
8585 idx_f .write (
8686 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):
9393 )
9494 # generate RST reference documentation
9595 _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
102112
103113
104114def 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 ,
106120):
107121 """Generate documentation."""
108- if only_md and only_rst :
122+ if only_md and only_rst_web and only_rst_man :
109123 return
110124 # copy readme as docs index file
111125 copy (source / "README.md" , source / "docs/index.md" )
@@ -114,21 +128,30 @@ def generate_documentation(
114128 if refs_path .exists () and refs_path .is_dir ():
115129 rmtree (refs_path )
116130 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 )
120132 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
122134 # optionally discard reference folder
123135 if discard_refs :
124136 rmtree (source / "docs/reference" )
137+ return rc
125138
126139
127140if __name__ == "__main__" :
128141 project_root = Path (__file__ ).parent .parent
129- generate_documentation (
142+
143+ # # standard
144+ # from sys import argv
145+
146+ rc = generate_documentation (
130147 project_root ,
131148 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
134156 )
157+ quit (rc )
0 commit comments