-
Notifications
You must be signed in to change notification settings - Fork 6
/
markdown.go
119 lines (99 loc) · 2.97 KB
/
markdown.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"sync"
"github.com/russross/blackfriday"
"mpldr.codes/ansi"
)
// markdowntohtml converts markdown documents to html
func markdownToHTML(inFolder, outFolder, templateFolder string) {
files, err := os.ReadDir(inFolder)
if err != nil {
fmt.Printf("%s: couldn't read '%s': %v\n", ansi.Bold(ansi.Red("ERROR")), inFolder, err)
return
}
var wg sync.WaitGroup
input := make(chan string, 128)
wg.Add(runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
fileProcessor(input, inFolder, outFolder, templateFolder)
wg.Done()
}()
}
for _, file := range files {
// only markdown files
if filepath.Ext(file.Name()) == ".md" {
input <- file.Name()
}
}
close(input)
wg.Wait()
}
func fileProcessor(input <-chan string, inFolder, outFolder, templateFolder string) {
header, err := os.ReadFile(filepath.Join(templateFolder, "header.html"))
if err != nil {
fmt.Printf("%s: couldn't read 'header.html': %v\n", ansi.Bold(ansi.Red("ERROR")), err)
return
}
footer, err := os.ReadFile(filepath.Join(templateFolder, "footer.html"))
if err != nil {
fmt.Printf("%s: couldn't read 'footer.html': %v\n", ansi.Bold(ansi.Red("ERROR")), err)
return
}
result := bytes.NewBuffer(make([]byte, 4096))
for infile := range input {
log.Println("Processing:\t", infile)
result.Reset()
// open the selected markdown file
markdownFile, err := os.Open(filepath.Join(inFolder, infile))
if err != nil {
fmt.Printf("%s: couldn't read '%s': %v\n", ansi.Bold(ansi.Red("ERROR")), infile, err)
continue
}
// create the html file
htmlFile, err := os.Create(filepath.Join(outFolder, infile+".html"))
if err != nil {
fmt.Printf("%s: couldn't create '%s': %v\n", ansi.Bold(ansi.Red("ERROR")), infile+".html", err)
markdownFile.Close()
continue
}
// read the md
reader := bufio.NewReader(markdownFile)
markdown, err := io.ReadAll(reader)
if err != nil {
fmt.Printf("%s: couldn't read from '%s': %v\n", ansi.Bold(ansi.Red("ERROR")), infile+".html", err)
markdownFile.Close()
htmlFile.Close()
continue
}
// send the md to blackfriday
html := blackfriday.MarkdownCommon(markdown)
// assemble in order
result.Write(header)
result.Write(bytes.TrimSpace(html))
result.Write(footer)
// pass the assembled html into scanforplugincalls
// var resultcopy []byte
resultCopy := make([]byte, result.Len())
copy(resultCopy, result.Bytes())
htmlAfterPlugins, err := ScanForPluginCalls(result.Bytes())
if err != nil {
fmt.Printf("%s: error parsing plugin in '%s': %v\n", ansi.Bold(ansi.Red("ERROR")), infile+".html", err)
log.Println("returning content without plugin...")
// if there's an error, let's just take the html from before the error and use that.
htmlAfterPlugins = resultCopy
}
io.Copy(htmlFile, bytes.NewReader(htmlAfterPlugins))
// don't forget to close it when done
markdownFile.Close()
htmlFile.Close()
}
}