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
This commit is contained in:
Niklas Fasching 2018-12-17 13:08:35 +01:00
parent ced166dc18
commit ac2597af4c
5 changed files with 17 additions and 31 deletions

View file

@ -68,9 +68,8 @@ var DefaultFrontMatterHandler = func(k, v string) interface{} {
func NewDocument() *Document { func NewDocument() *Document {
return &Document{ return &Document{
Footnotes: &Footnotes{ Footnotes: &Footnotes{
ExcludeHeading: true, Title: "Footnotes",
Title: "Footnotes", Definitions: map[string]*FootnoteDefinition{},
Definitions: map[string]*FootnoteDefinition{},
}, },
AutoLink: true, AutoLink: true,
MaxEmphasisNewLines: 1, MaxEmphasisNewLines: 1,

View file

@ -5,10 +5,9 @@ import (
) )
type Footnotes struct { type Footnotes struct {
ExcludeHeading bool Title string
Title string Definitions map[string]*FootnoteDefinition
Definitions map[string]*FootnoteDefinition addOrder []string
addOrder []string
} }
type FootnoteDefinition struct { 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" d.tokens[i].kind == "headline" || d.tokens[i].kind == "footnoteDefinition"
} }
consumed, nodes := d.parseMany(i, stop) consumed, nodes := d.parseMany(i, stop)
d.Footnotes.add(name, &FootnoteDefinition{name, nodes, false}) definition := FootnoteDefinition{name, nodes, false}
return consumed, nil d.Footnotes.add(name, &definition)
return consumed, definition
} }
func (fs *Footnotes) add(name string, definition *FootnoteDefinition) { func (fs *Footnotes) add(name string, definition *FootnoteDefinition) {

View file

@ -68,9 +68,5 @@ func (d *Document) parseHeadline(i int, parentStop stopFn) (int, Node) {
} }
} }
headline.Children = nodes headline.Children = nodes
if headline.Lvl == 1 && text == d.Footnotes.Title && d.Footnotes.ExcludeHeading {
return consumed + 1, nil
}
return consumed + 1, headline return consumed + 1, headline
} }

View file

@ -11,7 +11,8 @@ import (
type HTMLWriter struct { type HTMLWriter struct {
stringBuilder stringBuilder
HighlightCodeBlock func(source, lang string) string HighlightCodeBlock func(source, lang string) string
FootnotesHeadingTitle string
} }
var emphasisTags = map[string][]string{ var emphasisTags = map[string][]string{
@ -33,6 +34,7 @@ var listTags = map[string][]string{
func NewHTMLWriter() *HTMLWriter { func NewHTMLWriter() *HTMLWriter {
return &HTMLWriter{ return &HTMLWriter{
FootnotesHeadingTitle: "Footnotes",
HighlightCodeBlock: func(source, lang string) string { HighlightCodeBlock: func(source, lang string) string {
return fmt.Sprintf("%s\n<pre>\n%s\n</pre>\n</div>", `<div class="highlight">`, html.EscapeString(source)) return fmt.Sprintf("%s\n<pre>\n%s\n</pre>\n</div>", `<div class="highlight">`, html.EscapeString(source))
}, },
@ -181,11 +183,15 @@ func (w *HTMLWriter) writeFootnotes(d *Document) {
} }
func (w *HTMLWriter) writeHeadline(h Headline) { func (w *HTMLWriter) writeHeadline(h Headline) {
title := w.nodesAsString(h.Title...)
if h.Lvl == 1 && title == w.FootnotesHeadingTitle {
return
}
w.WriteString(fmt.Sprintf("<h%d>\n", h.Lvl)) w.WriteString(fmt.Sprintf("<h%d>\n", h.Lvl))
if h.Status != "" { if h.Status != "" {
w.WriteString(fmt.Sprintf(`<span class="todo">%s</span>`, h.Status) + "\n") w.WriteString(fmt.Sprintf(`<span class="todo">%s</span>`, h.Status) + "\n")
} }
w.writeNodes(h.Title...) w.WriteString(title)
if len(h.Tags) != 0 { if len(h.Tags) != 0 {
tags := make([]string, len(h.Tags)) tags := make([]string, len(h.Tags))
for i, tag := range h.Tags { for i, tag := range h.Tags {

View file

@ -31,9 +31,7 @@ func NewOrgWriter() *OrgWriter {
} }
func (w *OrgWriter) before(d *Document) {} func (w *OrgWriter) before(d *Document) {}
func (w *OrgWriter) after(d *Document) { func (w *OrgWriter) after(d *Document) {}
w.writeFootnotes(d)
}
func (w *OrgWriter) emptyClone() *OrgWriter { func (w *OrgWriter) emptyClone() *OrgWriter {
wcopy := *w wcopy := *w
@ -160,19 +158,6 @@ func (w *OrgWriter) writeDrawer(d Drawer) {
w.WriteString(w.indent + ":END:\n") 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) { func (w *OrgWriter) writeFootnoteDefinition(f FootnoteDefinition) {
w.WriteString(fmt.Sprintf("[fn:%s]", f.Name)) w.WriteString(fmt.Sprintf("[fn:%s]", f.Name))
if !(len(f.Children) >= 1 && isEmptyLineParagraph(f.Children[0])) { if !(len(f.Children) >= 1 && isEmptyLineParagraph(f.Children[0])) {