diff --git a/testdata/sunny/.fileRemove2.txt b/testdata/sunny/.fileRemove2.txt new file mode 100644 index 0000000..e69de29 diff --git a/testdata/sunny/.removableDir/.fileRemove1.txt b/testdata/sunny/.removableDir/.fileRemove1.txt new file mode 100644 index 0000000..e69de29 diff --git a/testdata/sunny/subcategory/file3.md b/testdata/sunny/subcategory/file3.md index 78414fe..996faa7 100644 --- a/testdata/sunny/subcategory/file3.md +++ b/testdata/sunny/subcategory/file3.md @@ -9,3 +9,8 @@ * vëns * do * trīs + +![nuruoda](test1.img) +![nuruoda2](../test0.img) +[nuruoda3](test1.img) +[url](http://test1.img) diff --git a/tree.go b/tree.go index 64d779c..6335cb8 100644 --- a/tree.go +++ b/tree.go @@ -2,6 +2,7 @@ package zord_tree import ( "bufio" + "bytes" "crypto/md5" "errors" "fmt" @@ -11,9 +12,11 @@ import ( "io" "io/fs" "io/ioutil" + "net/url" "os" "path" "path/filepath" + "regexp" "strings" ) @@ -111,8 +114,15 @@ func BuildTree(dirPath string, meta []string) (Tree, error) { } func PopulateTree(sourcePath string, destPath string, meta []string, customMeta map[string]func() string) error { + var err error + var attachmentRegistry map[string]string - attachmentRegistry, err := moveAttachments(sourcePath) + err = removeHidden(sourcePath) + if err != nil { + return err + } + + attachmentRegistry, err = moveAttachments(sourcePath) if err != nil { return err } @@ -144,6 +154,31 @@ func PopulateTree(sourcePath string, destPath string, meta []string, customMeta return err } +func removeHidden(dir string) error { + var toRemovePaths []string + + err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error { + if e != nil { + return e + } + + if strings.HasPrefix(info.Name(), ".") { + toRemovePaths = append(toRemovePaths, fullPath) + } + + return nil + }) + + for _, removePath := range toRemovePaths { + e := os.RemoveAll(removePath) + if e != nil { + return e + } + } + + return err +} + func moveAttachments(dir string) (map[string]string, error) { attachmentRegistry := make(map[string]string) @@ -213,7 +248,23 @@ func fixFormat(dir string, attachmentRegistry map[string]string) error { content = content + "\n" + line } } - err = ioutil.WriteFile(fullPath, []byte(content), 0644) + + // fix attachments + data := []byte(content) + re := regexp.MustCompile(`!?\[([^\]*]*)\]\(([^\) ]*)\)`) + + for _, match := range re.FindAllSubmatch(data, -1) { + _, err := url.ParseRequestURI(string(match[2][:])) + if err != nil { + aPath := path.Clean(fmt.Sprintf("%s/%s", path.Dir(fullPath), string(match[2][:]))) + if _, ok := attachmentRegistry[aPath]; ok { + link := fmt.Sprintf("(%s)[%s]", match[1], attachmentRegistry[aPath]) + data = bytes.Replace(data, match[0], []byte(link), 1) + } + } + } + + err = ioutil.WriteFile(fullPath, data, 0644) // format split line b, err := ioutil.ReadFile(fullPath) // just pass the file name diff --git a/tree_test.go b/tree_test.go index 9c5d62f..ee16652 100644 --- a/tree_test.go +++ b/tree_test.go @@ -9,10 +9,10 @@ import ( ) func prepare(t *testing.T) { - err := os.Remove("./testdata/sunny1") + err := os.RemoveAll("./testdata/sunny1") if err != nil { } - err = os.Remove("./testdata/sunny2") + err = os.RemoveAll("./testdata/sunny2") if err != nil { } err = cp.Copy("./testdata/sunny", "./testdata/sunny1")