Add support for NAME keyword
This commit is contained in:
parent
2afd11581c
commit
20970ec872
8 changed files with 63 additions and 3 deletions
|
@ -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).
|
||||
tokens []token
|
||||
Nodes []Node
|
||||
NamedNodes map[string]Node
|
||||
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.
|
||||
Error error
|
||||
|
@ -117,6 +118,7 @@ func (c *Configuration) Parse(input io.Reader, path string) (d *Document) {
|
|||
Configuration: c,
|
||||
Outline: Outline{outlineSection, outlineSection, 0},
|
||||
BufferSettings: map[string]string{},
|
||||
NamedNodes: map[string]Node{},
|
||||
Path: path,
|
||||
}
|
||||
defer func() {
|
||||
|
|
|
@ -397,6 +397,10 @@ func (w *HTMLWriter) WriteNodeWithMeta(n NodeWithMeta) {
|
|||
w.WriteString(out)
|
||||
}
|
||||
|
||||
func (w *HTMLWriter) WriteNodeWithName(n NodeWithName) {
|
||||
WriteNodes(w, n.Node)
|
||||
}
|
||||
|
||||
func (w *HTMLWriter) WriteTable(t Table) {
|
||||
w.WriteString("<table>\n")
|
||||
beforeFirstContentRow := true
|
||||
|
|
|
@ -14,6 +14,11 @@ type Keyword struct {
|
|||
Value string
|
||||
}
|
||||
|
||||
type NodeWithName struct {
|
||||
Name string
|
||||
Node Node
|
||||
}
|
||||
|
||||
type NodeWithMeta struct {
|
||||
Node Node
|
||||
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) {
|
||||
k := parseKeyword(d.tokens[i])
|
||||
switch k.Key {
|
||||
case "NAME":
|
||||
return d.parseNodeWithName(k, i, stop)
|
||||
case "SETUPFILE":
|
||||
return d.loadSetupFile(k)
|
||||
case "INCLUDE":
|
||||
return d.newInclude(k)
|
||||
return d.parseInclude(k)
|
||||
case "CAPTION", "ATTR_HTML":
|
||||
consumed, node := d.parseAffiliated(i, stop)
|
||||
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) {
|
||||
start, meta := i, Metadata{}
|
||||
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)}
|
||||
}
|
||||
|
||||
func (d *Document) newInclude(k Keyword) (int, Node) {
|
||||
func (d *Document) parseInclude(k Keyword) (int, Node) {
|
||||
resolve := func() Node {
|
||||
d.Log.Printf("Bad include %#v", 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 Keyword) 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) }
|
||||
|
|
|
@ -164,6 +164,11 @@ func (w *OrgWriter) WriteNodeWithMeta(n NodeWithMeta) {
|
|||
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) {
|
||||
w.WriteString(w.indent + "#" + c.Content + "\n")
|
||||
}
|
||||
|
|
10
org/testdata/keywords.html
vendored
10
org/testdata/keywords.html
vendored
|
@ -18,3 +18,13 @@ and an image with custom html attributes and a caption
|
|||
kittens!
|
||||
</figcaption>
|
||||
</figure>
|
||||
<p>
|
||||
named paragraph
|
||||
</p>
|
||||
<div class="src src-text">
|
||||
<div class="highlight">
|
||||
<pre>
|
||||
named block
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
|
|
8
org/testdata/keywords.org
vendored
8
org/testdata/keywords.org
vendored
|
@ -12,3 +12,11 @@ and an image with custom html attributes and a caption
|
|||
#+ATTR_HTML: :style height: 100%; :id overwritten
|
||||
#+ATTR_HTML: :style border: 10px solid black; :id kittens
|
||||
[[https://placekitten.com/200/200#.png]]
|
||||
|
||||
#+NAME: foo
|
||||
named paragraph
|
||||
|
||||
#+NAME: bar
|
||||
#+begin_src
|
||||
named block
|
||||
#+end_src
|
||||
|
|
8
org/testdata/keywords.pretty_org
vendored
8
org/testdata/keywords.pretty_org
vendored
|
@ -12,3 +12,11 @@ and an image with custom html attributes and a caption
|
|||
#+ATTR_HTML: :style height: 100%; :id overwritten
|
||||
#+ATTR_HTML: :style border: 10px solid black; :id kittens
|
||||
[[https://placekitten.com/200/200#.png]]
|
||||
|
||||
#+NAME: foo
|
||||
named paragraph
|
||||
|
||||
#+NAME: bar
|
||||
#+BEGIN_SRC
|
||||
named block
|
||||
#+END_SRC
|
||||
|
|
|
@ -12,6 +12,7 @@ type Writer interface {
|
|||
WriteInclude(Include)
|
||||
WriteComment(Comment)
|
||||
WriteNodeWithMeta(NodeWithMeta)
|
||||
WriteNodeWithName(NodeWithName)
|
||||
WriteHeadline(Headline)
|
||||
WriteBlock(Block)
|
||||
WriteExample(Example)
|
||||
|
@ -46,6 +47,8 @@ func WriteNodes(w Writer, nodes ...Node) {
|
|||
w.WriteComment(n)
|
||||
case NodeWithMeta:
|
||||
w.WriteNodeWithMeta(n)
|
||||
case NodeWithName:
|
||||
w.WriteNodeWithName(n)
|
||||
case Headline:
|
||||
w.WriteHeadline(n)
|
||||
case Block:
|
||||
|
@ -90,7 +93,7 @@ func WriteNodes(w Writer, nodes ...Node) {
|
|||
w.WriteFootnoteDefinition(n)
|
||||
default:
|
||||
if n != nil {
|
||||
panic(fmt.Sprintf("bad node %#v", n))
|
||||
panic(fmt.Sprintf("bad node %T %#v", n, n))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue