| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "flag" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "regexp" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | func infof(w io.Writer, format string, a ...interface{}) { |
| 15 | if !*verbose { |
| 16 | return |
| 17 | } |
| 18 | fmt.Fprintf(w, format, a...) |
| 19 | } |
| 20 | |
| 21 | func warnf(w io.Writer, format string, a ...interface{}) { |
| 22 | fmt.Fprintf(w, format, a...) |
| 23 | } |
| 24 | |
| 25 | func readManDir() (string, []os.DirEntry) { |
| 26 | rootDirs := []string{ |
| 27 | "..", |
| 28 | "/tmp/docker_run/git-lfs", |
| 29 | } |
| 30 | |
| 31 | var err error |
| 32 | for _, rootDir := range rootDirs { |
| 33 | fs, err := os.ReadDir(filepath.Join(rootDir, "docs", "man")) |
| 34 | if err == nil { |
| 35 | return rootDir, fs |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | warnf(os.Stderr, "Failed to open man dir: %v\n", err) |
| 40 | os.Exit(2) |
| 41 | return "", nil |
| 42 | } |
| 43 | |
| 44 | func titleizeXref(s string) string { |
| 45 | return strings.Replace(strings.ToTitle(s[1:2])+s[2:], "_", " ", -1) |
| 46 | } |
| 47 | |
| 48 | var ( |
| 49 | verbose = flag.Bool("verbose", false, "Show verbose output.") |
| 50 | ) |
| 51 | |
| 52 | // Reads all .adoc files & and converts them to string literals |
| 53 | // triggered by "go generate" comment |
| 54 | // Literals are inserted into a map using an init function, this means |
| 55 | // that there are no compilation errors if 'go generate' hasn't been run, just |
| 56 | // blank man files. |
| 57 | func main() { |
| 58 | flag.Parse() |
| 59 | |
| 60 | infof(os.Stderr, "Converting man pages into code...\n") |
| 61 | rootDir, fs := readManDir() |
| 62 | manDir := filepath.Join(rootDir, "docs", "man") |
| 63 | out, err := os.Create(filepath.Join(rootDir, "commands", "mancontent_gen.go")) |
| 64 | if err != nil { |
| 65 | warnf(os.Stderr, "Failed to create go file: %v\n", err) |
| 66 | os.Exit(2) |
| 67 | } |
| 68 | out.WriteString("package commands\n\nfunc init() {\n") |
| 69 | out.WriteString("\t// THIS FILE IS GENERATED, DO NOT EDIT\n") |
| 70 | out.WriteString("\t// Use 'go generate ./commands' to update\n") |
| 71 | fileregex := regexp.MustCompile(`git-lfs(?:-([A-Za-z\-]+))?.adoc`) |
| 72 | headerregex := regexp.MustCompile(`^(===?)\s+([A-Za-z0-9 -]+)`) |
| 73 | // cross-references |
| 74 | linkregex := regexp.MustCompile(`<<([^,>]+)(?:,([^>]+))?>>`) |
| 75 | // man links |
| 76 | manlinkregex := regexp.MustCompile(`(git)(?:-(lfs))?-([a-z\-]+)\(\d\)`) |
| 77 | // source blocks |
| 78 | sourceblockregex := regexp.MustCompile(`\[source(,.*)?\]`) |
| 79 | // synopsis source block quotes substitution group syntax |
| 80 | synopsisQuotesRegex := regexp.MustCompile(`[\*_]+`) |
| 81 | // anchors |
| 82 | anchorregex := regexp.MustCompile(`\[\[(.+)\]\]`) |
| 83 | count := 0 |
| 84 | for _, f := range fs { |
| 85 | if match := fileregex.FindStringSubmatch(f.Name()); match != nil { |
| 86 | infof(os.Stderr, "%v\n", f.Name()) |
| 87 | cmd := match[1] |
| 88 | if len(cmd) == 0 { |
| 89 | // This is git-lfs.1.adoc |
| 90 | cmd = "git-lfs" |
| 91 | } |
| 92 | out.WriteString("\tManPages[\"" + cmd + "\"] = `") |
| 93 | contentf, err := os.Open(filepath.Join(manDir, f.Name())) |
| 94 | if err != nil { |
| 95 | warnf(os.Stderr, "Failed to open %v: %v\n", f.Name(), err) |
| 96 | os.Exit(2) |
| 97 | } |
| 98 | // Process the asciidoc to make it nicer as help text |
| 99 | scanner := bufio.NewScanner(contentf) |
| 100 | firstHeaderDone := false |
| 101 | skipNextLineIfBlank := false |
| 102 | lastLineWasList := false |
| 103 | isSourceBlock := false |
| 104 | isSynopsisSourceBlock := false |
| 105 | sourceBlockLine := "" |
| 106 | scanloop: |
| 107 | for scanner.Scan() { |
| 108 | line := scanner.Text() |
| 109 | trimmedline := strings.TrimSpace(line) |
| 110 | if skipNextLineIfBlank && len(trimmedline) == 0 { |
| 111 | skipNextLineIfBlank = false |
| 112 | lastLineWasList = false |
| 113 | continue |
| 114 | } |
| 115 | |
| 116 | // Special case headers |
| 117 | if hmatch := headerregex.FindStringSubmatch(line); hmatch != nil { |
| 118 | if len(hmatch[1]) == 2 { |
| 119 | header := strings.ToLower(hmatch[2]) |
| 120 | switch header { |
| 121 | case "name": |
| 122 | continue |
| 123 | case "synopsis": |
| 124 | // Ignore this, just go direct to command |
| 125 | |
| 126 | case "description": |
| 127 | // Just skip the header & newline |
| 128 | skipNextLineIfBlank = true |
| 129 | case "options": |
| 130 | out.WriteString("Options:" + "\n") |
| 131 | case "see also": |
| 132 | // don't include any content after this |
| 133 | break scanloop |
| 134 | default: |
| 135 | out.WriteString(strings.ToUpper(header[:1]) + header[1:] + "\n") |
| 136 | out.WriteString(strings.Repeat("-", len(header)) + "\n") |
| 137 | } |
| 138 | firstHeaderDone = true |
| 139 | } else { |
| 140 | out.WriteString(hmatch[2] + "\n") |
| 141 | out.WriteString(strings.Repeat("~", len(hmatch[2])) + "\n") |
| 142 | } |
| 143 | lastLineWasList = false |
| 144 | continue |
| 145 | } |
| 146 | |
| 147 | if lmatches := linkregex.FindAllStringSubmatch(line, -1); lmatches != nil { |
| 148 | for _, lmatch := range lmatches { |
| 149 | if len(lmatch) > 2 && lmatch[2] != "" { |
| 150 | line = strings.Replace(line, lmatch[0], `"`+lmatch[2]+`"`, 1) |
| 151 | } else { |
| 152 | line = strings.Replace(line, lmatch[0], `"`+titleizeXref(lmatch[1])+`"`, 1) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | if manmatches := manlinkregex.FindAllStringSubmatch(line, -1); manmatches != nil { |
| 157 | for _, manmatch := range manmatches { |
| 158 | line = strings.Replace(line, manmatch[0], strings.Join(manmatch[1:], " "), 1) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if sourceblockmatches := sourceblockregex.FindStringIndex(line); sourceblockmatches != nil { |
| 163 | isSourceBlock = true |
| 164 | if strings.Contains(line, "role=synopsis") { |
| 165 | isSynopsisSourceBlock = true |
| 166 | } |
| 167 | continue |
| 168 | } |
| 169 | |
| 170 | if anchormatches := anchorregex.FindStringIndex(line); anchormatches != nil { |
| 171 | // Skip anchors. |
| 172 | continue |
| 173 | } |
| 174 | |
| 175 | // Skip content until after first header |
| 176 | if !firstHeaderDone { |
| 177 | continue |
| 178 | } |
| 179 | // OK, content here |
| 180 | |
| 181 | // handle source block headers |
| 182 | if isSourceBlock { |
| 183 | sourceBlockLine = line |
| 184 | isSourceBlock = false |
| 185 | line = "" |
| 186 | continue |
| 187 | } else if sourceBlockLine != "" && line == sourceBlockLine { |
| 188 | isSynopsisSourceBlock = false |
| 189 | line = "" |
| 190 | sourceBlockLine = "" |
| 191 | } |
| 192 | |
| 193 | // remove characters that asciidoc would render invisible in a text env. |
| 194 | for _, invis := range []string{"`", "...."} { |
| 195 | line = strings.Replace(line, invis, "", -1) |
| 196 | } |
| 197 | line = strings.TrimSuffix(line, " +") |
| 198 | |
| 199 | if isSynopsisSourceBlock { |
| 200 | line = synopsisQuotesRegex.ReplaceAllString(line, "") |
| 201 | } |
| 202 | |
| 203 | // indent bullets and definition lists |
| 204 | if strings.HasPrefix(line, "*") { |
| 205 | lastLineWasList = true |
| 206 | } else if strings.HasSuffix(line, "::") { |
| 207 | lastLineWasList = true |
| 208 | line = strings.TrimSuffix(line, ":") |
| 209 | } else if lastLineWasList && line == "+" { |
| 210 | line = "" |
| 211 | } else if lastLineWasList && line == "" { |
| 212 | lastLineWasList = false |
| 213 | } else if lastLineWasList && !strings.HasPrefix(line, " ") { |
| 214 | // indent paragraphs under bullets if not already done |
| 215 | line = " " + line |
| 216 | } |
| 217 | |
| 218 | out.WriteString(line + "\n") |
| 219 | } |
| 220 | out.WriteString("`\n") |
| 221 | contentf.Close() |
| 222 | count++ |
| 223 | } |
| 224 | } |
| 225 | out.WriteString("}\n") |
| 226 | infof(os.Stderr, "Successfully processed %d man pages.\n", count) |
| 227 | |
| 228 | } |