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,7 +68,6 @@ var DefaultFrontMatterHandler = func(k, v string) interface{} {
func NewDocument() *Document {
return &Document{
Footnotes: &Footnotes{
ExcludeHeading: true,
Title: "Footnotes",
Definitions: map[string]*FootnoteDefinition{},
},

View file

@ -5,7 +5,6 @@ import (
)
type Footnotes struct {
ExcludeHeading bool
Title string
Definitions map[string]*FootnoteDefinition
addOrder []string
@ -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) {

View file

@ -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
}

View file

@ -12,6 +12,7 @@ import (
type HTMLWriter struct {
stringBuilder
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<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) {
title := w.nodesAsString(h.Title...)
if h.Lvl == 1 && title == w.FootnotesHeadingTitle {
return
}
w.WriteString(fmt.Sprintf("<h%d>\n", h.Lvl))
if h.Status != "" {
w.WriteString(fmt.Sprintf(`<span class="todo">%s</span>`, 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 {

View file

@ -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])) {