summaryrefslogtreecommitdiff
path: root/build-programs.py
blob: 69c70574c6d01c7a10f1f28fa9f166bbce7fea24 (plain)
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
#!/usr/bin/env python3

import html
import re
from subprocess import run


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="images/chevron-down.svg">
    <img class="button close" src="images/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():
    for p in read_programs('programs.in'):
        print_program(p.groupdict())


if __name__ == '__main__':
    main()