Ignore hidden dirs; replace attachment urls
This commit is contained in:
parent
e6907b9e06
commit
6f76d2efd6
5 changed files with 60 additions and 4 deletions
0
testdata/sunny/.fileRemove2.txt
vendored
Normal file
0
testdata/sunny/.fileRemove2.txt
vendored
Normal file
0
testdata/sunny/.removableDir/.fileRemove1.txt
vendored
Normal file
0
testdata/sunny/.removableDir/.fileRemove1.txt
vendored
Normal file
5
testdata/sunny/subcategory/file3.md
vendored
5
testdata/sunny/subcategory/file3.md
vendored
|
@ -9,3 +9,8 @@
|
||||||
* vëns
|
* vëns
|
||||||
* do
|
* do
|
||||||
* trīs
|
* trīs
|
||||||
|
|
||||||
|
![nuruoda](test1.img)
|
||||||
|
![nuruoda2](../test0.img)
|
||||||
|
[nuruoda3](test1.img)
|
||||||
|
[url](http://test1.img)
|
||||||
|
|
55
tree.go
55
tree.go
|
@ -2,6 +2,7 @@ package zord_tree
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
@ -11,9 +12,11 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -144,6 +154,31 @@ func PopulateTree(sourcePath string, destPath string, meta []string, customMeta
|
||||||
return err
|
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) {
|
func moveAttachments(dir string) (map[string]string, error) {
|
||||||
attachmentRegistry := make(map[string]string)
|
attachmentRegistry := make(map[string]string)
|
||||||
|
|
||||||
|
@ -213,7 +248,23 @@ func fixFormat(dir string, attachmentRegistry map[string]string) error {
|
||||||
content = content + "\n" + line
|
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
|
// format split line
|
||||||
b, err := ioutil.ReadFile(fullPath) // just pass the file name
|
b, err := ioutil.ReadFile(fullPath) // just pass the file name
|
||||||
|
|
|
@ -9,10 +9,10 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func prepare(t *testing.T) {
|
func prepare(t *testing.T) {
|
||||||
err := os.Remove("./testdata/sunny1")
|
err := os.RemoveAll("./testdata/sunny1")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
err = os.Remove("./testdata/sunny2")
|
err = os.RemoveAll("./testdata/sunny2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
}
|
}
|
||||||
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
|
||||||
|
|
Loading…
Reference in a new issue