Skip to content

Commit

Permalink
Some enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelBoucey committed Aug 20, 2023
1 parent 71c29ac commit 277c0d0
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down Expand Up @@ -123,11 +123,12 @@ func main() {
}

rewrittenLine = header + " " + section + " " + title

}

if *tocFlag {

tocLines = append(tocLines, rewrittenLine)
headerLines = append(headerLines, rewrittenLine)

}

Expand All @@ -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)

}

Expand All @@ -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)

}

Expand All @@ -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))

}

Expand All @@ -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) + "."
Expand Down

0 comments on commit 277c0d0

Please sign in to comment.