arns-lt-tree-push-service/formatter.go

74 lines
1.8 KiB
Go
Raw Normal View History

2022-08-02 05:16:11 +00:00
package main
2022-08-17 11:32:20 +00:00
import (
2022-08-20 22:34:53 +00:00
zordTree "g.arns.lt/zordsdavini/zord-tree"
2022-08-17 11:32:20 +00:00
"strings"
)
2022-08-02 05:16:11 +00:00
2022-08-20 22:34:53 +00:00
func FormatTreeSummery(tree zordTree.Tree) Tree {
2023-06-28 21:10:54 +00:00
path := strings.Replace(tree.Path, source, "", 1)
2022-08-20 22:34:53 +00:00
pathParts := strings.Split(path, "/")
rootPath := strings.Join(pathParts[:len(pathParts)-1], "/")
ts := Tree{RootPath: rootPath, Path: path}
2022-08-17 11:32:20 +00:00
readTree(tree, &ts, true)
2022-08-02 05:16:11 +00:00
return ts
}
2022-08-20 22:34:53 +00:00
func readTree(tree zordTree.Tree, ts *Tree, isRoot bool) {
2022-08-02 05:16:11 +00:00
for _, subtree := range tree.Dirs {
2022-08-17 11:32:20 +00:00
readTree(subtree, ts, false)
category := strings.Replace(subtree.Path, tree.Path, "", 1)
category = strings.Replace(category, "/", "", 1)
if isRoot {
2022-08-19 22:03:00 +00:00
if ts.Categories == nil {
ts.Categories = make(map[string]int32)
}
if _, ok := ts.Categories[category]; ok {
ts.Categories[category]++
} else {
ts.Categories[category] = 1
}
2022-08-17 11:32:20 +00:00
}
2022-08-02 05:16:11 +00:00
}
for _, file := range tree.Files {
2022-08-19 22:03:00 +00:00
newFile := TreeFile{}
populateTreeFileData(&newFile, file)
ts.Files = append(ts.Files, &newFile)
2022-08-20 20:44:07 +00:00
if isRoot {
ts.RootFiles = append(ts.RootFiles, &newFile)
}
2022-08-02 05:16:11 +00:00
for _, tag := range file.Tags {
2022-08-19 22:03:00 +00:00
if ts.Tags == nil {
ts.Tags = make(map[string]int32)
}
if _, ok := ts.Tags[tag]; ok {
ts.Tags[tag]++
} else {
ts.Tags[tag] = 1
}
2022-08-02 05:16:11 +00:00
}
}
}
2022-08-20 22:34:53 +00:00
func populateTreeFileData(newFile *TreeFile, file zordTree.File) {
2022-08-19 22:03:00 +00:00
newFile.Name = file.Name
newFile.Category = file.Category
newFile.Description = file.Meta["description"]
newFile.Id = file.Id
newFile.Lang = file.Meta["lang"]
newFile.Created = file.Meta["created"]
newFile.Copyright = file.Meta["copyright"]
newFile.Tags = file.Tags
}
2022-08-20 22:34:53 +00:00
func GetContent(file zordTree.File) FileContent {
content, _ := zordTree.ReadFileContent(file)
2022-08-19 22:03:00 +00:00
newFile := TreeFile{}
populateTreeFileData(&newFile, file)
return FileContent{File: &newFile, Content: content}
2022-08-02 05:16:11 +00:00
}