Restructure directory layout: org subpackage

This commit is contained in:
Niklas Fasching 2018-12-02 16:52:27 +01:00
parent 6c683dfbdb
commit fc982125c9
17 changed files with 11 additions and 7 deletions

69
org/headline.go Normal file
View file

@ -0,0 +1,69 @@
package org
import (
"regexp"
"strings"
"unicode"
)
type Headline struct {
Lvl int
Status string
Priority string
Title []Node
Tags []string
Children []Node
}
var headlineRegexp = regexp.MustCompile(`^([*]+)\s+(.*)`)
var tagRegexp = regexp.MustCompile(`(.*?)\s*(:[A-Za-z0-9@#%:]+:\s*$)`)
func lexHeadline(line string) (token, bool) {
if m := headlineRegexp.FindStringSubmatch(line); m != nil {
return token{"headline", 0, m[2], m}, true
}
return nilToken, false
}
func (d *Document) todoKeywords() []string {
return strings.FieldsFunc(d.Get("TODO"), func(r rune) bool {
return unicode.IsSpace(r) || r == '|'
})
}
func (d *Document) parseHeadline(i int, parentStop stopFn) (int, Node) {
t, headline := d.tokens[i], Headline{}
headline.Lvl = len(t.matches[1])
text := t.content
for _, k := range d.todoKeywords() {
if strings.HasPrefix(text, k) && len(text) > len(k) && unicode.IsSpace(rune(text[len(k)])) {
headline.Status = k
text = text[len(k)+1:]
break
}
}
if len(text) >= 3 && text[0:2] == "[#" && strings.Contains("ABC", text[2:3]) && text[3] == ']' {
headline.Priority = text[2:3]
text = strings.TrimSpace(text[4:])
}
if m := tagRegexp.FindStringSubmatch(text); m != nil {
text = m[1]
headline.Tags = strings.FieldsFunc(m[2], func(r rune) bool { return r == ':' })
}
headline.Title = d.parseInline(text)
stop := func(d *Document, i int) bool {
return parentStop(d, i) || d.tokens[i].kind == "headline" && d.tokens[i].lvl <= headline.Lvl
}
consumed, nodes := d.parseMany(i+1, stop)
headline.Children = nodes
if headline.Lvl == 1 && text == d.Footnotes.Title && d.Footnotes.ExcludeHeading {
return consumed + 1, nil
}
return consumed + 1, headline
}