mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-05 16:37:19 -04:00
Use TrueColor color profile as default
Users can override the desired profile by calling WithColorProfile.
This commit is contained in:
+7
-7
@@ -73,22 +73,22 @@ func renderText(w io.Writer, p termenv.Profile, rules StylePrimitive, s string)
|
|||||||
func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error {
|
func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error {
|
||||||
bs := ctx.blockStack
|
bs := ctx.blockStack
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Prefix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Prefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Suffix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Suffix)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
rules := bs.With(e.Style)
|
rules := bs.With(e.Style)
|
||||||
// render unstyled prefix/suffix
|
// render unstyled prefix/suffix
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// render styled prefix/suffix
|
// render styled prefix/suffix
|
||||||
renderText(w, ctx.colorProfile, rules, rules.Prefix)
|
renderText(w, ctx.options.ColorProfile, rules, rules.Prefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
renderText(w, ctx.colorProfile, rules, rules.Suffix)
|
renderText(w, ctx.options.ColorProfile, rules, rules.Suffix)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s := e.Token
|
s := e.Token
|
||||||
@@ -99,6 +99,6 @@ func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
renderText(w, ctx.colorProfile, rules, s)
|
renderText(w, ctx.options.ColorProfile, rules, s)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ func (e *BlockElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
bs := ctx.blockStack
|
bs := ctx.blockStack
|
||||||
bs.Push(*e)
|
bs.Push(*e)
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockPrefix)
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockPrefix)
|
||||||
renderText(bs.Current().Block, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Style.Prefix)
|
renderText(bs.Current().Block, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Style.Prefix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,8 +50,8 @@ func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Style.Suffix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Style.Suffix)
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockSuffix)
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockSuffix)
|
||||||
|
|
||||||
bs.Current().Block.Reset()
|
bs.Current().Block.Reset()
|
||||||
bs.Pop()
|
bs.Pop()
|
||||||
|
|||||||
+3
-3
@@ -102,16 +102,16 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ")
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, " ")
|
||||||
})
|
})
|
||||||
|
|
||||||
if len(theme) > 0 {
|
if len(theme) > 0 {
|
||||||
renderText(iw, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
err := quick.Highlight(iw, e.Code, e.Language, "terminal256", theme)
|
err := quick.Highlight(iw, e.Code, e.Language, "terminal256", theme)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
renderText(iw, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+5
-8
@@ -5,7 +5,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
"github.com/muesli/termenv"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RenderContext holds the current rendering options and state.
|
// RenderContext holds the current rendering options and state.
|
||||||
@@ -15,18 +14,16 @@ type RenderContext struct {
|
|||||||
blockStack *BlockStack
|
blockStack *BlockStack
|
||||||
table *TableElement
|
table *TableElement
|
||||||
|
|
||||||
stripper *bluemonday.Policy
|
stripper *bluemonday.Policy
|
||||||
colorProfile termenv.Profile
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRenderContext returns a new RenderContext.
|
// NewRenderContext returns a new RenderContext.
|
||||||
func NewRenderContext(options Options) RenderContext {
|
func NewRenderContext(options Options) RenderContext {
|
||||||
return RenderContext{
|
return RenderContext{
|
||||||
options: options,
|
options: options,
|
||||||
blockStack: &BlockStack{},
|
blockStack: &BlockStack{},
|
||||||
table: &TableElement{},
|
table: &TableElement{},
|
||||||
stripper: bluemonday.StrictPolicy(),
|
stripper: bluemonday.StrictPolicy(),
|
||||||
colorProfile: termenv.ColorProfile(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+6
-6
@@ -34,7 +34,7 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !e.First {
|
if !e.First {
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, "\n")
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
be := BlockElement{
|
be := BlockElement{
|
||||||
@@ -43,8 +43,8 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
bs.Push(be)
|
bs.Push(be)
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
renderText(bs.Current().Block, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Prefix)
|
renderText(bs.Current().Block, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.Prefix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, " ")
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, " ")
|
||||||
})
|
})
|
||||||
|
|
||||||
flow := wordwrap.NewWriter(int(bs.Width(ctx) - indentation - margin*2))
|
flow := wordwrap.NewWriter(int(bs.Width(ctx) - indentation - margin*2))
|
||||||
@@ -77,8 +77,8 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Suffix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.Suffix)
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
|
|
||||||
bs.Current().Block.Reset()
|
bs.Current().Block.Reset()
|
||||||
bs.Pop()
|
bs.Pop()
|
||||||
|
|||||||
+2
-2
@@ -29,7 +29,7 @@ func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWr
|
|||||||
}
|
}
|
||||||
|
|
||||||
pw := padding.NewWriterPipe(w, bs.Width(ctx), func(wr io.Writer) {
|
pw := padding.NewWriterPipe(w, bs.Width(ctx), func(wr io.Writer) {
|
||||||
renderText(w, ctx.colorProfile, rules.StylePrimitive, " ")
|
renderText(w, ctx.options.ColorProfile, rules.StylePrimitive, " ")
|
||||||
})
|
})
|
||||||
|
|
||||||
ic := " "
|
ic := " "
|
||||||
@@ -37,7 +37,7 @@ func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWr
|
|||||||
ic = *rules.IndentToken
|
ic = *rules.IndentToken
|
||||||
}
|
}
|
||||||
iw := indent.NewWriterPipe(pw, indentation+margin, func(wr io.Writer) {
|
iw := indent.NewWriterPipe(pw, indentation+margin, func(wr io.Writer) {
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, ic)
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, ic)
|
||||||
})
|
})
|
||||||
|
|
||||||
return &MarginWriter{
|
return &MarginWriter{
|
||||||
|
|||||||
+4
-4
@@ -26,8 +26,8 @@ func (e *ParagraphElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
bs.Push(be)
|
bs.Push(be)
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
renderText(bs.Current().Block, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Prefix)
|
renderText(bs.Current().Block, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.Prefix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,8 +49,8 @@ func (e *ParagraphElement) Finish(w io.Writer, ctx RenderContext) error {
|
|||||||
_, _ = mw.Write([]byte("\n"))
|
_, _ = mw.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Suffix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.Suffix)
|
||||||
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(w, ctx.options.ColorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
|
|
||||||
bs.Current().Block.Reset()
|
bs.Current().Block.Reset()
|
||||||
bs.Pop()
|
bs.Pop()
|
||||||
|
|||||||
+5
-3
@@ -5,6 +5,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/muesli/termenv"
|
||||||
"github.com/yuin/goldmark/ast"
|
"github.com/yuin/goldmark/ast"
|
||||||
astext "github.com/yuin/goldmark/extension/ast"
|
astext "github.com/yuin/goldmark/extension/ast"
|
||||||
"github.com/yuin/goldmark/renderer"
|
"github.com/yuin/goldmark/renderer"
|
||||||
@@ -13,9 +14,10 @@ import (
|
|||||||
|
|
||||||
// Options is used to configure an ANSIRenderer.
|
// Options is used to configure an ANSIRenderer.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
BaseURL string
|
BaseURL string
|
||||||
WordWrap int
|
WordWrap int
|
||||||
Styles StyleConfig
|
ColorProfile termenv.Profile
|
||||||
|
Styles StyleConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
// ANSIRenderer renders markdown content as ANSI escaped sequences.
|
// ANSIRenderer renders markdown content as ANSI escaped sequences.
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/muesli/termenv"
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
"github.com/yuin/goldmark/extension"
|
"github.com/yuin/goldmark/extension"
|
||||||
"github.com/yuin/goldmark/parser"
|
"github.com/yuin/goldmark/parser"
|
||||||
@@ -43,7 +44,8 @@ func TestRenderer(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
options := Options{
|
options := Options{
|
||||||
WordWrap: 80,
|
WordWrap: 80,
|
||||||
|
ColorProfile: termenv.TrueColor,
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(b, &options.Styles)
|
err = json.Unmarshal(b, &options.Styles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -113,7 +115,8 @@ func TestRendererIssues(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
options := Options{
|
options := Options{
|
||||||
WordWrap: 80,
|
WordWrap: 80,
|
||||||
|
ColorProfile: termenv.TrueColor,
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(b, &options.Styles)
|
err = json.Unmarshal(b, &options.Styles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
+1
-1
@@ -28,6 +28,6 @@ func (w *StyleWriter) Write(b []byte) (int, error) {
|
|||||||
|
|
||||||
// Close must be called when you're finished writing to a StyleWriter.
|
// Close must be called when you're finished writing to a StyleWriter.
|
||||||
func (w *StyleWriter) Close() error {
|
func (w *StyleWriter) Close() error {
|
||||||
renderText(w.w, w.ctx.colorProfile, w.rules, w.buf.String())
|
renderText(w.w, w.ctx.options.ColorProfile, w.rules, w.buf.String())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -43,14 +43,14 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ")
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, " ")
|
||||||
})
|
})
|
||||||
|
|
||||||
style := bs.With(rules.StylePrimitive)
|
style := bs.With(rules.StylePrimitive)
|
||||||
ctx.table.styleWriter = NewStyleWriter(ctx, iw, style)
|
ctx.table.styleWriter = NewStyleWriter(ctx, iw, style)
|
||||||
|
|
||||||
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
renderText(ctx.table.styleWriter, ctx.colorProfile, style, rules.Prefix)
|
renderText(ctx.table.styleWriter, ctx.options.ColorProfile, style, rules.Prefix)
|
||||||
ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter)
|
ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -72,8 +72,8 @@ func (e *TableElement) Finish(w io.Writer, ctx RenderContext) error {
|
|||||||
ctx.table.writer.Render()
|
ctx.table.writer.Render()
|
||||||
ctx.table.writer = nil
|
ctx.table.writer = nil
|
||||||
|
|
||||||
renderText(ctx.table.styleWriter, ctx.colorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix)
|
renderText(ctx.table.styleWriter, ctx.options.ColorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix)
|
||||||
renderText(ctx.table.styleWriter, ctx.colorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(ctx.table.styleWriter, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
return ctx.table.styleWriter.Close()
|
return ctx.table.styleWriter.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+12
-1
@@ -7,6 +7,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/muesli/termenv"
|
||||||
"github.com/yuin/goldmark"
|
"github.com/yuin/goldmark"
|
||||||
"github.com/yuin/goldmark/extension"
|
"github.com/yuin/goldmark/extension"
|
||||||
"github.com/yuin/goldmark/parser"
|
"github.com/yuin/goldmark/parser"
|
||||||
@@ -60,7 +61,8 @@ func NewTermRenderer(options ...TermRendererOption) (*TermRenderer, error) {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
ansiOptions: ansi.Options{
|
ansiOptions: ansi.Options{
|
||||||
WordWrap: 80,
|
WordWrap: 80,
|
||||||
|
ColorProfile: termenv.TrueColor,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, o := range options {
|
for _, o := range options {
|
||||||
@@ -87,6 +89,15 @@ func WithBaseURL(baseURL string) TermRendererOption {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithColorProfile sets the TermRenderer's color profile
|
||||||
|
// (TrueColor / ANSI256 / ANSI).
|
||||||
|
func WithColorProfile(profile termenv.Profile) TermRendererOption {
|
||||||
|
return func(tr *TermRenderer) error {
|
||||||
|
tr.ansiOptions.ColorProfile = profile
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// WithStandardStyle sets a TermRenderer's styles with a standard (builtin)
|
// WithStandardStyle sets a TermRenderer's styles with a standard (builtin)
|
||||||
// style.
|
// style.
|
||||||
func WithStandardStyle(style string) TermRendererOption {
|
func WithStandardStyle(style string) TermRendererOption {
|
||||||
|
|||||||
Reference in New Issue
Block a user