leave only source, destination removed
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Arnas Udovic 2023-06-28 23:49:01 +03:00
parent b1501ba23d
commit f0ffa78266
3 changed files with 18 additions and 35 deletions

View file

@ -3,11 +3,12 @@
[![Build Status](https://drone.arns.lt/api/badges/zordsdavini/zord-tree/status.svg)](https://drone.arns.lt/zordsdavini/zord-tree) [![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 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: Process should go in two steps:
* populate existing `source` directory with formatting files, add missing `id` and meta data * 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 * build `Tree` to operate on object for your app
* read file content without meta data by path * read file content without meta data by path
@ -40,8 +41,9 @@ 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.
to `destination` 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. 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 ## 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. `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. Filter contains array of meta key and searching value. `tag` key is searched as equal and other meta values of keys can contain part.

View file

@ -7,7 +7,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"g.arns.lt/zordsdavini/abcex" "g.arns.lt/zordsdavini/abcex"
cp "github.com/otiai10/copy"
"golang.org/x/exp/slices" "golang.org/x/exp/slices"
"io" "io"
"io/fs" "io/fs"
@ -113,7 +112,7 @@ func BuildTree(dirPath string, meta []string) (Tree, error) {
return readPath(dirPath, []string{}, meta) 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 err error
var attachmentRegistry map[string]string var attachmentRegistry map[string]string
@ -145,11 +144,6 @@ func PopulateTree(sourcePath string, destPath string, meta []string, customMeta
} }
err = addMissingMeta(sourcePath, meta, customMeta) err = addMissingMeta(sourcePath, meta, customMeta)
if err != nil {
return err
}
err = cp.Copy(sourcePath, destPath)
return err return err
} }

View file

@ -12,14 +12,11 @@ func prepare(t *testing.T) {
err := os.RemoveAll("./testdata/sunny1") err := os.RemoveAll("./testdata/sunny1")
if err != nil { if err != nil {
} }
err = os.RemoveAll("./testdata/sunny2")
if err != nil {
}
err = cp.Copy("./testdata/sunny", "./testdata/sunny1") err = cp.Copy("./testdata/sunny", "./testdata/sunny1")
if err != nil { if err != nil {
t.Fatal("Couldn't prepare data for testing") 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 { if err != nil {
t.Fatal("Population ended in err:", err) t.Fatal("Population ended in err:", err)
} }
@ -63,12 +60,7 @@ func TestSunny(t *testing.T) {
func TestFixFormat(t *testing.T) { func TestFixFormat(t *testing.T) {
prepare(t) prepare(t)
err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) b, err := ioutil.ReadFile("./testdata/sunny1/file1.md")
if err != nil {
t.Fatal("Population ended in err:", err)
}
b, err := ioutil.ReadFile("./testdata/sunny2/file1.md")
if err != nil { if err != nil {
t.Fatal("No destination file 'file1.md'.") t.Fatal("No destination file 'file1.md'.")
} }
@ -104,12 +96,7 @@ func TestMeta(t *testing.T) {
func TestId(t *testing.T) { func TestId(t *testing.T) {
prepare(t) prepare(t)
err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{}, nil) b, err := ioutil.ReadFile("./testdata/sunny1/file1.md")
if err != nil {
t.Fatal("Population ended in err:", err)
}
b, err := ioutil.ReadFile("./testdata/sunny2/file1.md")
if err != nil { if err != nil {
t.Fatal("No destination file 'file1.md'.") t.Fatal("No destination file 'file1.md'.")
} }
@ -118,7 +105,7 @@ func TestId(t *testing.T) {
t.Fatal("No id in file 'file1.md'.") 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 { if err != nil {
t.Fatal("No destination file 'substring/file3.md'.") t.Fatal("No destination file 'substring/file3.md'.")
} }
@ -135,12 +122,12 @@ func TestMissingOptions(t *testing.T) {
customMeta["option2"] = func() string { customMeta["option2"] = func() string {
return "customDefaultValue" return "customDefaultValue"
} }
err := PopulateTree("./testdata/sunny1", "./testdata/sunny2", []string{"option1", "option2"}, customMeta) err := PopulateTree("./testdata/sunny1", []string{"option1", "option2"}, customMeta)
if err != nil { if err != nil {
t.Fatal("Population ended in err:", err) 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 { if err != nil {
t.Fatal("No destination file 'file1.md'.") 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'.") 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 { if err != nil {
t.Fatal("No destination file 'file2.md'.") 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'.") 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 { if err != nil {
t.Fatal("No destination file 'substring/file3.md'.") t.Fatal("No destination file 'substring/file3.md'.")
} }
@ -199,12 +186,12 @@ func TestReadingFileContent(t *testing.T) {
func TestGetSlice(t *testing.T) { func TestGetSlice(t *testing.T) {
prepare(t) prepare(t)
tree, err := BuildTree("./testdata/sunny2", []string{}) tree, err := BuildTree("./testdata/sunny1", []string{})
if err != nil { if err != nil {
t.Errorf("Got error: %v", err) t.Errorf("Got error: %v", err)
} }
tree2, err := tree.Slice("testdata/sunny2/subcategory") tree2, err := tree.Slice("testdata/sunny1/subcategory")
if err != nil { if err != nil {
t.Errorf("Error during search the tree: %v", err) t.Errorf("Error during search the tree: %v", err)
} }
@ -216,7 +203,7 @@ func TestGetSlice(t *testing.T) {
func TestGetFileById(t *testing.T) { func TestGetFileById(t *testing.T) {
prepare(t) prepare(t)
tree, err := BuildTree("./testdata/sunny2", []string{}) tree, err := BuildTree("./testdata/sunny1", []string{})
if err != nil { if err != nil {
t.Errorf("Got error: %v", err) t.Errorf("Got error: %v", err)
} }