forked from Kattis/problemtools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem2html.py
executable file
·147 lines (122 loc) · 4.69 KB
/
problem2html.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#! /usr/bin/env python2
# -*- coding: utf-8 -*-
import re
import os.path
import sys
import string
from string import Template
from optparse import OptionParser
from ProblemPlasTeX import ProblemRenderer
from ProblemPlasTeX import ProblemsetMacros
from plasTeX.TeX import TeX
from plasTeX.Logging import getLogger, disableLogging
import logging
import template
def convert(problem, options=None):
if (problem[-1] == '/'):
problem = problem[:-1]
problembase = os.path.splitext(os.path.basename(problem))[0]
destdir = Template(options.destdir).safe_substitute(problem=problembase)
destfile = Template(options.destfile).safe_substitute(problem=problembase)
imgbasedir = Template(options.imgbasedir).safe_substitute(problem=problembase)
if options.quiet:
disableLogging()
else:
getLogger().setLevel(eval("logging." + options.loglevel.upper()))
getLogger('status').setLevel(eval("logging." + options.loglevel.upper()))
texfile = problem
# Set up template if necessary
templ = None
if os.path.isdir(problem):
templ = template.Template(problem, language=options.language, title=options.title)
texfile = templ.get_file_name()
origcwd = os.getcwd()
# Setup parser and renderer etc
tex = TeX(file=texfile)
ProblemsetMacros.init(tex)
tex.ownerDocument.config['general']['copy-theme-extras'] = options.css
if not options.headers:
tex.ownerDocument.userdata['noheaders'] = True
tex.ownerDocument.config['files']['filename'] = destfile
tex.ownerDocument.config['images']['filenames'] = 'img-$num(4)'
tex.ownerDocument.config['images']['enabled'] = False
tex.ownerDocument.config['images']['imager'] = 'none'
tex.ownerDocument.config['images']['base-url'] = imgbasedir
renderer = ProblemRenderer()
if not options.quiet:
print 'Parsing TeX source...'
doc = tex.parse()
# Go to destdir
if destdir:
if not os.path.isdir(destdir):
os.makedirs(destdir)
os.chdir(destdir)
try:
if not options.quiet:
print 'Rendering!'
renderer.render(doc)
# Annoying: I have not figured out any way of stopping the plasTeX
# renderer from generating a .paux file
if os.path.isfile('.paux'):
os.remove('.paux')
if options.tidy:
os.system('tidy -utf8 -i -q -m %s 2> /dev/null' % destfile)
if options.bodyonly:
content = open(destfile).read()
body = re.search('<body>(.*)</body>', content, re.DOTALL)
assert body
open(destfile, 'w').write(body.group(1))
finally:
# restore cwd
os.chdir(origcwd)
if templ:
templ.cleanup()
return True
class ConvertOptions:
available = [
['bodyonly', 'store_true', '-b', '--body-only',
'only generate HTML body, no HTML headers.'],
['css', 'store_false', '-c', '--no-css',
"don't copy CSS file to output directory."],
['headers', 'store_false', '-H', '--headers',
"don't generate problem headers (title, problem id, time limit)"],
['tidy', 'store_false', '-m', '--messy',
"don't run tidy to postprocess the HTML"],
['destdir', 'store', '-d', '--dest-dir',
"output directory."],
['destfile', 'store', '-f', '--dest-file',
"output file name."],
['language', 'store', '-l', '--language',
'choose alternate language (2-letter code).'],
['title', 'store', '-T', '--title',
'set title (only used when there is no pre-existing template and -h not set).'],
['loglevel', 'store', '-L', '--log-level',
'set log level (debug, info, warning, error, critical).'],
['quiet', 'store_true', '-q', '--quiet',
"quiet."],
]
def __init__(self):
self.bodyonly = False
self.css = True
self.headers = True
self.tidy = True
self.destdir = "${problem}_html"
self.destfile = "index.html"
self.language = ""
self.title = "Problem Name"
self.loglevel = "warning"
self.imgbasedir = ''
self.quiet = False
if __name__ == '__main__':
options = ConvertOptions()
optparse = OptionParser(usage="usage: %prog [options] problem")
for (dest, action, short, long, help) in ConvertOptions.available:
if (action == 'store'):
help += ' default: "%s"' % options.__dict__[dest]
optparse.add_option(short, long, dest=dest, help=help, action=action)
(options, args) = optparse.parse_args(values=options)
if len(args) != 1:
optparse.print_help()
sys.exit(1)
texfile = args[0]
convert(texfile, options)