-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
243 lines (207 loc) · 6.45 KB
/
main.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
markdownDir := "markdown"
outputDir := "output"
templateFile := "template.html"
stylesFile := "styles.css"
// Read the template file
template, err := os.ReadFile(templateFile)
if err != nil {
fmt.Println("Error reading template file:", err)
return
}
// Ensure the output directory exists
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
err := os.Mkdir(outputDir, os.ModePerm)
if err != nil {
fmt.Println("Error creating output directory:", err)
return
}
}
// Ensure the js directory exists in the output
jsDir := filepath.Join(outputDir, "js")
if _, err := os.Stat(jsDir); os.IsNotExist(err) {
err := os.Mkdir(jsDir, os.ModePerm)
if err != nil {
fmt.Println("Error creating js directory:", err)
return
}
}
// Copy voiceover.js to the output/js directory
err = copyFile("js/voiceover.js", filepath.Join(jsDir, "voiceover.js"))
if err != nil {
fmt.Println("Error copying voiceover.js:", err)
return
}
// Copy styles.css to output directory
err = copyFile(stylesFile, filepath.Join(outputDir, stylesFile))
if err != nil {
fmt.Println("Error copying styles.css:", err)
return
}
// Process Markdown files
files, err := os.ReadDir(markdownDir)
if err != nil {
fmt.Println("Error reading markdown directory:", err)
return
}
for _, file := range files {
if strings.HasSuffix(file.Name(), ".md") {
processMarkdownFile(markdownDir, outputDir, template, file.Name())
}
}
fmt.Println("### Markdown processing complete. ###")
}
// parseMarkdown converts Markdown to HTML
func parseMarkdown(input string) (string, []string) {
lines := strings.Split(input, "\n")
var htmlLines []string
var toc []string
var inList, inCodeBlock bool
// Regex patterns for Markdown elements
headingPattern := regexp.MustCompile(`^(#{1,6})\s+(.*)`)
blockQuotePattern := regexp.MustCompile(`^>\s+(.*)`)
ulPattern := regexp.MustCompile(`^[-*+]\s+(.*)`)
olPattern := regexp.MustCompile(`^\d+\.\s+(.*)`)
hrPattern := regexp.MustCompile(`^---$`)
for _, line := range lines {
line = strings.TrimSpace(line)
if line == "" {
if inList {
htmlLines = append(htmlLines, "</ul>")
inList = false
}
continue
}
switch {
case hrPattern.MatchString(line):
htmlLines = append(htmlLines, "<hr>")
case headingPattern.MatchString(line):
matches := headingPattern.FindStringSubmatch(line)
level := len(matches[1]) // Number of # defines heading level
content := matches[2]
if level == 2 { // Only add h2 headings to TOC
anchor := strings.ReplaceAll(strings.ToLower(content), " ", "-")
htmlLines = append(htmlLines, fmt.Sprintf(`<h%d id="%s">%s</h%d>`, level, anchor, content, level))
toc = append(toc, fmt.Sprintf(`<a href="#%s">%s</a>`, anchor, content))
} else {
htmlLines = append(htmlLines, fmt.Sprintf("<h%d>%s</h%d>", level, content, level))
}
case blockQuotePattern.MatchString(line):
matches := blockQuotePattern.FindStringSubmatch(line)
content := matches[1]
htmlLines = append(htmlLines, fmt.Sprintf("<blockquote>%s</blockquote>", parseInline(content)))
case ulPattern.MatchString(line):
if !inList {
htmlLines = append(htmlLines, "<ul>")
inList = true
}
matches := ulPattern.FindStringSubmatch(line)
content := matches[1]
htmlLines = append(htmlLines, fmt.Sprintf("<li>%s</li>", parseInline(content)))
case olPattern.MatchString(line):
if !inList {
htmlLines = append(htmlLines, "<ol>")
inList = true
}
matches := olPattern.FindStringSubmatch(line)
content := matches[1]
htmlLines = append(htmlLines, fmt.Sprintf("<li>%s</li>", parseInline(content)))
case strings.HasPrefix(line, "```"):
if !inCodeBlock {
htmlLines = append(htmlLines, "<pre><code>")
inCodeBlock = true
} else {
htmlLines = append(htmlLines, "</code></pre>")
inCodeBlock = false
}
default:
if inCodeBlock {
htmlLines = append(htmlLines, escapeHTML(line))
} else {
htmlLines = append(htmlLines, fmt.Sprintf("<p>%s</p>", parseInline(line)))
}
}
}
if inList {
htmlLines = append(htmlLines, "</ul>")
}
return strings.Join(htmlLines, "\n"), toc
}
// parseInline handles inline Markdown elements
func parseInline(input string) string {
// Replace patterns for bold, italic, and links
input = regexp.MustCompile(`\*\*(.*?)\*\*`).ReplaceAllString(input, "<strong>$1</strong>")
input = regexp.MustCompile(`\*(.*?)\*`).ReplaceAllString(input, "<em>$1</em>")
input = regexp.MustCompile(`\[(.*?)\]\((.*?)\)`).ReplaceAllString(input, `<a href="$2">$1</a>`)
return input
}
// escapeHTML escapes HTML characters
func escapeHTML(input string) string {
replacements := map[string]string{
"&": "&",
"<": "<",
">": ">",
"\"": """,
"'": "'",
}
for old, new := range replacements {
input = strings.ReplaceAll(input, old, new)
}
return input
}
// copyFile copies a file from src to dst
func copyFile(src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()
destinationFile, err := os.Create(dst)
if err != nil {
return err
}
defer destinationFile.Close()
_, err = io.Copy(destinationFile, sourceFile)
return err
}
// processMarkdownFile processes a Markdown file into HTML
func processMarkdownFile(markdownDir, outputDir string, template []byte, fileName string) {
inputFilePath := filepath.Join(markdownDir, fileName)
markdownFile, err := os.Open(inputFilePath)
if err != nil {
fmt.Println("Error opening file:", fileName, err)
return
}
defer markdownFile.Close()
markdownContent, err := io.ReadAll(markdownFile)
if err != nil {
fmt.Println("Error reading file:", fileName, err)
return
}
htmlContent, headings := parseMarkdown(string(markdownContent))
toc := strings.Join(headings, "\n")
title := strings.TrimSuffix(fileName, ".md")
if title == "home" {
title = "index"
}
outputFileName := fmt.Sprintf("%s.html", title)
outputFilePath := filepath.Join(outputDir, outputFileName)
finalContent := strings.ReplaceAll(string(template), "{{title}}", strings.Title(title))
finalContent = strings.ReplaceAll(finalContent, "{{content}}", htmlContent)
finalContent = strings.ReplaceAll(finalContent, "{{toc}}", toc)
err = os.WriteFile(outputFilePath, []byte(finalContent), os.ModePerm)
if err != nil {
fmt.Println("Error writing file:", outputFileName, err)
return
}
fmt.Println("Generated:", outputFileName)
}