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\n'
+ for a in toplevel_articles:
+ toc_content += '- {}
\n'.format(a['href'], a['title'])
+
+ seen = set()
+ for article in articles:
+ if article['path'] not in seen:
+ path = article['path']
+ components = path.split('/')
+ hlvl = len(components) + 1
+ toc_content += '
'
+ toc_content += f'' + path.split('/')[-1] + f''
+ toc_content += ''
+ seen.add(path)
+ toc_content += '- {title}
'.format(**article)
+
+ 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'.format(update_title(header_content, 'table of contents'))
+ articles = []
copy(logo_path, dest_path)
copy(css_path, dest_path)
@@ -88,16 +117,17 @@ def compile_wiki(source_path: str,
footer_content)
output = relativize_links(output, depth)
dest_filename = source_filename.split('.')[0] + '.html'
- toc_content += '- {}
\n'.format(
- os.path.join(current_suffix, dest_filename),
- os.path.join(current_suffix,dest_filename.split('.')[0]))
+ articles.append({
+ 'title': dest_filename.split('.')[0],
+ 'href': os.path.join(current_suffix, dest_filename),
+ 'path': current_suffix})
final_path = os.path.join(dest_root, dest_filename)
with open(final_path, 'w') as f:
f.write(output)
on_create(final_path)
- 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))