-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
307 lines (271 loc) · 7.2 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/andybalholm/cascadia"
"github.com/mattn/godown"
"github.com/otiai10/copy"
"github.com/pkg/errors"
"golang.org/x/net/html"
)
var method string
var stuffToRemove []string
var globalRegexesToReplace = map[*regexp.Regexp]string{
regexp.MustCompile("\\n\\n\\n"): "\n\n",
}
var pandocRegexesToReplace = map[*regexp.Regexp]string{
regexp.MustCompile("^:::.*$"): "",
regexp.MustCompile("^``` .*"): "```",
regexp.MustCompile("[{][.#][A-Za-z0-9-]+?[}]"): "",
regexp.MustCompile("(\\W|^)\\[(.*)]"): "__$2__",
}
var godownRegexesToReplace = map[*regexp.Regexp]string{
regexp.MustCompile("^<div>$"): "",
}
func replaceAll(str string, regex *regexp.Regexp, repl string) string {
var result strings.Builder
result.Grow(len(str))
scanner := bufio.NewScanner(strings.NewReader(str))
for scanner.Scan() {
result.WriteString(regex.ReplaceAllString(scanner.Text(), repl))
result.WriteRune('\n')
}
if err := scanner.Err(); err != nil {
panic(err)
}
return result.String()
}
func postProcessMarkdown(markdown string) string {
if method == "pandoc" {
markdown = strings.ReplaceAll(markdown, "\n\\\n", "\n")
}
for _, thingToRemove := range stuffToRemove {
markdown = strings.ReplaceAll(markdown, thingToRemove, "")
}
var regexesToReplace map[*regexp.Regexp]string
switch method {
case "pandoc":
regexesToReplace = pandocRegexesToReplace
case "godown":
regexesToReplace = godownRegexesToReplace
}
for regex, repl := range regexesToReplace {
markdown = replaceAll(markdown, regex, repl)
}
for regex, repl := range globalRegexesToReplace {
markdown = regex.ReplaceAllString(markdown, repl)
}
return markdown
}
func createElement(HTML string) *html.Node {
elem, err := html.Parse(strings.NewReader(HTML))
if err != nil {
panic(err)
}
return elem
}
func preProcessHTML(node *html.Node) {
var newAttrs []html.Attribute
for _, attr := range node.Attr {
keep := true
switch attr.Key {
case "id", "class", "style":
keep = false
case "href":
if strings.HasPrefix(attr.Val, "http://") || strings.HasPrefix(attr.Val, "https://") {
break
}
if strings.HasSuffix(attr.Val, ".html") {
// Remove .html so that we have a proper reference
attr.Val = strings.TrimSuffix(attr.Val, ".html")
}
}
if keep {
newAttrs = append(newAttrs, attr)
}
}
node.Attr = newAttrs
child := node.FirstChild
for child != nil {
preProcessHTML(child)
child = child.NextSibling
}
}
func processFilePandoc(filePath string, outputFile string) error {
f, err := os.Open(filePath)
if err != nil {
return errors.Wrap(err, "could not open file "+filePath)
}
HTML, err := html.Parse(f)
if err != nil {
return errors.Wrap(err, "could not parse HTML in "+filePath)
}
_ = f.Close()
contentView := cascadia.MustCompile("#main-content > *").MatchAll(HTML)
pandocCommand := exec.Command(
"pandoc",
"-r", "html", "-w", "markdown",
)
stdin, err := pandocCommand.StdinPipe()
if err != nil {
return errors.Wrap(err,
"failed to open pipe to stdin of pandoc")
}
stdout, err := pandocCommand.StdoutPipe()
if err != nil {
return errors.Wrap(err,
"failed to open pipe to stdout of pandoc")
}
err = pandocCommand.Start()
if err != nil {
return errors.Wrap(err,
"could not start pandoc command with params: "+strings.Join(pandocCommand.Args, " "))
}
var markdownOutput string
var readWaiter sync.WaitGroup
readWaiter.Add(1)
var readErr error
go func() {
var byt []byte
byt, readErr = ioutil.ReadAll(stdout)
if readErr == nil {
markdownOutput = string(byt)
}
readWaiter.Done()
}()
for _, elem := range contentView {
preProcessHTML(elem)
err := html.Render(stdin, elem)
if err != nil {
fmt.Printf("Warning: failed to render element %v in file %s, is it malformed?\n%v\n",
elem, filePath, err)
}
}
_ = stdin.Close()
readWaiter.Wait()
if readErr != nil {
return errors.Wrap(readErr, "failed reading from pandoc output")
}
err = ioutil.WriteFile(outputFile, []byte(postProcessMarkdown(markdownOutput)), 0640)
if err != nil {
return errors.Wrap(err, "failed writing file "+outputFile)
}
return nil
}
func processFileGodown(filePath string, outputFile string) error {
f, err := os.Open(filePath)
if err != nil {
return errors.Wrap(err, "could not open file "+filePath)
}
HTML, err := html.Parse(f)
if err != nil {
return errors.Wrap(err, "could not parse HTML in "+filePath)
}
_ = f.Close()
contentView := cascadia.MustCompile("#main-content > *").MatchAll(HTML)
var markdownOutput strings.Builder
var htmlOutput strings.Builder
for _, elem := range contentView {
preProcessHTML(elem)
err := html.Render(&htmlOutput, elem)
if err != nil {
fmt.Printf("Warning: failed to render element %v in file %s, is it malformed?\n%v\n",
elem, filePath, err)
}
}
err = godown.Convert(&markdownOutput, strings.NewReader(htmlOutput.String()), nil)
if err != nil {
return errors.Wrap(err, "failed to convert html to markdown")
}
err = ioutil.WriteFile(outputFile, []byte(postProcessMarkdown(markdownOutput.String())), 0640)
if err != nil {
return errors.Wrap(err, "failed writing file "+outputFile)
}
return nil
}
func main() {
if len(os.Args) <= 2 {
fmt.Printf("Usage: %s <source dir> <dest dir> [method]\n", os.Args[0])
fmt.Println("Methods:")
fmt.Println("\tpandoc")
fmt.Println("\tgodown (recommended)")
os.Exit(1)
}
sourceDir := os.Args[1]
if !DirectoryExists(sourceDir) {
fmt.Printf("Source directory %s does not exist\n", sourceDir)
os.Exit(1)
}
destDir := os.Args[2]
if !DirectoryExists(destDir) {
fmt.Printf("Destination directory %s does not exist\n", destDir)
os.Exit(1)
}
if len(os.Args) == 4 {
method = os.Args[3]
} else {
method = "pandoc"
}
fmt.Println("Using method", method)
err := filepath.Walk(sourceDir, func(filePath string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if !strings.EqualFold(filepath.Ext(info.Name()), ".html") {
return nil
}
abs, err := filepath.Abs(filePath)
if err != nil {
fmt.Println(err)
return nil
}
relativeToSourceDir, err := filepath.Rel(sourceDir, filePath)
if err != nil {
fmt.Println(err)
return nil
}
outputFile := path.Join(destDir, ReplaceExtension(relativeToSourceDir, "md"))
suffixIndex := 0
for FileExists(outputFile) {
suffixIndex++
outputFile = fmt.Sprintf("%s_%d%s",
RemoveExtension(outputFile),
suffixIndex,
path.Ext(outputFile),
)
}
if method == "pandoc" {
err = processFilePandoc(abs, outputFile)
} else {
err = processFileGodown(abs, outputFile)
}
if err != nil {
fmt.Printf("Failed to process file (%v) %s -> %s, skipping\n", err, abs, outputFile)
return nil
}
fmt.Printf("File %s successfully processed, result: %s\n", filePath, outputFile)
return nil
})
if err != nil {
fmt.Printf("Error during walk: %v\n", err)
os.Exit(1)
}
attachmentsDir := path.Join(sourceDir, "attachments")
if DirectoryExists(attachmentsDir) {
fmt.Println("Copying attachments...")
err := copy.Copy(attachmentsDir, path.Join(destDir, "attachments"))
if err != nil {
fmt.Print(err)
os.Exit(1)
}
}
fmt.Println("Done.")
}