mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 00:47:15 -04:00
Move ANSIRenderer into a separate package
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package ansi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
|
||||
"github.com/muesli/reflow"
|
||||
)
|
||||
|
||||
type HeadingElement struct {
|
||||
Level int
|
||||
First bool
|
||||
}
|
||||
|
||||
func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
rules := ctx.options.Styles.Heading
|
||||
|
||||
switch e.Level {
|
||||
case 1:
|
||||
rules = cascadeStyles(false, rules, ctx.options.Styles.H1)
|
||||
case 2:
|
||||
rules = cascadeStyles(false, rules, ctx.options.Styles.H2)
|
||||
case 3:
|
||||
rules = cascadeStyles(false, rules, ctx.options.Styles.H3)
|
||||
case 4:
|
||||
rules = cascadeStyles(false, rules, ctx.options.Styles.H4)
|
||||
case 5:
|
||||
rules = cascadeStyles(false, rules, ctx.options.Styles.H5)
|
||||
case 6:
|
||||
rules = cascadeStyles(false, rules, ctx.options.Styles.H6)
|
||||
}
|
||||
|
||||
if !e.First {
|
||||
renderText(w, bs.Current().Style.StylePrimitive, "\n")
|
||||
}
|
||||
|
||||
be := BlockElement{
|
||||
Block: &bytes.Buffer{},
|
||||
Style: cascadeStyle(bs.Current().Style, rules, true),
|
||||
}
|
||||
bs.Push(be)
|
||||
|
||||
renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockPrefix)
|
||||
renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, rules.Prefix)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
rules := bs.Current().Style
|
||||
|
||||
var indent uint
|
||||
var margin uint
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Parent().Style.StylePrimitive, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
|
||||
flow := reflow.NewReflow(int(bs.Width(ctx) - indent - margin*2))
|
||||
_, err := flow.Write(bs.Current().Block.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
flow.Close()
|
||||
|
||||
_, err = iw.Write(flow.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
renderText(w, bs.Current().Style.StylePrimitive, rules.Suffix)
|
||||
renderText(w, bs.Parent().Style.StylePrimitive, rules.BlockSuffix)
|
||||
|
||||
bs.Current().Block.Reset()
|
||||
bs.Pop()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user