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

@ -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() {