add "-" before meta to avoid format errors
This commit is contained in:
parent
d87aa732a4
commit
069e8598c5
6 changed files with 30 additions and 30 deletions
|
@ -37,16 +37,16 @@ Text
|
|||
|
||||
### Meta data
|
||||
|
||||
Meta data separated by lines and in format `name: value`. By default library supports `tags` as comma separated array.
|
||||
Meta data separated by lines and in format `- name: value`. By default library supports `tags` as comma separated array.
|
||||
During populating tree it adds missing `id` in `abcex` format and empty meta data lines and saves in `source` directory then copy
|
||||
to `destination` directory.
|
||||
|
||||
### File structure
|
||||
|
||||
```
|
||||
tags: linux,command,video
|
||||
meta1: example
|
||||
meta2: some long description
|
||||
- tags: linux,command,video
|
||||
- meta1: example
|
||||
- meta2: some long description
|
||||
|
||||
---
|
||||
|
||||
|
|
6
testdata/sunny/file1.md
vendored
6
testdata/sunny/file1.md
vendored
|
@ -1,7 +1,7 @@
|
|||
tags: t1,t2
|
||||
- tags: t1,t2
|
||||
|
||||
description: Če test file __va__
|
||||
option1: test option
|
||||
- description: Če test file __va__
|
||||
- option1: test option
|
||||
---
|
||||
|
||||
# Če kažkas torietom būtė
|
||||
|
|
4
testdata/sunny/file2.md
vendored
4
testdata/sunny/file2.md
vendored
|
@ -1,5 +1,5 @@
|
|||
tags: t1
|
||||
description: Če test 2
|
||||
- tags: t1
|
||||
- description: Če test 2
|
||||
|
||||
---
|
||||
|
||||
|
|
6
testdata/sunny/subcategory/file3.md
vendored
6
testdata/sunny/subcategory/file3.md
vendored
|
@ -1,6 +1,6 @@
|
|||
tags: t1
|
||||
description: Če test 3
|
||||
option2: it exists
|
||||
- tags: t1
|
||||
- description: Če test 3
|
||||
- option2: it exists
|
||||
|
||||
---
|
||||
|
||||
|
|
16
tree.go
16
tree.go
|
@ -131,7 +131,7 @@ func addMissingMeta(dir string, meta []string) error {
|
|||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
for _, option := range meta {
|
||||
if strings.HasPrefix(line, option+":") {
|
||||
if strings.HasPrefix(line, "- "+option+":") {
|
||||
check[option] = true
|
||||
}
|
||||
}
|
||||
|
@ -200,7 +200,7 @@ func addMeta(path string, option string, value string) error {
|
|||
}
|
||||
|
||||
str := string(b)
|
||||
str = strings.Replace(str, "\n\n---\n", fmt.Sprintf("\n%s: %s\n\n---\n", option, value), 1)
|
||||
str = strings.Replace(str, "\n\n---\n", fmt.Sprintf("\n- %s: %s\n\n---\n", option, value), 1)
|
||||
err = ioutil.WriteFile(path, []byte(str), 0644)
|
||||
|
||||
return err
|
||||
|
@ -299,8 +299,8 @@ func readFile(file fs.FileInfo, fullPath string, category []string, meta []strin
|
|||
break
|
||||
}
|
||||
|
||||
if strings.HasPrefix(line, "tags:") {
|
||||
line = strings.TrimPrefix(line, "tags:")
|
||||
if strings.HasPrefix(line, "- tags:") {
|
||||
line = strings.TrimPrefix(line, "- tags:")
|
||||
t := strings.Split(line, ",")
|
||||
tags := []string{}
|
||||
for _, tag := range t {
|
||||
|
@ -309,13 +309,13 @@ func readFile(file fs.FileInfo, fullPath string, category []string, meta []strin
|
|||
|
||||
f.Tags = tags
|
||||
}
|
||||
if strings.HasPrefix(line, "id:") {
|
||||
line = strings.TrimPrefix(line, "id:")
|
||||
if strings.HasPrefix(line, "- id:") {
|
||||
line = strings.TrimPrefix(line, "- id:")
|
||||
f.Id = strings.Trim(line, " ")
|
||||
}
|
||||
for _, option := range meta {
|
||||
if strings.HasPrefix(line, option) {
|
||||
line = strings.TrimPrefix(line, option+":")
|
||||
if strings.HasPrefix(line, "- "+option) {
|
||||
line = strings.TrimPrefix(line, "- "+option+":")
|
||||
f.Meta[option] = strings.Trim(line, " ")
|
||||
}
|
||||
}
|
||||
|
|
18
tree_test.go
18
tree_test.go
|
@ -70,7 +70,7 @@ func TestFixFormat(t *testing.T) {
|
|||
}
|
||||
str := string(b)
|
||||
t.Log(str)
|
||||
if !strings.Contains(str, "tags: t1,t2\ndescription: Če test file __va__\n") {
|
||||
if !strings.Contains(str, "- tags: t1,t2\n- description: Če test file __va__\n") {
|
||||
t.Fatal("Empty lines has not been removed in file 'file1.md'.")
|
||||
}
|
||||
if !strings.Contains(str, "\n\n---\n") {
|
||||
|
@ -124,7 +124,7 @@ func TestId(t *testing.T) {
|
|||
t.Fatal("No destination file 'file1.md'.")
|
||||
}
|
||||
str := string(b)
|
||||
if !strings.Contains(str, "\nid: 1\n") {
|
||||
if !strings.Contains(str, "\n- id: 1\n") {
|
||||
t.Fatal("No id in file 'file1.md'.")
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ func TestId(t *testing.T) {
|
|||
t.Fatal("No destination file 'substring/file3.md'.")
|
||||
}
|
||||
str = string(b)
|
||||
if !strings.Contains(str, "\nid: 3\n") {
|
||||
if !strings.Contains(str, "\n- id: 3\n") {
|
||||
t.Fatal("No id in file 'file3.md'.")
|
||||
}
|
||||
}
|
||||
|
@ -160,10 +160,10 @@ func TestMissingOptions(t *testing.T) {
|
|||
t.Fatal("No destination file 'file1.md'.")
|
||||
}
|
||||
str := string(b)
|
||||
if !strings.Contains(str, "\noption1: test option\n") {
|
||||
if !strings.Contains(str, "\n- option1: test option\n") {
|
||||
t.Fatal("Changed old value for 'option1' in file 'file1.md'.")
|
||||
}
|
||||
if !strings.Contains(str, "\noption2: \n") {
|
||||
if !strings.Contains(str, "\n- option2: \n") {
|
||||
t.Fatal("'option2' has not been added to file 'file1.md'.")
|
||||
}
|
||||
|
||||
|
@ -172,10 +172,10 @@ func TestMissingOptions(t *testing.T) {
|
|||
t.Fatal("No destination file 'file2.md'.")
|
||||
}
|
||||
str = string(b)
|
||||
if !strings.Contains(str, "\noption1: \n") {
|
||||
if !strings.Contains(str, "\n- option1: \n") {
|
||||
t.Fatal("'option1' has not been added to file 'file2.md'.")
|
||||
}
|
||||
if !strings.Contains(str, "\noption2: \n") {
|
||||
if !strings.Contains(str, "\n- option2: \n") {
|
||||
t.Fatal("'option2' has not been added to file 'file2.md'.")
|
||||
}
|
||||
|
||||
|
@ -184,10 +184,10 @@ func TestMissingOptions(t *testing.T) {
|
|||
t.Fatal("No destination file 'substring/file3.md'.")
|
||||
}
|
||||
str = string(b)
|
||||
if !strings.Contains(str, "\noption1: \n") {
|
||||
if !strings.Contains(str, "\n- option1: \n") {
|
||||
t.Fatal("'option1' has not been added to file 'file3.md'.")
|
||||
}
|
||||
if !strings.Contains(str, "\noption2: it exists\n") {
|
||||
if !strings.Contains(str, "\n- option2: it exists\n") {
|
||||
t.Fatal("Changed old value for 'option2' in file 'file3.md'.")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue