-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (69 loc) · 1.59 KB
/
main.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 (
"io/ioutil"
"regexp"
"fmt"
"os"
"bufio"
"os/exec"
)
var titles = []string{}
func init() {
dir, _ := ioutil.ReadDir("./docs")
md_pattern := regexp.MustCompile(`.*.md`)
for _, fi := range dir {
if !md_pattern.MatchString(fi.Name()) {
continue
}
titles = append(titles, regexp.MustCompile(`.md$`).ReplaceAllString(fi.Name(), ""))
}
}
func main() {
for _, url := range titles {
fmt.Println(url)
}
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
keyword := s.Text()
if searchTitle(keyword) {
open(keyword)
}
if result, title := searchText(keyword); result {
open(title)
}
}
}
func open(dir string) {
s := bufio.NewScanner(os.Stdin)
url := "https://laravel.com/docs/5.3/" + dir
fmt.Println("found: " + url)
fmt.Print("open? [y/n]:")
s.Scan()
if (s.Text() == "y") {
exec.Command("open", url).Start()
}
}
func searchTitle(keyword string) bool {
if inArray(keyword, titles) {
return true
}
return false
}
func searchText(keyword string) (bool, string) {
pattern := regexp.MustCompile(keyword)
for _, title := range titles {
file, _ := ioutil.ReadFile("./docs/" + title + ".md")
if pattern.MatchString(string(file)) {
return true, title
}
}
return false, ""
}
func inArray(needle string, list []string) bool {
for _, str := range list {
if str == needle {
return true
}
}
return false
}