[go: up one dir, main page]

Skip to content

Commit

Permalink
Fix cascading style attributes from BlockElements
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Dec 28, 2019
1 parent d3bde07 commit 6feb55b
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion ansi/blockstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,5 @@ func (s BlockStack) Current() BlockElement {
func (s BlockStack) With(child StylePrimitive) StylePrimitive {
sb := StyleBlock{}
sb.StylePrimitive = child
return cascadeStyle(s.Current().Style, sb, true).StylePrimitive
return cascadeStyle(s.Current().Style, sb, false).StylePrimitive
}
8 changes: 4 additions & 4 deletions ansi/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
case ast.KindBlockquote:
e := &BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.BlockQuote, true),
Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.BlockQuote, false),
Margin: true,
Newline: true,
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {

e := &BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(ctx.blockStack.Current().Style, s, true),
Style: cascadeStyle(ctx.blockStack.Current().Style, s, false),
Margin: true,
Newline: true,
}
Expand Down Expand Up @@ -281,7 +281,7 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
// n := node.(*ast.CodeSpan)
e := &BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.Code, true),
Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.Code, false),
}
return Element{
Renderer: e,
Expand Down Expand Up @@ -344,7 +344,7 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {
case astext.KindDefinitionList:
e := &BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.DefinitionList, true),
Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.DefinitionList, false),
Margin: true,
Newline: true,
}
Expand Down
14 changes: 7 additions & 7 deletions ansi/heading.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {

switch e.Level {
case 1:
rules = cascadeStyles(false, rules, ctx.options.Styles.H1)
rules = cascadeStyles(true, rules, ctx.options.Styles.H1)
case 2:
rules = cascadeStyles(false, rules, ctx.options.Styles.H2)
rules = cascadeStyles(true, rules, ctx.options.Styles.H2)
case 3:
rules = cascadeStyles(false, rules, ctx.options.Styles.H3)
rules = cascadeStyles(true, rules, ctx.options.Styles.H3)
case 4:
rules = cascadeStyles(false, rules, ctx.options.Styles.H4)
rules = cascadeStyles(true, rules, ctx.options.Styles.H4)
case 5:
rules = cascadeStyles(false, rules, ctx.options.Styles.H5)
rules = cascadeStyles(true, rules, ctx.options.Styles.H5)
case 6:
rules = cascadeStyles(false, rules, ctx.options.Styles.H6)
rules = cascadeStyles(true, rules, ctx.options.Styles.H6)
}

if !e.First {
Expand All @@ -40,7 +40,7 @@ func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error {

be := BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(bs.Current().Style, rules, true),
Style: cascadeStyle(bs.Current().Style, rules, false),
}
bs.Push(be)

Expand Down
2 changes: 1 addition & 1 deletion ansi/paragraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (e *ParagraphElement) Render(w io.Writer, ctx RenderContext) error {
}
be := BlockElement{
Block: &bytes.Buffer{},
Style: cascadeStyle(bs.Current().Style, rules, true),
Style: cascadeStyle(bs.Current().Style, rules, false),
}
bs.Push(be)

Expand Down
27 changes: 13 additions & 14 deletions ansi/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,38 +136,37 @@ type StyleConfig struct {
HTMLSpan StyleBlock `json:"html_span"`
}

func cascadeStyles(onlyColors bool, s ...StyleBlock) StyleBlock {
func cascadeStyles(toBlock bool, s ...StyleBlock) StyleBlock {
var r StyleBlock

for _, v := range s {
r = cascadeStyle(r, v, onlyColors)
r = cascadeStyle(r, v, toBlock)
}
return r
}

func cascadeStyle(parent StyleBlock, child StyleBlock, onlyColors bool) StyleBlock {
func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock {
s := child

s.Color = parent.Color
s.BackgroundColor = parent.BackgroundColor
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 !onlyColors {
if toBlock {
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
s.BlockPrefix = parent.BlockPrefix
s.BlockSuffix = parent.BlockSuffix
s.Prefix = parent.Prefix
s.Suffix = parent.Suffix
s.Format = parent.Format
}

if child.Color != nil {
Expand Down
Binary file modified styles/examples/heading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion testdata/heading.test
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
 => h1 <= 
 => h1 <= 
 ## h2 
 ### h3
Loading

0 comments on commit 6feb55b

Please sign in to comment.