-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_html_proposals
executable file
·57 lines (49 loc) · 2.12 KB
/
generate_html_proposals
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
#!/usr/bin/env python3
#
# Generate the html for a burgerforslag.
#
# Simple usage: The sole argument is the path to a burgerforslag-conforming json
# file. Prints the html to standard output.
#
# Advanced usage: The first argument is '-b', the next N arguments are input
# json paths, and the final N arguments are output html paths. Use this for
# batch processing.
import sys
import os.path
import json
import html
import re
import subprocess
def make_paragraphs(s):
return '\n'.join('<p>{}</p>'.format(line) for line in s.split('\n'))
basedir = os.path.dirname(__file__)
head = open(os.path.join(basedir, 'templates', 'head.html')).read().format(relpath='../../')
header = open(os.path.join(basedir, 'templates', 'header.html')).read().format(relpath='../')
newest_commit = subprocess.run(['git', '-C', basedir, 'log', '--pretty=format:%h', 'HEAD^..HEAD'],
stdout=subprocess.PIPE).stdout.decode().strip()
footer = open(os.path.join(basedir, 'templates', 'footer.html')).read().format(
newest_commit=newest_commit)
with open(os.path.join(basedir, 'templates', 'side.html')) as f:
page_template = f.read()
fname = sys.argv[1]
if fname != '-b':
with open(fname) as f:
data = json.load(f)
out = page_template.format(title=html.escape(data['title']), id=data['id'],
teaser=make_paragraphs(html.escape(data['teaser'])),
body=make_paragraphs(html.escape(data['body'])),
head=head, header=header, footer=footer)
print(out)
else:
args = sys.argv[2:]
inputs = args[:len(args) // 2]
outputs = args[len(args) // 2:]
for i, o in zip(inputs, outputs):
with open(i) as f:
data = json.load(f)
out = page_template.format(title=html.escape(data['title']), id=data['id'],
teaser=make_paragraphs(html.escape(data['teaser'])),
body=make_paragraphs(html.escape(data['body'])),
head=head, header=header, footer=footer)
with open(o, 'w') as f:
f.write(out)