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
69
70
71
72
|
#!/usr/bin/env python3
import html
from os import path
from pathlib import Path
import re
from subprocess import run
from sys import argv
PROGRAM_RE = re.compile('\n'.join((
r'NOM : (?P<name>[^\n]+)',
r'COMPOSITEURS : (?P<composers>[^\n]+)',
'DESCRIPTION :',
'(?P<description>.+?)',
'MORCEAUX :',
r'(?P<pieces>(?:- [^\n]+\n)*- [^\n]+)'
)), flags=re.DOTALL)
def read_programs(programs):
with open(programs) as f:
return tuple(re.finditer(PROGRAM_RE, f.read()))
BLOCK_TEMPLATE = '''\
<details class="program">
<summary>
<div class="name">{name}</div>
<div class="composers">{composers}</div>
<img class="button open" src="{imgdir}/chevron-down.svg">
<img class="button close" src="{imgdir}/chevron-up.svg">
</summary>
{description}
<ol class="pieces">
{pieces}
</ol>
</details>
'''
def piece(p):
if p == 'entracte':
return '<li class="intermission">entracte</li>'
return f'<li>{html.escape(p)}</li>'
def print_program(info):
info['description'] = run(
('pandoc',),
input=info['description'], capture_output=True, text=True, check=True
).stdout
info['pieces'] = '\n'.join(
piece(p[2:]) # Assume p.startswith('- ').
for p in info['pieces'].splitlines()
)
print(BLOCK_TEMPLATE.format_map(info))
def main(programs_src):
# pathlib.Path(x).relative_to(y) cannot handle y not being under x,
# os.path.relpath('x') yields '' rather than '.'.
# 😮💨
imgdir = path.relpath('images', Path(programs_src).parent)
for p in read_programs(programs_src):
info = p.groupdict()
info['imgdir'] = imgdir
print_program(info)
if __name__ == '__main__':
main(argv[1])
|