Skip to content

Commit

Permalink
all: add language customization
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Feb 27, 2021
1 parent f020c25 commit 26b3a5f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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)

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

```go
import "golang.design/x/code2img"
Expand Down
2 changes: 1 addition & 1 deletion cmd/code2img/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

buf, err := code2img.Render(ctx, b.Code)
buf, err := code2img.Render(ctx, code2img.LangGo, b.Code)
if err != nil {
c.String(http.StatusBadRequest, fmt.Sprintf("Error: %s", err))
return
Expand Down
26 changes: 24 additions & 2 deletions code2img.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ import (
"github.com/chromedp/chromedp"
)

// Lang is represents a language
type Lang string

// All kinds of languages
const (
LangAuto Lang = "auto"
LangGo Lang = "text/x-go"
LangDiff Lang = "text/x-diff"
)

// Render renders the given code string and returns a binary buffer
// that contains a carbon-now based image.
func Render(ctx context.Context, code string) ([]byte, error) {
func Render(ctx context.Context, lang Lang, code string) ([]byte, error) {
// https://carbon.now.sh/?
// bg=rgba(74%2C144%2C226%2C1)&
// t=material&
Expand All @@ -43,11 +53,15 @@ func Render(ctx context.Context, code string) ([]byte, error) {
// es=2x&
// wm=false&
// code=func%2520main()
if _, ok := supportedLang[lang]; !ok {
lang = LangAuto
}

var carbonOptions = map[string]string{
"bg": "rgba(74,144,226,1)", // backgroundColor
"t": "material", // theme
"wt": "none", // windowTheme
"l": "auto", // language
"l": string(lang), // language
"ds": "true", // dropShadow
"dsyoff": "0px", // dropShadowOffsetY
"dsblur": "29px", // dropShadowBlurRadius
Expand Down Expand Up @@ -146,3 +160,11 @@ func screenshot(sel interface{}, picbuf *[]byte, opts ...chromedp.QueryOption) c
return nil
}, append(opts, chromedp.NodeVisible)...)
}

// if we need more languages, see:
// https://github.com/carbon-app/carbon/blob/c2357e63efb60330f1c9ef84be4df2e8e94f661c/lib/constants.js
var supportedLang = map[Lang]int{
LangAuto: 0,
LangGo: 0,
LangDiff: 0,
}
2 changes: 1 addition & 1 deletion code2img_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestRender(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

got, err := code2img.Render(ctx, `import "golang.design/x/code2img"`)
got, err := code2img.Render(ctx, code2img.LangGo, `import "golang.design/x/code2img"`)
if err != nil {
t.Fatalf("render failed: %v", err)
}
Expand Down
Binary file modified example/code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
code := string(f)

// render it!
b, err := code2img.Render(context.TODO(), code)
b, err := code2img.Render(context.TODO(), code2img.LangGo, code)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 26b3a5f

Please sign in to comment.