This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
forked from pre-commit/pre-commit.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_templates.py
81 lines (66 loc) · 2.24 KB
/
make_templates.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
from __future__ import annotations
import collections
import json
import os.path
from typing import Any
import mako.lookup
import markupsafe
from template_lib import md
SECTIONS = (
('Introduction', 'sections/intro.md'),
('Installation', 'sections/install.md'),
('Adding pre-commit plugins to your project', 'sections/plugins.md'),
('Usage', 'sections/usage.md'),
('Creating new hooks', 'sections/new-hooks.md'),
('Command line interface', 'sections/cli.md'),
('Advanced features', 'sections/advanced.md'),
('Contributing', 'sections/contributing.md'),
)
template_lookup = mako.lookup.TemplateLookup(
directories=['.'],
default_filters=['html_escape'],
imports=['from mako.filters import html_escape'],
)
ALL_TEMPLATES = [
filename for filename in os.listdir('.')
if filename.endswith('.mako') and filename != 'base.mako'
]
def get_env() -> dict[str, Any]:
body_parts = []
for title, filename in SECTIONS:
div_id, _ = os.path.splitext(os.path.basename(filename))
title_rendered = md(f'# {title}')
with open(filename) as f:
rendered = md(f.read())
body_parts.append(
markupsafe.Markup(
f'<div id="{div_id}">\n'
f' <div class="page-header">{title_rendered}</div>\n'
f' {rendered}\n'
f'</div>\n',
),
)
body = markupsafe.Markup().join(body_parts)
all_hooks = json.loads(
open('all-hooks.json').read(),
object_pairs_hook=collections.OrderedDict,
)
all_types = {
hook_type
for properties in all_hooks.values()
for hook_type in (
properties[0].get('types', []) + properties[0].get('types_or', [])
)
}
return {'all_hooks': all_hooks, 'all_types': all_types, 'body': body}
def main() -> int:
env = get_env()
for template in ALL_TEMPLATES:
template_name, _ = os.path.splitext(template)
env['template_name'] = template_name
with open(f'{template_name}.html', 'w') as html_file:
template_obj = template_lookup.get_template(template)
html_file.write(template_obj.render(**env))
return 0
if __name__ == '__main__':
raise SystemExit(main())