This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
202 lines (171 loc) · 3.54 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
package main
import (
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/skanehira/clipboard-image/v2"
"golang.org/x/term"
)
var version = "1.2.0"
func exitErr(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
type options struct {
useClipboard bool
source string
output string
ext string
theme string
}
func main() {
name := "code2img"
fs := flag.NewFlagSet(name, flag.ContinueOnError)
fs.SetOutput(os.Stderr)
fs.Usage = func() {
fs.SetOutput(os.Stdout)
fmt.Printf(`%[1]s - generate image of source code
Version: %s
Usage:
$ %[1]s -t monokai main.go main.png
$ echo 'fmt.Println("Hello World")' | %[1]s -ext go -t native -o sample.png
$ %[1]s -c main.go
-t color theme(default: solarized-dark)
-o output file name(default: out.png)
-c copy to clipboard
-l print line
-ext file extension
`, name, version)
}
theme := fs.String("t", "solarized-dark", "")
ext := fs.String("ext", "", "")
output := fs.String("o", "out.png", "")
useClipboard := fs.Bool("c", false, "")
printLine := fs.Bool("l", false, "")
if err := fs.Parse(os.Args[1:]); err != nil {
if err == flag.ErrHelp {
return
}
os.Exit(1)
}
var src io.Reader
// if use stdin, then require those argments
if !term.IsTerminal(int(os.Stdin.Fd())) {
if *ext == "" {
fs.Usage()
os.Exit(1)
}
src = os.Stdin
} else {
args := fs.Args()
if len(args) < 1 {
fs.Usage()
os.Exit(1)
}
in := args[0]
*ext = filepath.Ext(in)[1:]
var err error
src, err = os.Open(in)
if err != nil {
exitErr(err)
}
*ext = filepath.Ext(args[0])[1:]
if !*useClipboard && len(args) > 1 {
*output = args[1]
}
}
buf := &bytes.Buffer{}
if _, err := io.Copy(buf, src); err != nil {
exitErr(err)
}
source := buf.String()
fontSize := 20
w, h, lw := getSize(*printLine, source, fontSize)
formatters.Register("png", &pngFormat{
width: w,
height: h,
lineWidth: lw,
fontSize: fontSize,
printLine: *printLine,
})
opts := options{
useClipboard: *useClipboard,
source: source,
output: *output,
ext: *ext,
theme: *theme,
}
if err := drawImage(opts); err != nil {
exitErr(err)
}
}
func drawImage(opt options) error {
buf, err := highlight(opt)
if err != nil {
return err
}
if opt.useClipboard {
return clipboard.Write(buf)
}
tmp, err := ioutil.TempFile("", "")
if err != nil {
return err
}
if _, err := io.Copy(tmp, buf); err != nil {
return err
}
tmp.Close()
return os.Rename(tmp.Name(), opt.output)
}
func highlight(opt options) (io.Reader, error) {
l := lexers.Get(opt.ext)
if l == nil {
l = lexers.Analyse(opt.source)
}
if l == nil {
l = lexers.Fallback
}
l = chroma.Coalesce(l)
f := formatters.Get("png")
if f == nil {
f = formatters.Fallback
}
s := styles.Get(opt.theme)
if s == nil {
s = styles.Fallback
}
it, err := l.Tokenise(nil, opt.source)
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
if err := f.Format(buf, s, it); err != nil {
return nil, err
}
return buf, nil
}
func getSize(printLine bool, s string, fontSize int) (w int, h int, lw int) {
lines := strings.Split(s, "\n")
for _, s := range lines {
ww := len(s) + strings.Count(s, "\t")*3
if ww > w {
w = ww
}
h++
}
if printLine {
lw = len(strconv.Itoa(len(lines)))
w = w + lw
}
return (w + 6) * 10, (h + 1) * fontSize, lw
}