-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
95 lines (86 loc) · 2.16 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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)
type Markdown struct {
Path string
cmd *exec.Cmd
}
func NewMarkdown(path string) *Markdown {
return &Markdown{
Path: path,
}
}
func (m *Markdown) setCmd(cmd string, args string) {
m.cmd = exec.Command("bash", "-c", fmt.Sprintf("%s %s", cmd, args))
}
func (m Markdown) ListFiles() []string {
m.setCmd("ls", m.Path)
results, e := m.cmd.CombinedOutput()
if e != nil {
log.Println(e)
return nil
}
str := string(results)
replacer := strings.NewReplacer("\n", " ", "\t", " ")
list := strings.Split(replacer.Replace(str), " ")
var response []string
for _, i := range list {
if strings.Contains(i, ".md") {
response = append(response, i)
}
}
return response
}
func (m Markdown) ToDoc(source string, dst string) {
os.RemoveAll(dst)
os.MkdirAll(dst, os.ModePerm)
rename := func(v string) string {
replacer := strings.NewReplacer(".md", ".docx")
return replacer.Replace(v)
}
for _, i := range m.ListFiles() {
log.Println("pandoc", fmt.Sprintf(" -s %s/%s -o %s/%s", source, i, dst, rename(i)))
m.setCmd("pandoc", fmt.Sprintf(" -s %s/%s -o %s/%s", source, i, dst, rename(i)))
m.cmd.CombinedOutput()
}
}
func (m Markdown) ToHtml(source string, dst string) {
os.RemoveAll(dst)
os.MkdirAll(dst, os.ModePerm)
rename := func(v string) string {
replacer := strings.NewReplacer(".md", ".html")
return replacer.Replace(v)
}
for _, i := range m.ListFiles() {
log.Println("pandoc", fmt.Sprintf(" --ascii %s/%s -o %s/%s", source, i, dst, rename(i)))
m.setCmd("pandoc", fmt.Sprintf(" --ascii %s/%s -o %s/%s", source, i, dst, rename(i)))
m.cmd.CombinedOutput()
}
}
func (m Markdown) ToPdf(dst string) {
// pandoc not work
}
func (m Markdown) Copy(source string, dst string) {
os.RemoveAll(dst)
os.MkdirAll(dst, os.ModePerm)
for _, i := range m.ListFiles() {
m.setCmd("cp", fmt.Sprintf("%s/%s %s", source, i, dst))
m.cmd.CombinedOutput()
}
}
func main() {
md := NewMarkdown("./Book")
results := md.ListFiles()
fmt.Println(results)
// copy
md.Copy("./Book", "./BookTest/markdown")
// todocx
md.ToDoc("./Book", "./BookTest/docx")
// tohtml
md.ToHtml("./Book", "./BookTest/html")
}