forked from cosmicpython/book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render-diagrams.py
executable file
·56 lines (46 loc) · 1.68 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
#!/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 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]
lines.insert(1, '....')
lines.append('....')
return '\n'.join(lines)
def render_image(source, image_id):
source = _add_dots(source, image_id)
print(source)
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)
if __name__ == '__main__':
main()