From aeda17f93aa891134a10ea2dde347063e48e2ce7 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 14 Dec 2019 20:52:20 +0100 Subject: [PATCH] Generate chroma theme from our style settings --- ansi/codeblock.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++- ansi/style.go | 35 ++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 3 deletions(-) diff --git a/ansi/codeblock.go b/ansi/codeblock.go index a97f25e..4662776 100644 --- a/ansi/codeblock.go +++ b/ansi/codeblock.go @@ -3,7 +3,9 @@ package ansi import ( "io" + "github.com/alecthomas/chroma" "github.com/alecthomas/chroma/quick" + "github.com/alecthomas/chroma/styles" ) type CodeBlockElement struct { @@ -11,6 +13,22 @@ type CodeBlockElement struct { Language string } +func chromaColor(style StylePrimitive) string { + var s string + + if style.Color != nil { + s = *style.Color + } + if style.BackgroundColor != nil { + if s != "" { + s += " " + } + s += "bg:" + *style.BackgroundColor + } + + return s +} + func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack @@ -25,6 +43,41 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { } theme := rules.Theme + styles.Register(chroma.MustNewStyle("charm", + chroma.StyleEntries{ + chroma.Text: chromaColor(rules.Text), + chroma.Error: chromaColor(rules.Error), + chroma.Comment: chromaColor(rules.Comment), + chroma.CommentPreproc: chromaColor(rules.CommentPreproc), + chroma.Keyword: chromaColor(rules.Keyword), + chroma.KeywordReserved: chromaColor(rules.KeywordReserved), + chroma.KeywordNamespace: chromaColor(rules.KeywordNamespace), + chroma.KeywordType: chromaColor(rules.KeywordType), + chroma.Operator: chromaColor(rules.Operator), + chroma.Punctuation: chromaColor(rules.Punctuation), + chroma.Name: chromaColor(rules.Name), + chroma.NameBuiltin: chromaColor(rules.NameBuiltin), + chroma.NameTag: chromaColor(rules.NameTag), + chroma.NameAttribute: chromaColor(rules.NameAttribute), + chroma.NameClass: chromaColor(rules.NameClass), + chroma.NameConstant: chromaColor(rules.NameConstant), + chroma.NameDecorator: chromaColor(rules.NameDecorator), + chroma.NameException: chromaColor(rules.NameException), + chroma.NameFunction: chromaColor(rules.NameFunction), + chroma.NameOther: chromaColor(rules.NameOther), + chroma.Literal: chromaColor(rules.Literal), + chroma.LiteralNumber: chromaColor(rules.LiteralNumber), + chroma.LiteralDate: chromaColor(rules.LiteralDate), + chroma.LiteralString: chromaColor(rules.LiteralString), + chroma.LiteralStringEscape: chromaColor(rules.LiteralStringEscape), + chroma.GenericDeleted: chromaColor(rules.GenericDeleted), + chroma.GenericEmph: chromaColor(rules.GenericEmph), + chroma.GenericInserted: chromaColor(rules.GenericInserted), + chroma.GenericStrong: chromaColor(rules.GenericStrong), + chroma.GenericSubheading: chromaColor(rules.GenericSubheading), + chroma.Background: chromaColor(rules.Background), + })) + iw := &IndentWriter{ Indent: indent + margin, IndentFunc: func(wr io.Writer) { @@ -37,7 +90,7 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { if len(theme) > 0 { renderText(iw, bs.Current().Style.StylePrimitive, rules.BlockPrefix) - err := quick.Highlight(iw, e.Code, e.Language, "terminal16m", theme) + err := quick.Highlight(iw, e.Code, e.Language, "terminal256", theme) renderText(iw, bs.Current().Style.StylePrimitive, rules.BlockSuffix) return err } diff --git a/ansi/style.go b/ansi/style.go index afba71f..d93a48d 100644 --- a/ansi/style.go +++ b/ansi/style.go @@ -24,7 +24,7 @@ type StylePrimitive struct { } type StyleTask struct { - StyleBlock + StylePrimitive Ticked string `json:"ticked"` Unticked string `json:"unticked"` } @@ -37,7 +37,38 @@ type StyleBlock struct { type StyleCodeBlock struct { StyleBlock - Theme string `json:"theme"` + Theme string `json:"theme"` + Text StylePrimitive `json:"text"` + Error StylePrimitive `json:"error"` + Comment StylePrimitive `json:"comment"` + CommentPreproc StylePrimitive `json:"comment_preproc"` + Keyword StylePrimitive `json:"keyword"` + KeywordReserved StylePrimitive `json:"keyword_reserved"` + KeywordNamespace StylePrimitive `json:"keyword_namespace"` + KeywordType StylePrimitive `json:"keyword_type"` + Operator StylePrimitive `json:"operator"` + Punctuation StylePrimitive `json:"punctuation"` + Name StylePrimitive `json:"name"` + NameBuiltin StylePrimitive `json:"name_builtin"` + NameTag StylePrimitive `json:"name_tag"` + NameAttribute StylePrimitive `json:"name_attribute"` + NameClass StylePrimitive `json:"name_class"` + NameConstant StylePrimitive `json:"name_constant"` + NameDecorator StylePrimitive `json:"name_decorator"` + NameException StylePrimitive `json:"name_exception"` + NameFunction StylePrimitive `json:"name_function"` + NameOther StylePrimitive `json:"name_other"` + Literal StylePrimitive `json:"literal"` + LiteralNumber StylePrimitive `json:"literal_number"` + LiteralDate StylePrimitive `json:"literal_date"` + LiteralString StylePrimitive `json:"literal_string"` + LiteralStringEscape StylePrimitive `json:"literal_string_escape"` + GenericDeleted StylePrimitive `json:"generic_deleted"` + GenericEmph StylePrimitive `json:"generic_emph"` + GenericInserted StylePrimitive `json:"generic_inserted"` + GenericStrong StylePrimitive `json:"generic_strong"` + GenericSubheading StylePrimitive `json:"generic_subheading"` + Background StylePrimitive `json:"background"` } type StyleList struct {