-
Notifications
You must be signed in to change notification settings - Fork 64
/
frontmatter.go
124 lines (105 loc) · 2.77 KB
/
frontmatter.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
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
package main
import (
"bufio"
"encoding/csv"
"os"
"path/filepath"
"strings"
)
func parseFrontMatter(filePath string) (map[string]string, error) {
frontMatter := make(map[string]string)
file, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
readingFrontMatter := false
for scanner.Scan() {
line := scanner.Text()
if line == "---" {
if readingFrontMatter {
break
} else {
readingFrontMatter = true
continue
}
}
if readingFrontMatter {
parts := strings.SplitN(line, ":", 2)
if len(parts) == 2 {
key := strings.TrimSpace(parts[0])
value := strings.Trim(strings.TrimSpace(parts[1]), "\"[]")
frontMatter[key] = value
}
}
}
if err := scanner.Err(); err != nil {
return nil, err
}
return frontMatter, nil
}
func hasRequiredFields(frontMatter map[string]string, requiredFields []string) bool {
for _, field := range requiredFields {
if _, ok := frontMatter[field]; !ok {
return false
}
}
return true
}
func frontMatterToCSV(rootDirectory, csvPath, repoBaseURL string) error {
csvFile, err := os.Create(csvPath)
if err != nil {
return err
}
defer csvFile.Close()
writer := csv.NewWriter(csvFile)
defer writer.Flush()
headers := []string{"title", "vendor", "vendor_page", "description", "ip_rating", "backhaul", "image_url"}
if err := writer.Write(headers); err != nil {
return err
}
requiredFields := []string{"title", "vendor", "description", "image"}
err = filepath.Walk(rootDirectory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() || filepath.Base(path) != "_index.md" {
return nil
}
frontMatter, err := parseFrontMatter(path)
if err != nil {
return err
}
if hasRequiredFields(frontMatter, requiredFields) {
imageRelativePath := frontMatter["image"]
imagePath := filepath.Join(filepath.Dir(path), imageRelativePath)
relPath, err := filepath.Rel(rootDirectory, imagePath)
if err != nil {
return err
}
imageURL := repoBaseURL + "/" + strings.ReplaceAll(relPath, "\\", "/")
row := make([]string, len(headers))
for i, header := range headers[:len(headers)-1] { // Exclude image_url from headers for mapping
row[i] = frontMatter[header]
}
row[len(headers)-1] = imageURL // Add image_url at the end
if err := writer.Write(row); err != nil {
return err
}
}
return nil
})
if err != nil {
return err
}
return nil
}
func main() {
rootDirectoryPath := "doc/content/gateways/models"
csvPath := "gateways.csv"
repoBaseURL := "https://raw.githubusercontent.com/TheThingsIndustries/lorawan-stack-docs/master/doc/content/gateways/models"
if err := frontMatterToCSV(rootDirectoryPath, csvPath, repoBaseURL); err != nil {
panic(err)
}
}