mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-08-31 06:06:41 -04:00
57 lines
1.1 KiB
Go
57 lines
1.1 KiB
Go
package gold
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
|
|
"github.com/muesli/reflow"
|
|
)
|
|
|
|
type BlockElement struct {
|
|
Block *bytes.Buffer
|
|
Style StyleBlock
|
|
Margin bool
|
|
Newline bool
|
|
}
|
|
|
|
func (e *BlockElement) Render(w io.Writer, ctx RenderContext) error {
|
|
bs := ctx.blockStack
|
|
bs.Push(*e)
|
|
|
|
renderText(w, bs.Parent().Style.StylePrimitive, e.Style.Prefix)
|
|
renderText(bs.Current().Block, bs.Current().Style.StylePrimitive, e.Style.StyledPrefix)
|
|
return nil
|
|
}
|
|
|
|
func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error {
|
|
bs := ctx.blockStack
|
|
|
|
if e.Margin {
|
|
mw := NewMarginWriter(ctx, w, bs.Current().Style)
|
|
_, err := mw.Write(
|
|
reflow.Bytes(bs.Current().Block.Bytes(), int(bs.Width(ctx))))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if e.Newline {
|
|
_, err = mw.Write([]byte("\n"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
} else {
|
|
_, err := bs.Parent().Block.Write(bs.Current().Block.Bytes())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
renderText(w, bs.Current().Style.StylePrimitive, e.Style.StyledSuffix)
|
|
renderText(w, bs.Parent().Style.StylePrimitive, e.Style.Suffix)
|
|
|
|
bs.Current().Block.Reset()
|
|
bs.Pop()
|
|
return nil
|
|
}
|