Ignore not markdown files
This commit is contained in:
parent
c273b8fd96
commit
e6907b9e06
6 changed files with 73 additions and 20 deletions
1
go.mod
1
go.mod
|
@ -5,4 +5,5 @@ go 1.18
|
|||
require (
|
||||
g.arns.lt/zordsdavini/abcex v1.0.0
|
||||
github.com/otiai10/copy v1.6.0
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -7,3 +7,5 @@ github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6
|
|||
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
|
||||
github.com/otiai10/mint v1.3.2 h1:VYWnrP5fXmz1MXvjuUvcBrXSjGE6xjON+axB/UrpO3E=
|
||||
github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
|
||||
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
|
||||
|
|
0
testdata/sunny/subcategory/test1.img
vendored
Normal file
0
testdata/sunny/subcategory/test1.img
vendored
Normal file
0
testdata/sunny/test0.img
vendored
Normal file
0
testdata/sunny/test0.img
vendored
Normal file
75
tree.go
75
tree.go
|
@ -2,10 +2,13 @@ package zord_tree
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
"g.arns.lt/zordsdavini/abcex"
|
||||
cp "github.com/otiai10/copy"
|
||||
"golang.org/x/exp/slices"
|
||||
"io"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -29,6 +32,10 @@ type Tree struct {
|
|||
Files []File
|
||||
}
|
||||
|
||||
var readableFormats = []string{".md"}
|
||||
|
||||
const attachmentDirName = "__a"
|
||||
|
||||
func (t Tree) FileById(id string) (File, error) {
|
||||
for _, f := range t.Files {
|
||||
if f.Id == id {
|
||||
|
@ -104,7 +111,13 @@ func BuildTree(dirPath string, meta []string) (Tree, error) {
|
|||
}
|
||||
|
||||
func PopulateTree(sourcePath string, destPath string, meta []string, customMeta map[string]func() string) error {
|
||||
err := fixFormat(sourcePath)
|
||||
|
||||
attachmentRegistry, err := moveAttachments(sourcePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = fixFormat(sourcePath, attachmentRegistry)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -131,14 +144,51 @@ func PopulateTree(sourcePath string, destPath string, meta []string, customMeta
|
|||
return err
|
||||
}
|
||||
|
||||
func fixFormat(dir string) error {
|
||||
err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
||||
func moveAttachments(dir string) (map[string]string, error) {
|
||||
attachmentRegistry := make(map[string]string)
|
||||
|
||||
err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error {
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
if info.Mode().IsRegular() {
|
||||
osf, err := os.Open(path)
|
||||
var clearDir = strings.TrimPrefix(dir, "./")
|
||||
if info.Mode().IsRegular() && !slices.Contains(readableFormats, path.Ext(fullPath)) && !strings.HasPrefix(fullPath, fmt.Sprintf("%s/%s", clearDir, attachmentDirName)) {
|
||||
f, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
h := md5.New()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_ = os.Mkdir(fmt.Sprintf("%s/%s", dir, attachmentDirName), 0755)
|
||||
|
||||
attachmentRegistry[fullPath] = fmt.Sprintf("%s/%x%s", attachmentDirName, h.Sum(nil), path.Ext(fullPath))
|
||||
newPath := fmt.Sprintf("%s/%s/%x%s", dir, attachmentDirName, h.Sum(nil), path.Ext(fullPath))
|
||||
err = os.Rename(fullPath, newPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
return attachmentRegistry, err
|
||||
}
|
||||
|
||||
func fixFormat(dir string, attachmentRegistry map[string]string) error {
|
||||
err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error {
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
if info.Mode().IsRegular() && slices.Contains(readableFormats, path.Ext(fullPath)) {
|
||||
osf, err := os.Open(fullPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -163,17 +213,17 @@ func fixFormat(dir string) error {
|
|||
content = content + "\n" + line
|
||||
}
|
||||
}
|
||||
err = ioutil.WriteFile(path, []byte(content), 0644)
|
||||
err = ioutil.WriteFile(fullPath, []byte(content), 0644)
|
||||
|
||||
// format split line
|
||||
b, err := ioutil.ReadFile(path) // just pass the file name
|
||||
b, err := ioutil.ReadFile(fullPath) // just pass the file name
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
str := string(b)
|
||||
str = strings.Replace(str, "\n---\n", "\n\n---\n", 1)
|
||||
err = ioutil.WriteFile(path, []byte(str), 0644)
|
||||
err = ioutil.WriteFile(fullPath, []byte(str), 0644)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
@ -320,9 +370,10 @@ func getMaxId(dir string) (int64, error) {
|
|||
}
|
||||
|
||||
func readPath(dirPath string, category []string, meta []string) (Tree, error) {
|
||||
tree := Tree{}
|
||||
var clearDir = strings.TrimPrefix(dirPath, "./")
|
||||
|
||||
tree.Path = strings.TrimPrefix(dirPath, "./")
|
||||
tree := Tree{}
|
||||
tree.Path = clearDir
|
||||
|
||||
files, err := ioutil.ReadDir(dirPath)
|
||||
if err != nil {
|
||||
|
@ -332,6 +383,10 @@ func readPath(dirPath string, category []string, meta []string) (Tree, error) {
|
|||
for _, file := range files {
|
||||
fullPath := path.Join(dirPath, file.Name())
|
||||
if file.IsDir() {
|
||||
if strings.HasPrefix(fullPath, fmt.Sprintf("%s/%s", clearDir, attachmentDirName)) {
|
||||
continue
|
||||
}
|
||||
|
||||
nextDir, err := readPath(fullPath, append(category, file.Name()), meta)
|
||||
if err != nil {
|
||||
return tree, err
|
||||
|
|
15
tree_test.go
15
tree_test.go
|
@ -19,6 +19,11 @@ func prepare(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal("Couldn't prepare data for testing")
|
||||
}
|
||||
err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil)
|
||||
if err != nil {
|
||||
t.Fatal("Population ended in err:", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestFromNotExistingDirectory(t *testing.T) {
|
||||
|
@ -194,11 +199,6 @@ func TestReadingFileContent(t *testing.T) {
|
|||
func TestGetSlice(t *testing.T) {
|
||||
prepare(t)
|
||||
|
||||
err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil)
|
||||
if err != nil {
|
||||
t.Fatal("Population ended in err:", err)
|
||||
}
|
||||
|
||||
tree, err := BuildTree("./testdata/sunny2", []string{})
|
||||
if err != nil {
|
||||
t.Errorf("Got error: %v", err)
|
||||
|
@ -216,11 +216,6 @@ func TestGetSlice(t *testing.T) {
|
|||
func TestGetFileById(t *testing.T) {
|
||||
prepare(t)
|
||||
|
||||
err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil)
|
||||
if err != nil {
|
||||
t.Fatal("Population ended in err:", err)
|
||||
}
|
||||
|
||||
tree, err := BuildTree("./testdata/sunny2", []string{})
|
||||
if err != nil {
|
||||
t.Errorf("Got error: %v", err)
|
||||
|
|
Loading…
Reference in a new issue