diff --git a/org/document.go b/org/document.go index 657919f..8c89a23 100644 --- a/org/document.go +++ b/org/document.go @@ -41,14 +41,6 @@ type Document struct { Error error } -// 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. - WriteNodes(...Node) // WriteNodes is called with the nodes of the parsed document. - String() string // String is called at the very end to retrieve the final output. -} - // Node represents a parsed node of the document. It's an empty interface and can be ignored. type Node interface{} @@ -105,7 +97,7 @@ func (d *Document) Write(w Writer) (out string, err error) { return "", fmt.Errorf("could not write output: parse was not called") } w.Before(d) - w.WriteNodes(d.Nodes...) + WriteNodes(w, d.Nodes...) w.After(d) return w.String(), err } diff --git a/org/html_writer.go b/org/html_writer.go index 75320ed..e9ef2b0 100644 --- a/org/html_writer.go +++ b/org/html_writer.go @@ -63,87 +63,29 @@ func (w *HTMLWriter) emptyClone() *HTMLWriter { func (w *HTMLWriter) nodesAsString(nodes ...Node) string { tmp := w.emptyClone() - tmp.WriteNodes(nodes...) + WriteNodes(tmp, nodes...) return tmp.String() } func (w *HTMLWriter) Before(d *Document) { w.document = d w.log = d.Log - w.writeOutline(d) + w.WriteOutline(d) } func (w *HTMLWriter) After(d *Document) { - w.writeFootnotes(d) + w.WriteFootnotes(d) } -func (w *HTMLWriter) WriteNodes(ns ...Node) { - for _, n := range ns { - switch n := n.(type) { - case Keyword: - w.writeKeyword(n) - case Include: - w.writeInclude(n) - case Comment: - continue - case NodeWithMeta: - w.writeNodeWithMeta(n) - case Headline: - w.writeHeadline(n) - case Block: - w.writeBlock(n) - case Drawer: - w.writeDrawer(n) - case PropertyDrawer: - continue +func (w *HTMLWriter) WriteComment(Comment) {} +func (w *HTMLWriter) WritePropertyDrawer(PropertyDrawer) {} - case FootnoteDefinition: - continue - - case List: - w.writeList(n) - case ListItem: - w.writeListItem(n) - case DescriptiveListItem: - w.writeDescriptiveListItem(n) - - case Table: - w.writeTable(n) - - case Paragraph: - w.writeParagraph(n) - case Example: - w.writeExample(n) - case HorizontalRule: - w.writeHorizontalRule(n) - case Text: - w.writeText(n) - case Emphasis: - w.writeEmphasis(n) - case StatisticToken: - w.writeStatisticToken(n) - case ExplicitLineBreak: - w.writeExplicitLineBreak(n) - case LineBreak: - w.writeLineBreak(n) - case RegularLink: - w.writeRegularLink(n) - case FootnoteLink: - w.writeFootnoteLink(n) - default: - if n != nil { - panic(fmt.Sprintf("bad node %#v", n)) - } - } - } -} - -func (w *HTMLWriter) writeBlock(b Block) { +func (w *HTMLWriter) WriteBlock(b Block) { content := "" if isRawTextBlock(b.Name) { exportWriter := w.emptyClone() exportWriter.htmlEscape = false - exportWriter.WriteNodes(b.Children...) + WriteNodes(exportWriter, b.Children...) content = strings.TrimRightFunc(exportWriter.String(), unicode.IsSpace) } else { content = w.nodesAsString(b.Children...) @@ -170,30 +112,32 @@ func (w *HTMLWriter) writeBlock(b Block) { } } -func (w *HTMLWriter) writeDrawer(d Drawer) { - w.WriteNodes(d.Children...) +func (w *HTMLWriter) WriteDrawer(d Drawer) { + WriteNodes(w, d.Children...) } -func (w *HTMLWriter) writeKeyword(k Keyword) { +func (w *HTMLWriter) WriteKeyword(k Keyword) { if k.Key == "HTML" { w.WriteString(k.Value + "\n") } } -func (w *HTMLWriter) writeInclude(i Include) { - w.WriteNodes(i.Resolve()) +func (w *HTMLWriter) WriteInclude(i Include) { + WriteNodes(w, i.Resolve()) } +func (w *HTMLWriter) WriteFootnoteDefinition(FootnoteDefinition) {} + func (w *HTMLWriter) writeFootnoteDefinition(f FootnoteDefinition) { n := f.Name w.WriteString(`
` + "\n") w.WriteString(fmt.Sprintf(`%s`, n, n, n) + "\n") w.WriteString(`
` + "\n") - w.WriteNodes(f.Children...) + WriteNodes(w, f.Children...) w.WriteString("
\n
\n") } -func (w *HTMLWriter) writeFootnotes(d *Document) { +func (w *HTMLWriter) WriteFootnotes(d *Document) { if !w.document.GetOption("f") || len(d.Footnotes.Definitions) == 0 { return } @@ -206,7 +150,7 @@ func (w *HTMLWriter) writeFootnotes(d *Document) { w.WriteString("\n\n") } -func (w *HTMLWriter) writeOutline(d *Document) { +func (w *HTMLWriter) WriteOutline(d *Document) { if w.document.GetOption("toc") && len(d.Outline.Children) != 0 { w.WriteString("