From ac2597af4c459d229b7a6ba7e5c76da836d5281c Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Mon, 17 Dec 2018 13:08:35 +0100 Subject: [PATCH] Refactor Footnotes: Exclude footnotes heading during export, not parsing Until now the footnotes section was parsed but not included in the resulting AST - his required rebuilding it in the OrgWriter. It feels cleaner to include it in the AST and only exclude it in the export --- org/document.go | 5 ++--- org/footnote.go | 12 ++++++------ org/headline.go | 4 ---- org/html.go | 10 ++++++++-- org/org.go | 17 +---------------- 5 files changed, 17 insertions(+), 31 deletions(-) diff --git a/org/document.go b/org/document.go index abd7099..a7eb2b6 100644 --- a/org/document.go +++ b/org/document.go @@ -68,9 +68,8 @@ var DefaultFrontMatterHandler = func(k, v string) interface{} { func NewDocument() *Document { return &Document{ Footnotes: &Footnotes{ - ExcludeHeading: true, - Title: "Footnotes", - Definitions: map[string]*FootnoteDefinition{}, + Title: "Footnotes", + Definitions: map[string]*FootnoteDefinition{}, }, AutoLink: true, MaxEmphasisNewLines: 1, diff --git a/org/footnote.go b/org/footnote.go index a4007b0..7032a22 100644 --- a/org/footnote.go +++ b/org/footnote.go @@ -5,10 +5,9 @@ import ( ) type Footnotes struct { - ExcludeHeading bool - Title string - Definitions map[string]*FootnoteDefinition - addOrder []string + Title string + Definitions map[string]*FootnoteDefinition + addOrder []string } type FootnoteDefinition struct { @@ -35,8 +34,9 @@ func (d *Document) parseFootnoteDefinition(i int, parentStop stopFn) (int, Node) d.tokens[i].kind == "headline" || d.tokens[i].kind == "footnoteDefinition" } consumed, nodes := d.parseMany(i, stop) - d.Footnotes.add(name, &FootnoteDefinition{name, nodes, false}) - return consumed, nil + definition := FootnoteDefinition{name, nodes, false} + d.Footnotes.add(name, &definition) + return consumed, definition } func (fs *Footnotes) add(name string, definition *FootnoteDefinition) { diff --git a/org/headline.go b/org/headline.go index c72bb2f..b9d56c4 100644 --- a/org/headline.go +++ b/org/headline.go @@ -68,9 +68,5 @@ func (d *Document) parseHeadline(i int, parentStop stopFn) (int, Node) { } } headline.Children = nodes - - if headline.Lvl == 1 && text == d.Footnotes.Title && d.Footnotes.ExcludeHeading { - return consumed + 1, nil - } return consumed + 1, headline } diff --git a/org/html.go b/org/html.go index 67fe739..d562345 100644 --- a/org/html.go +++ b/org/html.go @@ -11,7 +11,8 @@ import ( type HTMLWriter struct { stringBuilder - HighlightCodeBlock func(source, lang string) string + HighlightCodeBlock func(source, lang string) string + FootnotesHeadingTitle string } var emphasisTags = map[string][]string{ @@ -33,6 +34,7 @@ var listTags = map[string][]string{ func NewHTMLWriter() *HTMLWriter { return &HTMLWriter{ + FootnotesHeadingTitle: "Footnotes", HighlightCodeBlock: func(source, lang string) string { return fmt.Sprintf("%s\n
\n%s\n
\n", `
`, html.EscapeString(source)) }, @@ -181,11 +183,15 @@ func (w *HTMLWriter) writeFootnotes(d *Document) { } func (w *HTMLWriter) writeHeadline(h Headline) { + title := w.nodesAsString(h.Title...) + if h.Lvl == 1 && title == w.FootnotesHeadingTitle { + return + } w.WriteString(fmt.Sprintf("\n", h.Lvl)) if h.Status != "" { w.WriteString(fmt.Sprintf(`%s`, h.Status) + "\n") } - w.writeNodes(h.Title...) + w.WriteString(title) if len(h.Tags) != 0 { tags := make([]string, len(h.Tags)) for i, tag := range h.Tags { diff --git a/org/org.go b/org/org.go index 224313c..e70162a 100644 --- a/org/org.go +++ b/org/org.go @@ -31,9 +31,7 @@ func NewOrgWriter() *OrgWriter { } func (w *OrgWriter) before(d *Document) {} -func (w *OrgWriter) after(d *Document) { - w.writeFootnotes(d) -} +func (w *OrgWriter) after(d *Document) {} func (w *OrgWriter) emptyClone() *OrgWriter { wcopy := *w @@ -160,19 +158,6 @@ func (w *OrgWriter) writeDrawer(d Drawer) { w.WriteString(w.indent + ":END:\n") } -func (w *OrgWriter) writeFootnotes(d *Document) { - fs := d.Footnotes - if len(fs.Definitions) == 0 { - return - } - w.WriteString("* " + fs.Title + "\n") - for _, definition := range fs.Ordered() { - if !definition.Inline { - w.writeNodes(definition) - } - } -} - func (w *OrgWriter) writeFootnoteDefinition(f FootnoteDefinition) { w.WriteString(fmt.Sprintf("[fn:%s]", f.Name)) if !(len(f.Children) >= 1 && isEmptyLineParagraph(f.Children[0])) {