Fix cascading style attributes from BlockElements

This commit is contained in:
Christian Muehlhaeuser
2019-12-28 07:44:29 +01:00
parent d3bde07b2f
commit 6feb55b98c
8 changed files with 33 additions and 34 deletions
+1 -1
View File
@@ -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
}
+4 -4
View File
@@ -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,
}
@@ -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,
}
@@ -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,
@@ -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,
}
+7 -7
View File
@@ -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 {
@@ -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)
+1 -1
View File
@@ -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)
+13 -14
View File
@@ -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 {
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

+1 -1
View File
@@ -1,3 +1,3 @@
 => h1 <= 
 => h1 <= 
 ## h2 
 ### h3
+6 -6
View File
@@ -1,20 +1,20 @@
  Gold                                                                         
  Gold                                                                         
                                                                               
 Render markdown on the CLI, with pizzazz!                                     
                                                                               
 ## What is it?                                                                
 ## What is it?                                                                
                                                                               
 Gold is a Golang library that allows you to use JSON based stylesheets to     
 render Markdown files in the terminal. Just like CSS, you can define color and
 style attributes on Markdown elements. The difference is that you use ANSI    
 color and terminal codes instead of CSS properties and hex colors.            
                                                                               
 ## Usage                                                                      
 ## Usage                                                                      
                                                                               
 See cmd/gold /cmd/gold/.                                                      
                                                                               
 ## Example Output                                                             
 ## Example Output                                                             
                                                                               
 Image: Gold Dark Style →                                                      
 https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png      
@@ -22,12 +22,12 @@
 Check out the Gold Style Gallery                                              
 https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!   
                                                                               
 ## Colors                                                                     
 ## Colors                                                                     
                                                                               
 Currently  gold  uses the Aurora ANSI colors                                  
 https://godoc.org/github.com/logrusorgru/aurora#Index.                        
                                                                               
 ## Development                                                                
 ## Development                                                                
                                                                               
 Style definitions located in  styles/  can be embedded into the binary by     
 running statik https://github.com/rakyll/statik: