-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
174 lines (144 loc) · 4.23 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
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
package main
import (
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)
var (
replacementFullPath = map[string]string{
"@repo1": "repo1-docs/",
"@repo2": "repo2-docs/",
"@repo3": "repo3-docs/",
}
)
func find(root, ext string) []string {
var files []string
filepath.WalkDir(root, func(path string, d fs.DirEntry, e error) error {
if e != nil {
return e
}
if filepath.Ext(d.Name()) == ext {
files = append(files, path)
}
return nil
})
return files
}
func main() {
for _, s := range find("./", ".md") {
convertReferences(s)
}
}
func convertReferences(file string) {
bytes, err := os.ReadFile(file)
if err != nil {
fmt.Println(err)
return
}
fileContents := string(bytes) // convert content to a 'string'
fullLinkRegex := regexp.MustCompile(`\[([^\[]+)\]\((.[^)]*)\)`)
referenceRegex := regexp.MustCompile(`(@[a-zA-Z0-9]*):.*`)
res := fullLinkRegex.FindAllStringSubmatch(fileContents, -1)
for _, item := range res {
if len(item) != 3 {
continue // invalid content
}
fullLink := item[0]
linkText := item[1]
linkUri := item[2]
found := false
for keyInDocs, newDirectory := range replacementFullPath {
newFileContents := replaceReferenceWithCorrectPath(fullLink, linkText, linkUri, keyInDocs, newDirectory, fileContents)
if newFileContents != "" {
fileContents = newFileContents
}
}
if !found && referenceRegex.Match([]byte(linkUri)) {
components := referenceRegex.FindAllStringSubmatch(linkUri, -1)
if len(components) == 0 {
fmt.Println("No valid reference to other repositories in link")
}
if len(components[0]) < 2 {
fmt.Println("No valid reference to other repositories in link")
}
fmt.Println(" ==> unknown component, no replacement found for", fmt.Sprintf("%s:", components[0][1]), "in", linkUri)
}
}
writeUpdatedContentsBackToFile(file, fileContents)
}
func replaceReferenceWithCorrectPath(fullLink, linkText, linkUri, keyInDocs, newDirectory, originalFileContents string) string {
if strings.HasPrefix(linkUri, fmt.Sprintf("%s:", keyInDocs)) {
trimmed := strings.TrimPrefix(linkUri, fmt.Sprintf("%s:", keyInDocs))
var replacement string
fileExists := doesReplacementFileExist(newDirectory + trimmed)
if !fileExists {
fileExists = doesReplacementFileExist(newDirectory + "docs/" + trimmed)
if !fileExists {
fmt.Println(" ==> couldn't find replacement file for trimmed")
return ""
}
mkDocsReference, err := getMkDocsSiteNameForRepo(newDirectory)
if err != nil {
fmt.Println("Error during getting mkdocs site name: ", err)
return ""
}
replacement = fmt.Sprintf("[%s](%s/%s)", linkText, mkDocsReference, trimmed)
} else {
mkDocsReference, err := getMkDocsSiteNameForRepo(newDirectory)
if err != nil {
fmt.Println("Error during getting mkdocs site name: ", err)
return ""
}
replacement = fmt.Sprintf("[%s](%s/%s)", linkText, mkDocsReference, trimmed)
}
fmt.Println(" ==> fixing up", fullLink, "with", replacement)
fileContents := strings.ReplaceAll(originalFileContents, fullLink, replacement)
return fileContents
}
return ""
}
func getMkDocsSiteNameForRepo(folder string) (string, error) {
var mkDocsReference string
fileIO, err := os.OpenFile(fmt.Sprintf("%s/mkdocs.yml", folder), os.O_RDWR, 0600)
if err != nil {
return "", err
}
defer fileIO.Close()
rawBytes, err := io.ReadAll(fileIO)
if err != nil {
return "", err
}
lines := strings.Split(string(rawBytes), "\n")
for _, line := range lines {
if strings.Contains(line, "site_name") {
mkDocsReference = strings.ReplaceAll(strings.ToLower(strings.TrimPrefix(line, "site_name: ")), " ", "-")
}
}
return mkDocsReference, nil
}
func doesReplacementFileExist(file string) bool {
_, err := os.OpenFile(file, os.O_WRONLY, os.ModeAppend)
if err != nil {
return false
}
return true
}
func writeUpdatedContentsBackToFile(file, fileContents string) {
f, err := os.OpenFile(file, os.O_WRONLY, os.ModeAppend)
if err != nil {
fmt.Println("Error during opening file to write contents back: ", err)
return
}
defer f.Close()
_, err = f.WriteString(fileContents)
if err != nil {
fmt.Println("Error during writing contents back to file: ", err)
return
}
// Issue a `Sync` to flush writes to stable storage.
f.Sync()
}