Skip to content

Commit 26b3a5f

Browse files
committed
all: add language customization
1 parent f020c25 commit 26b3a5f

File tree

6 files changed

+28
-6
lines changed

6 files changed

+28
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# code2img [![PkgGoDev](https://pkg.go.dev/badge/golang.design/x/code2img)](https://pkg.go.dev/golang.design/x/code2img) ![](https://changkun.de/urlstat?mode=github&repo=golang-design/code2img)
22

3-
a carbon service wrapper for Go users
3+
A carbon-now wrapper for Go users and supports for iOS Shortcut
44

55
```go
66
import "golang.design/x/code2img"

cmd/code2img/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func main() {
4747
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
4848
defer cancel()
4949

50-
buf, err := code2img.Render(ctx, b.Code)
50+
buf, err := code2img.Render(ctx, code2img.LangGo, b.Code)
5151
if err != nil {
5252
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
5353
return

code2img.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ import (
1919
"github.com/chromedp/chromedp"
2020
)
2121

22+
// Lang is represents a language
23+
type Lang string
24+
25+
// All kinds of languages
26+
const (
27+
LangAuto Lang = "auto"
28+
LangGo Lang = "text/x-go"
29+
LangDiff Lang = "text/x-diff"
30+
)
31+
2232
// Render renders the given code string and returns a binary buffer
2333
// that contains a carbon-now based image.
24-
func Render(ctx context.Context, code string) ([]byte, error) {
34+
func Render(ctx context.Context, lang Lang, code string) ([]byte, error) {
2535
// https://carbon.now.sh/?
2636
// bg=rgba(74%2C144%2C226%2C1)&
2737
// t=material&
@@ -43,11 +53,15 @@ func Render(ctx context.Context, code string) ([]byte, error) {
4353
// es=2x&
4454
// wm=false&
4555
// code=func%2520main()
56+
if _, ok := supportedLang[lang]; !ok {
57+
lang = LangAuto
58+
}
59+
4660
var carbonOptions = map[string]string{
4761
"bg": "rgba(74,144,226,1)", // backgroundColor
4862
"t": "material", // theme
4963
"wt": "none", // windowTheme
50-
"l": "auto", // language
64+
"l": string(lang), // language
5165
"ds": "true", // dropShadow
5266
"dsyoff": "0px", // dropShadowOffsetY
5367
"dsblur": "29px", // dropShadowBlurRadius
@@ -146,3 +160,11 @@ func screenshot(sel interface{}, picbuf *[]byte, opts ...chromedp.QueryOption) c
146160
return nil
147161
}, append(opts, chromedp.NodeVisible)...)
148162
}
163+
164+
// if we need more languages, see:
165+
// https://github.com/carbon-app/carbon/blob/c2357e63efb60330f1c9ef84be4df2e8e94f661c/lib/constants.js
166+
var supportedLang = map[Lang]int{
167+
LangAuto: 0,
168+
LangGo: 0,
169+
LangDiff: 0,
170+
}

code2img_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func TestRender(t *testing.T) {
2222
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
2323
defer cancel()
2424

25-
got, err := code2img.Render(ctx, `import "golang.design/x/code2img"`)
25+
got, err := code2img.Render(ctx, code2img.LangGo, `import "golang.design/x/code2img"`)
2626
if err != nil {
2727
t.Fatalf("render failed: %v", err)
2828
}

example/code.png

963 Bytes
Loading

example/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func main() {
1515
code := string(f)
1616

1717
// render it!
18-
b, err := code2img.Render(context.TODO(), code)
18+
b, err := code2img.Render(context.TODO(), code2img.LangGo, code)
1919
if err != nil {
2020
panic(err)
2121
}

0 commit comments

Comments
 (0)