Export WriteNodesAsString on writer interface

WriteNodesAsString is simple enough to implement but exposing it is helpful in
the implementation of extending writers and we don't aim to keep writer a small
interface so let's expose it.
This commit is contained in:
Niklas Fasching 2019-10-30 22:05:06 +01:00
parent c9d11e1556
commit 9a0a9c11eb
13 changed files with 42 additions and 41 deletions

View file

@ -69,7 +69,7 @@ func NewHTMLWriter() *HTMLWriter {
}
}
func (w *HTMLWriter) nodesAsString(nodes ...Node) string {
func (w *HTMLWriter) WriteNodesAsString(nodes ...Node) string {
original := w.Builder
w.Builder = strings.Builder{}
WriteNodes(w, nodes...)
@ -108,7 +108,7 @@ func (w *HTMLWriter) WriteBlock(b Block) {
w.Builder, w.htmlEscape = builder, htmlEscape
content = strings.TrimRightFunc(out, unicode.IsSpace)
} else {
content = w.nodesAsString(b.Children...)
content = w.WriteNodesAsString(b.Children...)
}
switch name := b.Name; {
case name == "SRC":
@ -193,7 +193,7 @@ func (w *HTMLWriter) writeSection(section *Section) {
// NOTE: To satisfy hugo ExtractTOC() check we cannot use `<li>\n` here. Doesn't really matter, just a note.
w.WriteString("<li>")
h := section.Headline
title := cleanHeadlineTitleForHTMLAnchorRegexp.ReplaceAllString(w.nodesAsString(h.Title...), "")
title := cleanHeadlineTitleForHTMLAnchorRegexp.ReplaceAllString(w.WriteNodesAsString(h.Title...), "")
w.WriteString(fmt.Sprintf("<a href=\"#%s\">%s</a>\n", h.ID(), title))
if len(section.Children) != 0 {
w.WriteString("<ul>\n")
@ -305,7 +305,7 @@ func (w *HTMLWriter) WriteRegularLink(l RegularLink) {
}
description := url
if l.Description != nil {
description = w.nodesAsString(l.Description...)
description = w.WriteNodesAsString(l.Description...)
}
switch l.Kind() {
case "image":
@ -383,10 +383,10 @@ func (w *HTMLWriter) WriteHorizontalRule(h HorizontalRule) {
}
func (w *HTMLWriter) WriteNodeWithMeta(n NodeWithMeta) {
out := w.nodesAsString(n.Node)
out := w.WriteNodesAsString(n.Node)
if p, ok := n.Node.(Paragraph); ok {
if len(p.Children) == 1 && isImageOrVideoLink(p.Children[0]) {
out = w.nodesAsString(p.Children[0])
out = w.WriteNodesAsString(p.Children[0])
}
}
for _, attributes := range n.Meta.HTMLAttributes {
@ -398,7 +398,7 @@ func (w *HTMLWriter) WriteNodeWithMeta(n NodeWithMeta) {
if i != 0 {
caption += " "
}
caption += w.nodesAsString(ns...)
caption += w.WriteNodesAsString(ns...)
}
out = fmt.Sprintf("<figure>\n%s<figcaption>\n%s\n</figcaption>\n</figure>\n", out, caption)
}