Refactor: Move some code
Example nodes are a kind of block and it's not much code so we can just put it next to the Block code. Feels cleaner but doesn't change much.
This commit is contained in:
parent
edd22b5014
commit
148eef5aeb
3 changed files with 21 additions and 26 deletions
|
@ -14,6 +14,7 @@ see https://orgmode.org/manual/External-links.html & https://orgmode.org/manual/
|
||||||
- link target: <<go-org>>
|
- link target: <<go-org>>
|
||||||
- link: [[go-org]]
|
- link: [[go-org]]
|
||||||
- link to headline
|
- link to headline
|
||||||
|
- links with image as description
|
||||||
- MyTarget <- this will automatically become a link - not sure i want this...
|
- MyTarget <- this will automatically become a link - not sure i want this...
|
||||||
* resources
|
* resources
|
||||||
- test files
|
- test files
|
||||||
|
|
20
org/block.go
20
org/block.go
|
@ -12,6 +12,11 @@ type Block struct {
|
||||||
Children []Node
|
Children []Node
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Example struct {
|
||||||
|
Children []Node
|
||||||
|
}
|
||||||
|
|
||||||
|
var exampleLineRegexp = regexp.MustCompile(`^(\s*):(\s(.*)|\s*$)`)
|
||||||
var beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`)
|
var beginBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+BEGIN_(\w+)(.*)`)
|
||||||
var endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`)
|
var endBlockRegexp = regexp.MustCompile(`(?i)^(\s*)#\+END_(\w+)`)
|
||||||
|
|
||||||
|
@ -24,6 +29,13 @@ func lexBlock(line string) (token, bool) {
|
||||||
return nilToken, false
|
return nilToken, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func lexExample(line string) (token, bool) {
|
||||||
|
if m := exampleLineRegexp.FindStringSubmatch(line); m != nil {
|
||||||
|
return token{"example", len(m[1]), m[3], m}, true
|
||||||
|
}
|
||||||
|
return nilToken, false
|
||||||
|
}
|
||||||
|
|
||||||
func isRawTextBlock(name string) bool { return name == "SRC" || name == "EXAMPLE" || name == "EXPORT" }
|
func isRawTextBlock(name string) bool { return name == "SRC" || name == "EXAMPLE" || name == "EXPORT" }
|
||||||
|
|
||||||
func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) {
|
func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) {
|
||||||
|
@ -51,6 +63,14 @@ func (d *Document) parseBlock(i int, parentStop stopFn) (int, Node) {
|
||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *Document) parseExample(i int, parentStop stopFn) (int, Node) {
|
||||||
|
example, start := Example{}, i
|
||||||
|
for ; !parentStop(d, i) && d.tokens[i].kind == "example"; i++ {
|
||||||
|
example.Children = append(example.Children, Text{d.tokens[i].content, true})
|
||||||
|
}
|
||||||
|
return i - start, example
|
||||||
|
}
|
||||||
|
|
||||||
func trimIndentUpTo(max int) func(string) string {
|
func trimIndentUpTo(max int) func(string) string {
|
||||||
return func(line string) string {
|
return func(line string) string {
|
||||||
i := 0
|
i := 0
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
package org
|
|
||||||
|
|
||||||
import (
|
|
||||||
"regexp"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Example struct {
|
|
||||||
Children []Node
|
|
||||||
}
|
|
||||||
|
|
||||||
var exampleLineRegexp = regexp.MustCompile(`^(\s*):(\s(.*)|\s*$)`)
|
|
||||||
|
|
||||||
func lexExample(line string) (token, bool) {
|
|
||||||
if m := exampleLineRegexp.FindStringSubmatch(line); m != nil {
|
|
||||||
return token{"example", len(m[1]), m[3], m}, true
|
|
||||||
}
|
|
||||||
return nilToken, false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d *Document) parseExample(i int, parentStop stopFn) (int, Node) {
|
|
||||||
example, start := Example{}, i
|
|
||||||
for ; !parentStop(d, i) && d.tokens[i].kind == "example"; i++ {
|
|
||||||
example.Children = append(example.Children, Text{d.tokens[i].content, true})
|
|
||||||
}
|
|
||||||
return i - start, example
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue