add "-" before meta to avoid format errors

This commit is contained in:
Arnas Udovicius 2022-07-28 22:59:58 +03:00
parent d87aa732a4
commit 069e8598c5
6 changed files with 30 additions and 30 deletions

View file

@ -37,16 +37,16 @@ Text
### Meta data ### 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 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. to `destination` directory.
### File structure ### File structure
``` ```
tags: linux,command,video - tags: linux,command,video
meta1: example - meta1: example
meta2: some long description - meta2: some long description
--- ---

View file

@ -1,7 +1,7 @@
tags: t1,t2 - tags: t1,t2
description: Če test file __va__ - description: Če test file __va__
option1: test option - option1: test option
--- ---
# Če kažkas torietom būtė # Če kažkas torietom būtė

View file

@ -1,8 +1,8 @@
tags: t1 - tags: t1
description: Če test 2 - description: Če test 2
--- ---
# Če kažkas torietom būtė 2 # Če kažkas torietom būtė 2
ale nie ale nie

View file

@ -1,6 +1,6 @@
tags: t1 - tags: t1
description: Če test 3 - description: Če test 3
option2: it exists - option2: it exists
--- ---

16
tree.go
View file

@ -131,7 +131,7 @@ func addMissingMeta(dir string, meta []string) error {
for scanner.Scan() { for scanner.Scan() {
line := scanner.Text() line := scanner.Text()
for _, option := range meta { for _, option := range meta {
if strings.HasPrefix(line, option+":") { if strings.HasPrefix(line, "- "+option+":") {
check[option] = true check[option] = true
} }
} }
@ -200,7 +200,7 @@ func addMeta(path string, option string, value string) error {
} }
str := string(b) 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) err = ioutil.WriteFile(path, []byte(str), 0644)
return err return err
@ -299,8 +299,8 @@ func readFile(file fs.FileInfo, fullPath string, category []string, meta []strin
break break
} }
if strings.HasPrefix(line, "tags:") { if strings.HasPrefix(line, "- tags:") {
line = strings.TrimPrefix(line, "tags:") line = strings.TrimPrefix(line, "- tags:")
t := strings.Split(line, ",") t := strings.Split(line, ",")
tags := []string{} tags := []string{}
for _, tag := range t { for _, tag := range t {
@ -309,13 +309,13 @@ func readFile(file fs.FileInfo, fullPath string, category []string, meta []strin
f.Tags = tags f.Tags = tags
} }
if strings.HasPrefix(line, "id:") { if strings.HasPrefix(line, "- id:") {
line = strings.TrimPrefix(line, "id:") line = strings.TrimPrefix(line, "- id:")
f.Id = strings.Trim(line, " ") f.Id = strings.Trim(line, " ")
} }
for _, option := range meta { for _, option := range meta {
if strings.HasPrefix(line, option) { if strings.HasPrefix(line, "- "+option) {
line = strings.TrimPrefix(line, option+":") line = strings.TrimPrefix(line, "- "+option+":")
f.Meta[option] = strings.Trim(line, " ") f.Meta[option] = strings.Trim(line, " ")
} }
} }

View file

@ -70,7 +70,7 @@ func TestFixFormat(t *testing.T) {
} }
str := string(b) str := string(b)
t.Log(str) 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'.") t.Fatal("Empty lines has not been removed in file 'file1.md'.")
} }
if !strings.Contains(str, "\n\n---\n") { if !strings.Contains(str, "\n\n---\n") {
@ -124,7 +124,7 @@ func TestId(t *testing.T) {
t.Fatal("No destination file 'file1.md'.") t.Fatal("No destination file 'file1.md'.")
} }
str := string(b) 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'.") 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'.") t.Fatal("No destination file 'substring/file3.md'.")
} }
str = string(b) 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'.") t.Fatal("No id in file 'file3.md'.")
} }
} }
@ -160,10 +160,10 @@ func TestMissingOptions(t *testing.T) {
t.Fatal("No destination file 'file1.md'.") t.Fatal("No destination file 'file1.md'.")
} }
str := string(b) 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'.") 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'.") 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'.") t.Fatal("No destination file 'file2.md'.")
} }
str = string(b) 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'.") 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'.") 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'.") t.Fatal("No destination file 'substring/file3.md'.")
} }
str = string(b) 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'.") 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'.") t.Fatal("Changed old value for 'option2' in file 'file3.md'.")
} }
} }