forked from pact-2022/pact-2022.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
99 lines (69 loc) · 2.46 KB
/
generate.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
#! /usr/bin/env python3
from pathlib import Path
# {{{ remove common indentation
def remove_common_indentation(code: str, require_leading_newline: bool = True):
r"""Remove leading indentation from one or more lines of code.
Removes an amount of indentation equal to the indentation level of the first
nonempty line in *code*.
:param code: Input string.
:param require_leading_newline: If *True*, only remove indentation if *code*
starts with ``\n``.
:returns: A copy of *code* stripped of leading common indentation.
"""
if "\n" not in code:
return code
if require_leading_newline and not code.startswith("\n"):
return code
lines = code.split("\n")
while lines[0].strip() == "":
lines.pop(0)
while lines[-1].strip() == "":
lines.pop(-1)
if lines:
base_indent = 0
while lines[0][base_indent] in " \t":
base_indent += 1
for line in lines[1:]:
if line[:base_indent].strip():
raise ValueError("inconsistent indentation")
return "\n".join(line[base_indent:] for line in lines)
# }}}
def filter_markdown(s):
import markdown
return markdown.markdown(remove_common_indentation(s),
extensions=["extra"])
OUTPUT_DIR = Path("output")
DATA = {
"base_dir": ".",
"conf_name": "PACT 2022",
"conf_dates": "October 10–12, 2022",
"conf_root_url": "https://pact22.cs.illinois.edu",
"submission_deadline": "April 25, 2022",
"markdown": filter_markdown,
}
def main():
import argparse
parser = argparse.ArgumentParser(
description="Run PACT-specific Jinja expansion on INFILE, "
"sending output to stdout.")
parser.add_argument("infile", metavar="INFILE",
help="an integer for the accumulator")
args = parser.parse_args()
from jinja2 import Environment, FileSystemLoader
jinja_env = Environment(
loader=FileSystemLoader([".", "template"]),
autoescape=False
)
jinja_env.filters["markdown"] = filter_markdown
fname = Path(args.infile)
with open(fname, "r") as infile:
tpl = jinja_env.from_string(infile.read())
basepath = Path(*fname.parts[1:])
data = DATA | {
"current_file": str(basepath)
}
from html import unescape
print(unescape(tpl.render(data)))
if __name__ == "__main__":
main()
# vim: foldmethod=marker