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

78 lines
1.9 KiB
Go
Raw Normal View History

2022-08-02 08:16:11 +03:00
package main
2022-08-17 14:32:20 +03:00
import (
"strings"
2025-03-16 14:24:27 +02:00
zordTree "g.arns.lt/zordsdavini/zord-tree"
2022-08-17 14:32:20 +03:00
)
2022-08-02 08:16:11 +03:00
2022-08-21 01:34:53 +03:00
func FormatTreeSummery(tree zordTree.Tree) Tree {
2023-06-29 00:10:54 +03:00
path := strings.Replace(tree.Path, source, "", 1)
2022-08-21 01:34:53 +03:00
pathParts := strings.Split(path, "/")
rootPath := strings.Join(pathParts[:len(pathParts)-1], "/")
ts := Tree{RootPath: rootPath, Path: path}
2022-08-17 14:32:20 +03:00
readTree(tree, &ts, true)
2022-08-02 08:16:11 +03:00
return ts
}
2022-08-21 01:34:53 +03:00
func readTree(tree zordTree.Tree, ts *Tree, isRoot bool) {
2022-08-02 08:16:11 +03:00
for _, subtree := range tree.Dirs {
2022-08-17 14:32:20 +03:00
category := strings.Replace(subtree.Path, tree.Path, "", 1)
category = strings.Replace(category, "/", "", 1)
if isRoot {
2022-08-20 01:03:00 +03:00
if ts.Categories == nil {
ts.Categories = make(map[string]int32)
}
2025-03-16 14:24:27 +02:00
ts.Categories[category] = int32(len(subtree.Files))
2025-03-16 14:51:43 +02:00
} else {
2025-03-16 15:12:45 +02:00
for _, subcategory := range subtree.Files[0].Category {
if _, ok := ts.Categories[subcategory]; ok {
ts.Categories[subcategory] += int32(len(subtree.Files))
2025-03-16 14:51:43 +02:00
}
}
2022-08-17 14:32:20 +03:00
}
2025-03-16 14:51:43 +02:00
readTree(subtree, ts, false)
2022-08-02 08:16:11 +03:00
}
for _, file := range tree.Files {
2022-08-20 01:03:00 +03:00
newFile := TreeFile{}
populateTreeFileData(&newFile, file)
ts.Files = append(ts.Files, &newFile)
2022-08-20 23:44:07 +03:00
if isRoot {
ts.RootFiles = append(ts.RootFiles, &newFile)
}
2022-08-02 08:16:11 +03:00
for _, tag := range file.Tags {
2022-08-20 01:03:00 +03: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 08:16:11 +03:00
}
}
}
2022-08-21 01:34:53 +03:00
func populateTreeFileData(newFile *TreeFile, file zordTree.File) {
2022-08-20 01:03:00 +03: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-21 01:34:53 +03:00
func GetContent(file zordTree.File) FileContent {
content, _ := zordTree.ReadFileContent(file)
2022-08-20 01:03:00 +03:00
newFile := TreeFile{}
populateTreeFileData(&newFile, file)
return FileContent{File: &newFile, Content: content}
2022-08-02 08:16:11 +03:00
}