forked from yosupo06/library-checker-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlgen.py
executable file
·308 lines (264 loc) · 9.34 KB
/
htmlgen.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python3
import argparse
from sys import exit
import hashlib
import math
import re
import shutil
import subprocess
import tempfile
import zipfile
from datetime import datetime
from logging import Logger, basicConfig, getLogger
from os import environ, getenv, path
from pathlib import Path
from struct import pack
from subprocess import (DEVNULL, PIPE, STDOUT, CalledProcessError,
TimeoutExpired, call, check_call, check_output, run)
from tempfile import TemporaryDirectory
import markdown.extensions
from jinja2 import Template, Environment, DictLoader
import toml
from markdown import Extension, markdown
from markdown.preprocessors import Preprocessor
from generate import Problem
from typing import Set
logger = getLogger(__name__) # type: Logger
# html tag for change lang
def lang_div_start(lang):
return '<div markdown="1" class="lang-{}">'.format(lang)
def lang_div_end(lang):
return '</div>'
# @{keyword.statement}, @{keyword.constraints}, ...
def to_keyword_str(lang_dict):
s = ''
for k, v in lang_dict.items():
s += lang_div_start(k) + v + lang_div_end(k)
return s
keywords = {
'statement': to_keyword_str({
'en': 'Problem Statement',
'ja': '問題文',
}),
'constraints': to_keyword_str({
'en': 'Constraints',
'ja': '制約',
}),
'input': to_keyword_str({
'en': 'Input',
'ja': '入力',
}),
'output': to_keyword_str({
'en': 'Output',
'ja': '出力',
}),
'sample': to_keyword_str({
'en': 'Sample',
'ja': 'サンプル',
}),
}
def param_to_str(value):
if isinstance(value, int):
if str(value).endswith('000000'):
k = math.floor(math.log10(abs(value)))
if value == 10 ** k:
return r'10^{{{}}}'.format(k)
elif value % (10 ** k) == 0:
return r'{} \\times 10^{{{}}}'.format(value // 10 ** k, k)
else:
return r'{} \\times 10^{{{}}}'.format(value / 10 ** k, k)
else:
return format(value, ',').replace(',', '{,}')
else:
return str(value)
def gen_params(toml_params):
params = dict() # type: Dict[str, str]
for key, value in toml_params.items():
params[key] = param_to_str(value)
return params
# @{example.example_00}
class ExampleReader:
sample_template = '''
<div class="uk-grid-small uk-child-width-1-2@s" uk-grid>
<div><pre>{}</pre></div>
<div><pre>{}</pre></div>
</div>
'''
def __init__(self, problem_dir: Path):
self.counter = 1
self.problem_dir = problem_dir
self.used = set() # type: Set[str]
def __getitem__(self, key: str):
logger.debug('read example: {}'.format(key))
self.used.add(key)
inpath = self.problem_dir / 'in' / (key + '.in') # type: Path
outpath = self.problem_dir / 'out' / (key + '.out') # type: Path
if not inpath.exists() or not outpath.exists():
logger.error('There is no example file {}'.format(key))
exit(1)
infile = open(str(inpath), 'r').read()
outfile = open(str(outpath), 'r').read()
s = r'<h3># {}</h3>'.format(self.counter)
self.counter += 1
s += self.sample_template.format(infile, outfile)
return s
def check_all_used(self) -> bool:
for file_path in Path(self.problem_dir).glob('in/example_*.in'):
name = file_path.stem
if name not in self.used:
logger.warn('Not use {} for task.md'.format(name))
return False
return True
class LangManager:
def __init__(self):
self.now_lang = ''
def __getitem__(self, lang: str):
if lang == 'end':
return self.reset_lang()
s = ''
if self.now_lang != '':
s += self.reset_lang()
self.now_lang = lang
return s + lang_div_start(lang)
def reset_lang(self):
s = lang_div_end(self.now_lang)
self.now_lang = ''
return s
class ForumExpander(Preprocessor):
forum_template = '''
<a class="uk-button uk-button-default" href={url}>Forum <span uk-icon="icon: link"></span></a>
'''
def __init__(self, url):
self.url = url[0]
def run(self, lines):
new_lines = []
for line in lines:
new_lines.append(line)
if self.url:
new_lines.append('')
new_lines.append('---')
new_lines.append('')
new_lines.append(self.forum_template.format(url=self.url))
new_lines.append('')
return new_lines
class ForumExtension(Extension):
def __init__(self, **kwargs):
self.config = {
'url': ['dummy issue URL', 'URL'],
}
super(ForumExtension, self).__init__(**kwargs)
def extendMarkdown(self, md):
md.preprocessors.register(ForumExpander(
self.config['url']), 'forum', 101)
html_header = '''
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<!-- Uikit -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://judge.yosupo.jp/public/css/uikit.min.css" />
<script src="https://judge.yosupo.jp/public/js/uikit.min.js"></script>
<script src="https://judge.yosupo.jp/public/js/uikit-icons.min.js"></script>
<!-- Katex -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css"
integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.js"
integrity="sha384-9Nhn55MVVN0/4OFx7EE5kpFBPsEMZxKTCnA+4fqDmg12eCTqGi6+BB2LjY8brQxJ"
crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/contrib/auto-render.min.js"
integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous"
onload="renderMathInElement(document.body);"></script>
<script>
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(
document.body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false }],
ignoredTags: [],
})
});
</script>
<script>
function getLang(lang) {
lang = localStorage.getItem("lang");
if (lang == "ja" || lang == "en") return lang;
for (e of navigator.languages) {
if (e.startsWith("en")) return "en";
if (e.startsWith("ja")) return "ja";
}
return "en";
}
function refLang() {
lang = getLang();
document.getElementById("nav-lang").text = `Lang(${lang})`;
document.querySelectorAll(".lang-en, .lang-ja").forEach(function(e) {
if (e.classList.contains(`lang-${lang}`)) {
e.style.display = 'block';
} else {
e.style.display = 'none';
}
})
}
function setLang(lang) {
localStorage.setItem("lang", lang)
refLang()
}
document.addEventListener("DOMContentLoaded", refLang);
</script>
</head>
'''
html_body = '''
<body>
<div class="uk-navbar-container" uk-navbar>
<div class="uk-navbar-right">
<ul class="uk-navbar-nav">
<li>
<a href="#" id="nav-lang">Lang()</a>
<div class="uk-navbar-dropdown">
<ul class="uk-nav uk-navbar-dropdown-nav">
<li><a href="#" onclick="setLang('ja')">Ja</a></li>
<li><a href="#" onclick="setLang('en')">En</a></li>
</ul>
</div>
</li>
</ul>
</div>
</div>
<section class="uk-section">
<div class="uk-container">
{}
</div>
</section>
</body>
</html>
'''
class ToHTMLConverter:
def __init__(self, probdir: Path, config):
logger.info("HTML Generate {}".format(probdir.name))
md_statement = open(str(probdir / 'task.md'), encoding='utf-8').read()
# evaluate jinja2
lang_manager = LangManager()
environment = Environment(
variable_start_string="@{", variable_end_string="}", loader=DictLoader({'task': md_statement}))
environment.globals['endlang'] = lang_manager.reset_lang
template = environment.get_template('task')
self.examples = ExampleReader(problem_dir=probdir)
mid_statement = template.render(
keyword=keywords,
param=gen_params(config.get('params', dict())),
lang=lang_manager,
example=self.examples,
)
# evaluate markdown
self.statement = markdown(
mid_statement, extensions=[
'md_in_html',
'markdown.extensions.fenced_code',
ForumExtension(url=config.get('forum', ''))
],
)
self.html = html_header + html_body.format(self.statement)
def check_all_samples_used(self) -> bool:
return self.examples.check_all_used()