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

71 lines
1.6 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 (
zord_tree "g.arns.lt/zordsdavini/zord-tree"
"strings"
)
2022-08-02 05:16:11 +00:00
2022-08-19 22:03:00 +00:00
func FormatTreeSummery(tree zord_tree.Tree) Tree {
ts := Tree{}
2022-08-17 11:32:20 +00:00
readTree(tree, &ts, true)
2022-08-02 05:16:11 +00:00
return ts
}
2022-08-19 22:03:00 +00:00
func readTree(tree zord_tree.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-19 22:03:00 +00:00
func populateTreeFileData(newFile *TreeFile, file zord_tree.File) {
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-02 05:16:11 +00:00
func GetContent(file zord_tree.File) FileContent {
content, _ := zord_tree.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
}