Add base table-of-contents, don't collapse headlines.

This commit is contained in:
Sergio Martínez Portela 2022-11-29 00:20:31 +01:00
parent 58bb7ccc49
commit e268734a3f
2 changed files with 64 additions and 22 deletions

View file

@ -620,7 +620,28 @@ def render_as_document(headline, doc, headlineLevel, graph, title):
</html>
"""
else:
return as_document(render(headline, doc, graph=graph, headlineLevel=headlineLevel), title)
return as_document(render(headline, doc, graph=graph, headlineLevel=headlineLevel), title, render_toc(doc))
def render_toc(doc):
acc = ['<ul>']
for headline in doc.getTopHeadlines():
render_toc_headline(headline, acc)
acc.append('</ul>')
return ''.join(acc)
def render_toc_headline(headline, acc):
acc.append('<li>')
acc.append(f'<a href="#{headline.id}">{html.escape(headline.title.get_text())}</a>')
children = list(headline.children)
if children:
acc.append('<ul>')
for child in children:
render_toc_headline(child, acc)
acc.append('</ul>')
acc.append('</li>')
def render_connections(headline_id, content, graph):
if headline_id not in graph['backlinks']:
@ -665,13 +686,13 @@ def render(headline, doc, graph, headlineLevel):
tags = f'<span class="tags">{"".join(tag_list)}</span>'
display_state = 'expanded'
# Update display based on document STARTUP config
visual_level = doc.get_keywords('STARTUP', 'showall')
if visual_level.startswith('show') and visual_level.endswith('levels'):
visual_level_num = int(visual_level[len('show'):-len('levels')]) - 1
# Note that level is 0 indexed inside this loop
if headlineLevel >= visual_level_num:
display_state = 'collapsed'
# # Update display based on document STARTUP config
# visual_level = doc.get_keywords('STARTUP', 'showall')
# if visual_level.startswith('show') and visual_level.endswith('levels'):
# visual_level_num = int(visual_level[len('show'):-len('levels')]) - 1
# # Note that level is 0 indexed inside this loop
# if headlineLevel >= visual_level_num:
# display_state = 'collapsed'
title = render_inline(headline.title, render_tag, headline, graph)
@ -692,7 +713,7 @@ def render(headline, doc, graph, headlineLevel):
"""
def as_document(html, title):
def as_document(html, title, global_toc):
return f"""<!DOCTYPE html>
<html>
<head>
@ -719,6 +740,10 @@ def as_document(html, title):
<h1><a href="./index.html">Código para llevar [Notes]</a></h1>
<input type="text" id="searchbox" disabled="true" placeholder="Search (requires JS)" />
</nav>
<div class="global-table-of-contents">
<h2>Table of contents</h2>
{global_toc}
</div>
{html}
<script src="../static/search-box.js"></script>