mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 00:47:15 -04:00
Simplify code
This commit is contained in:
+8
-12
@@ -16,6 +16,10 @@ type BaseElement struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func renderText(w io.Writer, rules *ElementStyle, s string) {
|
func renderText(w io.Writer, rules *ElementStyle, s string) {
|
||||||
|
if len(s) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
out := aurora.Reset(s)
|
out := aurora.Reset(s)
|
||||||
|
|
||||||
if rules != nil {
|
if rules != nil {
|
||||||
@@ -59,13 +63,9 @@ 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 {
|
||||||
if e.Prefix != "" {
|
renderText(w, tr.blockStyle.Current(), e.Prefix)
|
||||||
renderText(w, tr.blockStyle.Current(), e.Prefix)
|
|
||||||
}
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if e.Suffix != "" {
|
renderText(w, tr.blockStyle.Current(), e.Suffix)
|
||||||
renderText(w, tr.blockStyle.Current(), e.Suffix)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
rules := cascadeStyles(tr.blockStyle, tr.style[e.Style])
|
rules := cascadeStyles(tr.blockStyle, tr.style[e.Style])
|
||||||
@@ -74,13 +74,9 @@ func (e *BaseElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if rules.Prefix != "" {
|
renderText(w, rules, rules.Prefix)
|
||||||
renderText(w, rules, rules.Prefix)
|
|
||||||
}
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if rules.Suffix != "" {
|
renderText(w, rules, rules.Suffix)
|
||||||
renderText(w, rules, rules.Suffix)
|
|
||||||
}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
renderText(w, rules, e.Token)
|
renderText(w, rules, e.Token)
|
||||||
|
|||||||
+2
-6
@@ -13,9 +13,7 @@ func (e *DocumentElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) e
|
|||||||
rules := tr.style[Document]
|
rules := tr.style[Document]
|
||||||
if rules != nil {
|
if rules != nil {
|
||||||
tr.blockStyle.Push(rules)
|
tr.blockStyle.Push(rules)
|
||||||
if rules.Prefix != "" {
|
renderText(w, rules, rules.Prefix)
|
||||||
renderText(w, rules, rules.Prefix)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -43,8 +41,6 @@ func (e *DocumentElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer) e
|
|||||||
tr.document.Reset()
|
tr.document.Reset()
|
||||||
tr.blockStyle.Pop()
|
tr.blockStyle.Pop()
|
||||||
|
|
||||||
if suffix != "" {
|
renderText(w, rules, suffix)
|
||||||
renderText(w, rules, suffix)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,9 +95,7 @@ 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 {
|
||||||
if e.Entering != "" {
|
_, _ = tr.document.Write([]byte(e.Entering))
|
||||||
_, _ = tr.document.Write([]byte(e.Entering))
|
|
||||||
}
|
|
||||||
|
|
||||||
// each paragraph gets rendered into a separate buffer
|
// each paragraph gets rendered into a separate buffer
|
||||||
if tr.paragraph != nil {
|
if tr.paragraph != nil {
|
||||||
@@ -126,9 +124,7 @@ func (tr *TermRenderer) RenderNode(w io.Writer, node *bf.Node, entering bool) bf
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if e.Exiting != "" {
|
_, _ = tr.document.Write([]byte(e.Exiting))
|
||||||
_, _ = tr.document.Write([]byte(e.Exiting))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return bf.GoToNext
|
return bf.GoToNext
|
||||||
|
|||||||
+2
-4
@@ -22,7 +22,7 @@ func (e *ParagraphElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer)
|
|||||||
_, _ = w.Write([]byte("\n"))
|
_, _ = w.Write([]byte("\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
if rules != nil && rules.Prefix != "" {
|
if rules != nil {
|
||||||
renderText(w, tr.blockStyle.Current(), rules.Prefix)
|
renderText(w, tr.blockStyle.Current(), rules.Prefix)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -40,9 +40,7 @@ func (e *ParagraphElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer)
|
|||||||
indent = 0
|
indent = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if suffix != "" {
|
renderText(tr.paragraph, rules, suffix)
|
||||||
renderText(tr.paragraph, rules, suffix)
|
|
||||||
}
|
|
||||||
|
|
||||||
pw := &PaddingWriter{
|
pw := &PaddingWriter{
|
||||||
Padding: uint(tr.WordWrap + int(indent) - int(tr.blockStyle.Indent())),
|
Padding: uint(tr.WordWrap + int(indent) - int(tr.blockStyle.Indent())),
|
||||||
|
|||||||
Reference in New Issue
Block a user