Improve footnote handling

- Footnotes separator rather than headline to get around i18n
- Warn on footnote redefinition
- Do not export footnote definitions at point of definition, only in the
  footnote section.
- Do not automatically exclude Footnotes section to get around possibly hiding
  other content of such a section - and i18n.
  The user has the choice of explicitly hiding the section via a :noexport:
  tag.

and some other refactoring
This commit is contained in:
Niklas Fasching 2018-12-26 15:23:23 +01:00
parent beff0c0d8a
commit eb7db9b968
11 changed files with 114 additions and 60 deletions

View file

@ -13,11 +13,10 @@ import (
type HTMLWriter struct {
stringBuilder
HighlightCodeBlock func(source, lang string) string
FootnotesHeadingTitle string
htmlEscape bool
excludeTags []string
log *log.Logger
HighlightCodeBlock func(source, lang string) string
htmlEscape bool
excludeTags []string
log *log.Logger
}
var emphasisTags = map[string][]string{
@ -45,8 +44,7 @@ var listItemStatuses = map[string]string{
func NewHTMLWriter() *HTMLWriter {
return &HTMLWriter{
htmlEscape: true,
FootnotesHeadingTitle: "Footnotes",
htmlEscape: true,
HighlightCodeBlock: func(source, lang string) string {
return fmt.Sprintf("%s\n<pre>\n%s\n</pre>\n</div>", `<div class="highlight">`, html.EscapeString(source))
},
@ -95,7 +93,7 @@ func (w *HTMLWriter) writeNodes(ns ...Node) {
continue
case FootnoteDefinition:
w.writeFootnoteDefinition(n)
continue
case List:
w.writeList(n)
@ -191,24 +189,19 @@ func (w *HTMLWriter) writeFootnoteDefinition(f FootnoteDefinition) {
}
func (w *HTMLWriter) writeFootnotes(d *Document) {
fs := d.Footnotes
if len(fs.Definitions) == 0 {
if len(d.Footnotes.Definitions) == 0 {
return
}
w.WriteString(`<div class="footnotes">` + "\n")
w.WriteString(`<h1 class="footnotes-title">` + fs.Title + `</h1>` + "\n")
w.WriteString(`<hr class="footnotes-separatator">` + "\n")
w.WriteString(`<div class="footnote-definitions">` + "\n")
for _, definition := range d.Footnotes.Ordered() {
w.writeNodes(definition)
w.writeFootnoteDefinition(definition)
}
w.WriteString("</div>\n</div>\n")
}
func (w *HTMLWriter) writeHeadline(h Headline) {
title := w.nodesAsString(h.Title...)
if h.Lvl == 1 && title == w.FootnotesHeadingTitle {
return
}
for _, excludeTag := range w.excludeTags {
for _, tag := range h.Tags {
if excludeTag == tag {
@ -230,7 +223,7 @@ func (w *HTMLWriter) writeHeadline(h Headline) {
w.WriteString(fmt.Sprintf(`<span class="priority">[%s]</span>`, h.Priority) + "\n")
}
w.WriteString(title)
w.writeNodes(h.Title...)
if len(h.Tags) != 0 {
tags := make([]string, len(h.Tags))
for i, tag := range h.Tags {
@ -337,7 +330,7 @@ func (w *HTMLWriter) writeDescriptiveListItem(di DescriptiveListItem) {
}
func (w *HTMLWriter) writeParagraph(p Paragraph) {
if isEmptyLineParagraph(p) {
if len(p.Children) == 0 {
return
}
w.WriteString("<p>")