From abb81851be636de91f6903ec189eb7f46a056db8 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Tue, 13 Jul 2021 20:32:47 +0200 Subject: [PATCH] html: Hide excluded headlines from TOC emacs org mode did it first --- org/headline.go | 11 +++++++++++ org/html_writer.go | 10 +++------- org/testdata/headlines.html | 2 -- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/org/headline.go b/org/headline.go index 7e0b8eb..1f021b9 100644 --- a/org/headline.go +++ b/org/headline.go @@ -89,6 +89,17 @@ func (h Headline) ID() string { return fmt.Sprintf("headline-%d", h.Index) } +func (h Headline) IsExcluded(d *Document) bool { + for _, excludedTag := range strings.Fields(d.Get("EXCLUDE_TAGS")) { + for _, tag := range h.Tags { + if tag == excludedTag { + return true + } + } + } + return false +} + func (parent *Section) add(current *Section) { if parent.Headline == nil || parent.Headline.Lvl < current.Headline.Lvl { parent.Children = append(parent.Children, current) diff --git a/org/html_writer.go b/org/html_writer.go index 056612e..08a5b69 100644 --- a/org/html_writer.go +++ b/org/html_writer.go @@ -227,7 +227,7 @@ func (w *HTMLWriter) WriteOutline(d *Document, maxLvl int) { } func (w *HTMLWriter) writeSection(section *Section, maxLvl int) { - if maxLvl != 0 && section.Headline.Lvl > maxLvl { + if (maxLvl != 0 && section.Headline.Lvl > maxLvl) || section.Headline.IsExcluded(w.document) { return } // NOTE: To satisfy hugo ExtractTOC() check we cannot use `
  • \n` here. Doesn't really matter, just a note. @@ -250,12 +250,8 @@ func (w *HTMLWriter) writeSection(section *Section, maxLvl int) { } func (w *HTMLWriter) WriteHeadline(h Headline) { - for _, excludeTag := range strings.Fields(w.document.Get("EXCLUDE_TAGS")) { - for _, tag := range h.Tags { - if excludeTag == tag { - return - } - } + if h.IsExcluded(w.document) { + return } w.WriteString(fmt.Sprintf(`
    `, h.ID(), h.Lvl+1) + "\n") diff --git a/org/testdata/headlines.html b/org/testdata/headlines.html index 29b678a..a004144 100644 --- a/org/testdata/headlines.html +++ b/org/testdata/headlines.html @@ -10,8 +10,6 @@
  • headline with custom status
  • -
  • excluded headline -
  • malformed property drawer
  • level limit for headlines to be included in the table of contents