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

74 lines
1.8 KiB
Go

package main
import (
zordTree "g.arns.lt/zordsdavini/zord-tree"
"strings"
)
func FormatTreeSummery(tree zordTree.Tree) Tree {
path := strings.Replace(tree.Path, source, "", 1)
pathParts := strings.Split(path, "/")
rootPath := strings.Join(pathParts[:len(pathParts)-1], "/")
ts := Tree{RootPath: rootPath, Path: path}
readTree(tree, &ts, true)
return ts
}
func readTree(tree zordTree.Tree, ts *Tree, isRoot bool) {
for _, subtree := range tree.Dirs {
readTree(subtree, ts, false)
category := strings.Replace(subtree.Path, tree.Path, "", 1)
category = strings.Replace(category, "/", "", 1)
if isRoot {
if ts.Categories == nil {
ts.Categories = make(map[string]int32)
}
if _, ok := ts.Categories[category]; ok {
ts.Categories[category]++
} else {
ts.Categories[category] = 1
}
}
}
for _, file := range tree.Files {
newFile := TreeFile{}
populateTreeFileData(&newFile, file)
ts.Files = append(ts.Files, &newFile)
if isRoot {
ts.RootFiles = append(ts.RootFiles, &newFile)
}
for _, tag := range file.Tags {
if ts.Tags == nil {
ts.Tags = make(map[string]int32)
}
if _, ok := ts.Tags[tag]; ok {
ts.Tags[tag]++
} else {
ts.Tags[tag] = 1
}
}
}
}
func populateTreeFileData(newFile *TreeFile, file zordTree.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
}
func GetContent(file zordTree.File) FileContent {
content, _ := zordTree.ReadFileContent(file)
newFile := TreeFile{}
populateTreeFileData(&newFile, file)
return FileContent{File: &newFile, Content: content}
}