Skip to content

Commit

Permalink
Fix nested table of contents HTML (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb authored and tlienart committed Sep 29, 2019
1 parent 1dd2525 commit c24a885
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
41 changes: 23 additions & 18 deletions src/converter/html_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,33 +102,38 @@ The split is as follows:
* h[4] indicates the level of the title
"""
function hfun_toc()::String
inner = ""
curlvl = 1
inner = ""
baselvl = minimum(h[4] for h in values(PAGE_HEADERS)) - 1
curlvl = baselvl
for i 1:length(PAGE_HEADERS)
h = PAGE_HEADERS[i]
lvl = h[4]
if lvl < curlvl
# how many levels?
δ = curlvl - lvl
curlvl = lvl
for i = 1:δ
inner *= "</ol>"
if lvl curlvl
# Close previous list item
inner *= "</li>"

# Close additional sublists for each level eliminated
for i = curlvl-1:-1:lvl
inner *= "</ol></li>"
end

# Reopen for this list item
inner *= "<li>"
elseif lvl > curlvl
# how many levels?
δ = lvl - curlvl
curlvl = lvl
for i = 1:δ
inner *= "<ol>"
# Open additional sublists for each level added
for i = curlvl+1:lvl
inner *= "<ol><li>"
end
end
inner *= html_li(html_ahref_key(h[2], h[1]))
inner *= html_ahref_key(h[2], h[1])
curlvl = lvl
# At this point, number of sublists (<ol><li>) open equals curlvl
end
# close at whatever level we are
for i = curlvl:-1:2
inner *= "</ol>"
# Close remaining lists, as if going down to the base level
for i = curlvl-1:-1:baselvl
inner *= "</li></ol>"
end
toc = "<div class=\"jd-toc\"><ol>" * inner * "</ol></div>"
toc = "<div class=\"jd-toc\">" * inner * "</div>"
end


Expand Down
7 changes: 0 additions & 7 deletions src/misc_html.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@ Convenience function to add an id attribute to a html element
"""
attr(name::Symbol, val::AS) = ifelse(isempty(val), "", " $name=\"$val\"")

"""
$(SIGNATURES)
Convenience function for a list item
"""
html_li(in::AS) = "<li>$(in)</li>"

"""
$SIGNATURES
Expand Down
11 changes: 7 additions & 4 deletions test/converter/markdown2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,26 @@ end
h = raw"""
\toc
## Hello `jd`
#### weirdly nested
### Goodbye!
## Done
done.
""" * J.EOS |> seval
@test isapproxstr(h, raw"""
<div class="jd-toc">
<ol>
<ol>
<li><a href="/pub/ff/aa.html#hello_jd">Hello <code>jd</code></a></li>
<li>
<a href="/pub/ff/aa.html#hello_jd">Hello <code>jd</code></a>
<ol>
<li><ol><li><a href="/pub/ff/aa.html#weirdly_nested">weirdly nested</a></li></ol></li>
<li><a href="/pub/ff/aa.html#goodbye">Goodbye&#33;</a></li>
</ol>
<li><a href="/pub/ff/aa.html#done">Done</a></li>
</ol>
</li>
<li><a href="/pub/ff/aa.html#done">Done</a></li>
</ol>
</div>
<h2 id="hello_jd"><a href="/pub/ff/aa.html#hello_jd">Hello <code>jd</code></a></h2>
<h4 id="weirdly_nested"><a href="/pub/ff/aa.html#weirdly_nested">weirdly nested</a></h4>
<h3 id="goodbye"><a href="/pub/ff/aa.html#goodbye">Goodbye&#33;</a></h3>
<h2 id="done"><a href="/pub/ff/aa.html#done">Done</a></h2>done.
""")
Expand Down

0 comments on commit c24a885

Please sign in to comment.