2021-05-04 20:26:41 +00:00
|
|
|
package zord_tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-06-27 07:37:02 +00:00
|
|
|
"bytes"
|
2023-06-26 21:44:32 +00:00
|
|
|
"crypto/md5"
|
2022-08-01 14:02:25 +00:00
|
|
|
"errors"
|
2021-05-11 22:10:18 +00:00
|
|
|
"fmt"
|
2023-06-26 21:44:32 +00:00
|
|
|
"io"
|
2021-05-04 20:26:41 +00:00
|
|
|
"io/fs"
|
2023-06-27 07:37:02 +00:00
|
|
|
"net/url"
|
2021-05-04 20:26:41 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2021-05-11 22:10:18 +00:00
|
|
|
"path/filepath"
|
2023-06-27 07:37:02 +00:00
|
|
|
"regexp"
|
2021-05-04 20:26:41 +00:00
|
|
|
"strings"
|
2024-01-09 07:43:24 +00:00
|
|
|
|
|
|
|
"g.arns.lt/zordsdavini/abcex"
|
|
|
|
"golang.org/x/exp/slices"
|
2021-05-04 20:26:41 +00:00
|
|
|
)
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
type Config struct {
|
|
|
|
ReadableFormats []string
|
|
|
|
AttachmentDirName string
|
|
|
|
CustomMeta map[string]func() string
|
|
|
|
Excludes []string
|
|
|
|
}
|
|
|
|
|
2021-05-04 20:26:41 +00:00
|
|
|
type File struct {
|
2021-05-12 05:17:43 +00:00
|
|
|
Id string
|
|
|
|
Name string
|
|
|
|
FullPath string
|
|
|
|
Category []string
|
|
|
|
Tags []string
|
|
|
|
Meta map[string]string
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Tree struct {
|
2021-05-12 05:17:43 +00:00
|
|
|
Path string
|
|
|
|
Dirs []Tree
|
|
|
|
Files []File
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2022-08-01 14:02:25 +00:00
|
|
|
func (t Tree) FileById(id string) (File, error) {
|
|
|
|
for _, f := range t.Files {
|
|
|
|
if f.Id == id {
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, t2 := range t.Dirs {
|
|
|
|
f, err := t2.FileById(id)
|
|
|
|
if err == nil {
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return File{}, errors.New("file was not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t Tree) Slice(path string) (Tree, error) {
|
|
|
|
if t.Path == path {
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t2 := range t.Dirs {
|
|
|
|
t3, err := t2.Slice(path)
|
|
|
|
if err == nil {
|
|
|
|
return t3, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Tree{}, errors.New("tree was not found")
|
|
|
|
}
|
|
|
|
|
2022-08-30 16:39:40 +00:00
|
|
|
func (t Tree) Filter(filter map[string][]string) (Tree, bool) {
|
2022-08-01 14:02:25 +00:00
|
|
|
filtered := Tree{}
|
|
|
|
filtered.Path = t.Path
|
|
|
|
found := false
|
|
|
|
|
|
|
|
for _, f := range t.Files {
|
|
|
|
addFile := false
|
2022-08-30 16:39:40 +00:00
|
|
|
for option, values := range filter {
|
|
|
|
for _, value := range values {
|
|
|
|
if option == "tag" {
|
|
|
|
for _, tag := range f.Tags {
|
|
|
|
if tag == value {
|
|
|
|
addFile = true
|
|
|
|
}
|
2022-08-01 14:02:25 +00:00
|
|
|
}
|
2022-08-30 16:39:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.Contains(f.Meta[option], value) {
|
|
|
|
addFile = true
|
2022-08-01 14:02:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if addFile {
|
|
|
|
found = true
|
|
|
|
filtered.Files = append(filtered.Files, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t2 := range t.Dirs {
|
|
|
|
filteredChild, foundChild := t2.Filter(filter)
|
|
|
|
if foundChild {
|
|
|
|
found = true
|
|
|
|
filtered.Dirs = append(filtered.Dirs, filteredChild)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered, found
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
func NewConfig(
|
|
|
|
readableFormats []string,
|
|
|
|
attachmentDirName string,
|
|
|
|
customMeta map[string]func() string,
|
|
|
|
excludes []string,
|
|
|
|
) Config {
|
|
|
|
return Config{
|
|
|
|
ReadableFormats: readableFormats,
|
|
|
|
AttachmentDirName: attachmentDirName,
|
|
|
|
CustomMeta: customMeta,
|
|
|
|
Excludes: excludes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BuildTree(dirPath string, meta []string, config Config) (Tree, error) {
|
|
|
|
return readPath(dirPath, []string{}, meta, config)
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
func PopulateTree(sourcePath string, meta []string, config Config) error {
|
2023-06-27 07:37:02 +00:00
|
|
|
var err error
|
|
|
|
var attachmentRegistry map[string]string
|
2023-06-26 21:44:32 +00:00
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
attachmentRegistry, err = moveAttachments(sourcePath, config)
|
2023-06-26 21:44:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
err = fixFormat(sourcePath, attachmentRegistry, config)
|
2022-05-08 09:33:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-08 20:19:55 +00:00
|
|
|
var id int64 = 0
|
|
|
|
id, err = getMaxId(sourcePath)
|
2021-05-11 22:10:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id++
|
|
|
|
err = addMissingId(sourcePath, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
err = addMissingMeta(sourcePath, meta, config.CustomMeta)
|
2021-05-11 22:10:18 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
func moveAttachments(dir string, config Config) (map[string]string, error) {
|
2023-06-26 21:44:32 +00:00
|
|
|
attachmentRegistry := make(map[string]string)
|
|
|
|
|
|
|
|
err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error {
|
2022-05-08 09:33:40 +00:00
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
clearDir := strings.TrimPrefix(dir, "./")
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
for _, pattern := range config.Excludes {
|
|
|
|
matched, err := regexp.Match(pattern, []byte(fullPath))
|
|
|
|
if err == nil && matched {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsRegular() &&
|
|
|
|
!slices.Contains(config.ReadableFormats, path.Ext(fullPath)) &&
|
|
|
|
!strings.HasPrefix(fullPath, fmt.Sprintf("%s/%s", clearDir, config.AttachmentDirName)) {
|
2023-06-26 21:44:32 +00:00
|
|
|
f, err := os.Open(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
h := md5.New()
|
|
|
|
if _, err := io.Copy(h, f); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
_ = os.Mkdir(fmt.Sprintf("%s/%s", dir, config.AttachmentDirName), 0755)
|
|
|
|
|
|
|
|
attachmentRegistry[fullPath] = fmt.Sprintf(
|
|
|
|
"%s/%x%s",
|
|
|
|
config.AttachmentDirName,
|
|
|
|
h.Sum(nil),
|
|
|
|
path.Ext(fullPath),
|
|
|
|
)
|
|
|
|
newPath := fmt.Sprintf(
|
|
|
|
"%s/%s/%x%s",
|
|
|
|
dir,
|
|
|
|
config.AttachmentDirName,
|
|
|
|
h.Sum(nil),
|
|
|
|
path.Ext(fullPath),
|
|
|
|
)
|
2023-06-26 21:44:32 +00:00
|
|
|
err = os.Rename(fullPath, newPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return attachmentRegistry, err
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
func fixFormat(dir string, attachmentRegistry map[string]string, config Config) error {
|
2023-06-26 21:44:32 +00:00
|
|
|
err := filepath.Walk(dir, func(fullPath string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
if info.Mode().IsRegular() && slices.Contains(config.ReadableFormats, path.Ext(fullPath)) {
|
2023-06-26 21:44:32 +00:00
|
|
|
osf, err := os.Open(fullPath)
|
2022-05-08 09:33:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove all empty lines and separate split line
|
|
|
|
content := ""
|
|
|
|
format := true
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == "---" {
|
|
|
|
format = false
|
|
|
|
}
|
|
|
|
|
2022-05-08 20:19:55 +00:00
|
|
|
if format {
|
2022-05-08 09:33:40 +00:00
|
|
|
line = strings.Trim(line, " ")
|
2022-05-08 20:19:55 +00:00
|
|
|
if line != "" {
|
2022-05-08 09:33:40 +00:00
|
|
|
content = content + "\n" + line
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
content = content + "\n" + line
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 07:37:02 +00:00
|
|
|
|
|
|
|
// fix attachments
|
|
|
|
data := []byte(content)
|
|
|
|
re := regexp.MustCompile(`!?\[([^\]*]*)\]\(([^\) ]*)\)`)
|
|
|
|
|
|
|
|
for _, match := range re.FindAllSubmatch(data, -1) {
|
|
|
|
_, err := url.ParseRequestURI(string(match[2][:]))
|
|
|
|
if err != nil {
|
2024-01-09 07:43:24 +00:00
|
|
|
aPath := path.Clean(
|
|
|
|
fmt.Sprintf("%s/%s", path.Dir(fullPath), string(match[2][:])),
|
|
|
|
)
|
2023-06-27 07:37:02 +00:00
|
|
|
if _, ok := attachmentRegistry[aPath]; ok {
|
2023-06-27 08:25:32 +00:00
|
|
|
link := fmt.Sprintf("![%s](%s)", match[1], attachmentRegistry[aPath])
|
2023-06-27 07:37:02 +00:00
|
|
|
data = bytes.Replace(data, match[0], []byte(link), 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
err = os.WriteFile(fullPath, data, 0644)
|
2022-05-08 09:33:40 +00:00
|
|
|
|
|
|
|
// format split line
|
2024-01-09 07:43:24 +00:00
|
|
|
b, err := os.ReadFile(fullPath) // just pass the file name
|
2022-05-08 21:58:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-08 09:33:40 +00:00
|
|
|
str := string(b)
|
|
|
|
str = strings.Replace(str, "\n---\n", "\n\n---\n", 1)
|
2024-01-09 07:43:24 +00:00
|
|
|
err = os.WriteFile(fullPath, []byte(str), 0644)
|
2022-05-08 09:33:40 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:02:25 +00:00
|
|
|
func addMissingMeta(dir string, meta []string, customMeta map[string]func() string) error {
|
2021-05-11 22:10:18 +00:00
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
check := map[string]bool{}
|
|
|
|
for _, option := range meta {
|
|
|
|
check[option] = false
|
|
|
|
}
|
|
|
|
|
|
|
|
osf, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
for _, option := range meta {
|
2022-07-30 06:36:59 +00:00
|
|
|
if strings.HasPrefix(line, "* "+option+":") {
|
2021-05-11 22:10:18 +00:00
|
|
|
check[option] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if line == "---" {
|
|
|
|
for option, process := range check {
|
|
|
|
if !process {
|
2022-08-01 14:02:25 +00:00
|
|
|
defaultValue := ""
|
|
|
|
if _, ok := customMeta[option]; ok {
|
|
|
|
defaultValue = customMeta[option]()
|
|
|
|
}
|
|
|
|
err = addMeta(path, option, defaultValue)
|
2021-05-11 22:10:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-05-08 20:19:55 +00:00
|
|
|
return err
|
2021-05-11 22:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addMissingId(dir string, id int64) error {
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
osf, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2022-08-01 14:16:56 +00:00
|
|
|
if strings.HasPrefix(line, "* id:") {
|
2021-05-11 22:10:18 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if line == "---" {
|
|
|
|
err = addMeta(path, "id", abcex.Encode(id))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
id++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-08 20:19:55 +00:00
|
|
|
return nil
|
2021-05-11 22:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addMeta(path string, option string, value string) error {
|
2024-01-09 07:43:24 +00:00
|
|
|
b, err := os.ReadFile(path) // just pass the file name
|
2021-05-11 22:10:18 +00:00
|
|
|
if err != nil {
|
2022-05-08 20:19:55 +00:00
|
|
|
return err
|
2021-05-11 22:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
str := string(b)
|
2022-07-30 06:36:59 +00:00
|
|
|
str = strings.Replace(str, "\n\n---\n", fmt.Sprintf("\n* %s: %s\n\n---\n", option, value), 1)
|
2024-01-09 07:43:24 +00:00
|
|
|
err = os.WriteFile(path, []byte(str), 0644)
|
2021-05-11 22:10:18 +00:00
|
|
|
|
2022-05-08 20:19:55 +00:00
|
|
|
return err
|
2021-05-11 22:10:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getMaxId(dir string) (int64, error) {
|
|
|
|
var max int64 = 0
|
|
|
|
err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.Mode().IsRegular() {
|
|
|
|
osf, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == "---" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-08-01 14:16:56 +00:00
|
|
|
if strings.HasPrefix(line, "* id:") {
|
|
|
|
line = strings.TrimPrefix(line, "* id:")
|
2021-05-11 22:10:18 +00:00
|
|
|
i := abcex.Decode(strings.Trim(line, " "))
|
|
|
|
if i > max {
|
|
|
|
max = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return max, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return max, nil
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
func readPath(dirPath string, category []string, meta []string, config Config) (Tree, error) {
|
|
|
|
clearDir := strings.TrimPrefix(dirPath, "./")
|
2021-05-04 20:26:41 +00:00
|
|
|
|
2023-06-26 21:44:32 +00:00
|
|
|
tree := Tree{}
|
|
|
|
tree.Path = clearDir
|
2021-05-04 20:26:41 +00:00
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
files, err := os.ReadDir(dirPath)
|
2021-05-04 20:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
FILE_LOOP:
|
2021-05-04 20:26:41 +00:00
|
|
|
for _, file := range files {
|
2023-06-28 21:05:26 +00:00
|
|
|
if strings.HasPrefix(file.Name(), ".") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-05-04 20:26:41 +00:00
|
|
|
fullPath := path.Join(dirPath, file.Name())
|
|
|
|
if file.IsDir() {
|
2024-01-09 07:43:24 +00:00
|
|
|
if strings.HasPrefix(
|
|
|
|
fullPath,
|
|
|
|
fmt.Sprintf("%s/%s", clearDir, config.AttachmentDirName),
|
|
|
|
) {
|
2023-06-26 21:44:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
nextDir, err := readPath(fullPath, append(category, file.Name()), meta, config)
|
2021-05-04 20:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
2021-05-12 05:17:43 +00:00
|
|
|
tree.Dirs = append(tree.Dirs, nextDir)
|
2021-05-04 20:26:41 +00:00
|
|
|
continue
|
2024-01-09 07:43:24 +00:00
|
|
|
} else {
|
|
|
|
for _, pattern := range config.Excludes {
|
|
|
|
matched, err := regexp.Match(pattern, []byte(fullPath))
|
|
|
|
if err == nil && matched {
|
|
|
|
continue FILE_LOOP
|
|
|
|
}
|
|
|
|
}
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
_, err := os.ReadFile(fullPath)
|
2021-05-04 20:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
2021-05-09 18:17:07 +00:00
|
|
|
nextFile, err := readFile(file, fullPath, category, meta)
|
2021-05-04 20:26:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return tree, err
|
|
|
|
}
|
2021-05-12 05:17:43 +00:00
|
|
|
tree.Files = append(tree.Files, nextFile)
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tree, nil
|
|
|
|
}
|
|
|
|
|
2024-01-09 07:43:24 +00:00
|
|
|
func readFile(file fs.DirEntry, fullPath string, category []string, meta []string) (File, error) {
|
2021-05-04 20:26:41 +00:00
|
|
|
f := File{
|
2021-05-12 05:17:43 +00:00
|
|
|
Name: file.Name(),
|
|
|
|
FullPath: fullPath,
|
|
|
|
Category: category,
|
|
|
|
Meta: map[string]string{},
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
osf, err := os.Open(fullPath)
|
|
|
|
if err != nil {
|
|
|
|
return File{}, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == "---" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-07-30 06:36:59 +00:00
|
|
|
if strings.HasPrefix(line, "* tags:") {
|
|
|
|
line = strings.TrimPrefix(line, "* tags:")
|
2021-05-04 20:26:41 +00:00
|
|
|
t := strings.Split(line, ",")
|
|
|
|
tags := []string{}
|
|
|
|
for _, tag := range t {
|
|
|
|
tags = append(tags, strings.Trim(tag, " "))
|
|
|
|
}
|
|
|
|
|
2021-05-12 05:17:43 +00:00
|
|
|
f.Tags = tags
|
|
|
|
}
|
2022-07-30 06:36:59 +00:00
|
|
|
if strings.HasPrefix(line, "* id:") {
|
|
|
|
line = strings.TrimPrefix(line, "* id:")
|
2021-05-12 05:17:43 +00:00
|
|
|
f.Id = strings.Trim(line, " ")
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
2021-05-09 18:17:07 +00:00
|
|
|
for _, option := range meta {
|
2022-07-30 06:36:59 +00:00
|
|
|
if strings.HasPrefix(line, "* "+option) {
|
|
|
|
line = strings.TrimPrefix(line, "* "+option+":")
|
2021-05-12 05:17:43 +00:00
|
|
|
f.Meta[option] = strings.Trim(line, " ")
|
2021-05-09 18:17:07 +00:00
|
|
|
}
|
2021-05-04 20:26:41 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-08 20:19:55 +00:00
|
|
|
_ = osf.Close()
|
2021-05-04 20:26:41 +00:00
|
|
|
|
|
|
|
return f, nil
|
|
|
|
}
|
2022-05-08 09:33:40 +00:00
|
|
|
|
|
|
|
func ReadFileContent(file File) (string, error) {
|
2022-05-08 20:19:55 +00:00
|
|
|
osf, err := os.Open(file.FullPath)
|
2022-05-08 09:33:40 +00:00
|
|
|
if err != nil {
|
2022-05-08 20:19:55 +00:00
|
|
|
return "", err
|
2022-05-08 09:33:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
content := ""
|
2022-05-08 21:58:00 +00:00
|
|
|
separator := ""
|
2022-05-08 09:33:40 +00:00
|
|
|
removeEmptyLine := true
|
|
|
|
isMetaPart := true
|
|
|
|
scanner := bufio.NewScanner(osf)
|
|
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
if line == "---" {
|
|
|
|
isMetaPart = false
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-08 20:19:55 +00:00
|
|
|
if isMetaPart {
|
2022-05-08 09:33:40 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-05-08 20:19:55 +00:00
|
|
|
if removeEmptyLine {
|
2022-05-08 09:33:40 +00:00
|
|
|
line = strings.Trim(line, " ")
|
2022-05-08 20:19:55 +00:00
|
|
|
if line == "" {
|
2022-05-08 09:33:40 +00:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
removeEmptyLine = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-08 21:58:00 +00:00
|
|
|
content = content + separator + line
|
|
|
|
if separator == "" {
|
|
|
|
separator = "\n"
|
|
|
|
}
|
2022-05-08 09:33:40 +00:00
|
|
|
}
|
2022-05-08 20:19:55 +00:00
|
|
|
_ = osf.Close()
|
2022-05-08 09:33:40 +00:00
|
|
|
|
|
|
|
return content, nil
|
|
|
|
}
|