-
-
Notifications
You must be signed in to change notification settings - Fork 112
/
htmlgen.py
executable file
·173 lines (146 loc) · 5.49 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
#!/usr/bin/env python3
import argparse
import hashlib
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
import toml
from markdown import Extension, markdown
from markdown.preprocessors import Preprocessor
logger = getLogger(__name__) # type: Logger
class ExampreExpander(Preprocessor):
base_path = ''
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, base_path):
self.base_path = Path(base_path[0])
def run(self, lines):
used_examples = []
new_lines = []
counter = 1
for line in lines:
start = '{{example '
end = '}}'
if line.startswith(start) and line.endswith(end):
name = line[len(start):-len(end)]
inpath = str(self.base_path / 'in' / (name + '.in'))
used_examples.append(inpath)
infile = open(inpath).read()
outfile = open(str(self.base_path / 'out' /
(name + '.out')), 'r').read()
new_lines.append(r'### # {}'.format(counter))
new_lines.extend(self.sample_template.format(
infile, outfile).splitlines())
counter += 1
else:
new_lines.append(line)
for name in Path(self.base_path).glob('in/example_*.in'):
if str(name) not in used_examples:
logger.error('Not use {} for task'.format(name))
exit(1)
return new_lines
class ExampleExtension(Extension):
def __init__(self, **kwargs):
self.config = {
'base_path': ['.', 'Base path']
}
super(ExampleExtension, self).__init__(**kwargs)
def extendMarkdown(self, md):
md.preprocessors.register(ExampreExpander(
self.config['base_path']), 'example', 100)
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)
class ToHTMLConverter:
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>// <![CDATA[
document.addEventListener("DOMContentLoaded", function () {
renderMathInElement(
document.body, {
delimiters: [
{ left: "$$", right: "$$", display: true },
{ left: "$", right: "$", display: false }],
ignoredTags: [],
})
});
// ]]></script>
</head>
'''
body_template = '''
<body>
<section class="uk-section">
<div class="uk-container">
{}
</div>
</section>
</body>
</html>
'''
def __init__(self, probdir: Path, config):
with open(str(probdir / 'task.md'), encoding='utf-8') as f:
self.statement = markdown(
f.read(), extensions=[
'markdown.extensions.fenced_code',
'markdown.extensions.tables',
ExampleExtension(base_path=str(probdir)),
ForumExtension(url=config.get('forum', ''))
],
) # type: str
self.html = self.header + \
self.body_template.format(self.statement) # type: str