Add support for NAME keyword

This commit is contained in:
Niklas Fasching 2019-10-27 15:04:55 +01:00
parent 2afd11581c
commit 20970ec872
8 changed files with 63 additions and 3 deletions

View file

@ -36,6 +36,7 @@ type Document struct {
Path string // Path of the file containing the parse input - used to resolve relative paths during parsing (e.g. INCLUDE). Path string // Path of the file containing the parse input - used to resolve relative paths during parsing (e.g. INCLUDE).
tokens []token tokens []token
Nodes []Node Nodes []Node
NamedNodes map[string]Node
Outline Outline // Outline is a Table Of Contents for the document and contains all sections (headline + content). Outline Outline // Outline is a Table Of Contents for the document and contains all sections (headline + content).
BufferSettings map[string]string // Settings contains all settings that were parsed from keywords. BufferSettings map[string]string // Settings contains all settings that were parsed from keywords.
Error error Error error
@ -117,6 +118,7 @@ func (c *Configuration) Parse(input io.Reader, path string) (d *Document) {
Configuration: c, Configuration: c,
Outline: Outline{outlineSection, outlineSection, 0}, Outline: Outline{outlineSection, outlineSection, 0},
BufferSettings: map[string]string{}, BufferSettings: map[string]string{},
NamedNodes: map[string]Node{},
Path: path, Path: path,
} }
defer func() { defer func() {

View file

@ -397,6 +397,10 @@ func (w *HTMLWriter) WriteNodeWithMeta(n NodeWithMeta) {
w.WriteString(out) w.WriteString(out)
} }
func (w *HTMLWriter) WriteNodeWithName(n NodeWithName) {
WriteNodes(w, n.Node)
}
func (w *HTMLWriter) WriteTable(t Table) { func (w *HTMLWriter) WriteTable(t Table) {
w.WriteString("<table>\n") w.WriteString("<table>\n")
beforeFirstContentRow := true beforeFirstContentRow := true

View file

@ -14,6 +14,11 @@ type Keyword struct {
Value string Value string
} }
type NodeWithName struct {
Name string
Node Node
}
type NodeWithMeta struct { type NodeWithMeta struct {
Node Node Node Node
Meta Metadata Meta Metadata
@ -51,10 +56,12 @@ func (d *Document) parseComment(i int, stop stopFn) (int, Node) {
func (d *Document) parseKeyword(i int, stop stopFn) (int, Node) { func (d *Document) parseKeyword(i int, stop stopFn) (int, Node) {
k := parseKeyword(d.tokens[i]) k := parseKeyword(d.tokens[i])
switch k.Key { switch k.Key {
case "NAME":
return d.parseNodeWithName(k, i, stop)
case "SETUPFILE": case "SETUPFILE":
return d.loadSetupFile(k) return d.loadSetupFile(k)
case "INCLUDE": case "INCLUDE":
return d.newInclude(k) return d.parseInclude(k)
case "CAPTION", "ATTR_HTML": case "CAPTION", "ATTR_HTML":
consumed, node := d.parseAffiliated(i, stop) consumed, node := d.parseAffiliated(i, stop)
if consumed != 0 { if consumed != 0 {
@ -71,6 +78,18 @@ func (d *Document) parseKeyword(i int, stop stopFn) (int, Node) {
} }
} }
func (d *Document) parseNodeWithName(k Keyword, i int, stop stopFn) (int, Node) {
if stop(d, i+1) {
return 0, nil
}
consumed, node := d.parseOne(i+1, stop)
if consumed == 0 || node == nil {
return 0, nil
}
d.NamedNodes[k.Value] = node
return consumed + 1, NodeWithName{k.Value, node}
}
func (d *Document) parseAffiliated(i int, stop stopFn) (int, Node) { func (d *Document) parseAffiliated(i int, stop stopFn) (int, Node) {
start, meta := i, Metadata{} start, meta := i, Metadata{}
for ; !stop(d, i) && d.tokens[i].kind == "keyword"; i++ { for ; !stop(d, i) && d.tokens[i].kind == "keyword"; i++ {
@ -115,7 +134,7 @@ func parseKeyword(t token) Keyword {
return Keyword{strings.ToUpper(k), strings.TrimSpace(v)} return Keyword{strings.ToUpper(k), strings.TrimSpace(v)}
} }
func (d *Document) newInclude(k Keyword) (int, Node) { func (d *Document) parseInclude(k Keyword) (int, Node) {
resolve := func() Node { resolve := func() Node {
d.Log.Printf("Bad include %#v", k) d.Log.Printf("Bad include %#v", k)
return k return k
@ -161,4 +180,5 @@ func (d *Document) loadSetupFile(k Keyword) (int, Node) {
func (n Comment) String() string { return orgWriter.nodesAsString(n) } func (n Comment) String() string { return orgWriter.nodesAsString(n) }
func (n Keyword) 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 NodeWithMeta) String() string { return orgWriter.nodesAsString(n) }
func (n NodeWithName) String() string { return orgWriter.nodesAsString(n) }
func (n Include) String() string { return orgWriter.nodesAsString(n) } func (n Include) String() string { return orgWriter.nodesAsString(n) }

View file

@ -164,6 +164,11 @@ func (w *OrgWriter) WriteNodeWithMeta(n NodeWithMeta) {
WriteNodes(w, n.Node) WriteNodes(w, n.Node)
} }
func (w *OrgWriter) WriteNodeWithName(n NodeWithName) {
w.WriteString(fmt.Sprintf("#+NAME: %s\n", n.Name))
WriteNodes(w, n.Node)
}
func (w *OrgWriter) WriteComment(c Comment) { func (w *OrgWriter) WriteComment(c Comment) {
w.WriteString(w.indent + "#" + c.Content + "\n") w.WriteString(w.indent + "#" + c.Content + "\n")
} }

View file

@ -18,3 +18,13 @@ and an image with custom html attributes and a caption
kittens! kittens!
</figcaption> </figcaption>
</figure> </figure>
<p>
named paragraph
</p>
<div class="src src-text">
<div class="highlight">
<pre>
named block
</pre>
</div>
</div>

View file

@ -12,3 +12,11 @@ and an image with custom html attributes and a caption
#+ATTR_HTML: :style height: 100%; :id overwritten #+ATTR_HTML: :style height: 100%; :id overwritten
#+ATTR_HTML: :style border: 10px solid black; :id kittens #+ATTR_HTML: :style border: 10px solid black; :id kittens
[[https://placekitten.com/200/200#.png]] [[https://placekitten.com/200/200#.png]]
#+NAME: foo
named paragraph
#+NAME: bar
#+begin_src
named block
#+end_src

View file

@ -12,3 +12,11 @@ and an image with custom html attributes and a caption
#+ATTR_HTML: :style height: 100%; :id overwritten #+ATTR_HTML: :style height: 100%; :id overwritten
#+ATTR_HTML: :style border: 10px solid black; :id kittens #+ATTR_HTML: :style border: 10px solid black; :id kittens
[[https://placekitten.com/200/200#.png]] [[https://placekitten.com/200/200#.png]]
#+NAME: foo
named paragraph
#+NAME: bar
#+BEGIN_SRC
named block
#+END_SRC

View file

@ -12,6 +12,7 @@ type Writer interface {
WriteInclude(Include) WriteInclude(Include)
WriteComment(Comment) WriteComment(Comment)
WriteNodeWithMeta(NodeWithMeta) WriteNodeWithMeta(NodeWithMeta)
WriteNodeWithName(NodeWithName)
WriteHeadline(Headline) WriteHeadline(Headline)
WriteBlock(Block) WriteBlock(Block)
WriteExample(Example) WriteExample(Example)
@ -46,6 +47,8 @@ func WriteNodes(w Writer, nodes ...Node) {
w.WriteComment(n) w.WriteComment(n)
case NodeWithMeta: case NodeWithMeta:
w.WriteNodeWithMeta(n) w.WriteNodeWithMeta(n)
case NodeWithName:
w.WriteNodeWithName(n)
case Headline: case Headline:
w.WriteHeadline(n) w.WriteHeadline(n)
case Block: case Block:
@ -90,7 +93,7 @@ func WriteNodes(w Writer, nodes ...Node) {
w.WriteFootnoteDefinition(n) w.WriteFootnoteDefinition(n)
default: default:
if n != nil { if n != nil {
panic(fmt.Sprintf("bad node %#v", n)) panic(fmt.Sprintf("bad node %T %#v", n, n))
} }
} }
} }