-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcodepicdeck.go
107 lines (98 loc) · 2.44 KB
/
codepicdeck.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
// codepicdeck: make code+pic slide decks
package main
import (
"flag"
"fmt"
"image"
_ "image/png"
"os"
"strings"
"github.com/ajstarks/deck/generate"
)
// expand tabs to spaces, and escape XML
var codemap = strings.NewReplacer(
"\t", " ",
"<", "<",
">", ">",
"&", "&")
// includefile returns the content of a file as a tab-expanded, XML-escaped string
func includefile(filename string) string {
data, err := os.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return ""
}
return codemap.Replace(string(data))
}
// imagesize returns the dimensions (w,h) of an image file
func imagesize(filename string) (int, int) {
f, err := os.Open(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 0, 0
}
defer f.Close()
img, _, err := image.DecodeConfig(f)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return 0, 0
}
return img.Width, img.Height
}
// index makes an image index slide
func index(deck *generate.Deck, filenames []string, title string) {
deck.StartSlide()
x, y := 5.0, 90.0
for _, f := range filenames {
imagefile := swapext(f, ".go", ".png")
deck.Image(x, y, 72, 72, imagefile, "")
deck.TextMid(x, y-7, f, "sans", 1, "black")
x += 10.0
if x > 95 {
x = 5.0
y -= 20.0
}
}
deck.TextMid(50, 96, title, "sans", 2, "black")
deck.EndSlide()
}
// swapext swaps the specified file extensions
func swapext(s, fromext, toext string) string {
i := strings.LastIndex(s, fromext)
if i < 0 {
return ""
}
return s[:i] + toext
}
// codepic makes a code and picture slides
func codepic(deck *generate.Deck, filenames []string) {
slide := 0
for _, codefile := range filenames {
imagefile := swapext(codefile, ".go", ".png")
if len(imagefile) == 0 {
fmt.Fprintf(os.Stderr, "cannot get the basename for %s\n", codefile)
continue
}
imw, imh := imagesize(imagefile)
slide++
deck.StartSlide()
deck.Image(75, 68, imw, imh, imagefile, "")
deck.Text(2.5, 96, includefile(codefile), "mono", 1.2, "black")
deck.TextEnd(90, 2.5, codefile, "sans", 2, "black")
deck.TextEnd(95, 2.5, fmt.Sprintf("[%d]", slide), "sans", 2, "gray")
deck.EndSlide()
}
}
func main() {
doindex := flag.Bool("index", true, "generates an index slide")
title := flag.String("title", "", "index title")
flag.Parse()
files := flag.Args()
deck := generate.NewSlides(os.Stdout, 0, 0)
deck.StartDeck()
if *doindex {
index(deck, files, *title)
}
codepic(deck, files)
deck.EndDeck()
}