zim-wiki team mailing list archive
-
zim-wiki team
-
Mailing list archive
-
Message #04672
Generating Table of Contents
I am attempting to create a table of contents when exporting to HTML. I
have made some progress, but I am having trouble with closing remaining
tags when the last item is nested more than 1 level down the structure.
Sometimes it closes more tags than I have open, depending on the level
of the final item.
Sometimes the I get something like this for instance:
<ul>
<li>One</li>
<li>
<ul>
<li>Sub 1</li>
<li>Sub 2</li>
</ul>
</li>
</ul>
</li> Oops
</ul> Oops
This is what I have so far:
[% FOR page IN pages %]
[% SET prevLevel = 1 %]
<ul>
[% prevLevel = 1 %]
[% FOR section IN page.headings(6) %]
[% IF section.level > prevLevel %]
[% prevLevel = section.level %]
[% parenLevel = section.level %]
<li>
<ul>
<li>#[% section.level %] (going deeper) - [%
section.heading %]</li>
[% ELSIF section.level < prevLevel %]
<!-- close nested tags -->
[% FOR idx IN reversed(range(section.level, prevLevel)) %]
</ul>
</li>
[% END %]
<li>#[% section.level %] (after steps back) - [%
section.heading %]</li>
[% SET prevLevel = parenLevel %]
[% ELSE %]
<li>#[% section.level %] (same level) - [% section.heading
%]</li>
[% END %]
<!-- close any remaining tags -->
[% IF loop.last and prevLevel > 1 %]
[% FOR num IN range(1, prevLevel) %]
</ul>
</li>
[% END %]
[% END %]
[% END %]
</ul>
[% END %]
https://gitlab.com/snippets/1712677 (same code as above)
I wonder if there is a better approach or it is just my logic that is
broken (I guess both).