-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·77 lines (64 loc) · 2.32 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
#!/usr/bin/env python3
# The Rules of Acquisition @ ferengi.bible
# HTML generator
# Copyright (C) 2024 Joey Parrish
# Licensed under CC0 1.0 (see LICENSE)
import jinja2
import subprocess
# Used to generate a sparse array, since not every rule is known.
LAST_RULE = 285
# Metadata that gets repeated in various places in the HTML.
TITLE = "The Rules of Acquisition"
DESCRIPTION = "The Ferengi Rules of Acquisition, as handed down by Grand Nagus Gint; every Ferengi business transaction is governed by these rules to ensure a fair and honest deal for all parties concerned."
URL = "https://ferengi.bible/"
THEME_COLOR = "#a5836a"
# Placeholder text for rules that don't exist.
PLACEHOLDERS = [
"No such rule in Star Trek canon, but you can't see this because we blurred it. The upsell is a joke, hu-man! You can't subscribe.",
"This rule has never been given in Trek canon, so the upsell offer is just a gag.",
"Although this rule has never existed, this text may show up in search engines.",
"Are you so determined to see what this is? It's fake.",
"How do you translate \"lorem ipsum\" into Ferengi?",
]
def main():
# Create a jinja2 environment.
env = jinja2.Environment(
loader=jinja2.FileSystemLoader('.'),
autoescape=jinja2.select_autoescape())
# Load the rules.
with open('rules.txt', 'r') as f:
rule_lines = f.read().strip().split('\n')
rules = [None] * (LAST_RULE + 1)
for line in rule_lines:
number, rule = line.split('\t')
rules[int(number)] = rule
# Load the HTML template.
template = env.get_template('template.html')
# Render to index.html
with open('index.html', 'w') as f:
f.write(template.render(
rules=rules,
title=TITLE,
description=DESCRIPTION,
url=URL,
theme_color=THEME_COLOR,
placeholders=PLACEHOLDERS))
# Minify index.html.
subprocess.run([
'npx', '--yes', 'html-minifier-terser',
'--collapse-whitespace',
'--remove-comments',
'--minify-css', 'true',
'--minify-js', 'true',
'index.html',
'-o', 'index.html',
])
# Prepend a license header to the minified index.html.
with open('index.html', 'r') as f:
content = f.read()
with open('header', 'r') as f:
content = f.read() + content
with open('index.html', 'w') as f:
f.write(content)
if __name__ == '__main__':
main()