diff --git a/README.md b/README.md index 438d9a1..9a281f9 100644 --- a/README.md +++ b/README.md @@ -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" diff --git a/cmd/code2img/main.go b/cmd/code2img/main.go index fae604a..f7d9466 100644 --- a/cmd/code2img/main.go +++ b/cmd/code2img/main.go @@ -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 diff --git a/code2img.go b/code2img.go index cbe111d..d6d8f30 100644 --- a/code2img.go +++ b/code2img.go @@ -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& @@ -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 @@ -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, +} diff --git a/code2img_test.go b/code2img_test.go index 6550e8d..50b5392 100644 --- a/code2img_test.go +++ b/code2img_test.go @@ -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) } diff --git a/example/code.png b/example/code.png index 89389a3..afd09c6 100644 Binary files a/example/code.png and b/example/code.png differ diff --git a/example/main.go b/example/main.go index d7ee9be..aa7a090 100644 --- a/example/main.go +++ b/example/main.go @@ -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) }