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

68 lines
1.4 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
type FSummery struct {
Name string
Category []string
Description string
Id string
Lang string
2022-08-17 11:32:20 +00:00
Created string
Copyright string
2022-08-02 05:16:11 +00:00
Tags []string
}
type TSummery struct {
2022-08-17 11:32:20 +00:00
Files []FSummery
Tags map[string]int
Categories map[string]int
2022-08-02 05:16:11 +00:00
}
type FileContent struct {
File zord_tree.File
Content string
}
func FormatTreeSummery(tree zord_tree.Tree) TSummery {
2022-08-17 11:32:20 +00:00
ts := TSummery{Tags: map[string]int{}, Categories: map[string]int{}}
readTree(tree, &ts, true)
2022-08-02 05:16:11 +00:00
return ts
}
2022-08-17 11:32:20 +00:00
func readTree(tree zord_tree.Tree, ts *TSummery, 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 {
ts.Categories[category]++
}
2022-08-02 05:16:11 +00:00
}
for _, file := range tree.Files {
ts.Files = append(ts.Files, FSummery{
Name: file.Name,
Category: file.Category,
Description: file.Meta["description"],
Id: file.Id,
Lang: file.Meta["lang"],
2022-08-17 11:32:20 +00:00
Created: file.Meta["created"],
Copyright: file.Meta["copyright"],
2022-08-02 05:16:11 +00:00
Tags: file.Tags,
})
for _, tag := range file.Tags {
ts.Tags[tag]++
}
}
}
func GetContent(file zord_tree.File) FileContent {
content, _ := zord_tree.ReadFileContent(file)
return FileContent{File: file, Content: content}
}