diff --git a/testdata/sunny/file1.md b/testdata/sunny/file1.md index ec37c83..039c9b4 100644 --- a/testdata/sunny/file1.md +++ b/testdata/sunny/file1.md @@ -1,5 +1,6 @@ tags: t1,t2 description: Če test file __va__ +option1: test option --- diff --git a/tree.go b/tree.go index 768157f..7f1b9cc 100644 --- a/tree.go +++ b/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() diff --git a/tree_test.go b/tree_test.go index db739dd..c650a42 100644 --- a/tree_test.go +++ b/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'") + } } \ No newline at end of file