mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-01 22:47:25 -04:00
Add MarginWriter to avoid code repetition
This commit is contained in:
+3
-27
@@ -25,36 +25,12 @@ func (e *DocumentElement) Finish(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
rules := ctx.style[Document]
|
||||
|
||||
var indent uint
|
||||
var margin uint
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
suffix := rules.Suffix
|
||||
|
||||
pw := &PaddingWriter{
|
||||
Padding: uint(ctx.options.WordWrap) - margin,
|
||||
PadFunc: func(wr io.Writer) {
|
||||
renderText(w, rules, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
Forward: &AnsiWriter{
|
||||
Forward: pw,
|
||||
},
|
||||
}
|
||||
_, err := iw.Write(bs.Current().Block.Bytes())
|
||||
mw := NewMarginWriter(ctx, w, rules)
|
||||
_, err := mw.Write(bs.Current().Block.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
renderText(iw, rules, suffix)
|
||||
renderText(mw, rules, rules.Suffix)
|
||||
|
||||
bs.Current().Block.Reset()
|
||||
bs.Pop()
|
||||
|
||||
@@ -12,56 +12,29 @@ type ListElement struct {
|
||||
}
|
||||
|
||||
func (e *ListElement) Render(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
rules := ctx.style[List]
|
||||
|
||||
if !e.Nested {
|
||||
_, _ = w.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
be := BlockElement{
|
||||
Block: &bytes.Buffer{},
|
||||
Style: cascadeStyle(ctx.blockStack.Current().Style, rules, true),
|
||||
Style: cascadeStyle(bs.Current().Style, rules, true),
|
||||
}
|
||||
ctx.blockStack.Push(be)
|
||||
bs.Push(be)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *ListElement) Finish(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
|
||||
var indent uint
|
||||
var margin uint
|
||||
rules := bs.Current().Style
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
|
||||
suffix := rules.Suffix
|
||||
renderText(bs.Current().Block, rules, suffix)
|
||||
renderText(bs.Current().Block, rules, rules.Suffix)
|
||||
|
||||
pw := &PaddingWriter{
|
||||
Padding: bs.Width(ctx),
|
||||
PadFunc: func(wr io.Writer) {
|
||||
renderText(w, rules, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Parent().Style, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: pw,
|
||||
},
|
||||
}
|
||||
|
||||
_, err := iw.Write(
|
||||
_, err := NewMarginWriter(ctx, w, rules).Write(
|
||||
reflow.Bytes(bs.Current().Block.Bytes(), int(bs.Width(ctx))))
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package gold
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type MarginWriter struct {
|
||||
w io.Writer
|
||||
pw *PaddingWriter
|
||||
iw *IndentWriter
|
||||
}
|
||||
|
||||
func NewMarginWriter(ctx RenderContext, w io.Writer, rules ElementStyle) *MarginWriter {
|
||||
bs := ctx.blockStack
|
||||
|
||||
var indent uint
|
||||
var margin uint
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
|
||||
pw := &PaddingWriter{
|
||||
Padding: bs.Width(ctx),
|
||||
PadFunc: func(wr io.Writer) {
|
||||
renderText(w, rules, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Parent().Style, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: pw,
|
||||
},
|
||||
}
|
||||
|
||||
return &MarginWriter{
|
||||
w: w,
|
||||
pw: pw,
|
||||
iw: iw,
|
||||
}
|
||||
}
|
||||
|
||||
func (w *MarginWriter) Write(b []byte) (int, error) {
|
||||
return w.iw.Write(b)
|
||||
}
|
||||
+5
-33
@@ -14,8 +14,8 @@ type ParagraphElement struct {
|
||||
|
||||
func (e *ParagraphElement) Render(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
|
||||
var rules ElementStyle
|
||||
|
||||
if e.InsideList {
|
||||
// list item
|
||||
rules = ctx.style[List]
|
||||
@@ -37,8 +37,6 @@ func (e *ParagraphElement) Finish(w io.Writer, ctx RenderContext) error {
|
||||
bs := ctx.blockStack
|
||||
rules := bs.Current().Style
|
||||
|
||||
var indent uint
|
||||
var margin uint
|
||||
keepNewlines := false
|
||||
if e.InsideList {
|
||||
// remove indent & margin for list items
|
||||
@@ -46,46 +44,20 @@ func (e *ParagraphElement) Finish(w io.Writer, ctx RenderContext) error {
|
||||
keepNewlines = true
|
||||
}
|
||||
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
|
||||
suffix := rules.Suffix
|
||||
renderText(bs.Current().Block, rules, suffix)
|
||||
|
||||
pw := &PaddingWriter{
|
||||
Padding: bs.Width(ctx),
|
||||
PadFunc: func(wr io.Writer) {
|
||||
renderText(w, rules, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: w,
|
||||
},
|
||||
}
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
IndentFunc: func(wr io.Writer) {
|
||||
renderText(w, bs.Parent().Style, " ")
|
||||
},
|
||||
Forward: &AnsiWriter{
|
||||
Forward: pw,
|
||||
},
|
||||
}
|
||||
renderText(bs.Current().Block, rules, rules.Suffix)
|
||||
|
||||
mw := NewMarginWriter(ctx, w, rules)
|
||||
if len(strings.TrimSpace(bs.Current().Block.String())) > 0 {
|
||||
flow := reflow.NewReflow(int(bs.Width(ctx)))
|
||||
flow.KeepNewlines = keepNewlines
|
||||
_, _ = flow.Write(bs.Current().Block.Bytes())
|
||||
flow.Close()
|
||||
|
||||
_, err := iw.Write(flow.Bytes())
|
||||
_, err := mw.Write(flow.Bytes())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, _ = pw.Write([]byte("\n"))
|
||||
_, _ = mw.Write([]byte("\n"))
|
||||
}
|
||||
|
||||
bs.Current().Block.Reset()
|
||||
|
||||
Reference in New Issue
Block a user