Skip to content

Commit debc615

Browse files
committed
chg: dev: add initial docs processing bits for doorstop
Signed-off-by: Stephen L Arnold <[email protected]>
1 parent 6f54337 commit debc615

File tree

5 files changed

+177
-3
lines changed

5 files changed

+177
-3
lines changed

docs/process_md_urls.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
Get the path to markdown files and process image URLs based on files in
5+
args. One or more (SVG or PNG) files are required.
6+
"""
7+
import os
8+
import sys
9+
from pathlib import Path
10+
from string import Template
11+
from typing import List, Tuple
12+
13+
DEBUG = int(os.getenv('DEBUG', default='0'))
14+
EXTENSIONS = ['.svg', '.png']
15+
16+
FIG_TPL = """```{figure} ${figure_path}
17+
:width: 90 %
18+
:align: center
19+
:alt: ${caption_lc}
20+
21+
${caption_title} (captured from mermaid to SVG or PNG).
22+
```
23+
"""
24+
25+
CTX = {
26+
'caption_lc': '',
27+
'caption_title': '',
28+
'figure_path': '',
29+
}
30+
31+
32+
def render_caption(caption: str, path: str):
33+
"""
34+
Render a string template.
35+
"""
36+
CTX.update(
37+
{
38+
'caption_lc': caption.lower(),
39+
'caption_title': caption.title(),
40+
'figure_path': path,
41+
}
42+
)
43+
return Template(FIG_TPL).substitute(CTX)
44+
45+
46+
def find_mdfiles(
47+
start: str = '.', fglob: str = '*.md', excludes: Tuple = ('.github', '.tox', '.venv')
48+
) -> List:
49+
"""
50+
Find markdown files subject to specified exclude paths.
51+
52+
:param start: directory to start file search
53+
:param fglob: file extension glob
54+
:param excludes: tuple of excludes
55+
"""
56+
target_files: List = []
57+
files = Path(start).rglob(fglob)
58+
for file in list(files):
59+
if str(file).startswith(excludes):
60+
continue
61+
target_files.append(file)
62+
if DEBUG:
63+
print(f'Found file list: {target_files}')
64+
return target_files
65+
66+
67+
def process_files(new_files: List, target_files: List):
68+
"""
69+
process files if we found enough of each.
70+
"""
71+
for img_file in new_files:
72+
for md_file in target_files:
73+
doc_str = Path(md_file).read_text(encoding='utf-8')
74+
if Path(img_file).name not in doc_str:
75+
continue
76+
with Path(md_file).open(encoding='utf-8') as file:
77+
lines = file.readlines()
78+
with Path(md_file).open(mode='w', encoding='utf-8') as file:
79+
for line in lines:
80+
if line.startswith(('![', '[')) and Path(img_file).name in line:
81+
if DEBUG:
82+
print(line)
83+
cap_str = line.split('[', 1)[1].split(']')[0]
84+
path_str = line.split('(', 1)[1].split(')')[0]
85+
text = render_caption(cap_str, path_str)
86+
file.write(text + '\n')
87+
else:
88+
file.write(line)
89+
90+
91+
def main(argv: list[str] | None = None) -> None:
92+
"""
93+
Runs the program.
94+
95+
Args:
96+
argv: A list of arguments, not including the prog name.
97+
"""
98+
if not argv:
99+
if DEBUG:
100+
print("No image files, nothing to do ...")
101+
sys.exit(0)
102+
103+
if DEBUG:
104+
print(argv)
105+
new_files = [f for f in argv if Path(f).suffix in EXTENSIONS and Path(f).exists()]
106+
if len(new_files) < 1:
107+
if DEBUG:
108+
print(f"No valid input files (only {EXTENSIONS} are allowed)")
109+
sys.exit(1)
110+
if DEBUG:
111+
print(new_files)
112+
113+
target_files = find_mdfiles()
114+
if not target_files:
115+
if DEBUG:
116+
print("No markdown files, nothing to do ...")
117+
sys.exit(0)
118+
119+
if DEBUG:
120+
print(target_files)
121+
122+
process_files(new_files, target_files)
123+
124+
125+
# print(','.join(target_files))
126+
127+
if __name__ == "__main__":
128+
main(sys.argv[1:])

docs/source/ds/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*.svg
2+
/*.md

docs/source/ds/assets/.gitkeep

Whitespace-only changes.

docs/sphinx_prep.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env bash
2+
#
3+
# This removes ToC section from doorstop-generated markdown.
4+
# We need to cleanup sphinx doc list and quiet MyST warnings.
5+
6+
set -euo pipefail
7+
8+
failures=0
9+
trap 'failures=$((failures+1))' ERR
10+
11+
FILE_ARG=${1}
12+
13+
echo "Processing markdown file: ${FILE_ARG}"
14+
sed -i '/^# 1.0/,$!d' $FILE_ARG
15+
16+
if ((failures == 0)); then
17+
echo "Success"
18+
else
19+
echo "Something went wrong"
20+
exit 1
21+
fi

tox.ini

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,17 @@ allowlist_externals =
216216

217217
deps =
218218
{[base]deps}
219+
{[reqs]deps}
219220
.[doc]
220221

222+
#commands_pre =
223+
#docs: doorstop publish REQ docs/source/ds/reqs_tree.md
224+
#docs: doorstop publish TST docs/source/ds/unit_tests.md
225+
#docs: doorstop publish SDD docs/source/ds/sw_design.md
226+
#docs: bash docs/sphinx_prep.sh docs/source/ds/reqs_tree.md
227+
#docs: bash docs/sphinx_prep.sh docs/source/ds/unit_tests.md
228+
#docs: bash docs/sphinx_prep.sh docs/source/ds/sw_design.md
229+
221230
commands =
222231
docs: make -C docs html
223232
ldocs: make -C docs linkcheck
@@ -229,16 +238,14 @@ skip_install = true
229238
description =
230239
Run mypy type checker (needs all deps)
231240

232-
setenv = PYTHONPATH = {toxinidir}/src
233-
234241
deps =
235242
{[base]deps}
236243
mypy
237244
-r requirements.txt
238245
munch-stubs @ git+https://github.com/VCTLabs/munch-stubs.git@main
239246

240247
commands =
241-
python -m mypy --follow-imports=normal --install-types --non-interactive src/
248+
python -m mypy --follow-imports=normal --install-types --check-untyped-defs --non-interactive src/
242249

243250
[testenv:reuse]
244251
skip_install = true
@@ -281,6 +288,22 @@ deps =
281288
commands =
282289
bandit -c pyproject.toml -r src
283290

291+
[testenv:md]
292+
skip_install = true
293+
passenv =
294+
DEBUG
295+
PYTHON
296+
CI
297+
OS
298+
PYTHONIOENCODING
299+
PIP_DOWNLOAD_CACHE
300+
301+
deps =
302+
{[base]deps}
303+
304+
commands =
305+
python docs/process_md_urls.py {posargs}
306+
284307
[testenv:changes]
285308
skip_install = true
286309

0 commit comments

Comments
 (0)