From 6feb55b98c130ee4d77de6b24936a88d6e40d383 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 28 Dec 2019 07:44:29 +0100 Subject: [PATCH] Fix cascading style attributes from BlockElements --- ansi/blockstack.go | 2 +- ansi/elements.go | 8 ++++---- ansi/heading.go | 14 +++++++------- ansi/paragraph.go | 2 +- ansi/style.go | 27 +++++++++++++-------------- styles/examples/heading.png | Bin 1824 -> 1824 bytes testdata/heading.test | 2 +- testdata/readme.test | 12 ++++++------ 8 files changed, 33 insertions(+), 34 deletions(-) diff --git a/ansi/blockstack.go b/ansi/blockstack.go index 0ce8df1..96fdb91 100644 --- a/ansi/blockstack.go +++ b/ansi/blockstack.go @@ -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 } diff --git a/ansi/elements.go b/ansi/elements.go index 970a884..f4403da 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -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, } diff --git a/ansi/heading.go b/ansi/heading.go index a6f5660..c195ae5 100644 --- a/ansi/heading.go +++ b/ansi/heading.go @@ -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) diff --git a/ansi/paragraph.go b/ansi/paragraph.go index ed7f91d..20cd00e 100644 --- a/ansi/paragraph.go +++ b/ansi/paragraph.go @@ -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) diff --git a/ansi/style.go b/ansi/style.go index abfc36d..4b599da 100644 --- a/ansi/style.go +++ b/ansi/style.go @@ -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 { diff --git a/styles/examples/heading.png b/styles/examples/heading.png index bf53a3e111130602c5b0ce28d4c6533a9025401b..b10bc34524186e0522b2c117b415169cb0ab9edb 100644 GIT binary patch delta 104 zcmZ3$w}5X$2{VTbn>Npn0`92IHOxWGK}HrK24+?!##V+#+6D$z1_p;NXx?UEU{Eb_ sjVMV;EJ?M>%}>cpt7I@TFtpS)G}6V;u(@|)I#2_Hr>mdKI;Vst0RKN6>;M1& delta 104 zcmZ3$w}5X$2{VU~7?;e{g}3Hyu3-*h4l=X|F*33;G_W$V&^9ozGB99Zn%u#_z@S>< s8c~vxSdwa$o1c=IR>@#wU}&jpXrzmwp)g+C1gL?*)78&qol`;+0NRloJOBUy diff --git a/testdata/heading.test b/testdata/heading.test index 706ca5d..7769fff 100644 --- a/testdata/heading.test +++ b/testdata/heading.test @@ -1,3 +1,3 @@ - => h1 <=  + => h1 <=   ## h2   ### h3 \ No newline at end of file diff --git a/testdata/readme.test b/testdata/readme.test index e89c1db..14fa909 100644 --- a/testdata/readme.test +++ b/testdata/readme.test @@ -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: