mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-08-30 05:36:42 -04:00
Cascade other style options to children when requested
This commit is contained in:
+9
-9
@@ -16,7 +16,7 @@ type BaseElement struct {
|
||||
Token string
|
||||
Prefix string
|
||||
Suffix string
|
||||
Style StyleType
|
||||
Style *ElementStyle
|
||||
}
|
||||
|
||||
func color(c string) (uint8, error) {
|
||||
@@ -73,25 +73,25 @@ func renderText(w io.Writer, rules *ElementStyle, s string) {
|
||||
out = out.BgIndex(i)
|
||||
}
|
||||
|
||||
if rules.Underline {
|
||||
if rules.Underline != nil && *rules.Underline {
|
||||
out = out.Underline()
|
||||
}
|
||||
if rules.Bold {
|
||||
if rules.Bold != nil && *rules.Bold {
|
||||
out = out.Bold()
|
||||
}
|
||||
if rules.Italic {
|
||||
if rules.Italic != nil && *rules.Italic {
|
||||
out = out.Italic()
|
||||
}
|
||||
if rules.CrossedOut {
|
||||
if rules.CrossedOut != nil && *rules.CrossedOut {
|
||||
out = out.CrossedOut()
|
||||
}
|
||||
if rules.Overlined {
|
||||
if rules.Overlined != nil && *rules.Overlined {
|
||||
out = out.Overlined()
|
||||
}
|
||||
if rules.Inverse {
|
||||
if rules.Inverse != nil && *rules.Inverse {
|
||||
out = out.Reverse()
|
||||
}
|
||||
if rules.Blink {
|
||||
if rules.Blink != nil && *rules.Blink {
|
||||
out = out.Blink()
|
||||
}
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func (e *BaseElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
||||
renderText(w, tr.blockStack.Current().Style, e.Suffix)
|
||||
}()
|
||||
|
||||
rules := tr.blockStack.With(tr.style[e.Style])
|
||||
rules := tr.blockStack.With(e.Style)
|
||||
if rules != nil {
|
||||
renderText(w, rules, rules.Prefix)
|
||||
defer func() {
|
||||
|
||||
+4
-4
@@ -29,10 +29,10 @@ func (s BlockStack) Indent() uint {
|
||||
var i uint
|
||||
|
||||
for _, v := range s {
|
||||
if v.Style == nil {
|
||||
if v.Style == nil || v.Style.Indent == nil {
|
||||
continue
|
||||
}
|
||||
i += v.Style.Indent
|
||||
i += *v.Style.Indent
|
||||
}
|
||||
|
||||
return i
|
||||
@@ -42,10 +42,10 @@ func (s BlockStack) Margin() uint {
|
||||
var i uint
|
||||
|
||||
for _, v := range s {
|
||||
if v.Style == nil {
|
||||
if v.Style == nil || v.Style.Margin == nil {
|
||||
continue
|
||||
}
|
||||
i += v.Style.Margin
|
||||
i += *v.Style.Margin
|
||||
}
|
||||
|
||||
return i
|
||||
|
||||
+7
-3
@@ -18,8 +18,12 @@ func (e *CodeBlockElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer)
|
||||
var margin uint
|
||||
rules := tr.style[CodeBlock]
|
||||
if rules != nil {
|
||||
indent = rules.Indent
|
||||
margin = rules.Margin
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
theme = rules.Theme
|
||||
}
|
||||
|
||||
@@ -40,7 +44,7 @@ func (e *CodeBlockElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer)
|
||||
// fallback rendering
|
||||
el := &BaseElement{
|
||||
Token: string(e.Code),
|
||||
Style: CodeBlock,
|
||||
Style: rules,
|
||||
}
|
||||
|
||||
return el.Render(iw, node, tr)
|
||||
|
||||
+6
-2
@@ -30,8 +30,12 @@ func (e *DocumentElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer) e
|
||||
var suffix string
|
||||
rules := tr.style[Document]
|
||||
if rules != nil {
|
||||
indent = rules.Indent
|
||||
margin = rules.Margin
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
suffix = rules.Suffix
|
||||
}
|
||||
pw := &PaddingWriter{
|
||||
|
||||
+11
-11
@@ -37,7 +37,7 @@ func (tr *TermRenderer) NewElement(node *bf.Node) Element {
|
||||
Exiting: "\n",
|
||||
Renderer: &BaseElement{
|
||||
Token: string(node.Literal),
|
||||
Style: BlockQuote,
|
||||
Style: tr.style[BlockQuote],
|
||||
},
|
||||
}
|
||||
case bf.List:
|
||||
@@ -67,28 +67,28 @@ func (tr *TermRenderer) NewElement(node *bf.Node) Element {
|
||||
Exiting: "\n",
|
||||
Renderer: &BaseElement{
|
||||
Token: "---",
|
||||
Style: HorizontalRule,
|
||||
Style: tr.style[HorizontalRule],
|
||||
},
|
||||
}
|
||||
case bf.Emph:
|
||||
return Element{
|
||||
Renderer: &BaseElement{
|
||||
Token: string(node.FirstChild.Literal),
|
||||
Style: Emph,
|
||||
Style: tr.style[Emph],
|
||||
},
|
||||
}
|
||||
case bf.Strong:
|
||||
return Element{
|
||||
Renderer: &BaseElement{
|
||||
Token: string(node.FirstChild.Literal),
|
||||
Style: Strong,
|
||||
Style: tr.style[Strong],
|
||||
},
|
||||
}
|
||||
case bf.Del:
|
||||
return Element{
|
||||
Renderer: &BaseElement{
|
||||
Token: string(node.Literal),
|
||||
Style: Del,
|
||||
Style: tr.style[Del],
|
||||
},
|
||||
}
|
||||
case bf.Link:
|
||||
@@ -103,14 +103,14 @@ func (tr *TermRenderer) NewElement(node *bf.Node) Element {
|
||||
return Element{
|
||||
Renderer: &BaseElement{
|
||||
Token: html.UnescapeString(stripper.Sanitize(string(node.Literal))),
|
||||
Style: Text,
|
||||
Style: tr.style[Text],
|
||||
},
|
||||
}
|
||||
case bf.HTMLBlock:
|
||||
return Element{
|
||||
Renderer: &BaseElement{
|
||||
Token: html.UnescapeString(strings.TrimSpace(stripper.Sanitize(string(node.Literal)))) + "\n",
|
||||
Style: HTMLBlock,
|
||||
Style: tr.style[HTMLBlock],
|
||||
},
|
||||
}
|
||||
case bf.CodeBlock:
|
||||
@@ -126,7 +126,7 @@ func (tr *TermRenderer) NewElement(node *bf.Node) Element {
|
||||
Exiting: "\n",
|
||||
Renderer: &BaseElement{
|
||||
Token: string(node.Literal),
|
||||
Style: Softbreak,
|
||||
Style: tr.style[Softbreak],
|
||||
},
|
||||
}
|
||||
case bf.Hardbreak:
|
||||
@@ -134,21 +134,21 @@ func (tr *TermRenderer) NewElement(node *bf.Node) Element {
|
||||
Exiting: "\n",
|
||||
Renderer: &BaseElement{
|
||||
Token: string(node.Literal),
|
||||
Style: Hardbreak,
|
||||
Style: tr.style[Hardbreak],
|
||||
},
|
||||
}
|
||||
case bf.Code:
|
||||
return Element{
|
||||
Renderer: &BaseElement{
|
||||
Token: string(node.Literal),
|
||||
Style: Code,
|
||||
Style: tr.style[Code],
|
||||
},
|
||||
}
|
||||
case bf.HTMLSpan:
|
||||
return Element{
|
||||
Renderer: &BaseElement{
|
||||
Token: html.UnescapeString(strings.TrimSpace(stripper.Sanitize(string(node.Literal)))) + "\n",
|
||||
Style: HTMLSpan,
|
||||
Style: tr.style[HTMLSpan],
|
||||
},
|
||||
}
|
||||
case bf.Table:
|
||||
|
||||
+7
-3
@@ -33,8 +33,12 @@ func (e *HeadingElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) er
|
||||
}
|
||||
|
||||
if rules != nil {
|
||||
indent = rules.Indent
|
||||
margin = rules.Margin
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
}
|
||||
|
||||
iw := &IndentWriter{
|
||||
@@ -56,7 +60,7 @@ func (e *HeadingElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) er
|
||||
el := &BaseElement{
|
||||
Prefix: pre,
|
||||
Token: fmt.Sprintf("%s %s", strings.Repeat("#", node.HeadingData.Level), node.FirstChild.Literal),
|
||||
Style: Heading,
|
||||
Style: rules,
|
||||
}
|
||||
err := el.Render(flow, node, tr)
|
||||
if err != nil {
|
||||
|
||||
@@ -13,7 +13,7 @@ func (e *ImageElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) erro
|
||||
if len(node.LastChild.Literal) > 0 {
|
||||
el := &BaseElement{
|
||||
Token: string(node.LastChild.Literal),
|
||||
Style: ImageText,
|
||||
Style: tr.style[ImageText],
|
||||
}
|
||||
err := el.Render(w, node.LastChild, tr)
|
||||
if err != nil {
|
||||
@@ -24,7 +24,7 @@ func (e *ImageElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) erro
|
||||
el := &BaseElement{
|
||||
Token: resolveRelativeURL(tr.BaseURL, string(node.LinkData.Destination)),
|
||||
Prefix: " ",
|
||||
Style: Image,
|
||||
Style: tr.style[Image],
|
||||
}
|
||||
err := el.Render(w, node, tr)
|
||||
if err != nil {
|
||||
|
||||
@@ -21,7 +21,7 @@ func (e *LinkElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
||||
if len(node.LastChild.Literal) > 0 {
|
||||
el := &BaseElement{
|
||||
Token: string(node.LastChild.Literal),
|
||||
Style: LinkText,
|
||||
Style: tr.style[LinkText],
|
||||
}
|
||||
err := el.Render(w, node.LastChild, tr)
|
||||
if err != nil {
|
||||
@@ -33,7 +33,7 @@ func (e *LinkElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
||||
el := &BaseElement{
|
||||
Token: resolveRelativeURL(tr.BaseURL, string(node.LinkData.Destination)),
|
||||
Prefix: " ",
|
||||
Style: Link,
|
||||
Style: tr.style[Link],
|
||||
}
|
||||
err := el.Render(w, node, tr)
|
||||
if err != nil {
|
||||
|
||||
@@ -32,8 +32,12 @@ func (e *ListElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
||||
var suffix string
|
||||
rules := tr.blockStack.Current().Style
|
||||
if rules != nil {
|
||||
indent = rules.Indent
|
||||
margin = rules.Margin
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
suffix = rules.Suffix
|
||||
}
|
||||
renderText(tr.blockStack.Current().Block, rules, suffix)
|
||||
|
||||
+2
-2
@@ -23,13 +23,13 @@ func (e *ItemElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
||||
|
||||
el = &BaseElement{
|
||||
Token: string(node.Literal),
|
||||
Style: Enumeration,
|
||||
Style: tr.style[Enumeration],
|
||||
Prefix: strconv.FormatInt(l+1, 10),
|
||||
}
|
||||
} else {
|
||||
el = &BaseElement{
|
||||
Token: string(node.Literal),
|
||||
Style: Item,
|
||||
Style: tr.style[Item],
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-2
@@ -44,8 +44,12 @@ func (e *ParagraphElement) Finish(w io.Writer, node *bf.Node, tr *TermRenderer)
|
||||
}
|
||||
|
||||
if rules != nil {
|
||||
indent = rules.Indent
|
||||
margin = rules.Margin
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
suffix = rules.Suffix
|
||||
}
|
||||
renderText(tr.blockStack.Current().Block, rules, suffix)
|
||||
|
||||
@@ -47,17 +47,17 @@ const (
|
||||
type ElementStyle struct {
|
||||
Color string `json:"color"`
|
||||
BackgroundColor string `json:"background_color"`
|
||||
Underline bool `json:"underline"`
|
||||
Bold bool `json:"bold"`
|
||||
Italic bool `json:"italic"`
|
||||
CrossedOut bool `json:"crossed_out"`
|
||||
Faint bool `json:"faint"`
|
||||
Conceal bool `json:"conceal"`
|
||||
Overlined bool `json:"overlined"`
|
||||
Inverse bool `json:"inverse"`
|
||||
Blink bool `json:"blink"`
|
||||
Indent uint `json:"indent"`
|
||||
Margin uint `json:"margin"`
|
||||
Underline *bool `json:"underline"`
|
||||
Bold *bool `json:"bold"`
|
||||
Italic *bool `json:"italic"`
|
||||
CrossedOut *bool `json:"crossed_out"`
|
||||
Faint *bool `json:"faint"`
|
||||
Conceal *bool `json:"conceal"`
|
||||
Overlined *bool `json:"overlined"`
|
||||
Inverse *bool `json:"inverse"`
|
||||
Blink *bool `json:"blink"`
|
||||
Indent *uint `json:"indent"`
|
||||
Margin *uint `json:"margin"`
|
||||
Theme string `json:"theme"`
|
||||
Prefix string `json:"prefix"`
|
||||
Suffix string `json:"suffix"`
|
||||
@@ -86,8 +86,17 @@ func cascadeStyle(parent *ElementStyle, child *ElementStyle, onlyColors bool) *E
|
||||
s.BackgroundColor = parent.BackgroundColor
|
||||
|
||||
if !onlyColors {
|
||||
s.Margin = parent.Margin
|
||||
s.Indent = parent.Indent
|
||||
s.Margin = parent.Margin
|
||||
s.Underline = parent.Underline
|
||||
s.Bold = parent.Bold
|
||||
s.Italic = parent.Italic
|
||||
s.CrossedOut = parent.CrossedOut
|
||||
s.Faint = parent.Faint
|
||||
s.Conceal = parent.Conceal
|
||||
s.Overlined = parent.Overlined
|
||||
s.Inverse = parent.Inverse
|
||||
s.Blink = parent.Blink
|
||||
}
|
||||
|
||||
if child != nil {
|
||||
@@ -97,11 +106,39 @@ func cascadeStyle(parent *ElementStyle, child *ElementStyle, onlyColors bool) *E
|
||||
if child.BackgroundColor != "" {
|
||||
s.BackgroundColor = child.BackgroundColor
|
||||
}
|
||||
if child.Margin > 0 {
|
||||
|
||||
if child.Indent != nil {
|
||||
s.Indent = child.Indent
|
||||
}
|
||||
if child.Margin != nil {
|
||||
s.Margin = child.Margin
|
||||
}
|
||||
if child.Indent > 0 {
|
||||
s.Indent = child.Indent
|
||||
if child.Underline != nil {
|
||||
s.Underline = child.Underline
|
||||
}
|
||||
if child.Bold != nil {
|
||||
s.Bold = child.Bold
|
||||
}
|
||||
if child.Italic != nil {
|
||||
s.Italic = child.Italic
|
||||
}
|
||||
if child.CrossedOut != nil {
|
||||
s.CrossedOut = child.CrossedOut
|
||||
}
|
||||
if child.Faint != nil {
|
||||
s.Faint = child.Faint
|
||||
}
|
||||
if child.Conceal != nil {
|
||||
s.Conceal = child.Conceal
|
||||
}
|
||||
if child.Overlined != nil {
|
||||
s.Overlined = child.Overlined
|
||||
}
|
||||
if child.Inverse != nil {
|
||||
s.Inverse = child.Inverse
|
||||
}
|
||||
if child.Blink != nil {
|
||||
s.Blink = child.Blink
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -27,8 +27,12 @@ func (e *TableElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) erro
|
||||
var margin uint
|
||||
rules := tr.style[Table]
|
||||
if rules != nil {
|
||||
indent = rules.Indent
|
||||
margin = rules.Margin
|
||||
if rules.Indent != nil {
|
||||
indent = *rules.Indent
|
||||
}
|
||||
if rules.Margin != nil {
|
||||
margin = *rules.Margin
|
||||
}
|
||||
}
|
||||
iw := &IndentWriter{
|
||||
Indent: indent + margin,
|
||||
|
||||
Reference in New Issue
Block a user