mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 00:47:15 -04:00
Keep termenv profile in RenderContext
This commit is contained in:
+8
-9
@@ -31,12 +31,11 @@ func formatToken(format string, token string) (string, error) {
|
|||||||
return b.String(), err
|
return b.String(), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderText(w io.Writer, rules StylePrimitive, s string) {
|
func renderText(w io.Writer, p termenv.Profile, rules StylePrimitive, s string) {
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
p := termenv.ColorProfile()
|
|
||||||
out := termenv.String(s)
|
out := termenv.String(s)
|
||||||
|
|
||||||
if rules.Color != nil {
|
if rules.Color != nil {
|
||||||
@@ -74,22 +73,22 @@ func renderText(w io.Writer, 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, bs.Current().Style.StylePrimitive, e.Prefix)
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Prefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
renderText(w, bs.Current().Style.StylePrimitive, e.Suffix)
|
renderText(w, ctx.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, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
renderText(w, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// render styled prefix/suffix
|
// render styled prefix/suffix
|
||||||
renderText(w, rules, rules.Prefix)
|
renderText(w, ctx.colorProfile, rules, rules.Prefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
renderText(w, rules, rules.Suffix)
|
renderText(w, ctx.colorProfile, rules, rules.Suffix)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s := e.Token
|
s := e.Token
|
||||||
@@ -100,6 +99,6 @@ func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
renderText(w, rules, s)
|
renderText(w, ctx.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, bs.Parent().Style.StylePrimitive, e.Style.BlockPrefix)
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockPrefix)
|
||||||
renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, e.Style.Prefix)
|
renderText(bs.Current().Block, ctx.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, bs.Current().Style.StylePrimitive, e.Style.Suffix)
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, e.Style.Suffix)
|
||||||
renderText(w, bs.Parent().Style.StylePrimitive, e.Style.BlockSuffix)
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, e.Style.BlockSuffix)
|
||||||
|
|
||||||
bs.Current().Block.Reset()
|
bs.Current().Block.Reset()
|
||||||
bs.Pop()
|
bs.Pop()
|
||||||
|
|||||||
+3
-3
@@ -105,7 +105,7 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
iw := &indent.Writer{
|
iw := &indent.Writer{
|
||||||
Indent: indentation + margin,
|
Indent: indentation + margin,
|
||||||
IndentFunc: func(wr io.Writer) {
|
IndentFunc: func(wr io.Writer) {
|
||||||
renderText(w, bs.Current().Style.StylePrimitive, " ")
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ")
|
||||||
},
|
},
|
||||||
Forward: &ansi.Writer{
|
Forward: &ansi.Writer{
|
||||||
Forward: w,
|
Forward: w,
|
||||||
@@ -113,9 +113,9 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if len(theme) > 0 {
|
if len(theme) > 0 {
|
||||||
renderText(iw, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(iw, ctx.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)
|
||||||
renderText(iw, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(iw, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+8
-5
@@ -5,6 +5,7 @@ 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.
|
||||||
@@ -14,16 +15,18 @@ 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
@@ -35,7 +35,7 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !e.First {
|
if !e.First {
|
||||||
renderText(w, bs.Current().Style.StylePrimitive, "\n")
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, "\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
be := BlockElement{
|
be := BlockElement{
|
||||||
@@ -44,8 +44,8 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
bs.Push(be)
|
bs.Push(be)
|
||||||
|
|
||||||
renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, rules.Prefix)
|
renderText(bs.Current().Block, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Prefix)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,7 +65,7 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
|
|||||||
iw := &indent.Writer{
|
iw := &indent.Writer{
|
||||||
Indent: indentation + margin,
|
Indent: indentation + margin,
|
||||||
IndentFunc: func(wr io.Writer) {
|
IndentFunc: func(wr io.Writer) {
|
||||||
renderText(w, bs.Parent().Style.StylePrimitive, " ")
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, " ")
|
||||||
},
|
},
|
||||||
Forward: &ansi.Writer{
|
Forward: &ansi.Writer{
|
||||||
Forward: w,
|
Forward: w,
|
||||||
@@ -84,8 +84,8 @@ func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
renderText(w, bs.Current().Style.StylePrimitive, rules.Suffix)
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Suffix)
|
||||||
renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
|
|
||||||
bs.Current().Block.Reset()
|
bs.Current().Block.Reset()
|
||||||
bs.Pop()
|
bs.Pop()
|
||||||
|
|||||||
+2
-2
@@ -32,7 +32,7 @@ func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWr
|
|||||||
pw := &padding.Writer{
|
pw := &padding.Writer{
|
||||||
Padding: bs.Width(ctx),
|
Padding: bs.Width(ctx),
|
||||||
PadFunc: func(wr io.Writer) {
|
PadFunc: func(wr io.Writer) {
|
||||||
renderText(w, rules.StylePrimitive, " ")
|
renderText(w, ctx.colorProfile, rules.StylePrimitive, " ")
|
||||||
},
|
},
|
||||||
Forward: &ansi.Writer{
|
Forward: &ansi.Writer{
|
||||||
Forward: w,
|
Forward: w,
|
||||||
@@ -46,7 +46,7 @@ func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWr
|
|||||||
iw := &indent.Writer{
|
iw := &indent.Writer{
|
||||||
Indent: indentation + margin,
|
Indent: indentation + margin,
|
||||||
IndentFunc: func(wr io.Writer) {
|
IndentFunc: func(wr io.Writer) {
|
||||||
renderText(w, bs.Parent().Style.StylePrimitive, ic)
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, ic)
|
||||||
},
|
},
|
||||||
Forward: &ansi.Writer{
|
Forward: &ansi.Writer{
|
||||||
Forward: pw,
|
Forward: pw,
|
||||||
|
|||||||
+4
-4
@@ -26,8 +26,8 @@ func (e *ParagraphElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
}
|
}
|
||||||
bs.Push(be)
|
bs.Push(be)
|
||||||
|
|
||||||
renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, rules.Prefix)
|
renderText(bs.Current().Block, ctx.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, bs.Current().Style.StylePrimitive, rules.Suffix)
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.Suffix)
|
||||||
renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(w, ctx.colorProfile, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
|
|
||||||
bs.Current().Block.Reset()
|
bs.Current().Block.Reset()
|
||||||
bs.Pop()
|
bs.Pop()
|
||||||
|
|||||||
+3
-1
@@ -7,6 +7,7 @@ import (
|
|||||||
|
|
||||||
// StyleWriter is a Writer that applies styling on whatever you write to it.
|
// StyleWriter is a Writer that applies styling on whatever you write to it.
|
||||||
type StyleWriter struct {
|
type StyleWriter struct {
|
||||||
|
ctx RenderContext
|
||||||
w io.Writer
|
w io.Writer
|
||||||
buf bytes.Buffer
|
buf bytes.Buffer
|
||||||
rules StylePrimitive
|
rules StylePrimitive
|
||||||
@@ -15,6 +16,7 @@ type StyleWriter struct {
|
|||||||
// NewStyleWriter returns a new StyleWriter.
|
// NewStyleWriter returns a new StyleWriter.
|
||||||
func NewStyleWriter(ctx RenderContext, w io.Writer, rules StylePrimitive) *StyleWriter {
|
func NewStyleWriter(ctx RenderContext, w io.Writer, rules StylePrimitive) *StyleWriter {
|
||||||
return &StyleWriter{
|
return &StyleWriter{
|
||||||
|
ctx: ctx,
|
||||||
w: w,
|
w: w,
|
||||||
rules: rules,
|
rules: rules,
|
||||||
}
|
}
|
||||||
@@ -26,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.rules, w.buf.String())
|
renderText(w.w, w.ctx.colorProfile, w.rules, w.buf.String())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -46,7 +46,7 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
iw := &indent.Writer{
|
iw := &indent.Writer{
|
||||||
Indent: indentation + margin,
|
Indent: indentation + margin,
|
||||||
IndentFunc: func(wr io.Writer) {
|
IndentFunc: func(wr io.Writer) {
|
||||||
renderText(w, bs.Current().Style.StylePrimitive, " ")
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, " ")
|
||||||
},
|
},
|
||||||
Forward: &ansi.Writer{
|
Forward: &ansi.Writer{
|
||||||
Forward: w,
|
Forward: w,
|
||||||
@@ -56,8 +56,8 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
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, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
renderText(w, ctx.colorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
|
||||||
renderText(ctx.table.styleWriter, style, rules.Prefix)
|
renderText(ctx.table.styleWriter, ctx.colorProfile, style, rules.Prefix)
|
||||||
ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter)
|
ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -79,8 +79,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.blockStack.With(rules.StylePrimitive), rules.Suffix)
|
renderText(ctx.table.styleWriter, ctx.colorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix)
|
||||||
renderText(ctx.table.styleWriter, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix)
|
renderText(ctx.table.styleWriter, ctx.colorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix)
|
||||||
return ctx.table.styleWriter.Close()
|
return ctx.table.styleWriter.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user