From 95cecbbe6741f8f2cd2c17455fc26d94a6cedb25 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 17 Apr 2019 23:17:35 +0000 Subject: [PATCH] header-based toc generation --- tildewiki/compilation.py | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/tildewiki/compilation.py b/tildewiki/compilation.py index acecb79..80c668b 100644 --- a/tildewiki/compilation.py +++ b/tildewiki/compilation.py @@ -37,6 +37,35 @@ def depth_from(root: str, path: str) -> int: first = os.path.split(first)[0] return depth +def generate_toc(header_content, articles): + """given header_content and a list of dicts with keys title, href, and path this function + generates the toc page's content""" + + toc_content = '{}\n'.format(update_title(header_content, 'table of contents')) + + toplevel_articles = [a for a in articles if a['path'] == ''] + articles = [a for a in articles if a['path'] != ''] + sorted(articles, key=lambda a: a['path']) + toc_content += '

Table of Contents

\n' + toc_content += '

unsorted articles

\n' + toc_content += f'' + path.split('/')[-1] + f'' + toc_content += '' + return toc_content + def compile_wiki(source_path: str, dest_path: str, on_create: Callable[[str], None]=DEFAULT_ON_CREATE) -> None: @@ -60,7 +89,7 @@ def compile_wiki(source_path: str, articles_root = os.path.join(source_path, 'src/articles') - toc_content = '{}\n' + toc_content = generate_toc(header_content, articles) toc_path = os.path.join(dest_path, 'toc.html') with open(toc_path, 'w') as f: f.write(relativize_links(toc_content, 1))