This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
168 lines (147 loc) · 3.41 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
package main
import (
"bytes"
"encoding/base64"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/akamensky/argparse"
"gitlab.com/golang-commonmark/markdown"
"go/build"
"golang.org/x/net/html"
"io"
"io/ioutil"
"log"
"mime"
"os"
"regexp"
"strings"
"text/template"
)
func EncodeBase64(src string) string {
f, err := ioutil.ReadFile(src)
if err != nil {
log.Fatal(err)
}
var out strings.Builder
encoder := base64.NewEncoder(base64.StdEncoding, &out)
encoder.Write(f)
encoder.Close()
extParts := strings.Split(src, ".")
ext := "." + extParts[1]
return fmt.Sprintf("data:%v;base64,%s", mime.TypeByExtension(ext), out.String())
}
func Base64Images(document string) string {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(document))
if err != nil {
log.Fatal(err)
}
doc.Find("img").Each(func(i int, s *goquery.Selection) {
s.Each(func(i int, sel *goquery.Selection) {
src, _ := sel.Attr("src")
src = EncodeBase64(src)
sel.SetAttr("src", src)
})
})
var b bytes.Buffer
html.Render(&b, doc.Selection.Nodes[0])
return b.String()
}
func ParseStyle(s, base string) string {
_, err := os.Open(s)
if err != nil {
path := base + "styles/" + s + ".css"
_, err := os.Open(path)
if err != nil {
log.Fatal(err)
} else {
return path
}
} else {
return s
}
return ""
}
func SplitAfter(s string, re *regexp.Regexp) []string {
var (
r []string
p int
)
is := re.FindAllStringIndex(s, -1)
if is == nil {
return append(r, s)
}
for index := range is {
if index == len(is)-1 {
r = append(r, s[p:])
} else {
r = append(r, s[p:is[index+1][0]])
p = is[index+1][0]
}
}
return append(r, s[p:])
}
func Slides(m []byte) []string {
md := markdown.New(markdown.XHTMLOutput(true))
rendered := md.RenderToString(m)
rendered = Base64Images(rendered)
splitExpr := regexp.MustCompile("<h1>.+</h1>")
return SplitAfter(rendered, splitExpr)
}
func main() {
type Data struct {
Slides []string
Style string
Script string
}
// Where our files are
base := build.Default.GOPATH + "/src/github.com/skuzzymiglet/point/"
// Parse args
parser := argparse.NewParser("point", "html presentation tool")
inFile := parser.String("i", "in", &argparse.Options{Required: true, Help: "Input file"})
style := parser.String("s", "style", &argparse.Options{Required: false, Default: "", Help: "Stylesheet"})
outFile := parser.String("o", "out", &argparse.Options{Required: false, Help: "Output file. Default: stdout"})
err := parser.Parse(os.Args)
if err != nil {
log.Fatal(err)
}
// Read files
inBytes, err := ioutil.ReadFile(*inFile)
if err != nil {
log.Fatal(err)
}
script, err := ioutil.ReadFile(base + "script.js")
if err != nil {
log.Fatal(err)
}
// Read style
var styleBytes []byte
if *style == "" {
styleBytes, err = ioutil.ReadFile(base + "style.css")
if err != nil {
log.Fatal(err)
}
} else {
styleBytes, err = ioutil.ReadFile(ParseStyle(*style, base))
if err != nil {
log.Fatal(err)
}
}
// Render markdown, embed images and split into slides
parts := Slides(inBytes)
tl := template.Must(template.ParseFiles(base + "template.html"))
// Output
var out io.Writer
if *outFile == "" {
out = os.Stdout
} else {
out, err = os.Create(*outFile)
if err != nil {
log.Fatal(err)
}
}
// Insert slides, style and script into template
err = tl.Execute(out, Data{parts, string(styleBytes), string(script)})
if err != nil {
log.Fatal(err)
}
}