Cascade Heading style into new H1, H2, ..., H6 styles

This commit is contained in:
Christian Muehlhaeuser
2019-12-05 14:05:35 +01:00
parent 12d8828d4b
commit eaed263d06
5 changed files with 58 additions and 4 deletions
+1 -1
View File
@@ -70,5 +70,5 @@ func (s BlockStack) Current() BlockElement {
}
func (s BlockStack) With(child *ElementStyle) *ElementStyle {
return cascadeStyle(s.Current().Style, child)
return cascadeStyle(s.Current().Style, child, true)
}
+16
View File
@@ -16,6 +16,22 @@ func (e *HeadingElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) er
var indent uint
var margin uint
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 {
indent = rules.Indent
margin = rules.Margin
+1 -1
View File
@@ -19,7 +19,7 @@ func (e *ListElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer) error
be := BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(tr.blockStack.Current().Style, rules),
Style: cascadeStyle(tr.blockStack.Current().Style, rules, true),
}
tr.blockStack.Push(be)
+1 -1
View File
@@ -22,7 +22,7 @@ func (e *ParagraphElement) Render(w io.Writer, node *bf.Node, tr *TermRenderer)
_, _ = w.Write([]byte("\n"))
be := BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(tr.blockStack.Current().Style, rules),
Style: cascadeStyle(tr.blockStack.Current().Style, rules, true),
}
tr.blockStack.Push(be)
}
+39 -1
View File
@@ -16,6 +16,12 @@ const (
Enumeration
Paragraph
Heading
H1
H2
H3
H4
H5
H6
HorizontalRule
Emph
Strong
@@ -57,7 +63,16 @@ type ElementStyle struct {
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 {
return child
}
@@ -70,6 +85,11 @@ func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
s.Color = parent.Color
s.BackgroundColor = parent.BackgroundColor
if !onlyColors {
s.Margin = parent.Margin
s.Indent = parent.Indent
}
if child != nil {
if child.Color != "" {
s.Color = child.Color
@@ -77,6 +97,12 @@ func cascadeStyle(parent *ElementStyle, child *ElementStyle) *ElementStyle {
if child.BackgroundColor != "" {
s.BackgroundColor = child.BackgroundColor
}
if child.Margin > 0 {
s.Margin = child.Margin
}
if child.Indent > 0 {
s.Indent = child.Indent
}
}
return &s
@@ -148,6 +174,18 @@ func keyToType(key string) (StyleType, error) {
return Paragraph, nil
case "heading":
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":
return HorizontalRule, nil
case "emph":