wiki/util.go
Peter Stuifzand 1003c19662
Some checks failed
continuous-integration/drone/push Build is failing
Use block ids to deduplicate backref lines
2020-07-01 00:49:42 +02:00

90 lines
1.6 KiB
Go

package main
import (
"bufio"
"log"
"math/rand"
"regexp"
"strings"
"time"
)
type ParsedLink struct {
Id string
Name string
PageName string
Line string
}
var random *rand.Rand
func init() {
random = rand.New(rand.NewSource(time.Now().UnixNano()))
}
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*\]\])`)
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})
}
}
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())
}