Go does not support inheritance, just composition. While composition with type embedding (i.e. forwarding method calls to the embedded type) can replace inheritance for most use cases this is not one of them. We really want to overwrite methods so that method calls from inside the base writer also use the custom methods ouf our extending writer - naive embedding does not work here as the this in this.WriteText refers to the embedded type rather than the outer extending type (see open recursion). A simple solution is to make a reference of the extending type available from the extended type and use that for nested method calls. We'll go with that one as it does not require huge code changes. Another solution would be to flatten the writing process and not use nested method calls - this is what blackfriday does. Assuming the current solution works I feel it's cleaner and keeps the ugliness of simulating inheritance with composition contained to a small portion of the code while blackfridays approach requires all write methods to be written in a flat style (i.e. not do nested calls to write by being called twice with entering / leaving). The current solution becomes ugly if we want to do multiple levels of extending but i don't expect that to be a valid use case - if it turns out to be one we can always adapt to it later. YAGNI.
103 lines
2.5 KiB
Go
103 lines
2.5 KiB
Go
package org
|
|
|
|
import "fmt"
|
|
|
|
// Writer is the interface that is used to export a parsed document into a new format. See Document.Write().
|
|
type Writer interface {
|
|
Before(*Document) // Before is called before any nodes are passed to the writer.
|
|
After(*Document) // After is called after all nodes have been passed to the writer.
|
|
String() string // String is called at the very end to retrieve the final output.
|
|
|
|
WriterWithExtensions() Writer
|
|
|
|
WriteKeyword(Keyword)
|
|
WriteInclude(Include)
|
|
WriteComment(Comment)
|
|
WriteNodeWithMeta(NodeWithMeta)
|
|
WriteNodeWithName(NodeWithName)
|
|
WriteHeadline(Headline)
|
|
WriteBlock(Block)
|
|
WriteExample(Example)
|
|
WriteDrawer(Drawer)
|
|
WritePropertyDrawer(PropertyDrawer)
|
|
WriteList(List)
|
|
WriteListItem(ListItem)
|
|
WriteDescriptiveListItem(DescriptiveListItem)
|
|
WriteTable(Table)
|
|
WriteHorizontalRule(HorizontalRule)
|
|
WriteParagraph(Paragraph)
|
|
WriteText(Text)
|
|
WriteEmphasis(Emphasis)
|
|
WriteLatexFragment(LatexFragment)
|
|
WriteStatisticToken(StatisticToken)
|
|
WriteExplicitLineBreak(ExplicitLineBreak)
|
|
WriteLineBreak(LineBreak)
|
|
WriteRegularLink(RegularLink)
|
|
WriteTimestamp(Timestamp)
|
|
WriteFootnoteLink(FootnoteLink)
|
|
WriteFootnoteDefinition(FootnoteDefinition)
|
|
}
|
|
|
|
func WriteNodes(w Writer, nodes ...Node) {
|
|
w = w.WriterWithExtensions()
|
|
for _, n := range nodes {
|
|
switch n := n.(type) {
|
|
case Keyword:
|
|
w.WriteKeyword(n)
|
|
case Include:
|
|
w.WriteInclude(n)
|
|
case Comment:
|
|
w.WriteComment(n)
|
|
case NodeWithMeta:
|
|
w.WriteNodeWithMeta(n)
|
|
case NodeWithName:
|
|
w.WriteNodeWithName(n)
|
|
case Headline:
|
|
w.WriteHeadline(n)
|
|
case Block:
|
|
w.WriteBlock(n)
|
|
case Example:
|
|
w.WriteExample(n)
|
|
case Drawer:
|
|
w.WriteDrawer(n)
|
|
case PropertyDrawer:
|
|
w.WritePropertyDrawer(n)
|
|
case List:
|
|
w.WriteList(n)
|
|
case ListItem:
|
|
w.WriteListItem(n)
|
|
case DescriptiveListItem:
|
|
w.WriteDescriptiveListItem(n)
|
|
case Table:
|
|
w.WriteTable(n)
|
|
case HorizontalRule:
|
|
w.WriteHorizontalRule(n)
|
|
case Paragraph:
|
|
w.WriteParagraph(n)
|
|
case Text:
|
|
w.WriteText(n)
|
|
case Emphasis:
|
|
w.WriteEmphasis(n)
|
|
case LatexFragment:
|
|
w.WriteLatexFragment(n)
|
|
case StatisticToken:
|
|
w.WriteStatisticToken(n)
|
|
case ExplicitLineBreak:
|
|
w.WriteExplicitLineBreak(n)
|
|
case LineBreak:
|
|
w.WriteLineBreak(n)
|
|
case RegularLink:
|
|
w.WriteRegularLink(n)
|
|
case Timestamp:
|
|
w.WriteTimestamp(n)
|
|
case FootnoteLink:
|
|
w.WriteFootnoteLink(n)
|
|
case FootnoteDefinition:
|
|
w.WriteFootnoteDefinition(n)
|
|
default:
|
|
if n != nil {
|
|
panic(fmt.Sprintf("bad node %T %#v", n, n))
|
|
}
|
|
}
|
|
}
|
|
}
|