/
main.go
354 lines (316 loc) · 8.42 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
var (
inputPath *string
outputPath *string
title *string
debugEnabled *bool
wikiLinks *bool
capitalize *bool
tagLinks *bool
toc *bool
re = regexp.MustCompile("`[^`]+`")
)
func newRootCmd() *cobra.Command {
tagsCommand := &cobra.Command{
Use: "tags",
RunE: tagsRunFn,
}
inputPath = tagsCommand.Flags().StringP("input", "i", "", "The location of the input files")
outputPath = tagsCommand.Flags().StringP("output", "o", "", "The location of the output files")
title = tagsCommand.Flags().StringP("title", "t", "Index", "The title of the generated index file")
debugEnabled = tagsCommand.Flags().Bool("debug", false, "If set, debug logging will be enabled")
wikiLinks = tagsCommand.Flags().Bool("wiki-links", false, "If set, links will be generated for a wiki with file extensions excluded")
capitalize = tagsCommand.Flags().Bool("capitalize", false, "If set, tag names in the generated index will have their first character capitalized.")
tagLinks = tagsCommand.Flags().Bool("tag-links", false, "If set, links to files in the generated index will be annotated with the list of other tags they have.")
toc = tagsCommand.Flags().Bool("toc", false, "If set, a table of contents will be generated containing a link to the heading of each tag")
rootCmd := &cobra.Command{
Use: "markasten",
}
rootCmd.AddCommand(tagsCommand)
return rootCmd
}
func debug(format string, v ...any) {
if *debugEnabled {
log.Printf(format, v...)
}
}
func main() {
rootCmd := newRootCmd()
rootCmd.Execute()
}
type indexedFile struct {
fileName string
title string
otherTags []string
}
func writeOrPanic(file *os.File, text string) {
_, err := io.WriteString(file, text)
if err != nil {
panic(err)
}
}
func tagsRunFn(cmd *cobra.Command, args []string) error {
debug("tags called with -i %s and -o %s\n", *inputPath, *outputPath)
inputDirEntires, err := newFullDirEntryList(*inputPath)
if err != nil {
panic(err)
}
filesByTags := make(map[string][]indexedFile)
searchResults, err := searchForMarkdownFiles(inputDirEntires, *inputPath)
if err != nil {
panic(err)
}
for _, dirEntry := range searchResults {
fileBytes, err := os.ReadFile(dirEntry.Name())
if err != nil {
panic(err)
}
scrapedTags, title := scrapeTagsAndTitle(fileBytes)
filesByTags = appendFilesByTags(scrapedTags, filesByTags, title, dirEntry.Name())
}
outputFile, err := os.Create(*outputPath)
if err != nil {
panic(err)
}
defer outputFile.Close()
writeOrPanic(outputFile, fmt.Sprintf("# %s\n", *title))
var sortedTags []string
for tag, _ := range filesByTags {
sortedTags = append(sortedTags, tag)
}
sort.Strings(sortedTags)
if *toc {
writeOrPanic(outputFile, "\n")
writeOrPanic(outputFile, "---\n")
writeOrPanic(outputFile, "\n")
writeOrPanic(outputFile, "## Table of contents\n")
for _, tag := range sortedTags {
header := tagToHeader(tag)
writeOrPanic(outputFile, fmt.Sprintf("- [%s](#%s)\n", header, headerToLink(header)))
}
writeOrPanic(outputFile, "\n")
writeOrPanic(outputFile, "---\n")
writeOrPanic(outputFile, "\n")
}
for n, tag := range sortedTags {
files := filesByTags[tag]
writeOrPanic(outputFile, fmt.Sprintf("## %s\n", tagToHeader(tag)))
countedTitles := countTitles(files)
for m, f := range files {
trailingChar := "\n"
if m == len(files)-1 && n == len(sortedTags)-1 {
trailingChar = ""
}
relativePath := relativeTo(f.fileName, *outputPath)
if *wikiLinks {
relativePath = makeWikiLink(relativePath)
}
title := f.title
if count, ok := countedTitles[f.title]; ok && count > 1 {
title = relativePath
}
line := fmt.Sprintf(
"- [%s](%s)",
title,
relativePath,
)
if *tagLinks {
for _, otherTag := range f.otherTags {
line = fmt.Sprintf("%s `%s`", line, otherTag)
}
}
line = fmt.Sprintf(
"%s%s",
line,
trailingChar,
)
writeOrPanic(outputFile, line)
}
if n < len(sortedTags)-1 {
writeOrPanic(outputFile, "\n")
}
}
return nil
}
func tagToHeader(tag string) string {
if *capitalize && len(tag) > 0 {
return fmt.Sprintf("%s%s", strings.ToUpper(tag[0:1]), tag[1:])
}
return tag
}
func relativeTo(filePath string, relativeToPath string) string {
dir := filepath.Dir(relativeToPath)
relative, err := filepath.Rel(dir, filePath)
if err != nil {
return filePath
}
return relative
}
func firstNonEmptyLine(lines []string) string {
for _, line := range lines {
if len(line) > 0 {
return line
}
}
return ""
}
func scrapeTagsAndTitle(fileBytes []byte) ([]string, string) {
var scrapedTags []string
title := ""
lines := strings.Split(string(fileBytes), "\n")
if firstNonEmptyLine(lines) != "---" {
debug("first line was %s, no tags detected", lines[0])
return scrapedTags, title
}
foundYaml := false
finishedYaml := false
var yamlLines []string
for _, line := range lines {
if len(line) > 2 && line[0:2] == "# " {
title = line[2:]
break
}
if line == "---" {
if foundYaml {
// If we had previously found yaml, this line
// marks the end of the yaml.
finishedYaml = true
}
if !foundYaml {
// if we haven't found yaml yet, this line
// marks the beginning of yaml
foundYaml = true
}
continue
}
if len(line) == 0 {
continue
}
if foundYaml && !finishedYaml {
yamlLines = append(yamlLines, line)
}
}
debug("found yaml:")
for _, line := range yamlLines {
debug(line)
}
fm := frontmatter{}
err := yaml.Unmarshal([]byte(strings.Join(yamlLines, "\n")), &fm)
if err != nil {
debug("error unmarshalling yaml: %s", err)
return scrapedTags, title
}
return fm.Tags, title
}
type frontmatter struct {
Tags []string
}
func appendFilesByTags(scrapedTags []string, filesByTags map[string][]indexedFile, title string, name string) map[string][]indexedFile {
for _, tagName := range scrapedTags {
tagName := tagName
file := indexedFile{
fileName: name,
title: title,
otherTags: getOtherTags(scrapedTags, tagName),
}
if files, ok := filesByTags[tagName]; ok {
filesByTags[tagName] = append(files, file)
} else {
filesByTags[tagName] = []indexedFile{file}
}
}
return filesByTags
}
func getOtherTags(tags []string, tag string) []string {
var otherTags []string
for _, t := range tags {
if t == tag {
continue
}
otherTags = append(otherTags, t)
}
return otherTags
}
type fullDirEntry struct {
dirEntry fs.DirEntry
parentPath string
}
func (d fullDirEntry) IsDir() bool {
return d.dirEntry.IsDir()
}
func (d fullDirEntry) Name() string {
return filepath.Join(d.parentPath, d.dirEntry.Name())
}
func (d fullDirEntry) IsDotFile() bool {
return len(d.dirEntry.Name()) > 0 && d.dirEntry.Name()[0:1] == "."
}
func newFullDirEntryList(root string) ([]fullDirEntry, error) {
entries, err := os.ReadDir(root)
if err != nil {
return nil, err
}
var fullDirEntries []fullDirEntry
for _, entry := range entries {
fullDirEntries = append(fullDirEntries, fullDirEntry{
dirEntry: entry,
parentPath: root,
})
}
return fullDirEntries, nil
}
func searchForMarkdownFiles(dirEntries []fullDirEntry, root string) ([]fullDirEntry, error) {
debug("searching %s", root)
var entries []fullDirEntry
for _, dirEntry := range dirEntries {
if dirEntry.IsDotFile() {
continue
}
if dirEntry.IsDir() {
debug("found sub directory %s", dirEntry.dirEntry.Name())
subEntries, err := newFullDirEntryList(dirEntry.Name())
if err != nil {
return nil, err
}
searchResults, err := searchForMarkdownFiles(subEntries, dirEntry.Name())
if err != nil {
return nil, err
}
for _, subEntry := range searchResults {
entries = append(entries, subEntry)
}
} else {
debug("found file %s", dirEntry.Name())
entries = append(entries, dirEntry)
}
}
return entries, nil
}
func countTitles(files []indexedFile) map[string]int {
titleCounts := make(map[string]int)
for _, file := range files {
titleCounts[file.title]++
}
return titleCounts
}
func makeWikiLink(path string) string {
return path[:len(path)-len(filepath.Ext(path))]
}
func headerToLink(header string) string {
// Some special characters produce invalid heading links in GitHub.
// In this case, a heading of "foo:bar" requires a link of "foobar",
// and a heading of "foo bar" requires a link of "foo-bar".
return strings.ReplaceAll(strings.ReplaceAll(header, ":", ""), " ", "-")
}