2018-11-24 12:34:51 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-05-16 20:15:36 +00:00
|
|
|
"bufio"
|
2020-06-30 20:56:12 +00:00
|
|
|
"log"
|
2018-11-24 12:34:51 +00:00
|
|
|
"math/rand"
|
2020-05-07 13:20:36 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-02-25 19:09:52 +00:00
|
|
|
"time"
|
2018-11-24 12:34:51 +00:00
|
|
|
)
|
|
|
|
|
2020-05-07 13:20:36 +00:00
|
|
|
type ParsedLink struct {
|
2020-06-30 22:55:33 +00:00
|
|
|
ID string `json:"Id"`
|
2020-05-07 13:20:36 +00:00
|
|
|
Name string
|
|
|
|
PageName string
|
2020-05-16 19:02:19 +00:00
|
|
|
Line string
|
2020-05-07 13:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 19:09:52 +00:00
|
|
|
var random *rand.Rand
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
random = rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
}
|
|
|
|
|
2018-11-24 12:34:51 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-05-07 13:20:36 +00:00
|
|
|
|
2020-06-30 22:49:42 +00:00
|
|
|
func ParseLinks(blockId string, content string) ([]ParsedLink, error) {
|
2020-05-16 20:15:36 +00:00
|
|
|
hrefRE := regexp.MustCompile(`(#?\[\[\s*([^\]]+)\s*\]\])`)
|
2020-05-07 13:20:36 +00:00
|
|
|
|
2020-05-16 20:15:36 +00:00
|
|
|
scanner := bufio.NewScanner(strings.NewReader(content))
|
|
|
|
scanner.Split(bufio.ScanLines)
|
2020-05-07 13:20:36 +00:00
|
|
|
|
|
|
|
var result []ParsedLink
|
|
|
|
|
2020-05-16 20:15:36 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
2020-05-07 13:20:36 +00:00
|
|
|
|
2020-05-16 20:15:36 +00:00
|
|
|
links := hrefRE.FindAllStringSubmatch(line, -1)
|
|
|
|
|
|
|
|
for _, matches := range links {
|
|
|
|
link := matches[0]
|
2020-05-07 13:20:36 +00:00
|
|
|
|
2020-05-16 20:15:36 +00:00
|
|
|
if link[0] == '#' {
|
|
|
|
link = strings.TrimPrefix(link, "#[[")
|
|
|
|
} else {
|
|
|
|
link = strings.TrimPrefix(link, "[[")
|
|
|
|
}
|
|
|
|
|
|
|
|
link = strings.TrimSuffix(link, "]]")
|
|
|
|
link = strings.TrimSpace(link)
|
|
|
|
l := cleanNameURL(link)
|
2020-06-30 22:49:42 +00:00
|
|
|
result = append(result, ParsedLink{blockId, link, l, line})
|
2020-05-16 20:15:36 +00:00
|
|
|
}
|
2020-05-07 13:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
2020-05-13 20:36:03 +00:00
|
|
|
|
|
|
|
func cleanNameURL(name string) string {
|
|
|
|
return strings.Replace(name, " ", "_", -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func cleanTitle(name string) string {
|
|
|
|
return strings.Replace(name, "_", " ", -1)
|
|
|
|
}
|
2020-06-30 20:56:12 +00:00
|
|
|
|
|
|
|
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())
|
|
|
|
}
|