Add String() method to Node interface

Being able to very easily get the original [1] Org mode content seems like
something that will come up quite often and is very little code.

[1] it's not really the original content, but rather the pretty printed version
of that - as the semantics don't change it shouldn't matter.
This commit is contained in:
Niklas Fasching 2019-01-06 20:50:02 +01:00
parent 148eef5aeb
commit bd33e8885e
10 changed files with 40 additions and 2 deletions

View file

@ -79,3 +79,6 @@ func trimIndentUpTo(max int) func(string) string {
return line[i:]
}
}
func (n Example) String() string { return orgWriter.nodesAsString(n) }
func (n Block) String() string { return orgWriter.nodesAsString(n) }

View file

@ -41,8 +41,10 @@ type Document struct {
Error error
}
// Node represents a parsed node of the document. It's an empty interface and can be ignored.
type Node interface{}
// Node represents a parsed node of the document.
type Node interface {
String() string // String returns the pretty printed Org mode string for the node (see OrgWriter).
}
type lexFn = func(line string) (t token, ok bool)
type parseFn = func(*Document, int, stopFn) (int, Node)
@ -69,6 +71,7 @@ var lexFns = []lexFn{
}
var nilToken = token{"nil", -1, "", nil}
var orgWriter = NewOrgWriter()
// New returns a new Configuration with (hopefully) sane defaults.
func New() *Configuration {
@ -84,6 +87,9 @@ func New() *Configuration {
}
}
// String returns the pretty printed Org mode string for the given nodes (see OrgWriter).
func String(nodes []Node) string { return orgWriter.nodesAsString(nodes...) }
// Write is called after with an instance of the Writer interface to export a parsed Document into another format.
func (d *Document) Write(w Writer) (out string, err error) {
defer func() {

View file

@ -89,3 +89,6 @@ func (d *PropertyDrawer) Get(key string) (string, bool) {
}
return "", false
}
func (n Drawer) String() string { return orgWriter.nodesAsString(n) }
func (n PropertyDrawer) String() string { return orgWriter.nodesAsString(n) }

View file

@ -54,3 +54,5 @@ func (fs *Footnotes) Ordered() []FootnoteDefinition {
}
return append(definitions, inlineDefinitions...)
}
func (n FootnoteDefinition) String() string { return orgWriter.nodesAsString(n) }

View file

@ -97,3 +97,5 @@ func (parent *Section) add(current *Section) {
parent.Parent.add(current)
}
}
func (n Headline) String() string { return orgWriter.nodesAsString(n) }

View file

@ -274,3 +274,11 @@ func (l RegularLink) Kind() string {
}
return "regular"
}
func (n Text) String() string { return orgWriter.nodesAsString(n) }
func (n LineBreak) String() string { return orgWriter.nodesAsString(n) }
func (n ExplicitLineBreak) String() string { return orgWriter.nodesAsString(n) }
func (n StatisticToken) String() string { return orgWriter.nodesAsString(n) }
func (n Emphasis) String() string { return orgWriter.nodesAsString(n) }
func (n FootnoteLink) String() string { return orgWriter.nodesAsString(n) }
func (n RegularLink) String() string { return orgWriter.nodesAsString(n) }

View file

@ -158,3 +158,8 @@ func (d *Document) loadSetupFile(k Keyword) (int, Node) {
}
return 1, k
}
func (n Comment) String() string { return orgWriter.nodesAsString(n) }
func (n Keyword) String() string { return orgWriter.nodesAsString(n) }
func (n NodeWithMeta) String() string { return orgWriter.nodesAsString(n) }
func (n Include) String() string { return orgWriter.nodesAsString(n) }

View file

@ -108,3 +108,7 @@ func (d *Document) parseListItem(l List, i int, parentStop stopFn) (int, Node) {
}
return i - start, ListItem{bullet, status, nodes}
}
func (n List) String() string { return orgWriter.nodesAsString(n) }
func (n ListItem) String() string { return orgWriter.nodesAsString(n) }
func (n DescriptiveListItem) String() string { return orgWriter.nodesAsString(n) }

View file

@ -41,3 +41,6 @@ func (d *Document) parseParagraph(i int, parentStop stopFn) (int, Node) {
func (d *Document) parseHorizontalRule(i int, parentStop stopFn) (int, Node) {
return 1, HorizontalRule{}
}
func (n Paragraph) String() string { return orgWriter.nodesAsString(n) }
func (n HorizontalRule) String() string { return orgWriter.nodesAsString(n) }

View file

@ -126,3 +126,5 @@ func isSpecialRow(rawColumns []string) bool {
}
return isAlignRow
}
func (n Table) String() string { return orgWriter.nodesAsString(n) }