Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelBoucey committed Aug 29, 2023
1 parent 73c663d commit b176505
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
5 changes: 3 additions & 2 deletions example.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
<H1>Lorem</H1>

<!-- ToC -->
- [1.](#1-lorem-ipsum-dolor) Lorem ipsum dolor
- [1.1.](#11-lorem-ipsum-dolor-sit-amet) Lorem ipsum dolor sit amet
- [1.2.](#12-an-est-malis-dignissim-interpretaris) An est malis dignissim interpretaris
Expand All @@ -22,7 +25,6 @@
- [3.1.](#31-cu-qui-delectus-detraxit,-te-qui-veri-diceret-admodum) Cu qui delectus detraxit, te qui veri diceret admodum
- [3.2.](#32-pro-copiosae-erroribus-theophrastus-ex) Pro copiosae erroribus theophrastus ex
- [3.3.](#33-ex-eum-luptatum-molestiae) Ex eum luptatum molestiae
<H1>Lorem</H1>

# 1. Lorem ipsum dolor

Expand Down Expand Up @@ -115,4 +117,3 @@ Pro copiosae erroribus theophrastus ex, has adhuc expetenda reprimique ne. Id se
## 3.3. Ex eum luptatum molestiae

Ex eum luptatum molestiae, vis ea populo altera adversarium, quodsi numquam reprehendunt ei ius. Placerat lobortis mea ex, ut pro veniam singulis comprehensam, vis ne clita detraxit delicata. Ad sit prima utamur singulis. Ut sanctus ornatus vel. Eu quod perfecto omittantur vis, ne eum facer pericula.

39 changes: 22 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ func main() {
version := "2.0.1"

var headerCounters [7]int
var headerLines []string
var isToCInsertionLine bool = false
var mdLines []string
var mdTmpFile *os.File
var newLine string
var mdLines []string
var pathSep string
var rewrittenLine string
var section string
var headerLines []string
var upperHeaderLevel int

switch runtime.GOOS {
Expand Down Expand Up @@ -95,7 +96,7 @@ func main() {
log.Fatal(err)
}

tocComment := regexp.MustCompile(`^<!--\s+\bToC\b\s+-->\s*$`)
tocInsertionLine := regexp.MustCompile(`^<!--\s+\bToC\b\s+-->\s*$`)
tocLine := regexp.MustCompile(`^\s*-\s\[[\d\.]*\]\(#\d*`)
headerLine := regexp.MustCompile(`^(#{1,6})\s+([\d\.]*)\s*(.*)$`)

Expand Down Expand Up @@ -148,6 +149,10 @@ func main() {

} else if !tocLine.Match([]byte(line)) {

if tocInsertionLine.Match([]byte(line)) {
isToCInsertionLine = true
}

mdLines = append(mdLines, line)

}
Expand All @@ -174,19 +179,19 @@ func main() {
log.Fatal(err)
}

if *tocFlag {
for _, line := range mdLines {

for _, line := range headerLines {
_, _ = io.WriteString(mdTmpFile, line+newLine)

_, _ = io.WriteString(mdTmpFile, toToCEntry(upperHeaderLevel, headerLine, line)+newLine)
if isToCInsertionLine && tocInsertionLine.Match([]byte(line)) {

}
for _, hline := range headerLines {

}
_, _ = io.WriteString(mdTmpFile, toToCEntry(upperHeaderLevel, headerLine, hline)+newLine)

for _, line := range mdLines {
}

_, _ = io.WriteString(mdTmpFile, line+newLine)
}

}

Expand All @@ -199,19 +204,19 @@ func main() {

} else {

if *tocFlag {
for _, line := range mdLines {

for _, line := range headerLines {
fmt.Println(line)

fmt.Println(toToCEntry(upperHeaderLevel, headerLine, line))
if isToCInsertionLine && tocInsertionLine.Match([]byte(line)) {

}
for _, hline := range headerLines {

}
fmt.Println(toToCEntry(upperHeaderLevel, headerLine, hline))

for _, line := range mdLines {
}

fmt.Println(line)
}

}
}
Expand Down

0 comments on commit b176505

Please sign in to comment.