-
Notifications
You must be signed in to change notification settings - Fork 1
/
slideleech.go
268 lines (211 loc) · 6.29 KB
/
slideleech.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
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"html/template"
"io/ioutil"
"os"
"gopkg.in/yaml.v2"
)
var outputDir string
var inputFile string
var outputFile string
var templateFile string
var revealTemplColor string
var revealIntro string
var revealIntroAuthor string
var revealIntroTitle string
var revealIntroColor string
var revealClosing string
var revealClosingMsg string
var revealClosingColor string
type SlideEntry struct {
Content string
Color string
}
type Config struct {
InputFile string `yaml:"input_file"`
OutputDir string `yaml:"output_directory"`
OutputFile string `yaml:"output_filename"`
Reveal struct {
Templ string `yaml:"template"`
TemplColor string `yaml:"template_color"`
Intro string `yaml:"intro"`
IntroTitle string `yaml:"intro_title"`
IntroAuthor string `yaml:"intro_author"`
IntroColor string `yaml:"intro_color"`
Closing string `yaml:"closing"`
ClosingMsg string `yaml:"closing_message"`
ClosingColor string `yaml:"closing_color"`
}
}
func (c *Config) Parse(data []byte) error {
err := yaml.Unmarshal(data, c)
check(err)
if c.InputFile == "" {
return errors.New("Slideleech config: invalid `input_file`")
}
if c.OutputDir == "" {
return errors.New("Slideleech config: invalid `output_directory`")
}
if c.OutputFile == "" {
return errors.New("Slideleech config: invalid `output_filename`")
}
inputFile = c.InputFile
outputDir = c.OutputDir
outputFile = c.OutputFile
templateFile = c.Reveal.Templ
revealTemplColor = c.Reveal.TemplColor
revealIntro = c.Reveal.Intro
revealIntroTitle = c.Reveal.IntroTitle
revealIntroAuthor = c.Reveal.IntroAuthor
revealIntroColor = c.Reveal.IntroColor
revealClosing = c.Reveal.Closing
revealClosingMsg = c.Reveal.ClosingMsg
revealClosingColor = c.Reveal.ClosingColor
return nil
}
func init() {
var config Config
yml, err := ioutil.ReadFile(".leech.yml")
check(err)
err2 := config.Parse(yml)
check(err2)
fmt.Println("Config:", config)
// flag.StringVar(&inputFile, "i", "./README.md", "input filename")
// flag.StringVar(&outputDir, "o", "./slides", "output directory")
// flag.StringVar(&templateFile, "t", "", "full path to RevealJS template")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "\n*****************************************\n"+
"This is the slideleech. It will extract "+
"your slide text/bullets contained in a markdown file.\n\n"+
"Enclose your slide text/bullets in "+
"`[item]: # (slide)` and `[item]: # (/slide)`.\n"+
"Any content between those tags will be added to your slide file.\n"+
"Include as many opening and closing tag pairs as you like "+
"in your Markdown.\n\n"+
"Edit a `.leech.yml` to configure your project\n\n")
flag.PrintDefaults()
}
}
func check(e error) {
if e != nil {
panic(e)
}
}
type specialSlide struct {
template string
fields map[string]string
}
func CreateSpecialSlide(outputFileName string, slide specialSlide) {
slideFile, err := os.Create(outputDir + "/" + outputFileName)
templ, err := template.New("").Parse(slide.template)
err = templ.Execute(slideFile, slide.fields)
check(err)
slideFile.Close()
}
func CreateIntroSlide(outputFileName string) {
m := make(map[string]string)
m["Title"] = revealIntroTitle
m["Author"] = revealIntroAuthor
var slide specialSlide
slide.template = INTRO_SLIDE
slide.fields = m
slideName := outputFileName + "0.md"
fmt.Printf("Creating intro slide... %s\n", slideName)
CreateSpecialSlide(slideName, slide)
}
func CreateClosingSlide(slideCount int, outputFileName string) {
m := make(map[string]string)
m["Message"] = revealClosingMsg
var slide specialSlide
slide.template = CLOSING_SLIDE
slide.fields = m
slideName := fmt.Sprintf(outputFileName+"%d.md", slideCount)
fmt.Printf("Creating closing slide... %s\n", slideName)
CreateSpecialSlide(slideName, slide)
}
func CreateSite(slideCount int, outputFileName string) {
var slides []SlideEntry
// add intro slide to slice
var intro SlideEntry
intro.Content = outputFileName + "0.md"
intro.Color = revealIntroColor
slides = append(slides, intro)
// Add content slides
for i := 1; i <= slideCount; i++ {
var slideName SlideEntry
slideName.Content = fmt.Sprintf(outputFileName+"%d.md", i)
slideName.Color = revealTemplColor
slides = append(slides, slideName)
}
// add closing slide to slice
var closing SlideEntry
closing.Content = fmt.Sprintf(outputFileName+"%d.md", slideCount+1)
closing.Color = revealClosingColor
slides = append(slides, closing)
// Include custom template based on a flag
templ := template.New("index.html")
if templateFile != "" {
fmt.Println("Creating RevealJS index.html from EXTERNAL template...")
templ, _ = templ.ParseFiles(templateFile)
} else {
fmt.Println("Creating RevealJS index.html from INTERNAL template...")
templ, _ = templ.Parse(INDEX_TEMPLATE)
}
// Save the template to the output directory
indexFile, err := os.Create(outputDir + "/" + outputFileName + ".html")
err = templ.Execute(indexFile, slides)
indexFile.Close()
check(err)
}
func CreateSlides(inputFile string, outputFileName string) int {
file, err := os.Open(inputFile)
check(err)
scanner := bufio.NewScanner(file)
var matching = false
var slideFile *os.File
var slideNum int
fmt.Println("Creating slides...")
for slideNum = 1; scanner.Scan(); {
value := scanner.Text()
if value == "[item]: # (slide)" {
matching = true
slideFileName := fmt.Sprintf(outputDir+"/"+outputFileName+"%d.md", slideNum)
fmt.Println(slideFileName)
var err error
slideFile, err = os.Create(slideFileName)
check(err)
continue
} else if value == "[item]: # (/slide)" {
matching = false
slideFile.Close()
slideNum++
}
if matching {
_, err := slideFile.Write([]byte(scanner.Text() + "\n"))
check(err)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
return slideNum
}
func main() {
flag.Parse()
fmt.Println("Output Directory:", outputDir)
fmt.Println("Input Filename:", inputFile)
fmt.Println("Template File:", templateFile)
// Make a directory for the slideshow
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
os.Mkdir(outputDir, 0755)
}
CreateIntroSlide(outputFile)
slideNum := CreateSlides(inputFile, outputFile)
CreateSite(slideNum-1, outputFile)
CreateClosingSlide(slideNum, outputFile)
}