1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package converter
import (
"github.com/JohannesKaufmann/dom"
"golang.org/x/net/html"
)
func (conv *Converter) handleRenderNodes(ctx Context, w Writer, nodes ...*html.Node) {
for _, node := range nodes {
conv.handleRenderNode(ctx, w, node)
}
}
func (conv *Converter) handleRenderNode(ctx Context, w Writer, node *html.Node) RenderStatus {
name := dom.NodeName(node)
// - - A: the #text node - - //
if name == "#text" {
return conv.handleRenderText(ctx, w, node)
}
// - - B: the render handlers - - //
for _, handler := range conv.getRenderHandlers() {
status := handler.Value(ctx, w, node)
if status == RenderSuccess {
return status
}
}
// - - C: the fallback - - //
// If nothing works we fallback to this:
return conv.handleRenderFallback(ctx, w, node)
}
func (conv *Converter) handleRenderFallback(ctx Context, w Writer, node *html.Node) RenderStatus {
tagName := dom.NodeName(node)
tagType, _ := ctx.GetTagType(tagName)
if tagType == TagTypeBlock {
w.WriteRune('\n')
w.WriteRune('\n')
}
ctx.RenderChildNodes(ctx, w, node)
if tagType == TagTypeBlock {
w.WriteRune('\n')
w.WriteRune('\n')
}
return RenderSuccess
}
func (conv *Converter) handleRenderText(ctx Context, w Writer, node *html.Node) RenderStatus {
content := node.Data
for _, handler := range conv.getTextTransformHandlers() {
content = handler.Value(ctx, content)
}
w.WriteString(content)
return RenderSuccess
}
|