summaryrefslogtreecommitdiff
path: root/build-programs.py
blob: 513abbe5a81855933df8b523233112e33b3bb3c7 (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
63
64
65
66
67
68
69
#!/usr/bin/env python3

import html
from pathlib import Path
import re
from subprocess import run


def read_programs(plist):
    with open(plist) as l:
        return tuple(Path('programs', p.strip()) for p in l)


PROGRAM_RE = re.compile('\n'.join((
    'NOM : (?P<name>.+)',
    'COMPOSITEURS : (?P<composers>.+)',
    'DESCRIPTION :',
    '(?P<description>.+)',
    'MORCEAUX :',
    '(?P<pieces>.+)'
)), flags=re.DOTALL)

def parse(filename):
    with open(filename) as program:
        return PROGRAM_RE.match(program.read()).groupdict()


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(filename):
    info = parse(filename)

    info['description'] = run(
        ('pandoc',),
        input=info['description'], capture_output=True, text=True, check=True
    ).stdout

    info['pieces'] = '\n'.join(
        piece(p) for p in info['pieces'].splitlines()
    )

    print(BLOCK_TEMPLATE.format_map(info))


def main():
    for p in read_programs('programs/programs.list'):
        print_program(p)


if __name__ == '__main__':
    main()