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

67 lines
1.2 KiB
Go
Raw Normal View History

2022-08-02 08:16:11 +03:00
package main
import (
2025-01-12 00:47:25 +02:00
"fmt"
2023-06-29 15:33:25 +03:00
"log"
2022-08-02 08:16:11 +03:00
"os"
2025-01-12 00:47:25 +02:00
"path"
"path/filepath"
"strings"
2024-01-10 08:34:50 +02:00
zord_tree "g.arns.lt/zordsdavini/zord-tree"
)
var config = zord_tree.NewConfig(
[]string{".md"},
"__a",
customMeta,
[]string{"\\.sync_.{12}\\.db"},
2025-01-12 00:47:25 +02:00
map[string]zord_tree.AppCallback{"gallery": gallery},
2022-08-02 08:16:11 +03:00
)
2025-01-12 00:47:25 +02:00
func gallery(dir string, content string, info os.FileInfo) (string, error) {
if info.Name() != "index.md" {
return content, nil
}
fullPath := path.Join(dir, info.Name())
_, _, fileMeta, err := zord_tree.GetFileParams(fullPath, meta)
if err != nil {
return content, err
}
if !strings.HasPrefix(fileMeta["app"], "gallery") {
return content, nil
}
entries, err := os.ReadDir(dir)
if err != nil {
return content, err
}
content += "\n"
for _, e := range entries {
if e.Name() != "index.md" {
content += fmt.Sprintf(
"![%s](%s)\n",
strings.TrimSuffix(e.Name(), filepath.Ext(e.Name())),
e.Name(),
)
}
}
return content, nil
}
2022-08-02 08:16:11 +03:00
func Collect() {
2023-06-29 15:33:25 +03:00
log.Println("Collect: start")
2022-08-02 08:16:11 +03:00
if _, err := os.Stat(source); os.IsNotExist(err) {
2023-06-29 15:33:25 +03:00
log.Print(err)
2022-08-02 08:16:11 +03:00
panic("Source directory does not exist.")
}
2023-06-29 15:33:25 +03:00
log.Println("Collect: pre")
2024-01-10 08:34:50 +02:00
_ = zord_tree.PopulateTree(source, meta, config)
2023-06-29 15:33:25 +03:00
log.Println("Collect: post")
2022-08-02 08:16:11 +03:00
}