-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
378 lines (329 loc) · 7.66 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2015 The present-tex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Command present-tex generates LaTeX/Beamer slides from present.
//
// Usage of present-tex:
//
// $ present-tex [options] [input-file [output.tex]]
//
// Examples:
// $ present-tex input.slide > out.tex
// $ present-tex input.slide out.tex
// $ present-tex < input.slide > out.tex
//
// Options:
// -base="": base path for slide templates
package main
import (
"bytes"
"flag"
"fmt"
"html"
"html/template"
"io"
"io/fs"
"log"
"os"
"strings"
"golang.org/x/tools/present"
)
var (
tmpl *template.Template // beamer template
hasCode = false // whether the .slide has a .code or .play directive
beamerTheme = flag.String("beamer-theme", "default", "Beamer theme to use (e.g: Berkeley, Madrid, ...)")
dpi = flag.Int("dpi", 72, "DPI resolution to use for PDF")
)
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, `%[1]s - generates LaTeX/Beamer slides from present.
Usage of %[1]s:
$ %[1]s [options] [input-file [output.tex]]
Examples:
$ %[1]s input.slide > out.tex
$ %[1]s input.slide out.tex
$ %[1]s < input.slide > out.tex
Options:
`,
os.Args[0],
)
flag.PrintDefaults()
}
log.SetFlags(0)
log.SetPrefix("present-tex: ")
tmpldirFlag := flag.String("base", "", "base path for slide templates")
flag.Parse()
var tmpldir = func() fs.FS {
o, err := fs.Sub(tmplFS, "templates")
if err != nil {
log.Fatalf("could not locate embedded 'templates' directory: %+v", err)
}
return o
}()
if *tmpldirFlag != "" {
tmpldir = os.DirFS(*tmpldirFlag)
}
var (
r io.Reader
w io.Writer
input = "stdin"
output = "stdout"
)
switch flag.NArg() {
case 0:
r = os.Stdin
w = os.Stdout
case 1:
input = flag.Arg(0)
f, err := os.Open(input)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.Printf("input: [%s]...\n", input)
r = f
w = os.Stdout
case 2:
input = flag.Arg(0)
f, err := os.Open(input)
if err != nil {
log.Fatal(err)
}
defer f.Close()
log.Printf("input: [%s]...\n", input)
output = flag.Arg(1)
log.Printf("output: [%s]...\n", output)
tex, err := os.Create(output)
if err != nil {
log.Fatalf("could not create output file [%s]: %v\n", output, err)
}
defer func() {
err = tex.Close()
if err != nil {
log.Fatalf("could not close output file [%s]: %v\n", output, err)
}
}()
r = f
w = tex
default:
flag.Usage()
os.Exit(2)
}
err := xmain(w, r, input, tmpldir)
if err != nil {
log.Fatalf("could not run present-tex: %+v", err)
}
}
func xmain(w io.Writer, r io.Reader, input string, tmpldir fs.FS) error {
ctx := present.Context{
ReadFile: os.ReadFile,
Render: renderAsLaTeX,
}
doc, err := ctx.Parse(r, input, 0)
if err != nil {
return fmt.Errorf("could not parse input document: %w", err)
}
err = parseImages(doc)
if err != nil {
return fmt.Errorf("could not parse images: %w", err)
}
err = parseCode(doc)
if err != nil {
return fmt.Errorf("could not parse code fragments: %w", err)
}
tmpl, err = initTemplates(tmpldir)
if err != nil {
return fmt.Errorf("could not parse templates: %w", err)
}
buf := new(bytes.Buffer)
err = doc.Render(buf, tmpl)
if err != nil {
return fmt.Errorf("could not render document: %w", err)
}
out := []byte(html.UnescapeString(buf.String()))
_, err = w.Write(out)
if err != nil {
return fmt.Errorf("could not fill output: %w", err)
}
return nil
}
func initTemplates(root fs.FS) (*template.Template, error) {
tmpl := template.New("").Funcs(funcs).Delims("<<", ">>")
_, err := tmpl.ParseFS(root, "beamer.tmpl")
if err != nil {
return nil, err
}
return tmpl, err
}
// renderElem implements the elem template function, used to render
// sub-templates.
func renderElem(t *template.Template, e present.Elem) (template.HTML, error) {
var data interface{} = e
if s, ok := e.(present.Section); ok {
data = struct {
present.Section
Template *template.Template
}{s, t}
}
return execTemplate(t, e.TemplateName(), data)
}
var (
funcs = template.FuncMap{}
)
func init() {
funcs["elem"] = renderElem
funcs["stringFromBytes"] = func(raw []byte) string { return string(raw) }
funcs["join"] = func(lines []string) string { return strings.Join(lines, "\n") }
funcs["nodot"] = func(s string) string {
if strings.HasPrefix(s, ".") {
return s[1:]
}
return s
}
tex1 := strings.NewReplacer(
"&", `\&`,
"$", `\$`,
"^", `\^{}`,
"%", `\%`,
"~", `$\sim$`,
"#", `\#`,
"{", `\{`,
"}", `\}`,
`\`, `\textbackslash`,
"é", `\'e`,
"è", `\`+"`e",
"à", `\`+"`a",
"ù", `\`+"`u",
"â", `\^a`,
"ê", `\^e`,
"î", `\^i`,
"ô", `\^o`,
"û", `\^u`,
"ä", `\"a`,
"ë", `\"e`,
"ï", `\"i`,
"ü", `\"u`,
"ÿ", `\"y`,
"ç", `\c{c}`,
"Ç", `\c{C}`,
"œ", `\oe `,
">", `$>$`,
"<", `$<$`,
">=", `$\geq$`,
"<=", `$\leq$`,
"->", `$\rightarrow$`,
"=>", `$\Rightarrow$`,
)
tex2 := strings.NewReplacer(
"_", `\_`,
)
style := func(s string) string {
s = tex1.Replace(s)
s = string(renderStyle(s))
s = tex2.Replace(s)
return s
}
funcs["style"] = style
funcs["beamerTheme"] = func() string {
return *beamerTheme
}
funcs["hasCode"] = func() bool {
return hasCode
}
funcs["pdfAuthor"] = func(authors []present.Author) string {
out := make([]string, 0, len(authors))
for _, a := range authors {
name, _, _ := parseAuthor(a)
if name == "" {
continue
}
out = append(out, fmt.Sprintf("pdfauthor={%s},%%\n", style(name)))
}
return strings.Join(out, " ")
}
funcs["texAuthor"] = func(authors []present.Author) string {
const hdr = "\\parbox{0.26\\textwidth}{\n\t\\texorpdfstring\n\t {\n\t\t\\centering\n"
out := make([]string, 0, len(authors))
shorts := make([]string, 0, len(authors))
for _, a := range authors {
elems := renderAuthor(a)
if len(elems) == 0 {
continue
}
name := elems[0]
if name == "" {
continue
}
if len(shorts) > 0 {
shorts = append(shorts, "\\&")
}
shorts = append(shorts, name)
if len(out) > 0 {
out = append(out, "\\and %\n")
}
out = append(out, hdr)
for _, elem := range elems {
out = append(out, "\t\t"+elem+` \\`+"\n")
}
out = append(out, "\t }\n\t{"+elems[0]+"}\n}\n")
}
if len(out) > 0 {
out = append([]string{"\\author[" + strings.Join(shorts, " ") + "]{\n"}, out...)
out = append(out, "}\n")
}
return strings.Join(out, " ")
}
}
func renderAuthor(author present.Author) []string {
var elems []string
if len(author.Elem) == 0 {
return elems
}
for _, e := range author.Elem {
str, err := renderElem(tmpl, e)
if err != nil {
log.Fatal(err)
}
elem := html.UnescapeString(string(str))
elem = strings.Trim(elem, "\n")
elems = append(elems, elem)
}
return elems
}
func parseAuthor(author present.Author) (name string, inst string, mail present.Link) {
elems := author.TextElem()
if len(elems) == 0 {
return
}
getLines := func(i int) []string {
lines := elems[i].(present.Text).Lines
return lines
}
name = strings.TrimSpace(getLines(0)[0])
if name == "" {
return
}
if len(elems) > 1 {
inst = strings.TrimSpace(getLines(1)[0])
}
for _, elem := range author.Elem {
link, ok := elem.(present.Link)
if !ok {
continue
}
if strings.Contains(link.Label, "@") {
mail = link
}
}
return
}
// execTemplate is a helper to execute a template and return the output as a
// template.HTML value.
func execTemplate(t *template.Template, name string, data interface{}) (template.HTML, error) {
b := new(bytes.Buffer)
err := t.ExecuteTemplate(b, name, data)
if err != nil {
return "", err
}
return template.HTML(b.String()), nil
}