html: Allow limiting level of headlines to be included in toc
The org mode toc OPTION does not just support true/false - it also allows specifying the max headline level [1] to be included in the toc. [1] headline level as seen in org mode - not the html tag level
This commit is contained in:
parent
7a2cd1abb1
commit
f1361615ed
6 changed files with 59 additions and 5 deletions
|
@ -169,7 +169,7 @@ func (d *Document) Get(key string) string {
|
||||||
// - < (export timestamps)
|
// - < (export timestamps)
|
||||||
// - e (export org entities)
|
// - e (export org entities)
|
||||||
// - f (export footnotes)
|
// - f (export footnotes)
|
||||||
// - toc (export table of content)
|
// - toc (export table of content. an int limits the included org headline lvl)
|
||||||
// - todo (export headline todo status)
|
// - todo (export headline todo status)
|
||||||
// - pri (export headline priority)
|
// - pri (export headline priority)
|
||||||
// - tags (export headline tags)
|
// - tags (export headline tags)
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"html"
|
"html"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
@ -184,24 +185,32 @@ func (w *HTMLWriter) WriteFootnotes(d *Document) {
|
||||||
|
|
||||||
func (w *HTMLWriter) WriteOutline(d *Document) {
|
func (w *HTMLWriter) WriteOutline(d *Document) {
|
||||||
if w.document.GetOption("toc") != "nil" && len(d.Outline.Children) != 0 {
|
if w.document.GetOption("toc") != "nil" && len(d.Outline.Children) != 0 {
|
||||||
|
maxLvl, _ := strconv.Atoi(w.document.GetOption("toc"))
|
||||||
w.WriteString("<nav>\n<ul>\n")
|
w.WriteString("<nav>\n<ul>\n")
|
||||||
for _, section := range d.Outline.Children {
|
for _, section := range d.Outline.Children {
|
||||||
w.writeSection(section)
|
w.writeSection(section, maxLvl)
|
||||||
}
|
}
|
||||||
w.WriteString("</ul>\n</nav>\n")
|
w.WriteString("</ul>\n</nav>\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *HTMLWriter) writeSection(section *Section) {
|
func (w *HTMLWriter) writeSection(section *Section, maxLvl int) {
|
||||||
|
if maxLvl != 0 && section.Headline.Lvl > maxLvl {
|
||||||
|
return
|
||||||
|
}
|
||||||
// NOTE: To satisfy hugo ExtractTOC() check we cannot use `<li>\n` here. Doesn't really matter, just a note.
|
// NOTE: To satisfy hugo ExtractTOC() check we cannot use `<li>\n` here. Doesn't really matter, just a note.
|
||||||
w.WriteString("<li>")
|
w.WriteString("<li>")
|
||||||
h := section.Headline
|
h := section.Headline
|
||||||
title := cleanHeadlineTitleForHTMLAnchorRegexp.ReplaceAllString(w.WriteNodesAsString(h.Title...), "")
|
title := cleanHeadlineTitleForHTMLAnchorRegexp.ReplaceAllString(w.WriteNodesAsString(h.Title...), "")
|
||||||
w.WriteString(fmt.Sprintf("<a href=\"#%s\">%s</a>\n", h.ID(), title))
|
w.WriteString(fmt.Sprintf("<a href=\"#%s\">%s</a>\n", h.ID(), title))
|
||||||
if len(section.Children) != 0 {
|
hasChildren := false
|
||||||
|
for _, section := range section.Children {
|
||||||
|
hasChildren = hasChildren || maxLvl == 0 || section.Headline.Lvl <= maxLvl
|
||||||
|
}
|
||||||
|
if hasChildren {
|
||||||
w.WriteString("<ul>\n")
|
w.WriteString("<ul>\n")
|
||||||
for _, section := range section.Children {
|
for _, section := range section.Children {
|
||||||
w.writeSection(section)
|
w.writeSection(section, maxLvl)
|
||||||
}
|
}
|
||||||
w.WriteString("</ul>\n")
|
w.WriteString("</ul>\n")
|
||||||
}
|
}
|
||||||
|
|
21
org/testdata/headlines.html
vendored
21
org/testdata/headlines.html
vendored
|
@ -14,6 +14,8 @@
|
||||||
</li>
|
</li>
|
||||||
<li><a href="#headline-7">malformed property drawer</a>
|
<li><a href="#headline-7">malformed property drawer</a>
|
||||||
</li>
|
</li>
|
||||||
|
<li><a href="#headline-8">level limit for headlines to be included in the table of contents</a>
|
||||||
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
<h2 id="headline-1">
|
<h2 id="headline-1">
|
||||||
|
@ -88,3 +90,22 @@ not a property
|
||||||
<p>
|
<p>
|
||||||
:END:
|
:END:
|
||||||
</p>
|
</p>
|
||||||
|
<h2 id="headline-8">
|
||||||
|
level limit for headlines to be included in the table of contents
|
||||||
|
</h2>
|
||||||
|
<p>
|
||||||
|
The toc option allows setting a <a href="https://orgmode.org/manual/Export-settings.html">level limit</a>. For this file we set it to 1 - which means that the following headlines
|
||||||
|
won't be included in the table of contents.
|
||||||
|
</p>
|
||||||
|
<h3 id="headline-9">
|
||||||
|
headline 2 not in toc
|
||||||
|
</h3>
|
||||||
|
<h4 id="headline-10">
|
||||||
|
headline 3 not in toc
|
||||||
|
</h4>
|
||||||
|
<h3 id="headline-11">
|
||||||
|
anoter headline 2 not in toc
|
||||||
|
</h3>
|
||||||
|
<p>
|
||||||
|
you get the gist…
|
||||||
|
</p>
|
||||||
|
|
8
org/testdata/headlines.org
vendored
8
org/testdata/headlines.org
vendored
|
@ -1,4 +1,5 @@
|
||||||
#+SETUPFILE: setup_file_org
|
#+SETUPFILE: setup_file_org
|
||||||
|
#+OPTIONS: toc:1
|
||||||
* Simple Headline [1/2]
|
* Simple Headline [1/2]
|
||||||
- [X] checked
|
- [X] checked
|
||||||
- [ ] unchecked
|
- [ ] unchecked
|
||||||
|
@ -31,3 +32,10 @@ By default =EXCLUDE_TAGS= is just =:noexport:=.
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
not a property
|
not a property
|
||||||
:END:
|
:END:
|
||||||
|
* level limit for headlines to be included in the table of contents
|
||||||
|
The toc option allows setting a [[https://orgmode.org/manual/Export-settings.html][level limit]]. For this file we set it to 1 - which means that the following headlines
|
||||||
|
won't be included in the table of contents.
|
||||||
|
** headline 2 not in toc
|
||||||
|
*** headline 3 not in toc
|
||||||
|
** anoter headline 2 not in toc
|
||||||
|
you get the gist...
|
||||||
|
|
8
org/testdata/headlines.pretty_org
vendored
8
org/testdata/headlines.pretty_org
vendored
|
@ -1,4 +1,5 @@
|
||||||
#+SETUPFILE: setup_file_org
|
#+SETUPFILE: setup_file_org
|
||||||
|
#+OPTIONS: toc:1
|
||||||
* Simple Headline [1/2]
|
* Simple Headline [1/2]
|
||||||
- [X] checked
|
- [X] checked
|
||||||
- [ ] unchecked
|
- [ ] unchecked
|
||||||
|
@ -31,3 +32,10 @@ By default =EXCLUDE_TAGS= is just =:noexport:=.
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
not a property
|
not a property
|
||||||
:END:
|
:END:
|
||||||
|
* level limit for headlines to be included in the table of contents
|
||||||
|
The toc option allows setting a [[https://orgmode.org/manual/Export-settings.html][level limit]]. For this file we set it to 1 - which means that the following headlines
|
||||||
|
won't be included in the table of contents.
|
||||||
|
** headline 2 not in toc
|
||||||
|
*** headline 3 not in toc
|
||||||
|
** anoter headline 2 not in toc
|
||||||
|
you get the gist...
|
||||||
|
|
8
org/testdata/misc.html
vendored
8
org/testdata/misc.html
vendored
|
@ -131,6 +131,7 @@ src block
|
||||||
<div class="highlight">
|
<div class="highlight">
|
||||||
<pre>
|
<pre>
|
||||||
#+SETUPFILE: setup_file_org
|
#+SETUPFILE: setup_file_org
|
||||||
|
#+OPTIONS: toc:1
|
||||||
* Simple Headline [1/2]
|
* Simple Headline [1/2]
|
||||||
- [X] checked
|
- [X] checked
|
||||||
- [ ] unchecked
|
- [ ] unchecked
|
||||||
|
@ -163,6 +164,13 @@ By default =EXCLUDE_TAGS= is just =:noexport:=.
|
||||||
:PROPERTIES:
|
:PROPERTIES:
|
||||||
not a property
|
not a property
|
||||||
:END:
|
:END:
|
||||||
|
* level limit for headlines to be included in the table of contents
|
||||||
|
The toc option allows setting a [[https://orgmode.org/manual/Export-settings.html][level limit]]. For this file we set it to 1 - which means that the following headlines
|
||||||
|
won't be included in the table of contents.
|
||||||
|
** headline 2 not in toc
|
||||||
|
*** headline 3 not in toc
|
||||||
|
** anoter headline 2 not in toc
|
||||||
|
you get the gist...
|
||||||
</pre>
|
</pre>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue