2022-08-07 05:47:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-23 11:58:18 +00:00
|
|
|
"bytes"
|
2022-08-20 20:46:31 +00:00
|
|
|
"errors"
|
2022-08-23 11:58:18 +00:00
|
|
|
"fmt"
|
2022-08-20 20:46:31 +00:00
|
|
|
"net/url"
|
2022-08-23 12:56:17 +00:00
|
|
|
"regexp"
|
2022-08-07 05:47:15 +00:00
|
|
|
"sort"
|
2022-08-20 20:46:31 +00:00
|
|
|
"strings"
|
2022-08-07 05:47:15 +00:00
|
|
|
)
|
|
|
|
|
2022-08-23 10:20:07 +00:00
|
|
|
func (fileContent *FileContent) FormatContent() string {
|
|
|
|
content := fileContent.Content
|
2022-08-23 12:56:17 +00:00
|
|
|
content = strings.Trim(content, "/n")
|
|
|
|
|
2022-08-23 10:20:07 +00:00
|
|
|
badChars := make(map[string]string)
|
2022-08-23 10:39:25 +00:00
|
|
|
badChars["ā"] = "ā"
|
|
|
|
badChars["ē"] = "ē"
|
|
|
|
badChars["ī"] = "ī"
|
|
|
|
badChars["ō"] = "ō"
|
|
|
|
badChars["ū"] = "ū"
|
2022-08-23 10:20:07 +00:00
|
|
|
for k, v := range badChars {
|
|
|
|
content = strings.ReplaceAll(content, k, v)
|
|
|
|
}
|
|
|
|
|
2022-08-23 12:56:17 +00:00
|
|
|
var buffer []byte
|
|
|
|
data := []byte(content)
|
2025-01-11 23:26:06 +00:00
|
|
|
re := regexp.MustCompile(`!?\[([^\]*]*)\]\(([^ ]*?)\)`)
|
2022-08-23 12:56:17 +00:00
|
|
|
|
|
|
|
for i, match := range re.FindAllSubmatch(data, -1) {
|
2023-02-14 21:02:26 +00:00
|
|
|
if "!" == string(match[0][0:1]) {
|
2023-06-27 13:57:39 +00:00
|
|
|
uPath := string(match[2][:])
|
|
|
|
if strings.HasPrefix(uPath, "__a") {
|
|
|
|
uPath = "/" + uPath
|
|
|
|
}
|
|
|
|
link := fmt.Sprintf("=> %s %s\n", uPath, match[1])
|
2023-02-14 21:02:26 +00:00
|
|
|
data = bytes.Replace(data, match[0], []byte(link), 1)
|
|
|
|
} else {
|
|
|
|
replaceWithIndex := append(match[1], fmt.Sprintf("[%d]", i+1)...)
|
|
|
|
data = bytes.Replace(data, match[0], replaceWithIndex, 1)
|
|
|
|
// append entry to buffer to be added later
|
|
|
|
link := fmt.Sprintf("=> %s %d: %s\n", match[2], i+1, match[1])
|
|
|
|
buffer = append(buffer, link...)
|
|
|
|
}
|
2022-08-23 12:56:17 +00:00
|
|
|
}
|
|
|
|
// append links to that paragraph
|
|
|
|
if len(buffer) > 0 {
|
2023-02-14 14:52:14 +00:00
|
|
|
data = append(data, []byte("\n\n")...)
|
2022-08-23 12:56:17 +00:00
|
|
|
data = append(data, buffer...)
|
|
|
|
}
|
2022-08-23 11:58:18 +00:00
|
|
|
|
2022-08-23 12:56:17 +00:00
|
|
|
return string(data)
|
2022-08-23 10:20:07 +00:00
|
|
|
}
|
|
|
|
|
2022-08-20 20:46:31 +00:00
|
|
|
func (file *TreeFile) CategoriesAsUrl() string {
|
|
|
|
escapedCategories := []string{}
|
|
|
|
for _, c := range file.Category {
|
|
|
|
escapedCategories = append(escapedCategories, url.QueryEscape(c))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(escapedCategories, "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (file *TreeFile) GmiName() string {
|
|
|
|
return strings.Replace(file.Name, ".md", ".gmi", 1)
|
|
|
|
}
|
|
|
|
|
2022-08-20 22:33:27 +00:00
|
|
|
func (file *TreeFile) CategoryPath() string {
|
|
|
|
return "/" + strings.Join(file.Category, "/")
|
|
|
|
}
|
|
|
|
|
2022-08-20 20:46:31 +00:00
|
|
|
func (tree *Tree) GetIndexFile() (*TreeFile, error) {
|
|
|
|
for _, file := range tree.RootFiles {
|
|
|
|
if file.Name == "index.md" {
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("index file not found")
|
|
|
|
}
|
|
|
|
|
2022-08-19 22:03:57 +00:00
|
|
|
func GetLastFiles(files []*TreeFile) []*TreeFile {
|
|
|
|
sortingFiles := make(map[string]*TreeFile)
|
2022-08-07 05:47:15 +00:00
|
|
|
for _, f := range files {
|
2024-07-21 04:45:26 +00:00
|
|
|
sortingFiles[f.Created] = f
|
2022-08-07 05:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
keys := make([]string, 0, len(sortingFiles))
|
|
|
|
for k := range sortingFiles {
|
|
|
|
keys = append(keys, k)
|
|
|
|
}
|
2022-08-22 20:28:09 +00:00
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(keys)))
|
2022-08-07 05:47:15 +00:00
|
|
|
|
|
|
|
length := 10
|
|
|
|
if len(keys) < 10 {
|
|
|
|
length = len(keys)
|
|
|
|
}
|
2022-08-19 22:03:57 +00:00
|
|
|
lastFiles := make([]*TreeFile, 0, length)
|
2022-08-07 05:47:15 +00:00
|
|
|
for i, k := range keys {
|
|
|
|
if i == length {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
lastFiles = append(lastFiles, sortingFiles[k])
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastFiles
|
|
|
|
}
|