forked from mwiebe/scipy_proceedings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_template.py
executable file
·65 lines (48 loc) · 1.9 KB
/
build_template.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
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
import os
import sys
import shlex, subprocess
import tempita
from conf import bib_dir, build_dir, template_dir, html_dir
from options import get_config
def _from_template(tmpl_basename, config):
tmpl = os.path.join(template_dir, tmpl_basename + '.tmpl')
template = tempita.HTMLTemplate(open(tmpl, 'r').read())
return template.substitute(config)
def from_template(tmpl_basename, config, dest_fn):
outfile = _from_template(tmpl_basename, config)
extension = os.path.splitext(dest_fn)[1][1:]
outname = os.path.join(build_dir, extension, dest_fn)
with open(outname, mode='w') as f:
f.write(outfile)
def bib_from_tmpl(bib_type, config, target):
tmpl_basename = bib_type + '.bib'
dest_path = os.path.join(bib_dir, target + '.bib')
from_template(tmpl_basename, config, dest_path)
command_line = 'recode -d u8..ltex ' + dest_path
run = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE)
out, err = run.communicate()
def get_html_header(config):
return _from_template('header.html', config)
def get_html_content(tmpl, config):
return _from_template(tmpl, config)
def html_from_tmpl(src, config, target):
header = get_html_header(config)
content = _from_template(src, config)
outfile = header+content
dest_fn = os.path.join(html_dir, target + '.html')
extension = os.path.splitext(dest_fn)[1][1:]
outname = os.path.join(build_dir, extension, dest_fn)
with open(outname, mode='w') as f:
f.write(outfile)
if __name__ == "__main__":
if not len(sys.argv) == 2:
print "Usage: build_template.py destination_name"
sys.exit(-1)
dest_fn = sys.argv[1]
template_fn = os.path.join(template_dir, dest_fn+'.tmpl')
if not os.path.exists(template_fn):
print "Cannot find template."
sys.exit(-1)
config = get_config()
from_template(dest_fn, config, dest_fn)