mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 08:57:14 -04:00
Handle write buffers & cascading styles in BlockStack
This commit is contained in:
+3
-3
@@ -63,12 +63,12 @@ func renderText(w io.Writer, rules *ElementStyle, s string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *BaseElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
|
func (e *BaseElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
|
||||||
renderText(w, tr.blockStyle.Current(), e.Prefix)
|
renderText(w, tr.blockStack.Current().Style, e.Prefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
renderText(w, tr.blockStyle.Current(), e.Suffix)
|
renderText(w, tr.blockStack.Current().Style, e.Suffix)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
rules := tr.blockStyle.With(tr.style[e.Style])
|
rules := tr.blockStack.With(tr.style[e.Style])
|
||||||
if rules != nil {
|
if rules != nil {
|
||||||
renderText(w, rules, rules.Prefix)
|
renderText(w, rules, rules.Prefix)
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
+1
-1
@@ -26,7 +26,7 @@ func (e *CodeBlockElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer)
|
|||||||
iw := &IndentWriter{
|
iw := &IndentWriter{
|
||||||
Indent: indent + margin,
|
Indent: indent + margin,
|
||||||
IndentFunc: func(wr io.Writer) {
|
IndentFunc: func(wr io.Writer) {
|
||||||
renderText(w, tr.blockStyle.Parent(), " ")
|
renderText(w, tr.blockStack.Parent().Style, " ")
|
||||||
},
|
},
|
||||||
Forward: &AnsiWriter{
|
Forward: &AnsiWriter{
|
||||||
Forward: w,
|
Forward: w,
|
||||||
|
|||||||
+10
-5
@@ -1,6 +1,7 @@
|
|||||||
package gold
|
package gold
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
bf "gopkg.in/russross/blackfriday.v2"
|
bf "gopkg.in/russross/blackfriday.v2"
|
||||||
@@ -12,8 +13,12 @@ type DocumentElement struct {
|
|||||||
func (e *DocumentElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
|
func (e *DocumentElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
|
||||||
rules := tr.style[Document]
|
rules := tr.style[Document]
|
||||||
if rules != nil {
|
if rules != nil {
|
||||||
tr.blockStyle.Push(rules)
|
be := BlockElement{
|
||||||
renderText(w, rules, rules.Prefix)
|
Block: &bytes.Buffer{},
|
||||||
|
Style: rules,
|
||||||
|
}
|
||||||
|
tr.blockStack.Push(be)
|
||||||
|
renderText(tr.blockStack.Current().Block, rules, rules.Prefix)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -43,14 +48,14 @@ func (e *DocumentElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer) e
|
|||||||
Forward: pw,
|
Forward: pw,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
_, err := iw.Write(tr.document.Bytes())
|
_, err := iw.Write(tr.blockStack.Current().Block.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
renderText(iw, rules, suffix)
|
renderText(iw, rules, suffix)
|
||||||
|
|
||||||
tr.document.Reset()
|
tr.blockStack.Current().Block.Reset()
|
||||||
tr.blockStyle.Pop()
|
tr.blockStack.Pop()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package gold
|
package gold
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@@ -23,9 +22,7 @@ type TermRenderer struct {
|
|||||||
WordWrap int
|
WordWrap int
|
||||||
|
|
||||||
style map[StyleType]*ElementStyle
|
style map[StyleType]*ElementStyle
|
||||||
document bytes.Buffer
|
blockStack BlockStack
|
||||||
paragraph *bytes.Buffer
|
|
||||||
blockStyle StyleStack
|
|
||||||
table TableElement
|
table TableElement
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +84,8 @@ func (tr *TermRenderer) RenderBytes(in []byte) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
|
func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf.WalkStatus {
|
||||||
writeTo := io.Writer(&tr.document)
|
//tr.document.Write([]byte(node.Type.String()))
|
||||||
|
writeTo := io.Writer(nil)
|
||||||
|
|
||||||
if isChild(node) {
|
if isChild(node) {
|
||||||
return bf.GoToNext
|
return bf.GoToNext
|
||||||
@@ -95,11 +93,9 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
|
|||||||
|
|
||||||
e := tr.NewElement(node)
|
e := tr.NewElement(node)
|
||||||
if entering {
|
if entering {
|
||||||
_, _ = tr.document.Write([]byte(e.Entering))
|
if len(tr.blockStack) > 0 {
|
||||||
|
writeTo = io.Writer(tr.blockStack.Current().Block)
|
||||||
// each paragraph gets rendered into a separate buffer
|
_, _ = writeTo.Write([]byte(e.Entering))
|
||||||
if tr.paragraph != nil {
|
|
||||||
writeTo = tr.paragraph
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Renderer != nil {
|
if e.Renderer != nil {
|
||||||
@@ -110,6 +106,10 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
if len(tr.blockStack) > 0 {
|
||||||
|
writeTo = io.Writer(tr.blockStack.Parent().Block)
|
||||||
|
}
|
||||||
|
|
||||||
// if we're finished rendering the entire document,
|
// if we're finished rendering the entire document,
|
||||||
// flush to the real writer
|
// flush to the real writer
|
||||||
if node.Type == bf.Document {
|
if node.Type == bf.Document {
|
||||||
@@ -124,7 +124,7 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = tr.document.Write([]byte(e.Exiting))
|
_, _ = writeTo.Write([]byte(e.Exiting))
|
||||||
}
|
}
|
||||||
|
|
||||||
return bf.GoToNext
|
return bf.GoToNext
|
||||||
|
|||||||
+29
-25
@@ -3,6 +3,7 @@ package gold
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/muesli/reflow"
|
"github.com/muesli/reflow"
|
||||||
bf "gopkg.in/russross/blackfriday.v2"
|
bf "gopkg.in/russross/blackfriday.v2"
|
||||||
@@ -12,19 +13,22 @@ type ParagraphElement struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *ParagraphElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
|
func (e *ParagraphElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error {
|
||||||
tr.paragraph = &bytes.Buffer{}
|
var rules *ElementStyle
|
||||||
|
|
||||||
rules := tr.style[Paragraph]
|
|
||||||
tr.blockStyle.Push(rules)
|
|
||||||
|
|
||||||
if node.Parent != nil && node.Parent.Type == bf.Item {
|
if node.Parent != nil && node.Parent.Type == bf.Item {
|
||||||
// list item
|
// list item
|
||||||
|
rules = tr.style[List]
|
||||||
} else {
|
} else {
|
||||||
|
rules = tr.style[Paragraph]
|
||||||
_, _ = w.Write([]byte("\n"))
|
_, _ = w.Write([]byte("\n"))
|
||||||
|
be := BlockElement{
|
||||||
|
Block: &bytes.Buffer{},
|
||||||
|
Style: cascadeStyle(tr.blockStack.Current().Style, rules),
|
||||||
|
}
|
||||||
|
tr.blockStack.Push(be)
|
||||||
}
|
}
|
||||||
|
|
||||||
if rules != nil {
|
if rules != nil {
|
||||||
renderText(w, tr.blockStyle.Current(), rules.Prefix)
|
renderText(w, tr.blockStack.Current().Style, rules.Prefix)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -33,22 +37,21 @@ func (e *ParagraphElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer)
|
|||||||
var indent uint
|
var indent uint
|
||||||
var margin uint
|
var margin uint
|
||||||
var suffix string
|
var suffix string
|
||||||
rules := tr.blockStyle.Current()
|
rules := tr.blockStack.Current().Style
|
||||||
|
if node.Parent != nil && node.Parent.Type == bf.Item {
|
||||||
|
// remove indent & margin for list items
|
||||||
|
rules = tr.blockStack.Current().Style
|
||||||
|
}
|
||||||
|
|
||||||
if rules != nil {
|
if rules != nil {
|
||||||
indent = rules.Indent
|
indent = rules.Indent
|
||||||
margin = rules.Margin
|
margin = rules.Margin
|
||||||
suffix = rules.Suffix
|
suffix = rules.Suffix
|
||||||
|
|
||||||
if node.Parent != nil && node.Parent.Type == bf.Item {
|
|
||||||
// remove indent & margin for list items
|
|
||||||
indent = 0
|
|
||||||
margin = 0
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
renderText(tr.paragraph, rules, suffix)
|
renderText(tr.blockStack.Current().Block, rules, suffix)
|
||||||
|
|
||||||
pw := &PaddingWriter{
|
pw := &PaddingWriter{
|
||||||
Padding: uint(tr.WordWrap - int(tr.blockStyle.Indent()) - int(tr.blockStyle.Margin()*2)),
|
Padding: uint(tr.WordWrap - int(tr.blockStack.Indent()) - int(tr.blockStack.Margin()*2)),
|
||||||
PadFunc: func(wr io.Writer) {
|
PadFunc: func(wr io.Writer) {
|
||||||
renderText(w, rules, " ")
|
renderText(w, rules, " ")
|
||||||
},
|
},
|
||||||
@@ -59,24 +62,25 @@ func (e *ParagraphElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer)
|
|||||||
iw := &IndentWriter{
|
iw := &IndentWriter{
|
||||||
Indent: indent + margin,
|
Indent: indent + margin,
|
||||||
IndentFunc: func(wr io.Writer) {
|
IndentFunc: func(wr io.Writer) {
|
||||||
renderText(w, tr.blockStyle.Parent(), " ")
|
renderText(w, tr.blockStack.Parent().Style, " ")
|
||||||
},
|
},
|
||||||
Forward: &AnsiWriter{
|
Forward: &AnsiWriter{
|
||||||
Forward: pw,
|
Forward: pw,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := iw.Write(reflow.ReflowBytes(tr.paragraph.Bytes(), tr.WordWrap-int(tr.blockStyle.Indent())-int(tr.blockStyle.Margin())*2))
|
if len(strings.TrimSpace(tr.blockStack.Current().Block.String())) > 0 {
|
||||||
if err != nil {
|
_, err := iw.Write(reflow.ReflowBytes(tr.blockStack.Current().Block.Bytes(), tr.WordWrap-int(tr.blockStack.Indent())-int(tr.blockStack.Margin())*2))
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, _ = pw.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
_, _ = pw.Write([]byte("\n"))
|
|
||||||
|
|
||||||
tr.paragraph.Reset()
|
tr.blockStack.Current().Block.Reset()
|
||||||
tr.paragraph = nil
|
if node.Parent != nil && node.Parent.Type == bf.Item {
|
||||||
|
} else {
|
||||||
if rules != nil {
|
tr.blockStack.Pop()
|
||||||
tr.blockStyle.Pop()
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package gold
|
package gold
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,13 +56,18 @@ type ElementStyle struct {
|
|||||||
Suffix string `json:"suffix"`
|
Suffix string `json:"suffix"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type StyleStack []*ElementStyle
|
type BlockElement struct {
|
||||||
|
Block *bytes.Buffer
|
||||||
func (s *StyleStack) Push(style *ElementStyle) {
|
Style *ElementStyle
|
||||||
*s = append(*s, style)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *StyleStack) Pop() {
|
type BlockStack []BlockElement
|
||||||
|
|
||||||
|
func (s *BlockStack) Push(e BlockElement) {
|
||||||
|
*s = append(*s, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *BlockStack) Pop() {
|
||||||
stack := *s
|
stack := *s
|
||||||
if len(stack) == 0 {
|
if len(stack) == 0 {
|
||||||
return
|
return
|
||||||
@@ -71,50 +77,52 @@ func (s *StyleStack) Pop() {
|
|||||||
*s = stack
|
*s = stack
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StyleStack) Indent() uint {
|
func (s BlockStack) Indent() uint {
|
||||||
var i uint
|
var i uint
|
||||||
|
|
||||||
for _, v := range s {
|
for _, v := range s {
|
||||||
if v == nil {
|
if v.Style == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
i += v.Indent
|
i += v.Style.Indent
|
||||||
}
|
}
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StyleStack) Margin() uint {
|
func (s BlockStack) Margin() uint {
|
||||||
var i uint
|
var i uint
|
||||||
|
|
||||||
for _, v := range s {
|
for _, v := range s {
|
||||||
if v == nil {
|
if v.Style == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
i += v.Margin
|
i += v.Style.Margin
|
||||||
}
|
}
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StyleStack) Parent() *ElementStyle {
|
func (s BlockStack) Parent() BlockElement {
|
||||||
if len(s) < 2 {
|
if len(s) < 2 {
|
||||||
return s.Current()
|
return s.Current()
|
||||||
}
|
}
|
||||||
|
|
||||||
return cascadeStyles(s[0:len(s)-2], s[len(s)-2])
|
return s[len(s)-2]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StyleStack) Current() *ElementStyle {
|
func (s BlockStack) Current() BlockElement {
|
||||||
if len(s) == 0 {
|
if len(s) == 0 {
|
||||||
return nil
|
return BlockElement{
|
||||||
|
Block: &bytes.Buffer{},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cascadeStyles(s[0:len(s)-1], s[len(s)-1])
|
return s[len(s)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s StyleStack) With(child *ElementStyle) *ElementStyle {
|
func (s BlockStack) With(child *ElementStyle) *ElementStyle {
|
||||||
return cascadeStyles(s, child)
|
return cascadeStyle(s.Current().Style, child)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
|
func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
|
||||||
@@ -142,18 +150,6 @@ func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
|
|||||||
return &s
|
return &s
|
||||||
}
|
}
|
||||||
|
|
||||||
func cascadeStyles(parents StyleStack, child *ElementStyle) *ElementStyle {
|
|
||||||
if len(parents) == 0 {
|
|
||||||
return child
|
|
||||||
}
|
|
||||||
parent := parents[0]
|
|
||||||
for i := 1; i < len(parents); i++ {
|
|
||||||
parent = cascadeStyle(parent, parents[i])
|
|
||||||
}
|
|
||||||
|
|
||||||
return cascadeStyle(parent, child)
|
|
||||||
}
|
|
||||||
|
|
||||||
func keyToType(key string) (StyleType, error) {
|
func keyToType(key string) (StyleType, error) {
|
||||||
switch key {
|
switch key {
|
||||||
case "document":
|
case "document":
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ func (e *TableElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) erro
|
|||||||
iw := &IndentWriter{
|
iw := &IndentWriter{
|
||||||
Indent: indent + margin,
|
Indent: indent + margin,
|
||||||
IndentFunc: func(wr io.Writer) {
|
IndentFunc: func(wr io.Writer) {
|
||||||
renderText(w, tr.blockStyle.Parent(), " ")
|
renderText(w, tr.blockStack.Parent().Style, " ")
|
||||||
},
|
},
|
||||||
Forward: &AnsiWriter{
|
Forward: &AnsiWriter{
|
||||||
Forward: w,
|
Forward: w,
|
||||||
|
|||||||
Reference in New Issue
Block a user