forked from pcah/cosmic-python-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender-diagrams.py
executable file
·68 lines (55 loc) · 2.11 KB
/
render-diagrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import tempfile
import subprocess
from pathlib import Path
from lxml import html
IMAGES_DIR = Path(__file__).absolute().parent / 'images'
def main():
for fn in sorted(Path(__file__).absolute().parent.glob('*.html')):
chapter_name = fn.name.replace('.html', '')
if chapter_name == 'book':
continue
print('Rendering images for', chapter_name)
render_images(chapter_name)
def render_images(chapter_name):
raw_contents = Path(f'{chapter_name}.html').read_text()
parsed_html = html.fromstring(raw_contents)
for image_block in parsed_html.cssselect('.imageblock'):
[img] = image_block.cssselect('img')
image_id = img.get('src').replace('images/', '').replace('.png', '')
print(image_id)
parent = image_block.getparent()
next_sibling_pos = parent.index(image_block) + 1
try:
next_element = parent[next_sibling_pos]
except IndexError:
continue
if 'image-source' in next_element.classes:
code = next_element.cssselect('pre')[0].text
render_image(code, image_id)
def _add_dots(source, image_id):
lines = source.splitlines()
assert lines[0].startswith('[')
assert image_id in lines[0]
plantuml_cfg = str(Path('plantuml.cfg').absolute())
c4_include = 'images/C4_Context.puml'
lines[0] = lines[0].replace('config=plantuml.cfg', f'config={plantuml_cfg}')
for ix, l in enumerate(lines):
if c4_include in l:
lines[ix] = l.replace(c4_include, str(Path(c4_include).absolute()))
lines.insert(1, '....')
lines.append('....')
return '\n'.join(lines)
def render_image(source, image_id):
source = _add_dots(source, image_id)
print(source)
target = Path(f'images/{image_id}.png')
if target.exists():
target.unlink()
tf = Path(tempfile.NamedTemporaryFile().name)
tf.write_text(source)
cmd = ['asciidoctor', '-r', 'asciidoctor-diagram', '-a', f'imagesoutdir={IMAGES_DIR}', str(tf)]
print(' '.join(cmd))
subprocess.run(cmd, check=True)
if __name__ == '__main__':
main()