From 277c0d056ade4089b36a1ce807c09416e2ad89ff Mon Sep 17 00:00:00 2001 From: Michel Boucey Date: Sun, 20 Aug 2023 16:00:32 +0200 Subject: [PATCH] Some enhancements --- main.go | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 88f1037..941d1ed 100644 --- a/main.go +++ b/main.go @@ -48,7 +48,7 @@ func main() { var pathSep string var rewrittenLine string var section string - var tocLines []string + var headerLines []string switch runtime.GOOS { case "windows": @@ -123,11 +123,12 @@ func main() { } rewrittenLine = header + " " + section + " " + title + } if *tocFlag { - tocLines = append(tocLines, rewrittenLine) + headerLines = append(headerLines, rewrittenLine) } @@ -143,13 +144,9 @@ func main() { } - } else { - - if !tocLine.Match([]byte(line)) { + } else if !tocLine.Match([]byte(line)) { - mdLines = append(mdLines, line) - - } + mdLines = append(mdLines, line) } @@ -169,11 +166,9 @@ func main() { if *tocFlag { - for _, line := range tocLines { + for _, line := range headerLines { - matches := headerLine.FindStringSubmatch(line) - - _, _ = io.WriteString(mdTmpFile, strings.Repeat(" ", len(matches[1])-1)+"- ["+matches[2]+"](#"+strings.ToLower(strings.ReplaceAll(matches[2], ".", "")+"-"+strings.ReplaceAll(matches[3], " ", "-"))+") "+matches[3]+newLine) + _, _ = io.WriteString(mdTmpFile, toToCEntry(headerLine, line)+newLine) } @@ -196,11 +191,9 @@ func main() { if *tocFlag { - for _, line := range tocLines { - - matches := headerLine.FindStringSubmatch(line) + for _, line := range headerLines { - fmt.Println(strings.Repeat(" ", len(matches[1])-1) + "- [" + matches[2] + "](#" + strings.ToLower(strings.ReplaceAll(matches[2], ".", "")+"-"+strings.ReplaceAll(matches[3], " ", "-")) + ") " + matches[3]) + fmt.Println(toToCEntry(headerLine, line)) } @@ -214,6 +207,11 @@ func main() { } } +func toToCEntry(r *regexp.Regexp, l string) string { + ms := r.FindStringSubmatch(l) + return (strings.Repeat(" ", len(ms[1])-1) + "- [" + ms[2] + "](#" + strings.ToLower(strings.ReplaceAll(ms[2], ".", "")+"-"+strings.ReplaceAll(ms[3], " ", "-")) + ") " + ms[3]) +} + func addSectionChunk(s *string, hc int, cht int, ht int) { if hc > 0 && cht >= ht { *s += strconv.Itoa(hc) + "."