|
| 1 | +// SPDX-FileCopyrightText: 2023 Christoph Mewes |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + html "html/template" |
| 9 | + "log" |
| 10 | + "os" |
| 11 | + "regexp" |
| 12 | + "sort" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "go.xrstf.de/rudi/docs" |
| 16 | +) |
| 17 | + |
| 18 | +const ( |
| 19 | + filename = "docs/README.md" |
| 20 | + beginMarker = `<!-- BEGIN_TOC -->` |
| 21 | + endMarker = `<!-- END_TOC -->` |
| 22 | +) |
| 23 | + |
| 24 | +func main() { |
| 25 | + topics := docs.Topics() |
| 26 | + groups := getGroups(topics) |
| 27 | + |
| 28 | + rendered := renderTopics(topics, groups) |
| 29 | + rendered = fmt.Sprintf("%s\n%s\n%s", beginMarker, rendered, endMarker) |
| 30 | + |
| 31 | + content, err := os.ReadFile(filename) |
| 32 | + if err != nil { |
| 33 | + log.Fatalf("Failed to read %s: %v", filename, err) |
| 34 | + } |
| 35 | + |
| 36 | + regex := regexp.MustCompile(`(?s)` + beginMarker + `.+` + endMarker) |
| 37 | + output := regex.ReplaceAllString(string(content), rendered) |
| 38 | + |
| 39 | + os.WriteFile(filename, []byte(output), 0644) |
| 40 | +} |
| 41 | + |
| 42 | +func strSliceHas(haystack []string, needle string) bool { |
| 43 | + for _, val := range haystack { |
| 44 | + if val == needle { |
| 45 | + return true |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return false |
| 50 | +} |
| 51 | + |
| 52 | +func getGroups(topics []docs.Topic) []string { |
| 53 | + // determine a sorted list of functions, with some groups |
| 54 | + // hardcoded to be at the top, regardless of their name |
| 55 | + prioritizedGroups := []string{ |
| 56 | + "General", |
| 57 | + "Core Functions", |
| 58 | + } |
| 59 | + |
| 60 | + remainingGroups := []string{} |
| 61 | + |
| 62 | + for _, topic := range topics { |
| 63 | + if !strSliceHas(prioritizedGroups, topic.Group) { |
| 64 | + if !strSliceHas(remainingGroups, topic.Group) { |
| 65 | + remainingGroups = append(remainingGroups, topic.Group) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + sort.Strings(remainingGroups) |
| 71 | + |
| 72 | + return append(prioritizedGroups, remainingGroups...) |
| 73 | +} |
| 74 | + |
| 75 | +func renderTopics(topics []docs.Topic, groups []string) string { |
| 76 | + var out strings.Builder |
| 77 | + |
| 78 | + for _, group := range groups { |
| 79 | + out.WriteString(fmt.Sprintf("## %s\n", group)) |
| 80 | + out.WriteString("\n") |
| 81 | + |
| 82 | + topicNames := getTopicNames(topics, group) |
| 83 | + for _, topicName := range topicNames { |
| 84 | + topic := getTopic(topics, topicName) |
| 85 | + linkTitle := topicName |
| 86 | + |
| 87 | + if topic.IsFunction { |
| 88 | + linkTitle = fmt.Sprintf("`%s`", linkTitle) |
| 89 | + } |
| 90 | + |
| 91 | + out.WriteString(fmt.Sprintf("* [%s](%s) – %s\n", linkTitle, topic.Filename, topic.Description)) |
| 92 | + } |
| 93 | + |
| 94 | + out.WriteString("\n") |
| 95 | + } |
| 96 | + |
| 97 | + return strings.TrimSpace(out.String()) |
| 98 | +} |
| 99 | + |
| 100 | +func htmlencode(s string) string { |
| 101 | + return html.HTMLEscapeString(s) |
| 102 | +} |
| 103 | + |
| 104 | +func getTopicNames(topics []docs.Topic, group string) []string { |
| 105 | + names := []string{} |
| 106 | + |
| 107 | + for _, topic := range topics { |
| 108 | + if topic.Group != group { |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + primaryName := topic.CliNames[0] |
| 113 | + |
| 114 | + if !strSliceHas(names, primaryName) { |
| 115 | + names = append(names, primaryName) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + sort.Strings(names) |
| 120 | + |
| 121 | + return names |
| 122 | +} |
| 123 | + |
| 124 | +func getTopic(topics []docs.Topic, name string) docs.Topic { |
| 125 | + for _, topic := range topics { |
| 126 | + if topic.CliNames[0] == name { |
| 127 | + return topic |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + panic("this should never happen") |
| 132 | +} |
0 commit comments