From bd33e8885ee448af3a91207963e6043b4b752fe5 Mon Sep 17 00:00:00 2001 From: Niklas Fasching Date: Sun, 6 Jan 2019 20:50:02 +0100 Subject: [PATCH] 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. --- org/block.go | 3 +++ org/document.go | 10 ++++++++-- org/drawer.go | 3 +++ org/footnote.go | 2 ++ org/headline.go | 2 ++ org/inline.go | 8 ++++++++ org/keyword.go | 5 +++++ org/list.go | 4 ++++ org/paragraph.go | 3 +++ org/table.go | 2 ++ 10 files changed, 40 insertions(+), 2 deletions(-) diff --git a/org/block.go b/org/block.go index 5fc2ba6..0e7a526 100644 --- a/org/block.go +++ b/org/block.go @@ -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) } diff --git a/org/document.go b/org/document.go index 8c89a23..8bddcfb 100644 --- a/org/document.go +++ b/org/document.go @@ -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() { diff --git a/org/drawer.go b/org/drawer.go index fc8d936..ce22564 100644 --- a/org/drawer.go +++ b/org/drawer.go @@ -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) } diff --git a/org/footnote.go b/org/footnote.go index be050fd..628452c 100644 --- a/org/footnote.go +++ b/org/footnote.go @@ -54,3 +54,5 @@ func (fs *Footnotes) Ordered() []FootnoteDefinition { } return append(definitions, inlineDefinitions...) } + +func (n FootnoteDefinition) String() string { return orgWriter.nodesAsString(n) } diff --git a/org/headline.go b/org/headline.go index 74ff99e..3b54408 100644 --- a/org/headline.go +++ b/org/headline.go @@ -97,3 +97,5 @@ func (parent *Section) add(current *Section) { parent.Parent.add(current) } } + +func (n Headline) String() string { return orgWriter.nodesAsString(n) } diff --git a/org/inline.go b/org/inline.go index 7c810e0..1eacfdd 100644 --- a/org/inline.go +++ b/org/inline.go @@ -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) } diff --git a/org/keyword.go b/org/keyword.go index f8ffa4c..8752680 100644 --- a/org/keyword.go +++ b/org/keyword.go @@ -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) } diff --git a/org/list.go b/org/list.go index 5607c02..6ba28f6 100644 --- a/org/list.go +++ b/org/list.go @@ -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) } diff --git a/org/paragraph.go b/org/paragraph.go index 072a155..b7d3ea9 100644 --- a/org/paragraph.go +++ b/org/paragraph.go @@ -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) } diff --git a/org/table.go b/org/table.go index b31cfba..a404e1a 100644 --- a/org/table.go +++ b/org/table.go @@ -126,3 +126,5 @@ func isSpecialRow(rawColumns []string) bool { } return isAlignRow } + +func (n Table) String() string { return orgWriter.nodesAsString(n) }