-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathlinks.go
80 lines (68 loc) · 1.72 KB
/
links.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
type bookmarks map[string]string
func searchLinks(wikiPath string) {
showUpdateStatus()
bookmarks := make(bookmarks)
files := parseSummary()
for _, file := range files {
parseLinks(wikiPath, file, bookmarks)
}
for name, link := range bookmarks {
wf.NewItem(name).UID(name).Valid(true).Arg(link)
}
log.Println(len(bookmarks))
// TODO: message doesnt show
wf.WarnEmpty("No matching items", "Try a different query?")
wf.SendFeedback()
}
// Return a map of all links in wiki.
func getLinks() {
}
// Parse SUMMARY.md file and return all file paths inside the wiki.
func parseSummary() []string {
files := make([]string, 0)
data, err := ioutil.ReadFile("SUMMARY.md")
if err != nil {
log.Fatal(err)
}
re := regexp.MustCompile(`\[([^\]]*)\]\(([^)]*)\)`)
scanner := bufio.NewScanner(strings.NewReader(string(data)))
for scanner.Scan() {
matches := re.FindAllStringSubmatch(scanner.Text(), -1)
files = append(files, matches[0][2])
}
return files
}
// Parse file for links and update bookmarks.
func parseLinks(wikiPath string, filePath string, bookmarks bookmarks) {
file, err := os.Open(wikiPath + filePath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
re := regexp.MustCompile(`\[([^\]]*)\]\(([^)]*)\)`)
scanner := bufio.NewScanner(file)
reading := false
for scanner.Scan() {
if reading == false && strings.HasPrefix(scanner.Text(), "## Links") {
reading = true
continue
}
if reading == true && strings.HasPrefix(scanner.Text(), "#") {
break
} else if reading == true {
matches := re.FindAllStringSubmatch(scanner.Text(), -1)
if matches != nil {
bookmarks[matches[0][1]] = matches[0][2]
}
}
}
}