From f0ffa7826602e678d3168aa71e7f14892ffaefef Mon Sep 17 00:00:00 2001 From: Arnas Udovic Date: Wed, 28 Jun 2023 23:49:01 +0300 Subject: [PATCH] leave only source, destination removed --- README.md | 10 ++++++---- tree.go | 8 +------- tree_test.go | 35 +++++++++++------------------------ 3 files changed, 18 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 83976a2..814c222 100644 --- a/README.md +++ b/README.md @@ -3,11 +3,12 @@ [![Build Status](https://drone.arns.lt/api/badges/zordsdavini/zord-tree/status.svg)](https://drone.arns.lt/zordsdavini/zord-tree) Library for golang to build articles tree from file system. The file should be in specific format to hold meta data, id and -content. +content. For now it supports markdown format. Process should go in two steps: * populate existing `source` directory with formatting files, add missing `id` and meta data +* separate images and other data files to `__a` root directory * build `Tree` to operate on object for your app * read file content without meta data by path @@ -40,8 +41,9 @@ Text ### Meta data 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. +During populating tree it adds missing `id` in `abcex` format and empty meta data lines and saves in `source` directory. + +Additionally it moves images and other not `.md` files to separate `__a` root directory and replace in content linked urls. There is possibility to add custom default value. That is passed with custom function what returns string. @@ -60,7 +62,7 @@ Hear goes content. It can be written in html, markdown or whatever what can be p ## Usage -There are two main commands: `PopulateTree` to prepare source (format, add metadata and add Id) and copy from source to destination and `BuildTree` to get object of tree. +There are two main commands: `PopulateTree` to prepare source (format, add metadata and add Id) and separate images and other data files and `BuildTree` to get object of tree. `Tree` object has methods: `FileById` to get `File` by Id, `Slice` to get sub-tree of given path and `Filter` to filter tree by filter. Filter contains array of meta key and searching value. `tag` key is searched as equal and other meta values of keys can contain part. diff --git a/tree.go b/tree.go index c7469dc..a553d56 100644 --- a/tree.go +++ b/tree.go @@ -7,7 +7,6 @@ import ( "errors" "fmt" "g.arns.lt/zordsdavini/abcex" - cp "github.com/otiai10/copy" "golang.org/x/exp/slices" "io" "io/fs" @@ -113,7 +112,7 @@ func BuildTree(dirPath string, meta []string) (Tree, error) { return readPath(dirPath, []string{}, meta) } -func PopulateTree(sourcePath string, destPath string, meta []string, customMeta map[string]func() string) error { +func PopulateTree(sourcePath string, meta []string, customMeta map[string]func() string) error { var err error var attachmentRegistry map[string]string @@ -145,11 +144,6 @@ func PopulateTree(sourcePath string, destPath string, meta []string, customMeta } err = addMissingMeta(sourcePath, meta, customMeta) - if err != nil { - return err - } - - err = cp.Copy(sourcePath, destPath) return err } diff --git a/tree_test.go b/tree_test.go index ee16652..eca71c2 100644 --- a/tree_test.go +++ b/tree_test.go @@ -12,14 +12,11 @@ func prepare(t *testing.T) { err := os.RemoveAll("./testdata/sunny1") if err != nil { } - err = os.RemoveAll("./testdata/sunny2") - if err != nil { - } err = cp.Copy("./testdata/sunny", "./testdata/sunny1") if err != nil { t.Fatal("Couldn't prepare data for testing") } - err = PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) + err = PopulateTree("./testdata/sunny1", []string{}, nil) if err != nil { t.Fatal("Population ended in err:", err) } @@ -63,12 +60,7 @@ func TestSunny(t *testing.T) { func TestFixFormat(t *testing.T) { prepare(t) - err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) - if err != nil { - t.Fatal("Population ended in err:", err) - } - - b, err := ioutil.ReadFile("./testdata/sunny2/file1.md") + b, err := ioutil.ReadFile("./testdata/sunny1/file1.md") if err != nil { t.Fatal("No destination file 'file1.md'.") } @@ -104,12 +96,7 @@ func TestMeta(t *testing.T) { func TestId(t *testing.T) { prepare(t) - err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) - if err != nil { - t.Fatal("Population ended in err:", err) - } - - b, err := ioutil.ReadFile("./testdata/sunny2/file1.md") + b, err := ioutil.ReadFile("./testdata/sunny1/file1.md") if err != nil { t.Fatal("No destination file 'file1.md'.") } @@ -118,7 +105,7 @@ func TestId(t *testing.T) { t.Fatal("No id in file 'file1.md'.") } - b, err = ioutil.ReadFile("./testdata/sunny2/subcategory/file3.md") + b, err = ioutil.ReadFile("./testdata/sunny1/subcategory/file3.md") if err != nil { t.Fatal("No destination file 'substring/file3.md'.") } @@ -135,12 +122,12 @@ func TestMissingOptions(t *testing.T) { customMeta["option2"] = func() string { return "customDefaultValue" } - err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{"option1", "option2"}, customMeta) + err := PopulateTree("./testdata/sunny1", []string{"option1", "option2"}, customMeta) if err != nil { t.Fatal("Population ended in err:", err) } - b, err := ioutil.ReadFile("./testdata/sunny2/file1.md") + b, err := ioutil.ReadFile("./testdata/sunny1/file1.md") if err != nil { t.Fatal("No destination file 'file1.md'.") } @@ -152,7 +139,7 @@ func TestMissingOptions(t *testing.T) { t.Fatal("'option2' has not been added to file 'file1.md'.") } - b, err = ioutil.ReadFile("./testdata/sunny2/file2.md") + b, err = ioutil.ReadFile("./testdata/sunny1/file2.md") if err != nil { t.Fatal("No destination file 'file2.md'.") } @@ -164,7 +151,7 @@ func TestMissingOptions(t *testing.T) { t.Fatal("'option2' has not been added to file 'file2.md'.") } - b, err = ioutil.ReadFile("./testdata/sunny2/subcategory/file3.md") + b, err = ioutil.ReadFile("./testdata/sunny1/subcategory/file3.md") if err != nil { t.Fatal("No destination file 'substring/file3.md'.") } @@ -199,12 +186,12 @@ func TestReadingFileContent(t *testing.T) { func TestGetSlice(t *testing.T) { prepare(t) - tree, err := BuildTree("./testdata/sunny2", []string{}) + tree, err := BuildTree("./testdata/sunny1", []string{}) if err != nil { t.Errorf("Got error: %v", err) } - tree2, err := tree.Slice("testdata/sunny2/subcategory") + tree2, err := tree.Slice("testdata/sunny1/subcategory") if err != nil { t.Errorf("Error during search the tree: %v", err) } @@ -216,7 +203,7 @@ func TestGetSlice(t *testing.T) { func TestGetFileById(t *testing.T) { prepare(t) - tree, err := BuildTree("./testdata/sunny2", []string{}) + tree, err := BuildTree("./testdata/sunny1", []string{}) if err != nil { t.Errorf("Got error: %v", err) }