-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
196 lines (170 loc) · 3.73 KB
/
parser.go
File metadata and controls
196 lines (170 loc) · 3.73 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"bufio"
"github.com/go-yaml/yaml"
"github.com/pkg/errors"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
link = `\[(.+)\]\(.+\)`
header = `^#(.+)$`
)
type frontMatter struct {
Title string
}
func (f *frontMatter) MarshalYAML() ([]byte, error) {
b := []byte("---\n")
c, err := yaml.Marshal(f)
c = append(c, "\n---\n"...)
b = append(b, c...)
return b, err
}
func getFileLines(filePath string) ([]string, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return LinesFromReader(f)
}
func getTitle(path string) (string, int, error) {
f, err := os.Open(path)
if err != nil{
return "", 0, err
}
defer f.Close()
return titleFromReader(f)
}
func LinesFromReader(r io.Reader) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
func alreadyPresent(lines []string, str string) bool {
if len(lines) < 2 {
return false
}
if strings.Contains(lines[0], "---") || strings.Contains(lines[1], "---") {
return true
}
return false
}
func (f *frontMatter) addToFile(path string, titleIndex int) error {
b, err := f.MarshalYAML()
if err != nil {
return err
}
str := string(b)
lines, err := getFileLines(path)
if err != nil {
return err
}
fileContent := ""
for i, line := range lines {
// if it's the first line in file and the frontMatter isn't already there, add it
if i == 0 && !alreadyPresent(lines, str) {
fileContent += str
}
// if the line is a main header and contains the title string, remove it
if i == titleIndex {
line = ""
}
fileContent += line
fileContent += "\n"
}
return ioutil.WriteFile(path, []byte(fileContent), 0644)
}
func preFlightChecks(cwd string) error {
if !filepath.IsAbs(cwd) {
return errors.Errorf("filepath should be absolute, got: %v", cwd)
}
if !strings.Contains(cwd, "hugo") && !strings.Contains(cwd, "docs") {
return errors.Errorf("can't find hugo or docs in wd: %v", cwd)
}
return nil
}
func getCwd() string {
d, err := os.Getwd()
if err != nil {
log.Panic(err)
}
return d
}
func getExt(file string) string {
x := strings.Split(file, ".")
return x[len(x)-1]
}
func getFileList(cwd string) ([]string, error) {
var files []string
w := func(p string, info os.FileInfo, err error) error {
name := info.Name()
// switch for exclusions not related to names
switch {
case info.IsDir():
return nil
case strings.Contains(p, ".git"):
return nil
case getExt(name) != "md":
return nil
}
// switch for excluding named files/dirs
switch name {
case ".git", "hugo", ".DS_Store":
return nil
}
files = append(files, p)
return nil
}
err := filepath.Walk(cwd, w)
return files, err
}
func titleFromReader(r io.Reader) (string, int, error) {
var title string
var index int
var err error
lines,err := LinesFromReader(r)
if err != nil {
return title, index, err
}
y := len(lines)/2 + 1 // adding plus one helps to avoid problems if file is only 1 line
head := regexp.MustCompile(header)
for x := 0; x < y; x++ {
l := lines[x]
// get regex match groups for the line at index x
s := head.FindStringSubmatch(l)
if len(s) < 1 {
continue
}
if s[1] != "" {
title = titleFormatter(s[1])
index = x
return title, index, err
}
}
if len(title) == 0 {
// title can't be grabbed from file contents - will use filename
}
return title, index, err
}
func titleFormatter(title string) string {
l := regexp.MustCompile(link)
t := strings.TrimSpace(title)
if l.MatchString(t) {
// title is a link
g := l.FindStringSubmatch(t)
return strings.Title(g[1])
}
return strings.Title(t)
}