diff --git a/tildewiki/main.py b/tildewiki/main.py index ea782e1..9a708c0 100644 --- a/tildewiki/main.py +++ b/tildewiki/main.py @@ -173,7 +173,6 @@ def compile_wiki(source_path, dest_path): # TODO progress bar # TODO recursively delete dest_path (maybe after gzipping, backing up) # TODO lockfile on dest_path - # TODO pickup links and generate toc.html # TODO put useful metadata in footer header_content = compile_markdown(path_join(source_path, 'src/header.md')) @@ -181,12 +180,15 @@ def compile_wiki(source_path, dest_path): articles_root = path_join(source_path, 'src/articles') + toc_content = '{}\n' + with open(path_join(dest_path, 'toc.html'), 'w') as f: + f.write(toc_content) + f.write(footer_content) + def slurp(file_path): content = None with open(file_path, 'r') as f: @@ -227,13 +237,16 @@ def compile_source_file(source_file_path, header_content, footer_content): title = extract_title(content) if title is not None: - header_content = re.sub( - TITLE_RE, - '{}'.format(title), - header_content) + header_content = update_title(header_content, title) return '{}\n{}\n{}'.format(header_content, content, footer_content) +def update_title(content, title): + """Given a chunk of HTML, finds, updates, and returns the title element to + be the given title. If there is no title element, the content is returned + unmodified.""" + return re.sub(TITLE_RE, '{}'.format(title), content) + def extract_title(content): """Given a string of page content, look for a header in the first line. Returns it if found; returns None otherwise."""