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 = [ ('', 'back to pugl'), ] 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, '{title}'.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(('', '