header-based toc generation

master^2
vilmibm 2019-04-17 23:17:35 +00:00
parent a769128009
commit 95cecbbe67
1 changed files with 35 additions and 5 deletions

View File

@ -37,6 +37,35 @@ def depth_from(root: str, path: str) -> int:
first = os.path.split(first)[0] first = os.path.split(first)[0]
return depth 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 += '<h1>Table of Contents</h1>\n'
toc_content += '<h2>unsorted articles</h2>\n<ul>\n'
for a in toplevel_articles:
toc_content += '<li><a href="{}">{}</a></li>\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 += '</ul>'
toc_content += f'<h{hlvl}>' + path.split('/')[-1] + f'</h{hlvl}>'
toc_content += '<ul>'
seen.add(path)
toc_content += '<li><a href="{href}">{title}</a></li>'.format(**article)
toc_content += '</ul>'
return toc_content
def compile_wiki(source_path: str, def compile_wiki(source_path: str,
dest_path: str, dest_path: str,
on_create: Callable[[str], None]=DEFAULT_ON_CREATE) -> None: 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') articles_root = os.path.join(source_path, 'src/articles')
toc_content = '{}\n<ul>'.format(update_title(header_content, 'table of contents')) articles = []
copy(logo_path, dest_path) copy(logo_path, dest_path)
copy(css_path, dest_path) copy(css_path, dest_path)
@ -88,16 +117,17 @@ def compile_wiki(source_path: str,
footer_content) footer_content)
output = relativize_links(output, depth) output = relativize_links(output, depth)
dest_filename = source_filename.split('.')[0] + '.html' dest_filename = source_filename.split('.')[0] + '.html'
toc_content += '<li><a href="{}">{}</a></li>\n'.format( articles.append({
os.path.join(current_suffix, dest_filename), 'title': dest_filename.split('.')[0],
os.path.join(current_suffix,dest_filename.split('.')[0])) 'href': os.path.join(current_suffix, dest_filename),
'path': current_suffix})
final_path = os.path.join(dest_root, dest_filename) final_path = os.path.join(dest_root, dest_filename)
with open(final_path, 'w') as f: with open(final_path, 'w') as f:
f.write(output) f.write(output)
on_create(final_path) on_create(final_path)
toc_content += '\n</ul>' toc_content = generate_toc(header_content, articles)
toc_path = os.path.join(dest_path, 'toc.html') toc_path = os.path.join(dest_path, 'toc.html')
with open(toc_path, 'w') as f: with open(toc_path, 'w') as f:
f.write(relativize_links(toc_content, 1)) f.write(relativize_links(toc_content, 1))