summaryrefslogtreecommitdiff
path: root/guide-src/make.py
blob: 69a0fd13023a021223f3e8eeaf10a71b7711efda (plain)
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
import os, html
from urllib.parse import quote
import shutil

INPUT_DIR = 'guide-src'
OUTPUT_DIR = 'guide'
TITLE_PREFIX = '---'

os.makedirs(OUTPUT_DIR, exist_ok=True)

try:
	for f in os.listdir(INPUT_DIR):
		if f.endswith('.png') or f.endswith('.svg') or f.endswith('.jpg') or f.endswith('.jpeg'):
			shutil.copyfile(INPUT_DIR + '/' + f, OUTPUT_DIR + '/' + f)
		elif not '.' in f:
			os.makedirs(OUTPUT_DIR + '/' + f, exist_ok=True)
except FileNotFoundError:
	print('this script must be run from the root directory.')
	quit()

def readlines(path):
	f = open(path)
	l = f.readlines()
	f.close()
	return l

class Section:
	def __init__(self, path, name):
		self.path = path
		self.name = name
		self.items = []
	
	def __repr__(self):
		return ''.join(['Section(path=', repr(self.path), ', name=', repr(self.name), ', items=', repr(self.items), ')'])

outline = [l.strip() for l in readlines('{}/outline.txt'.format(INPUT_DIR)) if l.strip()]
entries = []
for item in outline:
	if item[0] == '/':
		assert item.endswith('.html'), 'expected path ending in .html'
		entries[-1].items.append(item[1:])
	elif ':' in item:
		[path, name] = item.split(': ')
		entries.append(Section(path, name))
	else:
		entries.append(item)

sidebar_content = [
	('', '<a class="guide-sidebar-item" href="<ROOT>../index.html">back to pugl</a>'),
]

def add_sidebar_link_for_page(path, indented):
	file = open(INPUT_DIR + '/' + path)
	first_line = file.readline().strip()
	file.close()
	assert first_line.startswith(TITLE_PREFIX), 'file {} doesn\'t start with title prefix {}'.format(path, TITLE_PREFIX)
	title = first_line[len(TITLE_PREFIX):].strip()
	sidebar_content.append((
		path,
		'<a href="<ROOT>{url}" class="guide-sidebar-item{c}">{title}</a>'.format(
			url=quote(path), c=' guide-sidebar-item-indented' if indented else '', title=html.escape(title)
		)
	))

for entry in entries:
	if isinstance(entry, Section):
		section = entry
		sidebar_content.append(('', '<h3 class="guide-sidebar-heading">{}</h3>'.format(section.name)))
		for item in section.items:
			path = '{}/{}/{}'.format(INPUT_DIR, section.path, item)
			add_sidebar_link_for_page(section.path + '/' + item, True)
	else:
		assert isinstance(entry, str)
		add_sidebar_link_for_page(entry, False)

def process_file(path):
	count = path.count('/')
	if count == 0:
		guide_root = ''
	elif count == 1:
		guide_root = '../'
	else:
		assert False, 'whats goin on with ' + path
	lines = readlines(INPUT_DIR + '/' + path)
	assert lines[0].startswith(TITLE_PREFIX)
	title = lines[0][len(TITLE_PREFIX):].strip()
	contents = ''.join(lines[1:])
	sidebar = []
	for (p, item) in sidebar_content:
		if p == path:
			item = item.replace('class="', 'class="guide-sidebar-item-active ')
		item = item.replace('<ROOT>', guide_root)
		sidebar.append(item.replace('<ROOT>', guide_root))
	output = '''<!DOCTYPE html>
<!-- This file was auto-generated by make.py. DO NOT EDIT IT DIRECTLY. -->
<html>
<head>
	<title>pugl - {title}</title>
	<meta charset="utf-8">
	<meta content="width=device-width,initial-scale=1" name="viewport">
	<link rel="icon" href="{root}../favicon.ico">
	<link rel="stylesheet" href="{root}../style.css">
</head>
<body id="guide-body">
<div id="guide-sidebar">
{sidebar}
</div>
<div id="guide-contents">
{contents}
</div>
</body>
</html>'''.format(title=title, sidebar=''.join(sidebar), contents=contents, root=guide_root)
	f = open(OUTPUT_DIR + '/' + path, 'w')
	f.write(output)
	f.close()

for entry in entries:
	if isinstance(entry, Section):
		section = entry
		for item in section.items:
			process_file('{}/{}'.format(section.path, item))
	else:
		process_file(entry)