70 lines
1.6 KiB
Go
70 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
zord_tree "g.arns.lt/zordsdavini/zord-tree"
|
|
"strings"
|
|
)
|
|
|
|
func FormatTreeSummery(tree zord_tree.Tree) Tree {
|
|
ts := Tree{}
|
|
readTree(tree, &ts, true)
|
|
|
|
return ts
|
|
}
|
|
|
|
func readTree(tree zord_tree.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 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
|
|
}
|
|
|
|
func GetContent(file zord_tree.File) FileContent {
|
|
content, _ := zord_tree.ReadFileContent(file)
|
|
newFile := TreeFile{}
|
|
populateTreeFileData(&newFile, file)
|
|
|
|
return FileContent{File: &newFile, Content: content}
|
|
}
|