add meta
This commit is contained in:
parent
91163b7b0e
commit
1710a3cf21
3 changed files with 39 additions and 17 deletions
1
testdata/sunny/file1.md
vendored
1
testdata/sunny/file1.md
vendored
|
@ -1,5 +1,6 @@
|
|||
tags: t1,t2
|
||||
description: Če test file __va__
|
||||
option1: test option
|
||||
|
||||
---
|
||||
|
||||
|
|
27
tree.go
27
tree.go
|
@ -16,8 +16,6 @@ type File struct {
|
|||
category []string
|
||||
tags []string
|
||||
meta map[string]string
|
||||
description string
|
||||
lang string
|
||||
}
|
||||
|
||||
type Tree struct {
|
||||
|
@ -26,11 +24,11 @@ type Tree struct {
|
|||
files []File
|
||||
}
|
||||
|
||||
func BuildTree(dirPath string) (Tree, error) {
|
||||
return readPath(dirPath, []string{})
|
||||
func BuildTree(dirPath string, meta []string) (Tree, error) {
|
||||
return readPath(dirPath, []string{}, meta)
|
||||
}
|
||||
|
||||
func readPath( dirPath string, category []string ) (Tree, error) {
|
||||
func readPath( dirPath string, category []string, meta []string) (Tree, error) {
|
||||
tree := Tree{}
|
||||
|
||||
tree.path = dirPath
|
||||
|
@ -43,7 +41,7 @@ func readPath( dirPath string, category []string ) (Tree, error) {
|
|||
for _, file := range files {
|
||||
fullPath := path.Join(dirPath, file.Name())
|
||||
if file.IsDir() {
|
||||
nextDir, err := readPath(fullPath, append(category, file.Name()))
|
||||
nextDir, err := readPath(fullPath, append(category, file.Name()), meta)
|
||||
if err != nil {
|
||||
return tree, err
|
||||
}
|
||||
|
@ -55,7 +53,7 @@ func readPath( dirPath string, category []string ) (Tree, error) {
|
|||
if err != nil {
|
||||
return tree, err
|
||||
}
|
||||
nextFile, err := readFile(file, fullPath, category)
|
||||
nextFile, err := readFile(file, fullPath, category, meta)
|
||||
if err != nil {
|
||||
return tree, err
|
||||
}
|
||||
|
@ -65,11 +63,12 @@ func readPath( dirPath string, category []string ) (Tree, error) {
|
|||
return tree, nil
|
||||
}
|
||||
|
||||
func readFile(file fs.FileInfo, fullPath string, category []string) (File, error) {
|
||||
func readFile(file fs.FileInfo, fullPath string, category []string, meta []string) (File, error) {
|
||||
f := File{
|
||||
name: file.Name(),
|
||||
fullPath: fullPath,
|
||||
category: category,
|
||||
meta: map[string]string{},
|
||||
}
|
||||
|
||||
osf, err := os.Open(fullPath)
|
||||
|
@ -94,13 +93,11 @@ func readFile(file fs.FileInfo, fullPath string, category []string) (File, error
|
|||
|
||||
f.tags = tags
|
||||
}
|
||||
if strings.HasPrefix(line, "description:") {
|
||||
line = strings.TrimPrefix(line, "description:")
|
||||
f.description = strings.Trim(line, " ")
|
||||
}
|
||||
if strings.HasPrefix(line, "lang:") {
|
||||
line = strings.TrimPrefix(line, "lang:")
|
||||
f.lang = strings.Trim(line, " ")
|
||||
for _, option := range meta {
|
||||
if strings.HasPrefix(line, option) {
|
||||
line = strings.TrimPrefix(line, option + ":")
|
||||
f.meta[option] = strings.Trim(line, " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
osf.Close()
|
||||
|
|
28
tree_test.go
28
tree_test.go
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func TestFromNotExistingDirectory(t *testing.T) {
|
||||
_, err := BuildTree("./testing/i_dont_exist")
|
||||
_, err := BuildTree("./testing/i_dont_exist", []string{})
|
||||
if err == nil {
|
||||
t.Error("Not existing directory should return error, got NIL.")
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ func TestSunny(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal("Couldn't prepare data for testing")
|
||||
}
|
||||
tree, err := BuildTree("./testdata/sunny1")
|
||||
tree, err := BuildTree("./testdata/sunny1", []string{})
|
||||
if err != nil {
|
||||
t.Errorf("Got error: %v", err)
|
||||
}
|
||||
|
@ -43,4 +43,28 @@ func TestSunny(t *testing.T) {
|
|||
t.Errorf("File 'file1.md' 2nd tag 't2', got %v", tree.files[0].tags[1])
|
||||
}
|
||||
t.Log(tree)
|
||||
}
|
||||
|
||||
func TestMeta(t *testing.T) {
|
||||
err := os.Remove("./testdata/sunny1")
|
||||
if err != nil {
|
||||
}
|
||||
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
||||
if err != nil {
|
||||
t.Fatal("Couldn't prepare data for testing")
|
||||
}
|
||||
meta := []string{"option1", "option2"}
|
||||
tree, err := BuildTree("./testdata/sunny1", meta)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Got error: %v", err)
|
||||
}
|
||||
|
||||
if tree.files[0].name != "file1.md" {
|
||||
t.Errorf("File should be 'file1.md', got %v", tree.files[0].name)
|
||||
}
|
||||
|
||||
if tree.files[0].meta["option1"] != "test option" {
|
||||
t.Error("File 'file1.md' should have 'option1' with value 'test option'")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue