Peter Stuifzand
e2f3656474
All checks were successful
continuous-integration/drone/push Build is passing
139 lines
2.7 KiB
Go
139 lines
2.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"math/rand"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
var (
|
|
MetaKV = regexp.MustCompile(`(\w+)::\s+(.*)`)
|
|
)
|
|
|
|
type ParsedLink struct {
|
|
ID string `json:"ID"`
|
|
Name string `json:"title"`
|
|
PageName string `json:"name"`
|
|
Line string `json:"line"`
|
|
Href string `json:"href"`
|
|
}
|
|
|
|
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
func RandStringBytes(n int) string {
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = letterBytes[rand.Intn(len(letterBytes))]
|
|
}
|
|
return string(b)
|
|
}
|
|
|
|
func ParseLinks(blockId string, content string) ([]ParsedLink, error) {
|
|
hrefRE := regexp.MustCompile(`(#?\[\[\s*([^\]]+)\s*\]\])`)
|
|
keywordsRE := regexp.MustCompile(`(\w+)::`)
|
|
|
|
scanner := bufio.NewScanner(strings.NewReader(content))
|
|
scanner.Split(bufio.ScanLines)
|
|
|
|
var result []ParsedLink
|
|
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
|
|
links := hrefRE.FindAllStringSubmatch(line, -1)
|
|
|
|
for _, matches := range links {
|
|
link := matches[0]
|
|
|
|
if link[0] == '#' {
|
|
link = strings.TrimPrefix(link, "#[[")
|
|
} else {
|
|
link = strings.TrimPrefix(link, "[[")
|
|
}
|
|
|
|
link = strings.TrimSuffix(link, "]]")
|
|
link = strings.TrimSpace(link)
|
|
l := cleanNameURL(link)
|
|
result = append(result, ParsedLink{blockId, link, l, line, ""})
|
|
}
|
|
|
|
keywords := keywordsRE.FindAllStringSubmatch(line, -1)
|
|
for _, matches := range keywords {
|
|
link := matches[1]
|
|
l := cleanNameURL(link)
|
|
result = append(result, ParsedLink{blockId, link, l, line, ""})
|
|
}
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
func cleanNameURL(name string) string {
|
|
return strings.Replace(name, " ", "_", -1)
|
|
}
|
|
|
|
func cleanTitle(name string) string {
|
|
return strings.Replace(name, "_", " ", -1)
|
|
}
|
|
|
|
type stopwatch struct {
|
|
start time.Time
|
|
label string
|
|
}
|
|
|
|
func (sw *stopwatch) Start(label string) {
|
|
sw.start = time.Now()
|
|
sw.label = label
|
|
}
|
|
|
|
func (sw *stopwatch) Stop() {
|
|
endTime := time.Now()
|
|
d := endTime.Sub(sw.start)
|
|
log.Printf("%-20s: %s\n", sw.label, d.String())
|
|
}
|
|
|
|
func todayPage() string {
|
|
now := time.Now()
|
|
months := []string{
|
|
"",
|
|
"januari",
|
|
"februari",
|
|
"maart",
|
|
"april",
|
|
"mei",
|
|
"juni",
|
|
"juli",
|
|
"augustus",
|
|
"september",
|
|
"oktober",
|
|
"november",
|
|
"december",
|
|
}
|
|
return fmt.Sprintf("%d_%s_%d", now.Day(), months[now.Month()], now.Year())
|
|
}
|
|
|
|
func PageTitle(pageText string) (string, error) {
|
|
var listItems []struct {
|
|
Indented int
|
|
Text string
|
|
}
|
|
err := json.NewDecoder(strings.NewReader(pageText)).Decode(&listItems)
|
|
if err != nil {
|
|
return "", fmt.Errorf("while decoding page text: %w", err)
|
|
}
|
|
|
|
for _, li := range listItems {
|
|
if matches := MetaKV.FindStringSubmatch(li.Text); matches != nil {
|
|
if matches[1] == "Title" {
|
|
return matches[2], nil
|
|
}
|
|
}
|
|
}
|
|
return "", fmt.Errorf("no meta title found in page text")
|
|
}
|