mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-05 08:27:18 -04:00
Cascade Heading style into new H1, H2, ..., H6 styles
This commit is contained in:
+1
-1
@@ -70,5 +70,5 @@ func (s BlockStack) Current() BlockElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s BlockStack) With(child *ElementStyle) *ElementStyle {
|
func (s BlockStack) With(child *ElementStyle) *ElementStyle {
|
||||||
return cascadeStyle(s.Current().Style, child)
|
return cascadeStyle(s.Current().Style, child, true)
|
||||||
}
|
}
|
||||||
|
|||||||
+16
@@ -16,6 +16,22 @@ func (e *HeadingElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) er
|
|||||||
var indent uint
|
var indent uint
|
||||||
var margin uint
|
var margin uint
|
||||||
rules := tr.style[Heading]
|
rules := tr.style[Heading]
|
||||||
|
|
||||||
|
switch node.HeadingData.Level {
|
||||||
|
case 1:
|
||||||
|
rules = cascadeStyles(false, rules, tr.style[H1])
|
||||||
|
case 2:
|
||||||
|
rules = cascadeStyles(false, rules, tr.style[H2])
|
||||||
|
case 3:
|
||||||
|
rules = cascadeStyles(false, rules, tr.style[H3])
|
||||||
|
case 4:
|
||||||
|
rules = cascadeStyles(false, rules, tr.style[H4])
|
||||||
|
case 5:
|
||||||
|
rules = cascadeStyles(false, rules, tr.style[H5])
|
||||||
|
case 6:
|
||||||
|
rules = cascadeStyles(false, rules, tr.style[H6])
|
||||||
|
}
|
||||||
|
|
||||||
if rules != nil {
|
if rules != nil {
|
||||||
indent = rules.Indent
|
indent = rules.Indent
|
||||||
margin = rules.Margin
|
margin = rules.Margin
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func (e *ListElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
|
|||||||
|
|
||||||
be := BlockElement{
|
be := BlockElement{
|
||||||
Block: &bytes.Buffer{},
|
Block: &bytes.Buffer{},
|
||||||
Style: cascadeStyle(tr.blockStack.Current().Style, rules),
|
Style: cascadeStyle(tr.blockStack.Current().Style, rules, true),
|
||||||
}
|
}
|
||||||
tr.blockStack.Push(be)
|
tr.blockStack.Push(be)
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -22,7 +22,7 @@ func (e *ParagraphElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer)
|
|||||||
_, _ = w.Write([]byte("\n"))
|
_, _ = w.Write([]byte("\n"))
|
||||||
be := BlockElement{
|
be := BlockElement{
|
||||||
Block: &bytes.Buffer{},
|
Block: &bytes.Buffer{},
|
||||||
Style: cascadeStyle(tr.blockStack.Current().Style, rules),
|
Style: cascadeStyle(tr.blockStack.Current().Style, rules, true),
|
||||||
}
|
}
|
||||||
tr.blockStack.Push(be)
|
tr.blockStack.Push(be)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,12 @@ const (
|
|||||||
Enumeration
|
Enumeration
|
||||||
Paragraph
|
Paragraph
|
||||||
Heading
|
Heading
|
||||||
|
H1
|
||||||
|
H2
|
||||||
|
H3
|
||||||
|
H4
|
||||||
|
H5
|
||||||
|
H6
|
||||||
HorizontalRule
|
HorizontalRule
|
||||||
Emph
|
Emph
|
||||||
Strong
|
Strong
|
||||||
@@ -57,7 +63,16 @@ type ElementStyle struct {
|
|||||||
Suffix string `json:"suffix"`
|
Suffix string `json:"suffix"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
|
func cascadeStyles(onlyColors bool, s ...*ElementStyle) *ElementStyle {
|
||||||
|
var r *ElementStyle
|
||||||
|
|
||||||
|
for _, v := range s {
|
||||||
|
r = cascadeStyle(r, v, onlyColors)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func cascadeStyle(parent *ElementStyle, child *ElementStyle, onlyColors bool) *ElementStyle {
|
||||||
if parent == nil {
|
if parent == nil {
|
||||||
return child
|
return child
|
||||||
}
|
}
|
||||||
@@ -70,6 +85,11 @@ func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
|
|||||||
s.Color = parent.Color
|
s.Color = parent.Color
|
||||||
s.BackgroundColor = parent.BackgroundColor
|
s.BackgroundColor = parent.BackgroundColor
|
||||||
|
|
||||||
|
if !onlyColors {
|
||||||
|
s.Margin = parent.Margin
|
||||||
|
s.Indent = parent.Indent
|
||||||
|
}
|
||||||
|
|
||||||
if child != nil {
|
if child != nil {
|
||||||
if child.Color != "" {
|
if child.Color != "" {
|
||||||
s.Color = child.Color
|
s.Color = child.Color
|
||||||
@@ -77,6 +97,12 @@ func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
|
|||||||
if child.BackgroundColor != "" {
|
if child.BackgroundColor != "" {
|
||||||
s.BackgroundColor = child.BackgroundColor
|
s.BackgroundColor = child.BackgroundColor
|
||||||
}
|
}
|
||||||
|
if child.Margin > 0 {
|
||||||
|
s.Margin = child.Margin
|
||||||
|
}
|
||||||
|
if child.Indent > 0 {
|
||||||
|
s.Indent = child.Indent
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return &s
|
return &s
|
||||||
@@ -148,6 +174,18 @@ func keyToType(key string) (StyleType, error) {
|
|||||||
return Paragraph, nil
|
return Paragraph, nil
|
||||||
case "heading":
|
case "heading":
|
||||||
return Heading, nil
|
return Heading, nil
|
||||||
|
case "h1":
|
||||||
|
return H1, nil
|
||||||
|
case "h2":
|
||||||
|
return H2, nil
|
||||||
|
case "h3":
|
||||||
|
return H3, nil
|
||||||
|
case "h4":
|
||||||
|
return H4, nil
|
||||||
|
case "h5":
|
||||||
|
return H5, nil
|
||||||
|
case "h6":
|
||||||
|
return H6, nil
|
||||||
case "hr":
|
case "hr":
|
||||||
return HorizontalRule, nil
|
return HorizontalRule, nil
|
||||||
case "emph":
|
case "emph":
|
||||||
|
|||||||
Reference in New Issue
Block a user