Remove FrontMatter

After giving it some thought I don't think this belongs in go-org - there's
just no use for it outside of hugo front matter handling
This commit is contained in:
Niklas Fasching 2019-01-01 19:24:53 +01:00
parent 348f697b41
commit f62a00863f
2 changed files with 0 additions and 119 deletions

View file

@ -32,7 +32,6 @@ type Writer interface {
}
type Node interface{}
type FrontMatter = map[string]interface{}
type lexFn = func(line string) (t token, ok bool)
type parseFn = func(*Document, int, stopFn) (int, Node)
@ -60,16 +59,6 @@ var lexFns = []lexFn{
var nilToken = token{"nil", -1, "", nil}
func FrontMatterHandler(fm FrontMatter, k, v string) error {
switch k := strings.ToLower(k); k {
case "tags", "categories", "aliases":
fm[k] = strings.Fields(v)
default:
fm[k] = v
}
return nil
}
func NewDocument() *Document {
outlineSection := &Section{}
return &Document{
@ -134,31 +123,6 @@ func (d *Document) Silent() *Document {
return d
}
func GetFrontMatter(input io.Reader, f func(FrontMatter, string, string) error) (_ FrontMatter, err error) {
d := NewDocument()
defer func() {
if recovered := recover(); recovered != nil {
err = fmt.Errorf("could not parse input: %s", recovered)
}
}()
d.tokenize(input)
d.parseMany(0, func(d *Document, i int) bool {
if !(i < len(d.tokens)) {
return true
}
t := d.tokens[i]
return t.kind != "keyword" && !(t.kind == "text" && t.content == "")
})
frontMatter := make(FrontMatter, len(d.BufferSettings))
for k, v := range d.BufferSettings {
err := f(frontMatter, k, v)
if err != nil {
return nil, err
}
}
return frontMatter, err
}
func (d *Document) tokenize(input io.Reader) {
d.tokens = []token{}
scanner := bufio.NewScanner(input)

View file

@ -1,83 +0,0 @@
package org
import (
"reflect"
"strings"
"testing"
)
type frontMatterTest struct {
name string
input string
handler func(FrontMatter, string, string) error
expected map[string]interface{}
}
var frontMatterTests = []frontMatterTest{
{`basic`,
`#+TITLE: The Title`,
FrontMatterHandler,
map[string]interface{}{"title": "The Title"}},
{`empty`,
`* No frontmatter here`,
FrontMatterHandler,
map[string]interface{}{}},
{`custom handler`,
`
#+TITLE: The Title
#+TAGS: foo bar
`,
func(fm FrontMatter, k, v string) error {
switch k := strings.ToLower(k); k {
case "title":
fm[k] = "Thanks For All The Fish"
return nil
default:
return FrontMatterHandler(fm, k, v)
}
},
map[string]interface{}{
"title": "Thanks For All The Fish",
"tags": []string{"foo", "bar"},
}},
{`multiple + ignored keyword`,
`
#+TITLE: The Title
#+AUTHOR: The Author
#+OTHER: some other keyword
#+TAGS: this will become []string
#+ALIASES: foo bar
#+ALIASES: baz bam
#+categories: foo bar
something that's not a keyword or a text line without content
#+SUBTITLE: The Subtitle`,
FrontMatterHandler,
map[string]interface{}{
"title": "The Title",
"author": "The Author",
"other": "some other keyword",
"tags": []string{"this", "will", "become", "[]string"},
"aliases": []string{"foo", "bar", "baz", "bam"},
"categories": []string{"foo", "bar"},
},
},
}
func TestParseFrontMatter(t *testing.T) {
for _, test := range frontMatterTests {
actual, err := GetFrontMatter(strings.NewReader(test.input), test.handler)
if err != nil {
t.Errorf("%s\n got error: %s", test.name, err)
continue
}
if !reflect.DeepEqual(test.expected, actual) {
t.Errorf("%s\n got: %#v\nexpected: %#v\n%s'", test.name, actual, test.expected, test.input)
}
}
}