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'):
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()]
sections = []
for item in outline:
if item[0] == '/':
assert item.endswith('.html'), 'expected path ending in .html'
sections[-1].items.append(item[1:])
else:
[path, name] = item.split(': ')
sections.append(Section(path, name))
sidebar_content = [
('', 'back to pugl'),
('index.html', 'the basics'),
]
for section in sections:
sidebar_content.append(('', '
{}
'.format(section.name)))
for item in section.items:
path = '{}/{}/{}'.format(INPUT_DIR, section.path, item)
file = open(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()
assert '"' not in path, 'path {} should not contain "'.format(path)
url = quote('{}/{}'.format(section.path, item))
sidebar_content.append((section.path + '/' + item, '{}'.format(url, html.escape(title))))
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('', guide_root)
sidebar.append(item.replace('', guide_root))
output = '''
pugl - {title}
{sidebar}
{contents}
'''.format(title=title, sidebar=''.join(sidebar), contents=contents, root=guide_root)
f = open(OUTPUT_DIR + '/' + path, 'w')
f.write(output)
f.close()
process_file('index.html')
for section in sections:
for item in section.items:
process_file('{}/{}'.format(section.path, item))