From 95f2dead982fb660611233326058b80014d96840 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Sun, 26 Aug 2018 12:30:20 +0100 Subject: [PATCH] Allow hooking into footnote generation This adds an (empty) *ast.Footnote before the footnotes are added. This can be used to customize the it, i.e. putting it in a new section. The HR output is put behind a flag html.FootnoteNoHRTag to suppress that as well. Signed-off-by: Miek Gieben --- ast/node.go | 5 +++++ html/renderer.go | 9 +++++++-- parser/parser.go | 1 + 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ast/node.go b/ast/node.go index 9156208..caae0d9 100644 --- a/ast/node.go +++ b/ast/node.go @@ -391,6 +391,11 @@ type Superscript struct { Leaf } +// Footnotes is a node that contains all footnotes +type Footnotes struct { + Container +} + func removeNodeFromArray(a []Node, node Node) []Node { n := len(a) for i := 0; i < n; i++ { diff --git a/html/renderer.go b/html/renderer.go index 4a2fd95..1cb3039 100644 --- a/html/renderer.go +++ b/html/renderer.go @@ -32,6 +32,7 @@ const ( CompletePage // Generate a complete HTML page UseXHTML // Generate XHTML output instead of HTML FootnoteReturnLinks // Generate a link at the end of a footnote to return to the source + FootnoteNoHRTag // Do not output an HR after starting a footnote list. Smartypants // Enable smart punctuation substitutions SmartypantsFractions // Enable smart fractions (with Smartypants) SmartypantsDashes // Enable smart dashes (with Smartypants) @@ -654,8 +655,10 @@ func (r *Renderer) listEnter(w io.Writer, nodeData *ast.List) { if nodeData.IsFootnotesList { r.outs(w, "\n
\n\n") - r.outHRTag(w, nil) - r.cr(w) + if r.opts.Flags&FootnoteNoHRTag == 0 { + r.outHRTag(w, nil) + r.cr(w) + } } r.cr(w) if isListItem(nodeData.Parent) { @@ -990,6 +993,8 @@ func (r *Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.Wal Escape(w, node.Literal) } r.outOneOf(w, false, "", "") + case *ast.Footnotes: + // nothing by default; just output the list. default: panic(fmt.Sprintf("Unknown node %T", node)) } diff --git a/parser/parser.go b/parser/parser.go index 24dab88..50fb071 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -288,6 +288,7 @@ func (p *Parser) parseRefsToAST() { IsFootnotesList: true, ListFlags: ast.ListTypeOrdered, } + p.addBlock(&ast.Footnotes{}) block := p.addBlock(list) flags := ast.ListItemBeginningOfList // Note: this loop is intentionally explicit, not range-form. This is