From 15690b3dd657396a86c6eb1dc1ac39f2ba5c7525 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Fri, 12 Jul 2024 16:54:16 -0300 Subject: [PATCH 01/36] wip Signed-off-by: Carlos Alexandro Becker --- .gitignore | 1 + ansi/elements.go | 48 +++++++++++++++++++--------- ansi/emphasis.go | 35 ++++++++++++++++++++ ansi/link.go | 64 +++++++++---------------------------- ansi/renderer_test.go | 4 +-- examples/helloworld/main.go | 19 +++++++++++ testdata/issues/312.md | 3 ++ testdata/issues/312.test | 5 +++ testdata/issues/42.test | 4 +-- testdata/issues/43.test | 14 ++++---- 10 files changed, 122 insertions(+), 75 deletions(-) create mode 100644 ansi/emphasis.go create mode 100644 testdata/issues/312.md create mode 100644 testdata/issues/312.test diff --git a/.gitignore b/.gitignore index b3087b09..eafbdee6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ cmd/ +!*.test diff --git a/ansi/elements.go b/ansi/elements.go index ce41eb3a..30c7ba4c 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -176,16 +176,18 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { case ast.KindEmphasis: n := node.(*ast.Emphasis) - s := string(n.Text(source)) - style := ctx.options.Styles.Emph - if n.Level > 1 { - style = ctx.options.Styles.Strong + var children []ElementRenderer + if n.HasChildren() { + nn := n.FirstChild() + for nn != nil { + children = append(children, tr.NewElement(nn, source).Renderer) + nn = nn.NextSibling() + } } - return Element{ - Renderer: &BaseElement{ - Token: html.UnescapeString(s), - Style: style, + Renderer: &EmphasisElement{ + Level: n.Level, + Children: children, }, } @@ -213,26 +215,42 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { // Links case ast.KindLink: n := node.(*ast.Link) + var children []ElementRenderer + if n.HasChildren() { + nn := n.FirstChild() + for nn != nil { + children = append(children, tr.NewElement(nn, source).Renderer) + nn = nn.NextSibling() + } + } return Element{ Renderer: &LinkElement{ - Text: textFromChildren(node, source), - BaseURL: ctx.options.BaseURL, - URL: string(n.Destination), + BaseURL: ctx.options.BaseURL, + URL: string(n.Destination), + Children: children, }, } case ast.KindAutoLink: n := node.(*ast.AutoLink) u := string(n.URL(source)) - label := string(n.Label(source)) if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(u), "mailto:") { u = "mailto:" + u } + var children []ElementRenderer + if n.HasChildren() { + nn := n.FirstChild() + for nn != nil { + children = append(children, tr.NewElement(nn, source).Renderer) + nn = nn.NextSibling() + } + } + return Element{ Renderer: &LinkElement{ - Text: label, - BaseURL: ctx.options.BaseURL, - URL: u, + Children: children, + BaseURL: ctx.options.BaseURL, + URL: u, }, } diff --git a/ansi/emphasis.go b/ansi/emphasis.go new file mode 100644 index 00000000..35f1f1d9 --- /dev/null +++ b/ansi/emphasis.go @@ -0,0 +1,35 @@ +package ansi + +import ( + "bytes" + "html" + "io" +) + +// A EmphasisElement is used to render emphasis. +type EmphasisElement struct { + Children []ElementRenderer + Level int +} + +func (e *EmphasisElement) Render(w io.Writer, ctx RenderContext) error { + style := ctx.options.Styles.Emph + if e.Level > 1 { + style = ctx.options.Styles.Strong + } + + var b bytes.Buffer + for _, child := range e.Children { + if err := child.Render(&b, ctx); err != nil { + return err + } + } + + el := Element{ + Renderer: &BaseElement{ + Token: html.UnescapeString(b.String()), + Style: style, + }, + } + return el.Renderer.Render(w, ctx) +} diff --git a/ansi/link.go b/ansi/link.go index 9028395b..537b6b91 100644 --- a/ansi/link.go +++ b/ansi/link.go @@ -1,75 +1,41 @@ package ansi import ( + "bytes" "io" "net/url" ) // A LinkElement is used to render hyperlinks. type LinkElement struct { - Text string - BaseURL string - URL string - Child ElementRenderer + BaseURL string + URL string + Children []ElementRenderer } func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error { - var textRendered bool - if len(e.Text) > 0 && e.Text != e.URL { - textRendered = true - + for _, child := range e.Children { + var b bytes.Buffer + if err := child.Render(&b, ctx); err != nil { + return err + } el := &BaseElement{ - Token: e.Text, + Token: b.String(), Style: ctx.options.Styles.LinkText, } - err := el.Render(w, ctx) - if err != nil { + if err := el.Render(w, ctx); err != nil { return err } } - /* - if node.LastChild != nil { - if node.LastChild.Type == bf.Image { - el := tr.NewElement(node.LastChild) - err := el.Renderer.Render(w, node.LastChild, tr) - if err != nil { - return err - } - } - if len(node.LastChild.Literal) > 0 && - string(node.LastChild.Literal) != string(node.LinkData.Destination) { - textRendered = true - el := &BaseElement{ - Token: string(node.LastChild.Literal), - Style: ctx.style[LinkText], - } - err := el.Render(w, node.LastChild, tr) - if err != nil { - return err - } - } - } - */ - u, err := url.Parse(e.URL) - if err == nil && - "#"+u.Fragment != e.URL { // if the URL only consists of an anchor, ignore it - pre := " " - style := ctx.options.Styles.Link - if !textRendered { - pre = "" - style.BlockPrefix = "" - style.BlockSuffix = "" - } - + if err == nil && "#"+u.Fragment != e.URL { // if the URL only consists of an anchor, ignore it el := &BaseElement{ Token: resolveRelativeURL(e.BaseURL, e.URL), - Prefix: pre, - Style: style, + Prefix: " ", + Style: ctx.options.Styles.Link, } - err := el.Render(w, ctx) - if err != nil { + if err := el.Render(w, ctx); err != nil { return err } } diff --git a/ansi/renderer_test.go b/ansi/renderer_test.go index e391e917..1ff146f2 100644 --- a/ansi/renderer_test.go +++ b/ansi/renderer_test.go @@ -18,8 +18,8 @@ import ( ) const ( - generateExamples = false - generateIssues = false + generateExamples = true + generateIssues = true examplesDir = "../styles/examples/" issuesDir = "../testdata/issues/" ) diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 53282da3..cdfd7ea6 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -12,6 +12,25 @@ func main() { This is a simple example of Markdown rendering with Glamour! Check out the [other examples](https://github.com/charmbracelet/glamour/tree/master/examples) too. +**just bold text** + +_just italic text_ + +**[bold with url within](https://www.example.com)** + +[normal](https://www.example.com) + + +[url with **bold** within](https://www.example.com) + +[url with _italic_ within](https://www.example.com) + +[**entire url text is bold**](https://www.example.com) + +[_entire url text is italic_](https://www.example.com) + +_URL_ + Bye! ` diff --git a/testdata/issues/312.md b/testdata/issues/312.md new file mode 100644 index 00000000..b5f90372 --- /dev/null +++ b/testdata/issues/312.md @@ -0,0 +1,3 @@ +# File + +**[URL](https://www.example.com)** diff --git a/testdata/issues/312.test b/testdata/issues/312.test new file mode 100644 index 00000000..78d446ab --- /dev/null +++ b/testdata/issues/312.test @@ -0,0 +1,5 @@ + +  File                                                                        +                                                                              + URL https://www.example.com                                                  + diff --git a/testdata/issues/42.test b/testdata/issues/42.test index 09f61ffc..46560119 100644 --- a/testdata/issues/42.test +++ b/testdata/issues/42.test @@ -1,6 +1,6 @@ - If you want to make a more significant change, please first open an issue    + If you want to make a more significant change, please first open an issue     https://github.com/twpayne/chezmoi/issues/new to discuss the change that you - want to make. Dave Cheney gives a good rationale                             + want to make. Dave Cheney gives a good rationale                              https://dave.cheney.net/2019/02/18/talk-then-code as to why this is important. diff --git a/testdata/issues/43.test b/testdata/issues/43.test index f88488f2..6a98a895 100644 --- a/testdata/issues/43.test +++ b/testdata/issues/43.test @@ -1,10 +1,10 @@                                                                              - β€’ Getting started                                                            - β€’ Developing locally                                                         - β€’ Documentation changes                                                      - β€’ Contributing changes                                                       - β€’ Managing releases                                                          - β€’ Packaging                                                                  - β€’ Updating the website                                                       + β€’ Getting started                                                            + β€’ Developing locally                                                         + β€’ Documentation changes                                                      + β€’ Contributing changes                                                       + β€’ Managing releases                                                          + β€’ Packaging                                                                  + β€’ Updating the website                                                       From a4999c1338f4750137aaf73118a5543afc3a9f95 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 15 Jul 2024 16:23:10 -0300 Subject: [PATCH 02/36] wip --- ansi/baseelement.go | 44 +++++++++++++++++++++++++++++++++----------- ansi/elements.go | 22 ++++------------------ ansi/emphasis.go | 21 +++++++++------------ ansi/link.go | 27 +++++++++++++++++---------- ansi/table.go | 6 ++---- 5 files changed, 65 insertions(+), 55 deletions(-) diff --git a/ansi/baseelement.go b/ansi/baseelement.go index d4b6a4f3..1f33491a 100644 --- a/ansi/baseelement.go +++ b/ansi/baseelement.go @@ -38,7 +38,6 @@ func renderText(w io.Writer, p termenv.Profile, rules StylePrimitive, s string) } out := termenv.String(s) - if rules.Upper != nil && *rules.Upper { out = termenv.String(strings.ToUpper(s)) } @@ -79,35 +78,58 @@ func renderText(w io.Writer, p termenv.Profile, rules StylePrimitive, s string) _, _ = io.WriteString(w, out.String()) } +func (e *BaseElement) StyleOverrideRender(w io.Writer, ctx RenderContext, style StylePrimitive) error { + bs := ctx.blockStack + st1 := cascadeStyles(bs.Current().Style, StyleBlock{ + StylePrimitive: style, + }) + st2 := cascadeStyles( + StyleBlock{ + StylePrimitive: bs.With(e.Style), + }, + StyleBlock{ + StylePrimitive: style, + }, + ) + + return e.doRender(w, ctx.options.ColorProfile, st1, st2) +} + func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack + st1 := bs.Current().Style + st2 := StyleBlock{ + StylePrimitive: bs.With(e.Style), + } + return e.doRender(w, ctx.options.ColorProfile, st1, st2) +} - renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Prefix) +func (e *BaseElement) doRender(w io.Writer, p termenv.Profile, st1, st2 StyleBlock) error { + renderText(w, p, st1.StylePrimitive, e.Prefix) defer func() { - renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, e.Suffix) + renderText(w, p, st1.StylePrimitive, e.Suffix) }() - rules := bs.With(e.Style) // render unstyled prefix/suffix - renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) + renderText(w, p, st1.StylePrimitive, st2.BlockPrefix) defer func() { - renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockSuffix) + renderText(w, p, st1.StylePrimitive, st2.BlockSuffix) }() // render styled prefix/suffix - renderText(w, ctx.options.ColorProfile, rules, rules.Prefix) + renderText(w, p, st2.StylePrimitive, st2.Prefix) defer func() { - renderText(w, ctx.options.ColorProfile, rules, rules.Suffix) + renderText(w, p, st2.StylePrimitive, st2.Suffix) }() s := e.Token - if len(rules.Format) > 0 { + if len(st2.Format) > 0 { var err error - s, err = formatToken(rules.Format, s) + s, err = formatToken(st2.Format, s) if err != nil { return err } } - renderText(w, ctx.options.ColorProfile, rules, s) + renderText(w, p, st2.StylePrimitive, s) return nil } diff --git a/ansi/elements.go b/ansi/elements.go index 30c7ba4c..2b2a296e 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -17,6 +17,10 @@ type ElementRenderer interface { Render(w io.Writer, ctx RenderContext) error } +type StyleOverriderElementRenderer interface { + StyleOverrideRender(w io.Writer, ctx RenderContext, style StylePrimitive) error +} + // ElementFinisher is called when leaving a markdown node. type ElementFinisher interface { Finish(w io.Writer, ctx RenderContext) error @@ -416,21 +420,3 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { return Element{} } } - -func textFromChildren(node ast.Node, source []byte) string { - var s string - for c := node.FirstChild(); c != nil; c = c.NextSibling() { - if c.Kind() == ast.KindText { - cn := c.(*ast.Text) - s += string(cn.Segment.Value(source)) - - if cn.HardLineBreak() || (cn.SoftLineBreak()) { - s += "\n" - } - } else { - s += string(c.Text(source)) - } - } - - return s -} diff --git a/ansi/emphasis.go b/ansi/emphasis.go index 35f1f1d9..c06168ed 100644 --- a/ansi/emphasis.go +++ b/ansi/emphasis.go @@ -1,8 +1,6 @@ package ansi import ( - "bytes" - "html" "io" ) @@ -18,18 +16,17 @@ func (e *EmphasisElement) Render(w io.Writer, ctx RenderContext) error { style = ctx.options.Styles.Strong } - var b bytes.Buffer for _, child := range e.Children { - if err := child.Render(&b, ctx); err != nil { - return err + if r, ok := child.(StyleOverriderElementRenderer); ok { + if err := r.StyleOverrideRender(w, ctx, style); err != nil { + return err + } + } else { + if err := child.Render(w, ctx); err != nil { + return err + } } } - el := Element{ - Renderer: &BaseElement{ - Token: html.UnescapeString(b.String()), - Style: style, - }, - } - return el.Renderer.Render(w, ctx) + return nil } diff --git a/ansi/link.go b/ansi/link.go index 537b6b91..054612f3 100644 --- a/ansi/link.go +++ b/ansi/link.go @@ -15,16 +15,23 @@ type LinkElement struct { func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error { for _, child := range e.Children { - var b bytes.Buffer - if err := child.Render(&b, ctx); err != nil { - return err - } - el := &BaseElement{ - Token: b.String(), - Style: ctx.options.Styles.LinkText, - } - if err := el.Render(w, ctx); err != nil { - return err + if r, ok := child.(StyleOverriderElementRenderer); ok { + st := ctx.options.Styles.LinkText + if err := r.StyleOverrideRender(w, ctx, st); err != nil { + return err + } + } else { + var b bytes.Buffer + if err := child.Render(&b, ctx); err != nil { + return err + } + el := &BaseElement{ + Token: b.String(), + Style: ctx.options.Styles.LinkText, + } + if err := el.Render(w, ctx); err != nil { + return err + } } } diff --git a/ansi/table.go b/ansi/table.go index f6f549da..cd5ed70a 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -18,12 +18,10 @@ type TableElement struct { } // A TableRowElement is used to render a single row in a table. -type TableRowElement struct { -} +type TableRowElement struct{} // A TableHeadElement is used to render a table's head element. -type TableHeadElement struct { -} +type TableHeadElement struct{} // A TableCellElement is used to render a single cell in a row. type TableCellElement struct { From 72a11e8572c4ec66f8c7c7f58dc9f8ec10f024a3 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 10:49:42 -0300 Subject: [PATCH 03/36] wip --- ansi/elements.go | 52 +++++++++++++++++++----------------------------- ansi/emphasis.go | 12 +++++++++++ ansi/style.go | 26 +++++++++++++++--------- 3 files changed, 49 insertions(+), 41 deletions(-) diff --git a/ansi/elements.go b/ansi/elements.go index 2b2a296e..269821f9 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -181,12 +181,10 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { case ast.KindEmphasis: n := node.(*ast.Emphasis) var children []ElementRenderer - if n.HasChildren() { - nn := n.FirstChild() - for nn != nil { - children = append(children, tr.NewElement(nn, source).Renderer) - nn = nn.NextSibling() - } + nn := n.FirstChild() + for nn != nil { + children = append(children, tr.NewElement(nn, source).Renderer) + nn = nn.NextSibling() } return Element{ Renderer: &EmphasisElement{ @@ -220,12 +218,10 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { case ast.KindLink: n := node.(*ast.Link) var children []ElementRenderer - if n.HasChildren() { - nn := n.FirstChild() - for nn != nil { - children = append(children, tr.NewElement(nn, source).Renderer) - nn = nn.NextSibling() - } + nn := n.FirstChild() + for nn != nil { + children = append(children, tr.NewElement(nn, source).Renderer) + nn = nn.NextSibling() } return Element{ Renderer: &LinkElement{ @@ -242,12 +238,10 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { } var children []ElementRenderer - if n.HasChildren() { - nn := n.FirstChild() - for nn != nil { - children = append(children, tr.NewElement(nn, source).Renderer) - nn = nn.NextSibling() - } + nn := n.FirstChild() + for nn != nil { + children = append(children, tr.NewElement(nn, source).Renderer) + nn = nn.NextSibling() } return Element{ @@ -324,23 +318,17 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { } case astext.KindTableCell: - s := "" - n := node.FirstChild() - for n != nil { - switch t := n.(type) { - case *ast.AutoLink: - s += string(t.Label(source)) - default: - s += string(n.Text(source)) - } - - n = n.NextSibling() + n := node.(*astext.TableCell) + var children []ElementRenderer + nn := n.FirstChild() + for nn != nil { + children = append(children, tr.NewElement(nn, source).Renderer) + nn = nn.NextSibling() } - return Element{ Renderer: &TableCellElement{ - Text: s, - Head: node.Parent().Kind() == astext.KindTableHeader, + Children: children, + Head: node.Parent().Kind() == astext.KindTableHeader, }, } diff --git a/ansi/emphasis.go b/ansi/emphasis.go index c06168ed..c47cb72d 100644 --- a/ansi/emphasis.go +++ b/ansi/emphasis.go @@ -16,6 +16,18 @@ func (e *EmphasisElement) Render(w io.Writer, ctx RenderContext) error { style = ctx.options.Styles.Strong } + return e.doRender(w, ctx, style) +} + +func (e *EmphasisElement) StyleOverrideRender(w io.Writer, ctx RenderContext, style StylePrimitive) error { + base := ctx.options.Styles.Emph + if e.Level > 1 { + base = ctx.options.Styles.Strong + } + return e.doRender(w, ctx, cascadeStylePrimitive(base, style, false)) +} + +func (e *EmphasisElement) doRender(w io.Writer, ctx RenderContext, style StylePrimitive) error { for _, child := range e.Children { if r, ok := child.(StyleOverriderElementRenderer); ok { if err := r.StyleOverrideRender(w, ctx, style); err != nil { diff --git a/ansi/style.go b/ansi/style.go index fd521471..aea30e18 100644 --- a/ansi/style.go +++ b/ansi/style.go @@ -147,7 +147,7 @@ func cascadeStyles(s ...StyleBlock) StyleBlock { return r } -func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock { +func cascadeStylePrimitive(parent, child StylePrimitive, toBlock bool) StylePrimitive { s := child s.Color = parent.Color @@ -166,8 +166,6 @@ func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock s.Blink = parent.Blink if toBlock { - s.Indent = parent.Indent - s.Margin = parent.Margin s.BlockPrefix = parent.BlockPrefix s.BlockSuffix = parent.BlockSuffix s.Prefix = parent.Prefix @@ -180,12 +178,6 @@ func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock if child.BackgroundColor != nil { s.BackgroundColor = child.BackgroundColor } - if child.Indent != nil { - s.Indent = child.Indent - } - if child.Margin != nil { - s.Margin = child.Margin - } if child.Underline != nil { s.Underline = child.Underline } @@ -240,3 +232,19 @@ func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock return s } + +func cascadeStyle(parent StyleBlock, child StyleBlock, toBlock bool) StyleBlock { + s := child + s.StylePrimitive = cascadeStylePrimitive(parent.StylePrimitive, child.StylePrimitive, toBlock) + + if toBlock { + s.Indent = parent.Indent + s.Margin = parent.Margin + } + + if child.Indent != nil { + s.Indent = child.Indent + } + + return s +} From 8cb80e7056ec54f505b3f98786f77982e3cd3a6b Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 10:52:48 -0300 Subject: [PATCH 04/36] wip --- ansi/baseelement.go | 35 ++++++++++++----------------------- ansi/emphasis.go | 2 +- ansi/style.go | 9 ++++++++- 3 files changed, 21 insertions(+), 25 deletions(-) diff --git a/ansi/baseelement.go b/ansi/baseelement.go index 1f33491a..5ccc7103 100644 --- a/ansi/baseelement.go +++ b/ansi/baseelement.go @@ -80,46 +80,35 @@ func renderText(w io.Writer, p termenv.Profile, rules StylePrimitive, s string) func (e *BaseElement) StyleOverrideRender(w io.Writer, ctx RenderContext, style StylePrimitive) error { bs := ctx.blockStack - st1 := cascadeStyles(bs.Current().Style, StyleBlock{ - StylePrimitive: style, - }) - st2 := cascadeStyles( - StyleBlock{ - StylePrimitive: bs.With(e.Style), - }, - StyleBlock{ - StylePrimitive: style, - }, - ) + st1 := cascadeStylePrimitives(bs.Current().Style.StylePrimitive, style) + st2 := cascadeStylePrimitives(bs.With(e.Style), style) return e.doRender(w, ctx.options.ColorProfile, st1, st2) } func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack - st1 := bs.Current().Style - st2 := StyleBlock{ - StylePrimitive: bs.With(e.Style), - } + st1 := bs.Current().Style.StylePrimitive + st2 := bs.With(e.Style) return e.doRender(w, ctx.options.ColorProfile, st1, st2) } -func (e *BaseElement) doRender(w io.Writer, p termenv.Profile, st1, st2 StyleBlock) error { - renderText(w, p, st1.StylePrimitive, e.Prefix) +func (e *BaseElement) doRender(w io.Writer, p termenv.Profile, st1, st2 StylePrimitive) error { + renderText(w, p, st1, e.Prefix) defer func() { - renderText(w, p, st1.StylePrimitive, e.Suffix) + renderText(w, p, st1, e.Suffix) }() // render unstyled prefix/suffix - renderText(w, p, st1.StylePrimitive, st2.BlockPrefix) + renderText(w, p, st1, st2.BlockPrefix) defer func() { - renderText(w, p, st1.StylePrimitive, st2.BlockSuffix) + renderText(w, p, st1, st2.BlockSuffix) }() // render styled prefix/suffix - renderText(w, p, st2.StylePrimitive, st2.Prefix) + renderText(w, p, st2, st2.Prefix) defer func() { - renderText(w, p, st2.StylePrimitive, st2.Suffix) + renderText(w, p, st2, st2.Suffix) }() s := e.Token @@ -130,6 +119,6 @@ func (e *BaseElement) doRender(w io.Writer, p termenv.Profile, st1, st2 StyleBlo return err } } - renderText(w, p, st2.StylePrimitive, s) + renderText(w, p, st2, s) return nil } diff --git a/ansi/emphasis.go b/ansi/emphasis.go index c47cb72d..5e210840 100644 --- a/ansi/emphasis.go +++ b/ansi/emphasis.go @@ -24,7 +24,7 @@ func (e *EmphasisElement) StyleOverrideRender(w io.Writer, ctx RenderContext, st if e.Level > 1 { base = ctx.options.Styles.Strong } - return e.doRender(w, ctx, cascadeStylePrimitive(base, style, false)) + return e.doRender(w, ctx, cascadeStylePrimitives(base, style)) } func (e *EmphasisElement) doRender(w io.Writer, ctx RenderContext, style StylePrimitive) error { diff --git a/ansi/style.go b/ansi/style.go index aea30e18..a660a455 100644 --- a/ansi/style.go +++ b/ansi/style.go @@ -140,13 +140,20 @@ type StyleConfig struct { func cascadeStyles(s ...StyleBlock) StyleBlock { var r StyleBlock - for _, v := range s { r = cascadeStyle(r, v, true) } return r } +func cascadeStylePrimitives(s ...StylePrimitive) StylePrimitive { + var r StylePrimitive + for _, v := range s { + r = cascadeStylePrimitive(r, v, true) + } + return r +} + func cascadeStylePrimitive(parent, child StylePrimitive, toBlock bool) StylePrimitive { s := child From 23f13a356560231f8fb2079d083d86691bce985a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 11:00:55 -0300 Subject: [PATCH 05/36] fix: autolink closes #290 --- ansi/elements.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/ansi/elements.go b/ansi/elements.go index 269821f9..8a250d5a 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -233,9 +233,6 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { case ast.KindAutoLink: n := node.(*ast.AutoLink) u := string(n.URL(source)) - if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(u), "mailto:") { - u = "mailto:" + u - } var children []ElementRenderer nn := n.FirstChild() @@ -244,6 +241,16 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { nn = nn.NextSibling() } + if len(children) == 0 { + children = append(children, &BaseElement{ + Token: u, + }) + } + + if n.AutoLinkType == ast.AutoLinkEmail && !strings.HasPrefix(strings.ToLower(u), "mailto:") { + u = "mailto:" + u + } + return Element{ Renderer: &LinkElement{ Children: children, From fb8db4afe621771b6c57ec6053c85cab2297d4a7 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 11:28:30 -0300 Subject: [PATCH 06/36] fix: escape characters closes #106 closes #274 closes #311 --- ansi/baseelement.go | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ansi/baseelement.go b/ansi/baseelement.go index 5ccc7103..6bd29455 100644 --- a/ansi/baseelement.go +++ b/ansi/baseelement.go @@ -119,6 +119,28 @@ func (e *BaseElement) doRender(w io.Writer, p termenv.Profile, st1, st2 StylePri return err } } - renderText(w, p, st2, s) + renderText(w, p, st2, escapeReplacer.Replace(s)) return nil } + +// https://www.markdownguide.org/basic-syntax/#characters-you-can-escape +var escapeReplacer = strings.NewReplacer( + "\\\\", "\\", + "\\`", "`", + "\\*", "*", + "\\_", "_", + "\\{", "{", + "\\}", "}", + "\\[", "[", + "\\]", "]", + "\\<", "<", + "\\>", ">", + "\\(", "(", + "\\)", ")", + "\\#", "#", + "\\+", "+", + "\\-", "-", + "\\.", ".", + "\\!", "!", + "\\|", "|", +) From 7b94cd15355c399af7cd12fb4075692eb009c981 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 11:32:46 -0300 Subject: [PATCH 07/36] fix: table --- ansi/table.go | 17 +++++++++++++---- go.mod | 3 ++- go.sum | 6 ++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ansi/table.go b/ansi/table.go index cd5ed70a..80e9a87d 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -1,8 +1,10 @@ package ansi import ( + "bytes" "io" + "github.com/charmbracelet/x/ansi" "github.com/muesli/reflow/indent" "github.com/olekukonko/tablewriter" astext "github.com/yuin/goldmark/extension/ast" @@ -25,8 +27,8 @@ type TableHeadElement struct{} // A TableCellElement is used to render a single cell in a row. type TableCellElement struct { - Text string - Head bool + Children []ElementRenderer + Head bool } func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { @@ -113,10 +115,17 @@ func (e *TableHeadElement) Finish(w io.Writer, ctx RenderContext) error { } func (e *TableCellElement) Render(w io.Writer, ctx RenderContext) error { + var b bytes.Buffer + for _, child := range e.Children { + if err := child.Render(&b, ctx); err != nil { + return err + } + } + if e.Head { - ctx.table.header = append(ctx.table.header, e.Text) + ctx.table.header = append(ctx.table.header, ansi.Strip(b.String())) } else { - ctx.table.cell = append(ctx.table.cell, e.Text) + ctx.table.cell = append(ctx.table.cell, ansi.Strip(b.String())) } return nil diff --git a/go.mod b/go.mod index 1674306e..9258514d 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.21 require ( github.com/alecthomas/chroma/v2 v2.14.0 + github.com/charmbracelet/x/ansi v0.1.4 github.com/microcosm-cc/bluemonday v1.0.27 github.com/muesli/reflow v0.3.0 github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a @@ -20,7 +21,7 @@ require ( github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect - github.com/rivo/uniseg v0.4.4 // indirect + github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.22.0 // indirect ) diff --git a/go.sum b/go.sum index 9ff84c44..a82de744 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= +github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM= +github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8= @@ -32,8 +34,8 @@ github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= -github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg= github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= From 50a0c1ac314155e292f218b0ef2ad625071f5cc4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 11:32:52 -0300 Subject: [PATCH 08/36] ci: golangci lint update --- .golangci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.golangci.yml b/.golangci.yml index 36f9966b..1d7f0d46 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -14,7 +14,7 @@ linters: - godot - godox - goimports - - gomnd + - mnd - goprintffuncname - gosec - misspell From e20c9e663976bd086dcb9e222a0b116ca8fde4ff Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 11:52:26 -0300 Subject: [PATCH 09/36] feat: use x/golden --- ansi/renderer.go | 2 +- ansi/renderer_test.go | 132 ++++++------------ .../testdata/TestRenderer/block_quote.golden | 0 .../testdata/TestRenderer/code.golden | 0 .../testdata/TestRenderer/code_block.golden | 0 .../testdata/TestRenderer/emoji.golden | 0 .../testdata/TestRenderer/emph.golden | 0 .../testdata/TestRenderer/enumeration.golden | 0 ansi/testdata/TestRenderer/heading.golden | 3 + .../testdata/TestRenderer/hr.golden | 0 .../testdata/TestRenderer/image.golden | 0 .../testdata/TestRenderer/link.golden | 0 .../testdata/TestRenderer/list.golden | 0 .../testdata/TestRenderer/ordered_list.golden | 0 .../TestRenderer/strikethrough.golden | 0 .../testdata/TestRenderer/strong.golden | 0 ansi/testdata/TestRenderer/table.golden | 7 + ansi/testdata/TestRenderer/table_align.golden | 6 + .../testdata/TestRenderer/task.golden | 0 .../testdata/TestRendererIssues/312.golden | 2 +- ansi/testdata/TestRendererIssues/42.golden | 6 + ansi/testdata/TestRendererIssues/43.golden | 10 ++ ansi/testdata/TestRendererIssues/44.golden | 3 + .../testdata/TestRendererIssues/46_1.golden | 0 ansi/testdata/TestRendererIssues/46_2.golden | 3 + .../testdata/TestRendererIssues/47.golden | 0 .../testdata/TestRendererIssues/48.golden | 0 examples/helloworld/main.go | 49 ++++++- glamour_test.go | 71 ++-------- go.mod | 2 + go.sum | 4 + ...ization.test => TestCapitalization.golden} | 0 .../{readme.test => TestRenderHelpers.golden} | 0 testdata/TestTermRenderer.golden | 40 ++++++ testdata/TestTermRendererWriter.golden | 40 ++++++ ....test => TestWithPreservedNewLines.golden} | 0 testdata/heading.test | 3 - testdata/issues/42.test | 6 - testdata/issues/43.test | 10 -- testdata/issues/44.test | 13 -- testdata/issues/46_2.test | 21 --- testdata/table.test | 5 - testdata/table_align.test | 5 - 43 files changed, 222 insertions(+), 221 deletions(-) rename testdata/block_quote.test => ansi/testdata/TestRenderer/block_quote.golden (100%) rename testdata/code.test => ansi/testdata/TestRenderer/code.golden (100%) rename testdata/code_block.test => ansi/testdata/TestRenderer/code_block.golden (100%) rename testdata/emoji.test => ansi/testdata/TestRenderer/emoji.golden (100%) rename testdata/emph.test => ansi/testdata/TestRenderer/emph.golden (100%) rename testdata/enumeration.test => ansi/testdata/TestRenderer/enumeration.golden (100%) create mode 100644 ansi/testdata/TestRenderer/heading.golden rename testdata/hr.test => ansi/testdata/TestRenderer/hr.golden (100%) rename testdata/image.test => ansi/testdata/TestRenderer/image.golden (100%) rename testdata/link.test => ansi/testdata/TestRenderer/link.golden (100%) rename testdata/list.test => ansi/testdata/TestRenderer/list.golden (100%) rename testdata/ordered_list.test => ansi/testdata/TestRenderer/ordered_list.golden (100%) rename testdata/strikethrough.test => ansi/testdata/TestRenderer/strikethrough.golden (100%) rename testdata/strong.test => ansi/testdata/TestRenderer/strong.golden (100%) create mode 100644 ansi/testdata/TestRenderer/table.golden create mode 100644 ansi/testdata/TestRenderer/table_align.golden rename testdata/task.test => ansi/testdata/TestRenderer/task.golden (100%) rename testdata/issues/312.test => ansi/testdata/TestRendererIssues/312.golden (92%) create mode 100644 ansi/testdata/TestRendererIssues/42.golden create mode 100644 ansi/testdata/TestRendererIssues/43.golden create mode 100644 ansi/testdata/TestRendererIssues/44.golden rename testdata/issues/46_1.test => ansi/testdata/TestRendererIssues/46_1.golden (100%) create mode 100644 ansi/testdata/TestRendererIssues/46_2.golden rename testdata/issues/47.test => ansi/testdata/TestRendererIssues/47.golden (100%) rename testdata/issues/48.test => ansi/testdata/TestRendererIssues/48.golden (100%) rename testdata/{capitalization.test => TestCapitalization.golden} (100%) rename testdata/{readme.test => TestRenderHelpers.golden} (100%) create mode 100644 testdata/TestTermRenderer.golden create mode 100644 testdata/TestTermRendererWriter.golden rename testdata/{preserved_newline.test => TestWithPreservedNewLines.golden} (100%) delete mode 100644 testdata/heading.test delete mode 100644 testdata/issues/42.test delete mode 100644 testdata/issues/43.test delete mode 100644 testdata/issues/44.test delete mode 100644 testdata/issues/46_2.test delete mode 100644 testdata/table.test delete mode 100644 testdata/table_align.test diff --git a/ansi/renderer.go b/ansi/renderer.go index e312550b..5ba422e6 100644 --- a/ansi/renderer.go +++ b/ansi/renderer.go @@ -143,7 +143,7 @@ func isChild(node ast.Node) bool { for n := node.Parent(); n != nil; n = n.Parent() { // These types are already rendered by their parent switch n.Kind() { - case ast.KindLink, ast.KindImage, ast.KindEmphasis, astext.KindStrikethrough, astext.KindTableCell: + case ast.KindAutoLink, ast.KindLink, ast.KindImage, ast.KindEmphasis, astext.KindStrikethrough, astext.KindTableCell: return true } } diff --git a/ansi/renderer_test.go b/ansi/renderer_test.go index 1ff146f2..056e4b32 100644 --- a/ansi/renderer_test.go +++ b/ansi/renderer_test.go @@ -8,6 +8,7 @@ import ( "strings" "testing" + "github.com/charmbracelet/x/exp/golden" "github.com/muesli/termenv" "github.com/yuin/goldmark" emoji "github.com/yuin/goldmark-emoji" @@ -18,10 +19,8 @@ import ( ) const ( - generateExamples = true - generateIssues = true - examplesDir = "../styles/examples/" - issuesDir = "../testdata/issues/" + examplesDir = "../styles/examples/" + issuesDir = "../testdata/issues/" ) func TestRenderer(t *testing.T) { @@ -32,68 +31,50 @@ func TestRenderer(t *testing.T) { for _, f := range files { bn := strings.TrimSuffix(filepath.Base(f), ".md") - sn := filepath.Join(examplesDir, bn+".style") - tn := filepath.Join("../testdata", bn+".test") - - in, err := os.ReadFile(f) - if err != nil { - t.Fatal(err) - } - b, err := os.ReadFile(sn) - if err != nil { - t.Fatal(err) - } - - options := Options{ - WordWrap: 80, - ColorProfile: termenv.TrueColor, - } - err = json.Unmarshal(b, &options.Styles) - if err != nil { - t.Fatal(err) - } - - md := goldmark.New( - goldmark.WithExtensions( - extension.GFM, - extension.DefinitionList, - emoji.Emoji, - ), - goldmark.WithParserOptions( - parser.WithAutoHeadingID(), - ), - ) - - ar := NewRenderer(options) - md.SetRenderer( - renderer.NewRenderer( - renderer.WithNodeRenderers(util.Prioritized(ar, 1000)))) - - var buf bytes.Buffer - err = md.Convert(in, &buf) - if err != nil { - t.Error(err) - } - - // generate - if generateExamples { - err = os.WriteFile(tn, buf.Bytes(), 0o644) + t.Run(bn, func(t *testing.T) { + sn := filepath.Join(examplesDir, bn+".style") + + in, err := os.ReadFile(f) + if err != nil { + t.Fatal(err) + } + b, err := os.ReadFile(sn) if err != nil { t.Fatal(err) } - continue - } - - // verify - td, err := os.ReadFile(tn) - if err != nil { - t.Fatal(err) - } - - if !bytes.Equal(td, buf.Bytes()) { - t.Errorf("Rendered output for %s doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", - bn, string(td), buf.String()) - } + + options := Options{ + WordWrap: 80, + ColorProfile: termenv.TrueColor, + } + err = json.Unmarshal(b, &options.Styles) + if err != nil { + t.Fatal(err) + } + + md := goldmark.New( + goldmark.WithExtensions( + extension.GFM, + extension.DefinitionList, + emoji.Emoji, + ), + goldmark.WithParserOptions( + parser.WithAutoHeadingID(), + ), + ) + + ar := NewRenderer(options) + md.SetRenderer( + renderer.NewRenderer( + renderer.WithNodeRenderers(util.Prioritized(ar, 1000)))) + + var buf bytes.Buffer + if err := md.Convert(in, &buf); err != nil { + t.Error(err) + } + + golden.RequireEqual(t, buf.Bytes()) + }) } } @@ -106,8 +87,6 @@ func TestRendererIssues(t *testing.T) { for _, f := range files { bn := strings.TrimSuffix(filepath.Base(f), ".md") t.Run(bn, func(t *testing.T) { - tn := filepath.Join(issuesDir, bn+".test") - in, err := os.ReadFile(f) if err != nil { t.Fatal(err) @@ -143,30 +122,11 @@ func TestRendererIssues(t *testing.T) { renderer.WithNodeRenderers(util.Prioritized(ar, 1000)))) var buf bytes.Buffer - err = md.Convert(in, &buf) - if err != nil { + if err := md.Convert(in, &buf); err != nil { t.Error(err) } - // generate - if generateIssues { - err = os.WriteFile(tn, buf.Bytes(), 0o644) - if err != nil { - t.Fatal(err) - } - return - } - - // verify - td, err := os.ReadFile(tn) - if err != nil { - t.Fatal(err) - } - - if !bytes.Equal(td, buf.Bytes()) { - t.Errorf("Rendered output for %s doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", - bn, string(td), buf.String()) - } + golden.RequireEqual(t, buf.Bytes()) }) } } diff --git a/testdata/block_quote.test b/ansi/testdata/TestRenderer/block_quote.golden similarity index 100% rename from testdata/block_quote.test rename to ansi/testdata/TestRenderer/block_quote.golden diff --git a/testdata/code.test b/ansi/testdata/TestRenderer/code.golden similarity index 100% rename from testdata/code.test rename to ansi/testdata/TestRenderer/code.golden diff --git a/testdata/code_block.test b/ansi/testdata/TestRenderer/code_block.golden similarity index 100% rename from testdata/code_block.test rename to ansi/testdata/TestRenderer/code_block.golden diff --git a/testdata/emoji.test b/ansi/testdata/TestRenderer/emoji.golden similarity index 100% rename from testdata/emoji.test rename to ansi/testdata/TestRenderer/emoji.golden diff --git a/testdata/emph.test b/ansi/testdata/TestRenderer/emph.golden similarity index 100% rename from testdata/emph.test rename to ansi/testdata/TestRenderer/emph.golden diff --git a/testdata/enumeration.test b/ansi/testdata/TestRenderer/enumeration.golden similarity index 100% rename from testdata/enumeration.test rename to ansi/testdata/TestRenderer/enumeration.golden diff --git a/ansi/testdata/TestRenderer/heading.golden b/ansi/testdata/TestRenderer/heading.golden new file mode 100644 index 00000000..134dfc58 --- /dev/null +++ b/ansi/testdata/TestRenderer/heading.golden @@ -0,0 +1,3 @@ +=> h1 <= +## h2 +### h3 \ No newline at end of file diff --git a/testdata/hr.test b/ansi/testdata/TestRenderer/hr.golden similarity index 100% rename from testdata/hr.test rename to ansi/testdata/TestRenderer/hr.golden diff --git a/testdata/image.test b/ansi/testdata/TestRenderer/image.golden similarity index 100% rename from testdata/image.test rename to ansi/testdata/TestRenderer/image.golden diff --git a/testdata/link.test b/ansi/testdata/TestRenderer/link.golden similarity index 100% rename from testdata/link.test rename to ansi/testdata/TestRenderer/link.golden diff --git a/testdata/list.test b/ansi/testdata/TestRenderer/list.golden similarity index 100% rename from testdata/list.test rename to ansi/testdata/TestRenderer/list.golden diff --git a/testdata/ordered_list.test b/ansi/testdata/TestRenderer/ordered_list.golden similarity index 100% rename from testdata/ordered_list.test rename to ansi/testdata/TestRenderer/ordered_list.golden diff --git a/testdata/strikethrough.test b/ansi/testdata/TestRenderer/strikethrough.golden similarity index 100% rename from testdata/strikethrough.test rename to ansi/testdata/TestRenderer/strikethrough.golden diff --git a/testdata/strong.test b/ansi/testdata/TestRenderer/strong.golden similarity index 100% rename from testdata/strong.test rename to ansi/testdata/TestRenderer/strong.golden diff --git a/ansi/testdata/TestRenderer/table.golden b/ansi/testdata/TestRenderer/table.golden new file mode 100644 index 00000000..3e354d69 --- /dev/null +++ b/ansi/testdata/TestRenderer/table.golden @@ -0,0 +1,7 @@ + + LABEL | VALUE | URL + ---------+-------+--------------------------------- + First | foo | https://charm.sh + | | https://charm.sh + Second | bar | https://charm.sh + | | https://charm.sh diff --git a/ansi/testdata/TestRenderer/table_align.golden b/ansi/testdata/TestRenderer/table_align.golden new file mode 100644 index 00000000..0f19571f --- /dev/null +++ b/ansi/testdata/TestRenderer/table_align.golden @@ -0,0 +1,6 @@ + + LABEL | VALUE | URL + ---------+-------+--------------------------------- + First | foo | charm.sh + Second | bar | https://charm.sh + | | https://charm.sh diff --git a/testdata/task.test b/ansi/testdata/TestRenderer/task.golden similarity index 100% rename from testdata/task.test rename to ansi/testdata/TestRenderer/task.golden diff --git a/testdata/issues/312.test b/ansi/testdata/TestRendererIssues/312.golden similarity index 92% rename from testdata/issues/312.test rename to ansi/testdata/TestRendererIssues/312.golden index 78d446ab..ece50d64 100644 --- a/testdata/issues/312.test +++ b/ansi/testdata/TestRendererIssues/312.golden @@ -1,5 +1,5 @@   File                                                                                                                                                      - URL https://www.example.com                                                  + URL https://www.example.com                                                  diff --git a/ansi/testdata/TestRendererIssues/42.golden b/ansi/testdata/TestRendererIssues/42.golden new file mode 100644 index 00000000..63c12a20 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/42.golden @@ -0,0 +1,6 @@ + + If you want to make a more significant change, please first open an issue    + https://github.com/twpayne/chezmoi/issues/new to discuss the change that you + want to make. Dave Cheney gives a good rationale                             + https://dave.cheney.net/2019/02/18/talk-then-code as to why this is important. + diff --git a/ansi/testdata/TestRendererIssues/43.golden b/ansi/testdata/TestRendererIssues/43.golden new file mode 100644 index 00000000..f88488f2 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/43.golden @@ -0,0 +1,10 @@ + +                                                                              + β€’ Getting started                                                            + β€’ Developing locally                                                         + β€’ Documentation changes                                                      + β€’ Contributing changes                                                       + β€’ Managing releases                                                          + β€’ Packaging                                                                  + β€’ Updating the website                                                       + diff --git a/ansi/testdata/TestRendererIssues/44.golden b/ansi/testdata/TestRendererIssues/44.golden new file mode 100644 index 00000000..58699f7b --- /dev/null +++ b/ansi/testdata/TestRendererIssues/44.golden @@ -0,0 +1,3 @@ + +  + \ No newline at end of file diff --git a/testdata/issues/46_1.test b/ansi/testdata/TestRendererIssues/46_1.golden similarity index 100% rename from testdata/issues/46_1.test rename to ansi/testdata/TestRendererIssues/46_1.golden diff --git a/ansi/testdata/TestRendererIssues/46_2.golden b/ansi/testdata/TestRendererIssues/46_2.golden new file mode 100644 index 00000000..58699f7b --- /dev/null +++ b/ansi/testdata/TestRendererIssues/46_2.golden @@ -0,0 +1,3 @@ + +  + \ No newline at end of file diff --git a/testdata/issues/47.test b/ansi/testdata/TestRendererIssues/47.golden similarity index 100% rename from testdata/issues/47.test rename to ansi/testdata/TestRendererIssues/47.golden diff --git a/testdata/issues/48.test b/ansi/testdata/TestRendererIssues/48.golden similarity index 100% rename from testdata/issues/48.test rename to ansi/testdata/TestRendererIssues/48.golden diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index cdfd7ea6..76e2f356 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -6,20 +6,22 @@ import ( "github.com/charmbracelet/glamour" ) -func main() { - in := `# Hello World +const in1 = `# Hello World This is a simple example of Markdown rendering with Glamour! Check out the [other examples](https://github.com/charmbracelet/glamour/tree/master/examples) too. +## links + **just bold text** _just italic text_ **[bold with url within](https://www.example.com)** -[normal](https://www.example.com) +_[italic with url within](https://www.example.com)_ +[normal](https://www.example.com) [url with **bold** within](https://www.example.com) @@ -27,13 +29,46 @@ _just italic text_ [**entire url text is bold**](https://www.example.com) -[_entire url text is italic_](https://www.example.com) +[aaa _entire url_ aaaa _text is italic_](https://www.example.com) + +**test@example.com** + +https://google.com -_URL_ +_https://google.com_ -Bye! +This is a [link](https://charm.sh). + +## tables + +| h1 | h2 | +|---|---| +| a | b | + +### table with markup and escapes inside + +| a | b | c | +|---|---|---| +|test1|test2|test3| +|pipe \| pipe | 2 | 3 **bold** | +` + "|test1|var a = 1|test3|\n\n## escapes\n" + + "- \\`hi\\`\n" + + `- \\hi +- \*hi +- \_hi +- \{hi\} +- \[hi\] +- \ +- \(hi\) +- \# hi +- \+ hi +- \- hi +- \. hi +- \! hi +- \| hi ` - out, _ := glamour.Render(in, "dark") +func main() { + out, _ := glamour.Render(in1, "dark") fmt.Print(out) } diff --git a/glamour_test.go b/glamour_test.go index 3a20b470..38979f65 100644 --- a/glamour_test.go +++ b/glamour_test.go @@ -7,14 +7,12 @@ import ( "os" "strings" "testing" -) -const ( - generate = false - markdown = "testdata/readme.markdown.in" - testFile = "testdata/readme.test" + "github.com/charmbracelet/x/exp/golden" ) +const markdown = "testdata/readme.markdown.in" + func TestTermRendererWriter(t *testing.T) { r, err := NewTermRenderer( WithStandardStyle(DarkStyle), @@ -42,25 +40,7 @@ func TestTermRendererWriter(t *testing.T) { t.Fatal(err) } - // generate - if generate { - err := os.WriteFile(testFile, b, 0o644) - if err != nil { - t.Fatal(err) - } - return - } - - // verify - td, err := os.ReadFile(testFile) - if err != nil { - t.Fatal(err) - } - - if !bytes.Equal(td, b) { - t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", - string(td), b) - } + golden.RequireEqual(t, b) } func TestTermRenderer(t *testing.T) { @@ -81,16 +61,7 @@ func TestTermRenderer(t *testing.T) { t.Fatal(err) } - // verify - td, err := os.ReadFile(testFile) - if err != nil { - t.Fatal(err) - } - - if !bytes.Equal(td, []byte(b)) { - t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", - string(td), b) - } + golden.RequireEqual(t, []byte(b)) } func TestWithEmoji(t *testing.T) { @@ -133,16 +104,7 @@ func TestWithPreservedNewLines(t *testing.T) { t.Fatal(err) } - // verify - td, err := os.ReadFile("testdata/preserved_newline.test") - if err != nil { - t.Fatal(err) - } - - if !bytes.Equal(td, []byte(b)) { - t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", - string(td), b) - } + golden.RequireEqual(t, []byte(b)) } func TestStyles(t *testing.T) { @@ -219,16 +181,7 @@ func TestRenderHelpers(t *testing.T) { t.Error(err) } - // verify - td, err := os.ReadFile(testFile) - if err != nil { - t.Fatal(err) - } - - if b != string(td) { - t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", - string(td), b) - } + golden.RequireEqual(t, []byte(b)) } func TestCapitalization(t *testing.T) { @@ -250,15 +203,7 @@ func TestCapitalization(t *testing.T) { t.Fatal(err) } - // expected outcome - td, err := os.ReadFile("testdata/capitalization.test") - if err != nil { - t.Fatal(err) - } - - if string(td) != b { - t.Errorf("Rendered output doesn't match!\nExpected: `\n%s`\nGot: `\n%s`\n", td, b) - } + golden.RequireEqual(t, []byte(b)) } func FuzzData(f *testing.F) { diff --git a/go.mod b/go.mod index 9258514d..2bc63ca9 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/alecthomas/chroma/v2 v2.14.0 github.com/charmbracelet/x/ansi v0.1.4 + github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 github.com/microcosm-cc/bluemonday v1.0.27 github.com/muesli/reflow v0.3.0 github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a @@ -15,6 +16,7 @@ require ( require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/aymanbagabas/go-udiff v0.2.0 // indirect github.com/aymerick/douceur v0.2.0 // indirect github.com/dlclark/regexp2 v1.11.0 // indirect github.com/gorilla/css v1.0.1 // indirect diff --git a/go.sum b/go.sum index a82de744..6e0e829a 100644 --- a/go.sum +++ b/go.sum @@ -6,10 +6,14 @@ github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8= +github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM= github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= +github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 h1:6KzMkQeAF56rggw2NZu1L+TH7j9+DM1/2Kmh7KUxg1I= +github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U= github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI= github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8= diff --git a/testdata/capitalization.test b/testdata/TestCapitalization.golden similarity index 100% rename from testdata/capitalization.test rename to testdata/TestCapitalization.golden diff --git a/testdata/readme.test b/testdata/TestRenderHelpers.golden similarity index 100% rename from testdata/readme.test rename to testdata/TestRenderHelpers.golden diff --git a/testdata/TestTermRenderer.golden b/testdata/TestTermRenderer.golden new file mode 100644 index 00000000..f9f36d27 --- /dev/null +++ b/testdata/TestTermRenderer.golden @@ -0,0 +1,40 @@ + +  Gold                                                                        +                                                                              + Render markdown on the CLI, with pizzazz!                                    +                                                                              + ## 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                                                                     +                                                                              + See cmd/gold /cmd/gold/.                                                     +                                                                              + ## Example Output                                                            +                                                                              + Image: Gold Dark Style β†’                                                     + https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png     +                                                                              + Check out the Gold Style Gallery                                             + https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!  +                                                                              + ## Colors                                                                    +                                                                              + Currently  gold  uses the Aurora ANSI colors                                 + https://godoc.org/github.com/logrusorgru/aurora#Index.                       +                                                                              + ## Development                                                               +                                                                              + Style definitions located in  styles/  can be embedded into the binary by    + running statik https://github.com/rakyll/statik:                             +                                                                              +   statik -f -src styles -include "*.json"                                    +                                                                              + You can re-generate screenshots of all available styles by running           + gallery.sh . This requires  termshot  and  pngcrush  installed on your       + system!                                                                      + diff --git a/testdata/TestTermRendererWriter.golden b/testdata/TestTermRendererWriter.golden new file mode 100644 index 00000000..f9f36d27 --- /dev/null +++ b/testdata/TestTermRendererWriter.golden @@ -0,0 +1,40 @@ + +  Gold                                                                        +                                                                              + Render markdown on the CLI, with pizzazz!                                    +                                                                              + ## 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                                                                     +                                                                              + See cmd/gold /cmd/gold/.                                                     +                                                                              + ## Example Output                                                            +                                                                              + Image: Gold Dark Style β†’                                                     + https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png     +                                                                              + Check out the Gold Style Gallery                                             + https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!  +                                                                              + ## Colors                                                                    +                                                                              + Currently  gold  uses the Aurora ANSI colors                                 + https://godoc.org/github.com/logrusorgru/aurora#Index.                       +                                                                              + ## Development                                                               +                                                                              + Style definitions located in  styles/  can be embedded into the binary by    + running statik https://github.com/rakyll/statik:                             +                                                                              +   statik -f -src styles -include "*.json"                                    +                                                                              + You can re-generate screenshots of all available styles by running           + gallery.sh . This requires  termshot  and  pngcrush  installed on your       + system!                                                                      + diff --git a/testdata/preserved_newline.test b/testdata/TestWithPreservedNewLines.golden similarity index 100% rename from testdata/preserved_newline.test rename to testdata/TestWithPreservedNewLines.golden diff --git a/testdata/heading.test b/testdata/heading.test deleted file mode 100644 index b29a7e2a..00000000 --- a/testdata/heading.test +++ /dev/null @@ -1,3 +0,0 @@ - => h1 <= - ## h2 - ### h3 \ No newline at end of file diff --git a/testdata/issues/42.test b/testdata/issues/42.test deleted file mode 100644 index 46560119..00000000 --- a/testdata/issues/42.test +++ /dev/null @@ -1,6 +0,0 @@ - - If you want to make a more significant change, please first open an issue    - https://github.com/twpayne/chezmoi/issues/new to discuss the change that you - want to make. Dave Cheney gives a good rationale                             - https://dave.cheney.net/2019/02/18/talk-then-code as to why this is important. - diff --git a/testdata/issues/43.test b/testdata/issues/43.test deleted file mode 100644 index 6a98a895..00000000 --- a/testdata/issues/43.test +++ /dev/null @@ -1,10 +0,0 @@ - -                                                                              - β€’ Getting started                                                            - β€’ Developing locally                                                         - β€’ Documentation changes                                                      - β€’ Contributing changes                                                       - β€’ Managing releases                                                          - β€’ Packaging                                                                  - β€’ Updating the website                                                       - diff --git a/testdata/issues/44.test b/testdata/issues/44.test deleted file mode 100644 index 63da718e..00000000 --- a/testdata/issues/44.test +++ /dev/null @@ -1,13 +0,0 @@ - -                                                                              -  DISTRIBUTION β”‚ ARCHITECTURES β”‚ PACKAGE                   - ───────────────┼────────────────────────────────┼──────────                  -  Debian β”‚ amd64, arm64, armel, i386, β”‚ deb                   -  β”‚ ppc64, ppc64le β”‚                   -  RedHat β”‚ aarch64, armhfp, i686, ppc64, β”‚ rpm                   -  β”‚ ppc64le, x86_64 β”‚                   -  OpenSUSE β”‚ aarch64, armhfp, i686, ppc64, β”‚ rpm                   -  β”‚ ppc64le, x86_64 β”‚                   -  Ubuntu β”‚ amd64, arm64, armel, i386, β”‚ deb                   -  β”‚ ppc64, ppc64le β”‚                   - diff --git a/testdata/issues/46_2.test b/testdata/issues/46_2.test deleted file mode 100644 index 8bb49776..00000000 --- a/testdata/issues/46_2.test +++ /dev/null @@ -1,21 +0,0 @@ - -                                                                              -  DEPENDENCY β”‚ INSTALLATION β”‚ OPERATION        - ─────────────────────────────────┼──────────────┼───────────────────────────────── -  xdg-open (Linux), open(1) β”‚ base β”‚ desktop opener             -  (macOS), cygstart (Cygwin) β”‚ β”‚                            -  file, coreutils (cp, mv, rm), β”‚ base β”‚ file type, copy, move and  -  xargs β”‚ β”‚ remove                     -  tar, (un)zip [atool/bsdtar for β”‚ base β”‚ create, list, extract tar, -  more formats] β”‚ β”‚ gzip, bzip2, zip           -  archivemount, fusermount(3) β”‚ optional β”‚ mount, unmount archives    -  sshfs, rclone, fusermount(3) β”‚ optional β”‚ mount, unmount remotes     -  trash-cli β”‚ optional β”‚ trash files (default       - action:                                                                   -  β”‚ β”‚ rm)                        -  vlock (Linux), bashlock β”‚ optional β”‚ terminal locker (fallback: -  (macOS), lock(1) (BSD) β”‚ β”‚ cmatrix)                   -  advcpmv (Linux) (integration) β”‚ optional β”‚ copy, move progress        -  $VISUAL (else $EDITOR), β”‚ optional β”‚ fallback vi, less, sh      -  $PAGER, $SHELL β”‚ β”‚                            - diff --git a/testdata/table.test b/testdata/table.test deleted file mode 100644 index 0da2654e..00000000 --- a/testdata/table.test +++ /dev/null @@ -1,5 +0,0 @@ - - LABEL | VALUE | URL - ---------+-------+------------------- - First | foo | https://charm.sh - Second | bar | https://charm.sh diff --git a/testdata/table_align.test b/testdata/table_align.test deleted file mode 100644 index 984330f6..00000000 --- a/testdata/table_align.test +++ /dev/null @@ -1,5 +0,0 @@ - - LABEL | VALUE | URL - ---------+-------+------------------- - First | foo | charm.sh - Second | bar | https://charm.sh From 36654490f9a39f294af957cc1cbfd09a7141ed82 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 13:49:53 -0300 Subject: [PATCH 10/36] test: #106 Signed-off-by: Carlos Alexandro Becker --- ansi/table.go | 1 + ansi/testdata/TestRendererIssues/106.golden | 17 +++++++++++++++++ testdata/issues/106.md | 14 ++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/106.golden create mode 100644 testdata/issues/106.md diff --git a/ansi/table.go b/ansi/table.go index 80e9a87d..3d43475f 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -122,6 +122,7 @@ func (e *TableCellElement) Render(w io.Writer, ctx RenderContext) error { } } + // TODO: figure this out if e.Head { ctx.table.header = append(ctx.table.header, ansi.Strip(b.String())) } else { diff --git a/ansi/testdata/TestRendererIssues/106.golden b/ansi/testdata/TestRendererIssues/106.golden new file mode 100644 index 00000000..6a0e4633 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/106.golden @@ -0,0 +1,17 @@ + +                                                                              + β€’ `hi`                                                                       + β€’ \hi                                                                        + β€’ *hi                                                                        + β€’ _hi                                                                        + β€’ {hi}                                                                       + β€’ [hi]                                                                       + β€’                                                                        + β€’ (hi)                                                                       + β€’ # hi                                                                       + β€’ + hi                                                                       + β€’ - hi                                                                       + β€’ . hi                                                                       + β€’ ! hi                                                                       + β€’ | hi                                                                       + diff --git a/testdata/issues/106.md b/testdata/issues/106.md new file mode 100644 index 00000000..c37d79b0 --- /dev/null +++ b/testdata/issues/106.md @@ -0,0 +1,14 @@ +- \`hi\` +- \\hi +- \*hi +- \_hi +- \{hi\} +- \[hi\] +- \ +- \(hi\) +- \# hi +- \+ hi +- \- hi +- \. hi +- \! hi +- \| hi From 51a0a4620ad4ea5e7160b4282de105435781c095 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 13:51:12 -0300 Subject: [PATCH 11/36] test: #290 Signed-off-by: Carlos Alexandro Becker --- ansi/testdata/TestRendererIssues/290.golden | 6 ++++++ testdata/issues/290.md | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/290.golden create mode 100644 testdata/issues/290.md diff --git a/ansi/testdata/TestRendererIssues/290.golden b/ansi/testdata/TestRendererIssues/290.golden new file mode 100644 index 00000000..1cc53087 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/290.golden @@ -0,0 +1,6 @@ + +                                                                              + β€’ test@example.com mailto:test@example.com                                   + β€’ https://google.com https://google.com                                      + β€’ https://google.com https://google.com                                      + diff --git a/testdata/issues/290.md b/testdata/issues/290.md new file mode 100644 index 00000000..1e928721 --- /dev/null +++ b/testdata/issues/290.md @@ -0,0 +1,3 @@ +- **test@example.com** +- https://google.com +- _https://google.com_ From 8dee63859101414578d13544247cae3aad9dda91 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 13:52:59 -0300 Subject: [PATCH 12/36] test: #312 Signed-off-by: Carlos Alexandro Becker --- ansi/testdata/TestRendererIssues/312.golden | 12 ++++++++++++ testdata/issues/312.md | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/ansi/testdata/TestRendererIssues/312.golden b/ansi/testdata/TestRendererIssues/312.golden index ece50d64..48c9c0ba 100644 --- a/ansi/testdata/TestRendererIssues/312.golden +++ b/ansi/testdata/TestRendererIssues/312.golden @@ -1,5 +1,17 @@   File                                                                                                                                                      + bold text with URL https://www.example.com                                   +                                                                              + italic text with URL https://www.example.com                                 +                                                                               URL https://www.example.com                                                  +                                                                              + url with bold within https://www.example.com                                 +                                                                              + url with italic within https://www.example.com                               +                                                                              + entire url text is bold https://www.example.com                              +                                                                              + entire url text is italic https://www.example.com                            diff --git a/testdata/issues/312.md b/testdata/issues/312.md index b5f90372..53aae826 100644 --- a/testdata/issues/312.md +++ b/testdata/issues/312.md @@ -1,3 +1,15 @@ # File -**[URL](https://www.example.com)** +**bold text with [URL](https://www.example.com)** + +_italic text with [URL](https://www.example.com)_ + +[URL](https://www.example.com) + +[url with **bold** within](https://www.example.com) + +[url with _italic_ within](https://www.example.com) + +[**entire url text is bold**](https://www.example.com) + +[_entire url text is italic_](https://www.example.com) From 3fc00026ff67f0483c6bbc82a8b00ec7bee1c4e4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 13:55:38 -0300 Subject: [PATCH 13/36] test: #257 Signed-off-by: Carlos Alexandro Becker --- ansi/testdata/TestRendererIssues/257.golden | 4 ++++ testdata/issues/257.md | 3 +++ 2 files changed, 7 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/257.golden create mode 100644 testdata/issues/257.md diff --git a/ansi/testdata/TestRendererIssues/257.golden b/ansi/testdata/TestRendererIssues/257.golden new file mode 100644 index 00000000..eeffc6db --- /dev/null +++ b/ansi/testdata/TestRendererIssues/257.golden @@ -0,0 +1,4 @@ + +                                                                              +   set runtimepath^=$XDG_CONFIG_HOME/vim                                      + diff --git a/testdata/issues/257.md b/testdata/issues/257.md new file mode 100644 index 00000000..c648ac7c --- /dev/null +++ b/testdata/issues/257.md @@ -0,0 +1,3 @@ +```vim +set runtimepath^=$XDG_CONFIG_HOME/vim +``` From 5b1bc2a4dde5b8789431accfe54fae19179f6ae9 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 14:54:18 -0300 Subject: [PATCH 14/36] test: #149 --- ansi/testdata/TestRendererIssues/149.golden | 4 ++++ testdata/issues/149.md | 3 +++ 2 files changed, 7 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/149.golden create mode 100644 testdata/issues/149.md diff --git a/ansi/testdata/TestRendererIssues/149.golden b/ansi/testdata/TestRendererIssues/149.golden new file mode 100644 index 00000000..00f65eb4 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/149.golden @@ -0,0 +1,4 @@ + + a http://example.com/with-a-big-path/likely-to-use-more-than-one-line?why-not=after-all- + why-not-use-queryparams-too&abc=123                                          + diff --git a/testdata/issues/149.md b/testdata/issues/149.md new file mode 100644 index 00000000..d6fec510 --- /dev/null +++ b/testdata/issues/149.md @@ -0,0 +1,3 @@ +[a](http://example.com/with-a-big-path/likely-to-use-more-than-one-line?why-not=after-all-why-not-use-queryparams-too&abc=123) + + From b6567c688bac2bde18eb1ec80a01cc9f55abac5f Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 16 Jul 2024 15:13:33 -0300 Subject: [PATCH 15/36] fix: #239 --- ansi/elements.go | 3 ++- ansi/testdata/TestRendererIssues/239.golden | 9 +++++++++ testdata/issues/239.md | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 ansi/testdata/TestRendererIssues/239.golden create mode 100644 testdata/issues/239.md diff --git a/ansi/elements.go b/ansi/elements.go index 8a250d5a..83d2a812 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -375,13 +375,13 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { Newline: true, } return Element{ - Entering: "\n", Renderer: e, Finisher: e, } case astext.KindDefinitionTerm: return Element{ + Entering: "\n", Renderer: &BaseElement{ Style: ctx.options.Styles.DefinitionTerm, }, @@ -389,6 +389,7 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { case astext.KindDefinitionDescription: return Element{ + Exiting: "\n", Renderer: &BaseElement{ Style: ctx.options.Styles.DefinitionDescription, }, diff --git a/ansi/testdata/TestRendererIssues/239.golden b/ansi/testdata/TestRendererIssues/239.golden new file mode 100644 index 00000000..0d88ee10 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/239.golden @@ -0,0 +1,9 @@ + +                                                                              + First term                                                                   + 🠢 Definition one of first term.                                              +                                                                              + Second term                                                                  + 🠢 Definition one of second term.                                             +                                                                              + diff --git a/testdata/issues/239.md b/testdata/issues/239.md new file mode 100644 index 00000000..7b37c37e --- /dev/null +++ b/testdata/issues/239.md @@ -0,0 +1,5 @@ +First term +: Definition one of first term. + +Second term +: Definition one of second term. From 82d883e69845fa113d6bfc779a17b4782025811a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 13:18:34 -0300 Subject: [PATCH 16/36] feat: use lipgloss table closes #262 Co-authored-by: bashbunni --- ansi/elements.go | 4 +- ansi/table.go | 137 +++++++++++++++++++++++++++++------------------ go.mod | 8 +-- go.sum | 13 +++-- 4 files changed, 99 insertions(+), 63 deletions(-) diff --git a/ansi/elements.go b/ansi/elements.go index 83d2a812..5d81fe79 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -317,7 +317,9 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { // Tables case astext.KindTable: table := node.(*astext.Table) - te := &TableElement{table: table} + te := &TableElement{ + table: table, + } return Element{ Entering: "\n", Renderer: te, diff --git a/ansi/table.go b/ansi/table.go index 3d43475f..c16f9ef5 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -4,19 +4,18 @@ import ( "bytes" "io" - "github.com/charmbracelet/x/ansi" + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/lipgloss/table" "github.com/muesli/reflow/indent" - "github.com/olekukonko/tablewriter" astext "github.com/yuin/goldmark/extension/ast" ) // A TableElement is used to render tables. type TableElement struct { - writer *tablewriter.Table - styleWriter *StyleWriter - header []string - cell []string - table *astext.Table + lipgloss *table.Table + table *astext.Table + header []string + row []string } // A TableRowElement is used to render a single row in a table. @@ -31,6 +30,12 @@ type TableCellElement struct { Head bool } +// TODO: move this to the styles thingy? +var ( + cellStyle = lipgloss.NewStyle().Padding(0, 1) + headerStyle = lipgloss.NewStyle().Padding(0, 1).Bold(true) +) + func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack @@ -49,84 +54,114 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { }) style := bs.With(rules.StylePrimitive) - ctx.table.styleWriter = NewStyleWriter(ctx, iw, style) - - renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) - renderText(ctx.table.styleWriter, ctx.options.ColorProfile, style, rules.Prefix) - table := tablewriter.NewWriter(ctx.table.styleWriter) - - alignments := make([]int, len(e.table.Alignments)) - for i, a := range e.table.Alignments { - switch a { - case astext.AlignLeft: - alignments[i] = tablewriter.ALIGN_LEFT - case astext.AlignCenter: - alignments[i] = tablewriter.ALIGN_CENTER - case astext.AlignRight: - alignments[i] = tablewriter.ALIGN_RIGHT - } - } - table.SetColumnAlignment(alignments) - ctx.table.writer = table + renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) + renderText(iw, ctx.options.ColorProfile, style, rules.Prefix) + ctx.table.lipgloss = table.New(). + StyleFunc(func(row, col int) lipgloss.Style { + st := cellStyle + if row == 0 { + st = headerStyle + } + + switch e.table.Alignments[col] { + case astext.AlignLeft: + st = st.Align(lipgloss.Left) + case astext.AlignCenter: + st = st.Align(lipgloss.Center) + case astext.AlignRight: + st = st.Align(lipgloss.Right) + } + + return st + }). + Width(int(ctx.blockStack.Width(ctx))) + return nil } +func (e *TableElement) setBorders(ctx RenderContext) { + rules := ctx.options.Styles.Table + border := lipgloss.NormalBorder() + + if rules.RowSeparator != nil && rules.ColumnSeparator != nil { + border = lipgloss.Border{ + Top: *rules.RowSeparator, + Bottom: *rules.RowSeparator, + Left: *rules.ColumnSeparator, + Right: *rules.ColumnSeparator, + Middle: *rules.CenterSeparator, + } + } + ctx.table.lipgloss.Border(border) + ctx.table.lipgloss.BorderTop(false) + ctx.table.lipgloss.BorderLeft(false) + ctx.table.lipgloss.BorderRight(false) + ctx.table.lipgloss.BorderBottom(false) +} + func (e *TableElement) Finish(w io.Writer, ctx RenderContext) error { rules := ctx.options.Styles.Table - ctx.table.writer.SetBorders(tablewriter.Border{Left: false, Top: false, Right: false, Bottom: false}) - if rules.CenterSeparator != nil { - ctx.table.writer.SetCenterSeparator(*rules.CenterSeparator) - } - if rules.ColumnSeparator != nil { - ctx.table.writer.SetColumnSeparator(*rules.ColumnSeparator) - } - if rules.RowSeparator != nil { - ctx.table.writer.SetRowSeparator(*rules.RowSeparator) - } + e.setBorders(ctx) - ctx.table.writer.Render() - ctx.table.writer = nil + ow := ctx.blockStack.Current().Block + if _, err := ow.WriteString(ctx.table.lipgloss.String()); err != nil { + return err + } - renderText(ctx.table.styleWriter, ctx.options.ColorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix) - renderText(ctx.table.styleWriter, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix) - return ctx.table.styleWriter.Close() + renderText(ow, ctx.options.ColorProfile, ctx.blockStack.With(rules.StylePrimitive), rules.Suffix) + renderText(ow, ctx.options.ColorProfile, ctx.blockStack.Current().Style.StylePrimitive, rules.BlockSuffix) + ctx.table.lipgloss = nil + return nil } func (e *TableRowElement) Finish(w io.Writer, ctx RenderContext) error { - if ctx.table.writer == nil { + if ctx.table.lipgloss == nil { return nil } - ctx.table.writer.Append(ctx.table.cell) - ctx.table.cell = []string{} + ctx.table.lipgloss.Row(ctx.table.row...) + ctx.table.row = []string{} return nil } func (e *TableHeadElement) Finish(w io.Writer, ctx RenderContext) error { - if ctx.table.writer == nil { + if ctx.table.lipgloss == nil { return nil } - ctx.table.writer.SetHeader(ctx.table.header) + ctx.table.lipgloss.Headers(ctx.table.header...) ctx.table.header = []string{} return nil } func (e *TableCellElement) Render(w io.Writer, ctx RenderContext) error { var b bytes.Buffer + style := ctx.options.Styles.Table.StylePrimitive for _, child := range e.Children { - if err := child.Render(&b, ctx); err != nil { - return err + if r, ok := child.(StyleOverriderElementRenderer); ok { + if err := r.StyleOverrideRender(&b, ctx, style); err != nil { + return err + } + } else { + if err := child.Render(&b, ctx); err != nil { + return err + } + el := &BaseElement{ + Token: b.String(), + Style: style, + } + if err := el.Render(w, ctx); err != nil { + return err + } } } - // TODO: figure this out if e.Head { - ctx.table.header = append(ctx.table.header, ansi.Strip(b.String())) + ctx.table.header = append(ctx.table.header, b.String()) } else { - ctx.table.cell = append(ctx.table.cell, ansi.Strip(b.String())) + ctx.table.row = append(ctx.table.row, b.String()) } return nil diff --git a/go.mod b/go.mod index 2bc63ca9..054b8f1f 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,11 @@ go 1.21 require ( github.com/alecthomas/chroma/v2 v2.14.0 - github.com/charmbracelet/x/ansi v0.1.4 + github.com/charmbracelet/lipgloss v0.12.1 github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 github.com/microcosm-cc/bluemonday v1.0.27 github.com/muesli/reflow v0.3.0 github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a - github.com/olekukonko/tablewriter v0.0.5 github.com/yuin/goldmark v1.7.4 github.com/yuin/goldmark-emoji v1.0.3 ) @@ -18,11 +17,12 @@ require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/aymanbagabas/go-udiff v0.2.0 // indirect github.com/aymerick/douceur v0.2.0 // indirect + github.com/charmbracelet/x/ansi v0.1.4 // indirect github.com/dlclark/regexp2 v1.11.0 // indirect github.com/gorilla/css v1.0.1 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect - github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect github.com/rivo/uniseg v0.4.7 // indirect golang.org/x/net v0.27.0 // indirect golang.org/x/sys v0.22.0 // indirect diff --git a/go.sum b/go.sum index 6e0e829a..3a815755 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= +github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs= +github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8= github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM= github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 h1:6KzMkQeAF56rggw2NZu1L+TH7j9+DM1/2Kmh7KUxg1I= @@ -22,20 +24,17 @@ github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUq github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= -github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= -github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= -github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk= github.com/microcosm-cc/bluemonday v1.0.27/go.mod h1:jFi9vgW+H7c3V0lb6nR74Ib/DIB5OBs92Dimizgw2cA= github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a h1:2MaM6YC3mGu54x+RKAA6JiFFHlHDY1UbkxqppT7wYOg= github.com/muesli/termenv v0.15.3-0.20240618155329-98d742f6907a/go.mod h1:hxSnBBYLK21Vtq/PHd0S2FYCxBXzBua8ov5s1RobyRQ= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= From 791a8134f16dd894fc81ec9ce10ccd73ef35734f Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 13:19:27 -0300 Subject: [PATCH 17/36] fix: codespan is not a block --- ansi/elements.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ansi/elements.go b/ansi/elements.go index 5d81fe79..d13f2533 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -304,14 +304,12 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { } case ast.KindCodeSpan: - // n := node.(*ast.CodeSpan) - e := &BlockElement{ - Block: &bytes.Buffer{}, - Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.Code, false), + e := &BaseElement{ + Token: string(node.Text(source)), + Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.Code, false).StylePrimitive, } return Element{ Renderer: e, - Finisher: e, } // Tables From 041e267e676faaadb9740e339954618472706033 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 13:21:58 -0300 Subject: [PATCH 18/36] test: #315 --- ansi/testdata/TestRendererIssues/315.golden | 5 +++++ testdata/issues/315.md | 3 +++ 2 files changed, 8 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/315.golden create mode 100644 testdata/issues/315.md diff --git a/ansi/testdata/TestRendererIssues/315.golden b/ansi/testdata/TestRendererIssues/315.golden new file mode 100644 index 00000000..f92b525d --- /dev/null +++ b/ansi/testdata/TestRendererIssues/315.golden @@ -0,0 +1,5 @@ + +                                                                              + Expression β”‚ Value β”‚ Type + ───────────────────────────────────────────┼────────────────┼─────────────── +  (1 >= 26) || (12 >= 6) {.java} β”‚ s β”‚ a diff --git a/testdata/issues/315.md b/testdata/issues/315.md new file mode 100644 index 00000000..0c066127 --- /dev/null +++ b/testdata/issues/315.md @@ -0,0 +1,3 @@ +| Expression | Value | Type | +| --------------------------------- | ----- | ---- | +| `(1 >= 26) \|\| (12 >= 6)`{.java} | s | a | From 404fd7e83ecce4b48e54185e2162e43c37354934 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 13:27:25 -0300 Subject: [PATCH 19/36] test: #316 --- ansi/testdata/TestRendererIssues/316.golden | 6 ++++++ testdata/issues/316.md | 4 ++++ 2 files changed, 10 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/316.golden create mode 100644 testdata/issues/316.md diff --git a/ansi/testdata/TestRendererIssues/316.golden b/ansi/testdata/TestRendererIssues/316.golden new file mode 100644 index 00000000..72b58ba6 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/316.golden @@ -0,0 +1,6 @@ + +                                                                              + A β”‚ B + ────────────────────────────────────────────────────────┼─────────────────── + Here https://example.com β”‚ hello + https://autolink.com https://autolink.com β”‚ world diff --git a/testdata/issues/316.md b/testdata/issues/316.md new file mode 100644 index 00000000..2a81a0bd --- /dev/null +++ b/testdata/issues/316.md @@ -0,0 +1,4 @@ +| A | B | +| --------------------------: | ----- | +| [Here](https://example.com) | hello | +| https://autolink.com | world | From 31232f1a139db10b331d0d0042fcdf08029dd7f8 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 13:27:50 -0300 Subject: [PATCH 20/36] fix: #316 --- ansi/table.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/ansi/table.go b/ansi/table.go index c16f9ef5..eb17bce8 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -145,14 +145,15 @@ func (e *TableCellElement) Render(w io.Writer, ctx RenderContext) error { return err } } else { - if err := child.Render(&b, ctx); err != nil { + var bb bytes.Buffer + if err := child.Render(&bb, ctx); err != nil { return err } el := &BaseElement{ - Token: b.String(), + Token: bb.String(), Style: style, } - if err := el.Render(w, ctx); err != nil { + if err := el.Render(&b, ctx); err != nil { return err } } From 4a5ae520757f00dd4ac1c53ac8e9e77c64e316e4 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 13:29:06 -0300 Subject: [PATCH 21/36] test: table --- ansi/testdata/TestRenderer/table.golden | 10 ++++------ ansi/testdata/TestRenderer/table_align.golden | 9 ++++----- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/ansi/testdata/TestRenderer/table.golden b/ansi/testdata/TestRenderer/table.golden index 3e354d69..9314a16b 100644 --- a/ansi/testdata/TestRenderer/table.golden +++ b/ansi/testdata/TestRenderer/table.golden @@ -1,7 +1,5 @@ - LABEL | VALUE | URL - ---------+-------+--------------------------------- - First | foo | https://charm.sh - | | https://charm.sh - Second | bar | https://charm.sh - | | https://charm.sh + Label β”‚ Value β”‚ URL +──────────────────┼────────────────┼──────────────────────────────────────────── + First β”‚ foo β”‚ https://charm.sh https://charm.sh + Second β”‚ bar β”‚ https://charm.sh https://charm.sh \ No newline at end of file diff --git a/ansi/testdata/TestRenderer/table_align.golden b/ansi/testdata/TestRenderer/table_align.golden index 0f19571f..115ca160 100644 --- a/ansi/testdata/TestRenderer/table_align.golden +++ b/ansi/testdata/TestRenderer/table_align.golden @@ -1,6 +1,5 @@ - LABEL | VALUE | URL - ---------+-------+--------------------------------- - First | foo | charm.sh - Second | bar | https://charm.sh - | | https://charm.sh + Label β”‚ Value β”‚ URL +──────────────────┼────────────────┼──────────────────────────────────────────── + First β”‚ foo β”‚ charm.sh + Second β”‚ bar β”‚ https://charm.sh https://charm.sh \ No newline at end of file From a8b9af7e71dbe2d24d3e38c145a9f721e30a377a Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 16:38:57 -0300 Subject: [PATCH 22/36] fix: codespans, tables --- ansi/codespan.go | 14 ++++++ ansi/elements.go | 22 +++++---- ansi/renderer.go | 2 +- ansi/table.go | 51 ++++++++++++++------ ansi/testdata/TestRendererIssues/315.golden | 5 +- ansi/testdata/TestRendererIssues/316.golden | 7 +-- ansi/testdata/TestRendererIssues/44.golden | 10 +++- ansi/testdata/TestRendererIssues/46_2.golden | 15 +++++- ansi/testdata/TestRendererIssues/47.golden | 2 +- ansi/testdata/TestRendererIssues/48.golden | 2 +- go.mod | 2 +- go.sum | 4 +- styles.go | 8 +-- testdata/TestRenderHelpers.golden | 6 +-- testdata/TestTermRenderer.golden | 6 +-- testdata/TestTermRendererWriter.golden | 6 +-- 16 files changed, 112 insertions(+), 50 deletions(-) create mode 100644 ansi/codespan.go diff --git a/ansi/codespan.go b/ansi/codespan.go new file mode 100644 index 00000000..6551bf3f --- /dev/null +++ b/ansi/codespan.go @@ -0,0 +1,14 @@ +package ansi + +import "io" + +// A CodeSpanElement is used to render codespan. +type CodeSpanElement struct { + Text string + Style StylePrimitive +} + +func (e *CodeSpanElement) Render(w io.Writer, ctx RenderContext) error { + renderText(w, ctx.options.ColorProfile, e.Style, e.Style.Prefix+e.Text+e.Style.Suffix) + return nil +} diff --git a/ansi/elements.go b/ansi/elements.go index d13f2533..e2002e47 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -304,12 +304,13 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { } case ast.KindCodeSpan: - e := &BaseElement{ - Token: string(node.Text(source)), - Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.Code, false).StylePrimitive, - } + n := node.(*ast.CodeSpan) + s := string(n.Text(source)) return Element{ - Renderer: e, + Renderer: &CodeSpanElement{ + Text: html.UnescapeString(s), + Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.Code, false).StylePrimitive, + }, } // Tables @@ -320,6 +321,7 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { } return Element{ Entering: "\n", + Exiting: "\n", Renderer: te, Finisher: te, } @@ -332,11 +334,13 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { children = append(children, tr.NewElement(nn, source).Renderer) nn = nn.NextSibling() } + + r := &TableCellElement{ + Children: children, + Head: node.Parent().Kind() == astext.KindTableHeader, + } return Element{ - Renderer: &TableCellElement{ - Children: children, - Head: node.Parent().Kind() == astext.KindTableHeader, - }, + Renderer: r, } case astext.KindTableHeader: diff --git a/ansi/renderer.go b/ansi/renderer.go index 5ba422e6..69d284f1 100644 --- a/ansi/renderer.go +++ b/ansi/renderer.go @@ -143,7 +143,7 @@ func isChild(node ast.Node) bool { for n := node.Parent(); n != nil; n = n.Parent() { // These types are already rendered by their parent switch n.Kind() { - case ast.KindAutoLink, ast.KindLink, ast.KindImage, ast.KindEmphasis, astext.KindStrikethrough, astext.KindTableCell: + case ast.KindCodeSpan, ast.KindAutoLink, ast.KindLink, ast.KindImage, ast.KindEmphasis, astext.KindStrikethrough, astext.KindTableCell: return true } } diff --git a/ansi/table.go b/ansi/table.go index eb17bce8..381224e6 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -30,12 +30,6 @@ type TableCellElement struct { Head bool } -// TODO: move this to the styles thingy? -var ( - cellStyle = lipgloss.NewStyle().Padding(0, 1) - headerStyle = lipgloss.NewStyle().Padding(0, 1).Bold(true) -) - func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack @@ -57,11 +51,17 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) renderText(iw, ctx.options.ColorProfile, style, rules.Prefix) + width := int(ctx.blockStack.Width(ctx)) ctx.table.lipgloss = table.New(). + Width(width). StyleFunc(func(row, col int) lipgloss.Style { - st := cellStyle + st := lipgloss.NewStyle(). + MaxWidth(width) + if m := ctx.options.Styles.Table.Margin; m != nil { + st = st.Margin(0, int(*m)) + } if row == 0 { - st = headerStyle + st = st.Bold(true).Width(20) } switch e.table.Alignments[col] { @@ -74,8 +74,7 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { } return st - }). - Width(int(ctx.blockStack.Width(ctx))) + }) return nil } @@ -100,9 +99,33 @@ func (e *TableElement) setBorders(ctx RenderContext) { ctx.table.lipgloss.BorderBottom(false) } -func (e *TableElement) Finish(w io.Writer, ctx RenderContext) error { +func (e *TableElement) setStyles(ctx RenderContext) { + ctx.table.lipgloss.StyleFunc(func(row, col int) lipgloss.Style { + st := lipgloss.NewStyle() + if m := ctx.options.Styles.Table.Margin; m != nil { + st = st.Padding(0, int(*m)) + } + if row == 0 { + st = st.Bold(true) + } + + switch e.table.Alignments[col] { + case astext.AlignCenter: + st = st.Align(lipgloss.Center) + case astext.AlignRight: + st = st.Align(lipgloss.Right) + case astext.AlignLeft: + st = st.Align(lipgloss.Left) + } + + return st + }) +} + +func (e *TableElement) Finish(_ io.Writer, ctx RenderContext) error { rules := ctx.options.Styles.Table + e.setStyles(ctx) e.setBorders(ctx) ow := ctx.blockStack.Current().Block @@ -116,7 +139,7 @@ func (e *TableElement) Finish(w io.Writer, ctx RenderContext) error { return nil } -func (e *TableRowElement) Finish(w io.Writer, ctx RenderContext) error { +func (e *TableRowElement) Finish(_ io.Writer, ctx RenderContext) error { if ctx.table.lipgloss == nil { return nil } @@ -126,7 +149,7 @@ func (e *TableRowElement) Finish(w io.Writer, ctx RenderContext) error { return nil } -func (e *TableHeadElement) Finish(w io.Writer, ctx RenderContext) error { +func (e *TableHeadElement) Finish(_ io.Writer, ctx RenderContext) error { if ctx.table.lipgloss == nil { return nil } @@ -136,7 +159,7 @@ func (e *TableHeadElement) Finish(w io.Writer, ctx RenderContext) error { return nil } -func (e *TableCellElement) Render(w io.Writer, ctx RenderContext) error { +func (e *TableCellElement) Render(_ io.Writer, ctx RenderContext) error { var b bytes.Buffer style := ctx.options.Styles.Table.StylePrimitive for _, child := range e.Children { diff --git a/ansi/testdata/TestRendererIssues/315.golden b/ansi/testdata/TestRendererIssues/315.golden index f92b525d..4cbc3d8e 100644 --- a/ansi/testdata/TestRendererIssues/315.golden +++ b/ansi/testdata/TestRendererIssues/315.golden @@ -1,5 +1,6 @@                                                                              - Expression β”‚ Value β”‚ Type + Expression β”‚Value β”‚Type ───────────────────────────────────────────┼────────────────┼─────────────── -  (1 >= 26) || (12 >= 6) {.java} β”‚ s β”‚ a +  (1 >= 26) || (12 >= 6) {.java} β”‚s β”‚a + diff --git a/ansi/testdata/TestRendererIssues/316.golden b/ansi/testdata/TestRendererIssues/316.golden index 72b58ba6..4a7e2032 100644 --- a/ansi/testdata/TestRendererIssues/316.golden +++ b/ansi/testdata/TestRendererIssues/316.golden @@ -1,6 +1,7 @@                                                                              - A β”‚ B + Aβ”‚B ────────────────────────────────────────────────────────┼─────────────────── - Here https://example.com β”‚ hello - https://autolink.com https://autolink.com β”‚ world + Here https://example.comβ”‚hello + https://autolink.com https://autolink.comβ”‚world + diff --git a/ansi/testdata/TestRendererIssues/44.golden b/ansi/testdata/TestRendererIssues/44.golden index 58699f7b..60fb0347 100644 --- a/ansi/testdata/TestRendererIssues/44.golden +++ b/ansi/testdata/TestRendererIssues/44.golden @@ -1,3 +1,9 @@ -  - \ No newline at end of file +                                                                              + Distri…β”‚Architectures β”‚Package + ───────┼─────────────────────────────────┼────────────────────────────────── + Debian β”‚ amd64 ,  arm64 ,  armel ,  i386…β”‚ deb  https://github.com/twpayne/… + RedHat β”‚ aarch64 ,  armhfp ,  i686 ,  pp…β”‚ rpm  https://github.com/twpayne/… + OpenSU…β”‚ aarch64 ,  armhfp ,  i686 ,  pp…β”‚ rpm  https://github.com/twpayne/… + Ubuntu β”‚ amd64 ,  arm64 ,  armel ,  i386…β”‚ deb  https://github.com/twpayne/… + diff --git a/ansi/testdata/TestRendererIssues/46_2.golden b/ansi/testdata/TestRendererIssues/46_2.golden index 58699f7b..745200bc 100644 --- a/ansi/testdata/TestRendererIssues/46_2.golden +++ b/ansi/testdata/TestRendererIssues/46_2.golden @@ -1,3 +1,14 @@ -  - \ No newline at end of file +                                                                              + Dependency β”‚Installa…β”‚Operation + ─────────────────────────────────────────┼─────────┼──────────────────────── + xdg-open (Linux), open(1) (macOS), cygst…β”‚base β”‚desktop opener + file, coreutils (cp, mv, rm), xargs β”‚base β”‚file type, copy, move a… + tar, (un)zip [atool/bsdtar for more form…β”‚base β”‚create, list, extract t… + archivemount, fusermount(3) β”‚optional β”‚mount, unmount archives + sshfs, rclone https://rclone.org/, fuser…β”‚optional β”‚mount, unmount remotes + trash-cli β”‚optional β”‚trash files (default ac… + vlock (Linux), bashlock (macOS), lock(1)…β”‚optional β”‚terminal locker (fallba… + advcpmv (Linux) (integration https://git…β”‚optional β”‚copy, move progress +  $VISUAL  (else  $EDITOR ),  $PAGER ,  $…β”‚optional β”‚fallback vi, less, sh + diff --git a/ansi/testdata/TestRendererIssues/47.golden b/ansi/testdata/TestRendererIssues/47.golden index 581f1101..5422c7c3 100644 --- a/ansi/testdata/TestRendererIssues/47.golden +++ b/ansi/testdata/TestRendererIssues/47.golden @@ -1,3 +1,3 @@ - Example:                                                     + Example:                                                    diff --git a/ansi/testdata/TestRendererIssues/48.golden b/ansi/testdata/TestRendererIssues/48.golden index 4673d85a..a20338ce 100644 --- a/ansi/testdata/TestRendererIssues/48.golden +++ b/ansi/testdata/TestRendererIssues/48.golden @@ -13,5 +13,5 @@                                                                                no emoji in inline code                                                                                                                                   -  :octopus: :zap: :cat: = :heart:                                             +  :octopus: :zap: :cat: = :heart:                                             diff --git a/go.mod b/go.mod index 054b8f1f..1a48dcd0 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( github.com/alecthomas/chroma/v2 v2.14.0 - github.com/charmbracelet/lipgloss v0.12.1 + github.com/charmbracelet/lipgloss v0.12.2-0.20240712161825-87dd58def709 github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 github.com/microcosm-cc/bluemonday v1.0.27 github.com/muesli/reflow v0.3.0 diff --git a/go.sum b/go.sum index 3a815755..29fc2e11 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= -github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs= -github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8= +github.com/charmbracelet/lipgloss v0.12.2-0.20240712161825-87dd58def709 h1:xKyYI89iCZPBA4QOBN1WQ8Fqt+iH09K3Ywx0qOKVPUo= +github.com/charmbracelet/lipgloss v0.12.2-0.20240712161825-87dd58def709/go.mod h1:lVwxBOJ0KkZflL9y+DObgGY8V90IZUJ1e6jjZ+PBcnE= github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM= github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 h1:6KzMkQeAF56rggw2NZu1L+TH7j9+DM1/2Kmh7KUxg1I= diff --git a/styles.go b/styles.go index a8c16f32..840a4732 100644 --- a/styles.go +++ b/styles.go @@ -6,9 +6,11 @@ import ( "github.com/charmbracelet/glamour/ansi" ) -const defaultListIndent = 2 -const defaultListLevelIndent = 4 -const defaultMargin = 2 +const ( + defaultListIndent = 2 + defaultListLevelIndent = 4 + defaultMargin = 2 +) var ( // ASCIIStyleConfig uses only ASCII characters. diff --git a/testdata/TestRenderHelpers.golden b/testdata/TestRenderHelpers.golden index f9f36d27..e598ccaa 100644 --- a/testdata/TestRenderHelpers.golden +++ b/testdata/TestRenderHelpers.golden @@ -24,17 +24,17 @@                                                                                ## Colors                                                                                                                                                  - Currently  gold  uses the Aurora ANSI colors                                 + Currently  gold  uses the Aurora ANSI colors                                  https://godoc.org/github.com/logrusorgru/aurora#Index.                                                                                                      ## Development                                                                                                                                             - Style definitions located in  styles/  can be embedded into the binary by    + Style definitions located in  styles/  can be embedded into the binary by     running statik https://github.com/rakyll/statik:                                                                                                             statik -f -src styles -include "*.json"                                                                                                                   You can re-generate screenshots of all available styles by running           - gallery.sh . This requires  termshot  and  pngcrush  installed on your       + gallery.sh . This requires  termshot  and  pngcrush  installed on your        system!                                                                      diff --git a/testdata/TestTermRenderer.golden b/testdata/TestTermRenderer.golden index f9f36d27..e598ccaa 100644 --- a/testdata/TestTermRenderer.golden +++ b/testdata/TestTermRenderer.golden @@ -24,17 +24,17 @@                                                                                ## Colors                                                                                                                                                  - Currently  gold  uses the Aurora ANSI colors                                 + Currently  gold  uses the Aurora ANSI colors                                  https://godoc.org/github.com/logrusorgru/aurora#Index.                                                                                                      ## Development                                                                                                                                             - Style definitions located in  styles/  can be embedded into the binary by    + Style definitions located in  styles/  can be embedded into the binary by     running statik https://github.com/rakyll/statik:                                                                                                             statik -f -src styles -include "*.json"                                                                                                                   You can re-generate screenshots of all available styles by running           - gallery.sh . This requires  termshot  and  pngcrush  installed on your       + gallery.sh . This requires  termshot  and  pngcrush  installed on your        system!                                                                      diff --git a/testdata/TestTermRendererWriter.golden b/testdata/TestTermRendererWriter.golden index f9f36d27..e598ccaa 100644 --- a/testdata/TestTermRendererWriter.golden +++ b/testdata/TestTermRendererWriter.golden @@ -24,17 +24,17 @@                                                                                ## Colors                                                                                                                                                  - Currently  gold  uses the Aurora ANSI colors                                 + Currently  gold  uses the Aurora ANSI colors                                  https://godoc.org/github.com/logrusorgru/aurora#Index.                                                                                                      ## Development                                                                                                                                             - Style definitions located in  styles/  can be embedded into the binary by    + Style definitions located in  styles/  can be embedded into the binary by     running statik https://github.com/rakyll/statik:                                                                                                             statik -f -src styles -include "*.json"                                                                                                                   You can re-generate screenshots of all available styles by running           - gallery.sh . This requires  termshot  and  pngcrush  installed on your       + gallery.sh . This requires  termshot  and  pngcrush  installed on your        system!                                                                      From 54f5fac55d6d79cd6c0cf415678024d6e6813a52 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 16:41:30 -0300 Subject: [PATCH 23/36] test: table --- ansi/testdata/TestRenderer/table.golden | 6 +++--- ansi/testdata/TestRenderer/table_align.golden | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ansi/testdata/TestRenderer/table.golden b/ansi/testdata/TestRenderer/table.golden index 9314a16b..37e29920 100644 --- a/ansi/testdata/TestRenderer/table.golden +++ b/ansi/testdata/TestRenderer/table.golden @@ -1,5 +1,5 @@ - Label β”‚ Value β”‚ URL + Label β”‚ Value β”‚ URL ──────────────────┼────────────────┼──────────────────────────────────────────── - First β”‚ foo β”‚ https://charm.sh https://charm.sh - Second β”‚ bar β”‚ https://charm.sh https://charm.sh \ No newline at end of file + First β”‚ foo β”‚ https://charm.sh https://charm.sh + Second β”‚ bar β”‚ https://charm.sh https://charm.sh diff --git a/ansi/testdata/TestRenderer/table_align.golden b/ansi/testdata/TestRenderer/table_align.golden index 115ca160..430eead2 100644 --- a/ansi/testdata/TestRenderer/table_align.golden +++ b/ansi/testdata/TestRenderer/table_align.golden @@ -1,5 +1,5 @@ - Label β”‚ Value β”‚ URL + Label β”‚ Value β”‚ URL ──────────────────┼────────────────┼──────────────────────────────────────────── - First β”‚ foo β”‚ charm.sh - Second β”‚ bar β”‚ https://charm.sh https://charm.sh \ No newline at end of file + First β”‚ foo β”‚ charm.sh + Second β”‚ bar β”‚ https://charm.sh https://charm.sh From 4830eb21cd575b0baea21e6906cd7b9c5ac448a1 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 16:43:52 -0300 Subject: [PATCH 24/36] test: #117 --- ansi/testdata/TestRendererIssues/117.golden | 6 ++++++ testdata/issues/117.md | 3 +++ 2 files changed, 9 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/117.golden create mode 100644 testdata/issues/117.md diff --git a/ansi/testdata/TestRendererIssues/117.golden b/ansi/testdata/TestRendererIssues/117.golden new file mode 100644 index 00000000..6456bf56 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/117.golden @@ -0,0 +1,6 @@ + +                                                                              + cmd β”‚descr + ────────────────────────────────────┼─────────────────────────────────────── +  glow config  β”‚open glow config + diff --git a/testdata/issues/117.md b/testdata/issues/117.md new file mode 100644 index 00000000..8e00f2d4 --- /dev/null +++ b/testdata/issues/117.md @@ -0,0 +1,3 @@ +| cmd | descr | +| ------------- | ---------------- | +| `glow config` | open glow config | From f0e83554b2e0d6a966d6840cffcf4858b7479422 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Wed, 17 Jul 2024 16:48:04 -0300 Subject: [PATCH 25/36] test: #60 --- ansi/testdata/TestRendererIssues/60.golden | 28 ++++++++++++++++++++++ testdata/issues/60.md | 25 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 ansi/testdata/TestRendererIssues/60.golden create mode 100644 testdata/issues/60.md diff --git a/ansi/testdata/TestRendererIssues/60.golden b/ansi/testdata/TestRendererIssues/60.golden new file mode 100644 index 00000000..c9c8688a --- /dev/null +++ b/ansi/testdata/TestRendererIssues/60.golden @@ -0,0 +1,28 @@ + +                                                                              + Library β”‚Version + ─────────────────────────────────────────────────────────────┼────────────── + ESMF https://github.com/esmf-org/esmf β”‚v8.6.1 + FMS https://github.com/NOAA-GFDL/FMS/ β”‚2024.01.02 + netCDF https://github.com/Unidata/netcdf-c β”‚4.9.2 + netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚4.6.1 + netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚4.3.1 + HDF5 https://portal.hdfgroup.org/display/support β”‚1.10.11 + HDF4 https://portal.hdfgroup.org/display/support β”‚4.2.16-2 + GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚v1.16.0 + xgboost https://github.com/dmlc/xgboost β”‚v1.6.0 + libyaml https://github.com/yaml/libyaml.git β”‚0.2.5 + antlr2 https://www.antlr2.org/ β”‚2.7.7 + GSL https://www.gnu.org/software/gsl/ β”‚2.7 + jpeg http://www.ijg.org/ β”‚9e + zlib http://www.zlib.net/ β”‚1.3.1 + szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚2.1.1 + cURL https://curl.haxx.se/ β”‚8.8.0 + UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚2.2.28 + NCO http://nco.sourceforge.net/ β”‚5.2.6 + CDO https://code.mpimet.mpg.de/projects/cdo β”‚2.3.0 + nccmp https://gitlab.com/remikz/nccmp β”‚1.9.1.0 + HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚3.0 + HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚2.0 + SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚5.2.20 + diff --git a/testdata/issues/60.md b/testdata/issues/60.md new file mode 100644 index 00000000..8f673692 --- /dev/null +++ b/testdata/issues/60.md @@ -0,0 +1,25 @@ +| Library | Version | +| ----------------------------------------------------------- | ---------- | +| [ESMF](https://github.com/esmf-org/esmf) | v8.6.1 | +| [FMS](https://github.com/NOAA-GFDL/FMS/) | 2024.01.02 | +| [netCDF](https://github.com/Unidata/netcdf-c) | 4.9.2 | +| [netCDF Fortran](https://github.com/Unidata/netcdf-fortran) | 4.6.1 | +| [netCDF C++](https://github.com/Unidata/netcdf-cxx4) | 4.3.1 | +| [HDF5](https://portal.hdfgroup.org/display/support) | 1.10.11 | +| [HDF4](https://portal.hdfgroup.org/display/support) | 4.2.16-2 | +| [GFE](https://github.com/Goddard-Fortran-Ecosystem/GFE) | v1.16.0 | +| [xgboost](https://github.com/dmlc/xgboost) | v1.6.0 | +| [libyaml](https://github.com/yaml/libyaml.git) | 0.2.5 | +| [antlr2](https://www.antlr2.org/) | 2.7.7 | +| [GSL](https://www.gnu.org/software/gsl/) | 2.7 | +| [jpeg](http://www.ijg.org/) | 9e | +| [zlib](http://www.zlib.net/) | 1.3.1 | +| [szip](https://support.hdfgroup.org/doc_resource/SZIP/) | 2.1.1 | +| [cURL](https://curl.haxx.se/) | 8.8.0 | +| [UDUNITS2](https://github.com/GMAO-SI-Team/UDUNITS-2.git) | 2.2.28 | +| [NCO](http://nco.sourceforge.net/) | 5.2.6 | +| [CDO](https://code.mpimet.mpg.de/projects/cdo) | 2.3.0 | +| [nccmp](https://gitlab.com/remikz/nccmp) | 1.9.1.0 | +| [HDF-EOS2](https://wiki.earthdata.nasa.gov/display/DAS) | 3.0 | +| [HDF-EOS5](https://wiki.earthdata.nasa.gov/display/DAS) | 2.0 | +| [SDP Toolkit](https://wiki.earthdata.nasa.gov/display/DAS) | 5.2.20 | From bb802344b19e89c52f4a77025882d2aa9028940e Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 Jul 2024 11:39:09 -0300 Subject: [PATCH 26/36] fix: rm stylewriter --- ansi/stylewriter.go | 33 --------------------------------- 1 file changed, 33 deletions(-) delete mode 100644 ansi/stylewriter.go diff --git a/ansi/stylewriter.go b/ansi/stylewriter.go deleted file mode 100644 index 28c01e06..00000000 --- a/ansi/stylewriter.go +++ /dev/null @@ -1,33 +0,0 @@ -package ansi - -import ( - "bytes" - "io" -) - -// StyleWriter is a Writer that applies styling on whatever you write to it. -type StyleWriter struct { - ctx RenderContext - w io.Writer - buf bytes.Buffer - rules StylePrimitive -} - -// NewStyleWriter returns a new StyleWriter. -func NewStyleWriter(ctx RenderContext, w io.Writer, rules StylePrimitive) *StyleWriter { - return &StyleWriter{ - ctx: ctx, - w: w, - rules: rules, - } -} - -func (w *StyleWriter) Write(b []byte) (int, error) { - return w.buf.Write(b) -} - -// Close must be called when you're finished writing to a StyleWriter. -func (w *StyleWriter) Close() error { - renderText(w.w, w.ctx.options.ColorProfile, w.rules, w.buf.String()) - return nil -} From 49bac14e75ce7489094d90f7831bbaf94df9b0e1 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 Jul 2024 15:03:03 -0300 Subject: [PATCH 27/36] fix: #313 --- ansi/elements.go | 7 +++---- ansi/renderer.go | 4 ---- ansi/testdata/TestRendererIssues/313.golden | 8 ++++++++ testdata/issues/313.md | 5 +++++ 4 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 ansi/testdata/TestRendererIssues/313.golden create mode 100644 testdata/issues/313.md diff --git a/ansi/elements.go b/ansi/elements.go index e2002e47..2f6670ca 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -80,10 +80,9 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { // Blockquote case ast.KindBlockquote: e := &BlockElement{ - Block: &bytes.Buffer{}, - Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.BlockQuote, false), - Margin: true, - Newline: true, + Block: &bytes.Buffer{}, + Style: cascadeStyle(ctx.blockStack.Current().Style, ctx.options.Styles.BlockQuote, false), + Margin: true, } return Element{ Entering: "\n", diff --git a/ansi/renderer.go b/ansi/renderer.go index 69d284f1..160bf489 100644 --- a/ansi/renderer.go +++ b/ansi/renderer.go @@ -136,10 +136,6 @@ func (r *ANSIRenderer) renderNode(w util.BufWriter, source []byte, node ast.Node } func isChild(node ast.Node) bool { - if node.Parent() != nil && node.Parent().Kind() == ast.KindBlockquote { - // skip paragraph within blockquote to avoid reflowing text - return true - } for n := node.Parent(); n != nil; n = n.Parent() { // These types are already rendered by their parent switch n.Kind() { diff --git a/ansi/testdata/TestRendererIssues/313.golden b/ansi/testdata/TestRendererIssues/313.golden new file mode 100644 index 00000000..16cb7e99 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/313.golden @@ -0,0 +1,8 @@ + +                                                                              + β”‚ This is a block quote                                                       + β”‚                                                                             + β”‚ β”‚ This is the nested quote                                                   + β”‚                                                                             + β”‚ This is part of the outer block quote.                                      + diff --git a/testdata/issues/313.md b/testdata/issues/313.md new file mode 100644 index 00000000..4aab7d5f --- /dev/null +++ b/testdata/issues/313.md @@ -0,0 +1,5 @@ +> This is a block quote +> +> > This is the nested quote +> +> This is part of the outer block quote. From 63dca24e238fa65ac9fb881df5ee6c958e4f5112 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 Jul 2024 15:25:49 -0300 Subject: [PATCH 28/36] fix: margin --- ansi/blockstack.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ansi/blockstack.go b/ansi/blockstack.go index 5440dcbc..0af22a8f 100644 --- a/ansi/blockstack.go +++ b/ansi/blockstack.go @@ -59,10 +59,10 @@ func (s BlockStack) Margin() uint { // Width returns the available rendering width. func (s BlockStack) Width(ctx RenderContext) uint { - if s.Indent()+s.Margin()*2 > uint(ctx.options.WordWrap) { + if s.Indent()*s.Margin() > uint(ctx.options.WordWrap) { return 0 } - return uint(ctx.options.WordWrap) - s.Indent() - s.Margin()*2 + return uint(ctx.options.WordWrap) - s.Indent()*s.Margin() } // Parent returns the current BlockElement's parent. From a4e220d5290b5bd1935479064c5599f6a38cb9e8 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 Jul 2024 16:28:52 -0300 Subject: [PATCH 29/36] fix: blocks and word wrap --- ansi/blockelement.go | 12 ++- ansi/codeblock.go | 1 + ansi/testdata/TestRenderer/block_quote.golden | 3 +- ansi/testdata/TestRenderer/list.golden | 2 +- ansi/testdata/TestRendererIssues/106.golden | 30 ++++---- ansi/testdata/TestRendererIssues/107.golden | 7 ++ ansi/testdata/TestRendererIssues/117.golden | 8 +- ansi/testdata/TestRendererIssues/149.golden | 5 +- ansi/testdata/TestRendererIssues/172.golden | 6 ++ ansi/testdata/TestRendererIssues/237.golden | 9 +++ ansi/testdata/TestRendererIssues/239.golden | 14 ++-- ansi/testdata/TestRendererIssues/257.golden | 4 +- ansi/testdata/TestRendererIssues/290.golden | 8 +- ansi/testdata/TestRendererIssues/312.golden | 30 ++++---- ansi/testdata/TestRendererIssues/313.golden | 12 +-- ansi/testdata/TestRendererIssues/315.golden | 8 +- ansi/testdata/TestRendererIssues/316.golden | 10 +-- ansi/testdata/TestRendererIssues/42.golden | 8 +- ansi/testdata/TestRendererIssues/43.golden | 16 ++-- ansi/testdata/TestRendererIssues/44.golden | 14 ++-- ansi/testdata/TestRendererIssues/46_1.golden | 8 +- ansi/testdata/TestRendererIssues/46_2.golden | 24 +++--- ansi/testdata/TestRendererIssues/47.golden | 2 +- ansi/testdata/TestRendererIssues/48.golden | 30 ++++---- ansi/testdata/TestRendererIssues/60.golden | 52 ++++++------- ansi/testdata/TestRendererIssues/79.golden | 9 +++ examples/helloworld/main.go | 67 ++--------------- testdata/TestCapitalization.golden | 10 +-- testdata/TestRenderHelpers.golden | 75 +++++++++---------- testdata/TestTermRenderer.golden | 75 +++++++++---------- testdata/TestTermRendererWriter.golden | 75 +++++++++---------- testdata/issues/107.md | 4 + testdata/issues/172.md | 3 + testdata/issues/237.md | 3 + testdata/issues/79.md | 9 +++ 35 files changed, 324 insertions(+), 329 deletions(-) create mode 100644 ansi/testdata/TestRendererIssues/107.golden create mode 100644 ansi/testdata/TestRendererIssues/172.golden create mode 100644 ansi/testdata/TestRendererIssues/237.golden create mode 100644 ansi/testdata/TestRendererIssues/79.golden create mode 100644 testdata/issues/107.md create mode 100644 testdata/issues/172.md create mode 100644 testdata/issues/237.md create mode 100644 testdata/issues/79.md diff --git a/ansi/blockelement.go b/ansi/blockelement.go index aed1b658..2ca7bb34 100644 --- a/ansi/blockelement.go +++ b/ansi/blockelement.go @@ -4,7 +4,7 @@ import ( "bytes" "io" - "github.com/muesli/reflow/wordwrap" + "github.com/charmbracelet/x/ansi" ) // BlockElement provides a render buffer for children of a block element. @@ -30,10 +30,14 @@ func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error { bs := ctx.blockStack if e.Margin { + s := ansi.Wordwrap( + bs.Current().Block.String(), + int(bs.Width(ctx)), + " ,.;-+|", + ) + mw := NewMarginWriter(ctx, w, bs.Current().Style) - _, err := mw.Write( - wordwrap.Bytes(bs.Current().Block.Bytes(), int(bs.Width(ctx)))) - if err != nil { + if _, err := io.WriteString(mw, s); err != nil { return err } diff --git a/ansi/codeblock.go b/ansi/codeblock.go index 601e85ab..149aa8ca 100644 --- a/ansi/codeblock.go +++ b/ansi/codeblock.go @@ -128,6 +128,7 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error { if len(theme) > 0 { renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) + err := quick.Highlight(iw, e.Code, e.Language, "terminal256", theme) if err != nil { return err diff --git a/ansi/testdata/TestRenderer/block_quote.golden b/ansi/testdata/TestRenderer/block_quote.golden index 57dbd967..2b420000 100644 --- a/ansi/testdata/TestRenderer/block_quote.golden +++ b/ansi/testdata/TestRenderer/block_quote.golden @@ -1,3 +1,2 @@ -=> First line of quote                                                             -=> Second line                                                                     +=> First line of quote Second line                                                 diff --git a/ansi/testdata/TestRenderer/list.golden b/ansi/testdata/TestRenderer/list.golden index 4ba92dc8..8374a0aa 100644 --- a/ansi/testdata/TestRenderer/list.golden +++ b/ansi/testdata/TestRenderer/list.golden @@ -1,4 +1,4 @@ β€’ First Item                                                                     -    β€’ Nested List Item                                                           +    β€’ Nested List Item                                                             β€’ Second Item                                                                    diff --git a/ansi/testdata/TestRendererIssues/106.golden b/ansi/testdata/TestRendererIssues/106.golden index 6a0e4633..a1c0f80d 100644 --- a/ansi/testdata/TestRendererIssues/106.golden +++ b/ansi/testdata/TestRendererIssues/106.golden @@ -1,17 +1,17 @@ -                                                                              - β€’ `hi`                                                                       - β€’ \hi                                                                        - β€’ *hi                                                                        - β€’ _hi                                                                        - β€’ {hi}                                                                       - β€’ [hi]                                                                       - β€’                                                                        - β€’ (hi)                                                                       - β€’ # hi                                                                       - β€’ + hi                                                                       - β€’ - hi                                                                       - β€’ . hi                                                                       - β€’ ! hi                                                                       - β€’ | hi                                                                       +                                                                                  + β€’ `hi`                                                                           + β€’ \hi                                                                            + β€’ *hi                                                                            + β€’ _hi                                                                            + β€’ {hi}                                                                           + β€’ [hi]                                                                           + β€’                                                                            + β€’ (hi)                                                                           + β€’ # hi                                                                           + β€’ + hi                                                                           + β€’ - hi                                                                           + β€’ . hi                                                                           + β€’ ! hi                                                                           + β€’ | hi                                                                           diff --git a/ansi/testdata/TestRendererIssues/107.golden b/ansi/testdata/TestRendererIssues/107.golden new file mode 100644 index 00000000..424915d8 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/107.golden @@ -0,0 +1,7 @@ + +                                                                                  +   [Mount]                                                                        +   Options=reconnect,ServerAliveInterval=15,ServerAliveCountMax=3,noauto,_netdev, + allow_other,uid=1000,gid=1000,IdentityFile=/PATH/TO/SSH-KEY/id_rsa,              + StrictHostKeyChecking=no                                                         + diff --git a/ansi/testdata/TestRendererIssues/117.golden b/ansi/testdata/TestRendererIssues/117.golden index 6456bf56..22637fc2 100644 --- a/ansi/testdata/TestRendererIssues/117.golden +++ b/ansi/testdata/TestRendererIssues/117.golden @@ -1,6 +1,6 @@ -                                                                              - cmd β”‚descr - ────────────────────────────────────┼─────────────────────────────────────── -  glow config  β”‚open glow config +                                                                                  + cmd β”‚descr + ──────────────────────────────────────┼───────────────────────────────────────── +  glow config  β”‚open glow config diff --git a/ansi/testdata/TestRendererIssues/149.golden b/ansi/testdata/TestRendererIssues/149.golden index 00f65eb4..00a99f11 100644 --- a/ansi/testdata/TestRendererIssues/149.golden +++ b/ansi/testdata/TestRendererIssues/149.golden @@ -1,4 +1,5 @@ - a http://example.com/with-a-big-path/likely-to-use-more-than-one-line?why-not=after-all- - why-not-use-queryparams-too&abc=123                                          + a http://example.com/with-a-big-path/likely-to-use-more-than-one-line?why-       + not=after-all-why-                                                               + not-use-queryparams-too&abc=123                                                  diff --git a/ansi/testdata/TestRendererIssues/172.golden b/ansi/testdata/TestRendererIssues/172.golden new file mode 100644 index 00000000..e70f14c2 --- /dev/null +++ b/ansi/testdata/TestRendererIssues/172.golden @@ -0,0 +1,6 @@ + +                                                                                  + β”‚ The quick brown fox jumps over the lazy dog. The quick brown fox jumps over    + β”‚ the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox + β”‚ jumps over the lazy dog.                                                       + diff --git a/ansi/testdata/TestRendererIssues/237.golden b/ansi/testdata/TestRendererIssues/237.golden new file mode 100644 index 00000000..1b97b5ef --- /dev/null +++ b/ansi/testdata/TestRendererIssues/237.golden @@ -0,0 +1,9 @@ + +                                                                                  + β”‚ Content Security Policy (CSP) is an added layer of security that helps to      + β”‚ detect and mitigate certain types of attacks, including Cross-Site Scripting   + β”‚ (XSS) and data injection attacks. These attacks are used for everything from   + β”‚ data theft, to site defacement, to malware distribution.                       + β”‚                                                                                + β”‚ from MDN                                                                       + diff --git a/ansi/testdata/TestRendererIssues/239.golden b/ansi/testdata/TestRendererIssues/239.golden index 0d88ee10..8cf5a609 100644 --- a/ansi/testdata/TestRendererIssues/239.golden +++ b/ansi/testdata/TestRendererIssues/239.golden @@ -1,9 +1,9 @@ -                                                                              - First term                                                                   - 🠢 Definition one of first term.                                              -                                                                              - Second term                                                                  - 🠢 Definition one of second term.                                             -                                                                              +                                                                                  + First term                                                                       + 🠢 Definition one of first term.                                                  +                                                                                  + Second term                                                                      + 🠢 Definition one of second term.                                                 +                                                                                  diff --git a/ansi/testdata/TestRendererIssues/257.golden b/ansi/testdata/TestRendererIssues/257.golden index eeffc6db..0a3b9c2c 100644 --- a/ansi/testdata/TestRendererIssues/257.golden +++ b/ansi/testdata/TestRendererIssues/257.golden @@ -1,4 +1,4 @@ -                                                                              -   set runtimepath^=$XDG_CONFIG_HOME/vim                                      +                                                                                  +   set runtimepath^=$XDG_CONFIG_HOME/vim                                           diff --git a/ansi/testdata/TestRendererIssues/290.golden b/ansi/testdata/TestRendererIssues/290.golden index 1cc53087..95ed4cd4 100644 --- a/ansi/testdata/TestRendererIssues/290.golden +++ b/ansi/testdata/TestRendererIssues/290.golden @@ -1,6 +1,6 @@ -                                                                              - β€’ test@example.com mailto:test@example.com                                   - β€’ https://google.com https://google.com                                      - β€’ https://google.com https://google.com                                      +                                                                                  + β€’ test@example.com mailto:test@example.com                                       + β€’ https://google.com https://google.com                                          + β€’ https://google.com https://google.com                                          diff --git a/ansi/testdata/TestRendererIssues/312.golden b/ansi/testdata/TestRendererIssues/312.golden index 48c9c0ba..81dc1de2 100644 --- a/ansi/testdata/TestRendererIssues/312.golden +++ b/ansi/testdata/TestRendererIssues/312.golden @@ -1,17 +1,17 @@ -  File                                                                        -                                                                              - bold text with URL https://www.example.com                                   -                                                                              - italic text with URL https://www.example.com                                 -                                                                              - URL https://www.example.com                                                  -                                                                              - url with bold within https://www.example.com                                 -                                                                              - url with italic within https://www.example.com                               -                                                                              - entire url text is bold https://www.example.com                              -                                                                              - entire url text is italic https://www.example.com                            +  File                                                                            +                                                                                  + bold text with URL https://www.example.com                                       +                                                                                  + italic text with URL https://www.example.com                                     +                                                                                  + URL https://www.example.com                                                      +                                                                                  + url with bold within https://www.example.com                                     +                                                                                  + url with italic within https://www.example.com                                   +                                                                                  + entire url text is bold https://www.example.com                                  +                                                                                  + entire url text is italic https://www.example.com                                diff --git a/ansi/testdata/TestRendererIssues/313.golden b/ansi/testdata/TestRendererIssues/313.golden index 16cb7e99..c1c25dfe 100644 --- a/ansi/testdata/TestRendererIssues/313.golden +++ b/ansi/testdata/TestRendererIssues/313.golden @@ -1,8 +1,8 @@ -                                                                              - β”‚ This is a block quote                                                       - β”‚                                                                             - β”‚ β”‚ This is the nested quote                                                   - β”‚                                                                             - β”‚ This is part of the outer block quote.                                      +                                                                                  + β”‚ This is a block quote                                                          + β”‚                                                                                + β”‚ β”‚ This is the nested quote                                                     + β”‚                                                                                + β”‚ This is part of the outer block quote.                                         diff --git a/ansi/testdata/TestRendererIssues/315.golden b/ansi/testdata/TestRendererIssues/315.golden index 4cbc3d8e..e7ca1fc2 100644 --- a/ansi/testdata/TestRendererIssues/315.golden +++ b/ansi/testdata/TestRendererIssues/315.golden @@ -1,6 +1,6 @@ -                                                                              - Expression β”‚Value β”‚Type - ───────────────────────────────────────────┼────────────────┼─────────────── -  (1 >= 26) || (12 >= 6) {.java} β”‚s β”‚a +                                                                                  + Expression β”‚Value β”‚Type + ────────────────────────────────────────────┼──────────────────┼──────────────── +  (1 >= 26) || (12 >= 6) {.java} β”‚s β”‚a diff --git a/ansi/testdata/TestRendererIssues/316.golden b/ansi/testdata/TestRendererIssues/316.golden index 4a7e2032..131cba89 100644 --- a/ansi/testdata/TestRendererIssues/316.golden +++ b/ansi/testdata/TestRendererIssues/316.golden @@ -1,7 +1,7 @@ -                                                                              - Aβ”‚B - ────────────────────────────────────────────────────────┼─────────────────── - Here https://example.comβ”‚hello - https://autolink.com https://autolink.comβ”‚world +                                                                                  + Aβ”‚B + ──────────────────────────────────────────────────────────┼───────────────────── + Here https://example.comβ”‚hello + https://autolink.com https://autolink.comβ”‚world diff --git a/ansi/testdata/TestRendererIssues/42.golden b/ansi/testdata/TestRendererIssues/42.golden index 63c12a20..3e12e449 100644 --- a/ansi/testdata/TestRendererIssues/42.golden +++ b/ansi/testdata/TestRendererIssues/42.golden @@ -1,6 +1,6 @@ - If you want to make a more significant change, please first open an issue    - https://github.com/twpayne/chezmoi/issues/new to discuss the change that you - want to make. Dave Cheney gives a good rationale                             - https://dave.cheney.net/2019/02/18/talk-then-code as to why this is important. + If you want to make a more significant change, please first open an issue        + https://github.com/twpayne/chezmoi/issues/new to discuss the change that you     + want to make. Dave Cheney gives a good rationale                                 + https://dave.cheney.net/2019/02/18/talk-then-code as to why this is important.   diff --git a/ansi/testdata/TestRendererIssues/43.golden b/ansi/testdata/TestRendererIssues/43.golden index f88488f2..c2092e26 100644 --- a/ansi/testdata/TestRendererIssues/43.golden +++ b/ansi/testdata/TestRendererIssues/43.golden @@ -1,10 +1,10 @@ -                                                                              - β€’ Getting started                                                            - β€’ Developing locally                                                         - β€’ Documentation changes                                                      - β€’ Contributing changes                                                       - β€’ Managing releases                                                          - β€’ Packaging                                                                  - β€’ Updating the website                                                       +                                                                                  + β€’ Getting started                                                                + β€’ Developing locally                                                             + β€’ Documentation changes                                                          + β€’ Contributing changes                                                           + β€’ Managing releases                                                              + β€’ Packaging                                                                      + β€’ Updating the website                                                           diff --git a/ansi/testdata/TestRendererIssues/44.golden b/ansi/testdata/TestRendererIssues/44.golden index 60fb0347..cf12febd 100644 --- a/ansi/testdata/TestRendererIssues/44.golden +++ b/ansi/testdata/TestRendererIssues/44.golden @@ -1,9 +1,9 @@ -                                                                              - Distri…β”‚Architectures β”‚Package - ───────┼─────────────────────────────────┼────────────────────────────────── - Debian β”‚ amd64 ,  arm64 ,  armel ,  i386…β”‚ deb  https://github.com/twpayne/… - RedHat β”‚ aarch64 ,  armhfp ,  i686 ,  pp…β”‚ rpm  https://github.com/twpayne/… - OpenSU…β”‚ aarch64 ,  armhfp ,  i686 ,  pp…β”‚ rpm  https://github.com/twpayne/… - Ubuntu β”‚ amd64 ,  arm64 ,  armel ,  i386…β”‚ deb  https://github.com/twpayne/… +                                                                                  + Distri…β”‚Architectures β”‚Package + ───────┼───────────────────────────────────┼──────────────────────────────────── + Debian β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… + RedHat β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… + OpenSU…β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… + Ubuntu β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… diff --git a/ansi/testdata/TestRendererIssues/46_1.golden b/ansi/testdata/TestRendererIssues/46_1.golden index 0eb404ed..3150d107 100644 --- a/ansi/testdata/TestRendererIssues/46_1.golden +++ b/ansi/testdata/TestRendererIssues/46_1.golden @@ -1,6 +1,6 @@ -                                                                              - β€’ Navigation                                                                 -   β€’ Familiar shortcuts (arrows, ~, -, @), quick reference                    -                                                                              +                                                                                  + β€’ Navigation                                                                     +   β€’ Familiar shortcuts (arrows, ~, -, @), quick reference                        +                                                                                  diff --git a/ansi/testdata/TestRendererIssues/46_2.golden b/ansi/testdata/TestRendererIssues/46_2.golden index 745200bc..62aa4370 100644 --- a/ansi/testdata/TestRendererIssues/46_2.golden +++ b/ansi/testdata/TestRendererIssues/46_2.golden @@ -1,14 +1,14 @@ -                                                                              - Dependency β”‚Installa…β”‚Operation - ─────────────────────────────────────────┼─────────┼──────────────────────── - xdg-open (Linux), open(1) (macOS), cygst…β”‚base β”‚desktop opener - file, coreutils (cp, mv, rm), xargs β”‚base β”‚file type, copy, move a… - tar, (un)zip [atool/bsdtar for more form…β”‚base β”‚create, list, extract t… - archivemount, fusermount(3) β”‚optional β”‚mount, unmount archives - sshfs, rclone https://rclone.org/, fuser…β”‚optional β”‚mount, unmount remotes - trash-cli β”‚optional β”‚trash files (default ac… - vlock (Linux), bashlock (macOS), lock(1)…β”‚optional β”‚terminal locker (fallba… - advcpmv (Linux) (integration https://git…β”‚optional β”‚copy, move progress -  $VISUAL  (else  $EDITOR ),  $PAGER ,  $…β”‚optional β”‚fallback vi, less, sh +                                                                                  + Dependency β”‚Installa…β”‚Operation + ─────────────────────────────────────────────┼─────────┼──────────────────────── + xdg-open (Linux), open(1) (macOS), cygstart …β”‚base β”‚desktop opener + file, coreutils (cp, mv, rm), xargs β”‚base β”‚file type, copy, move a… + tar, (un)zip [atool/bsdtar for more formats] β”‚base β”‚create, list, extract t… + archivemount, fusermount(3) β”‚optional β”‚mount, unmount archives + sshfs, rclone https://rclone.org/, fusermoun…β”‚optional β”‚mount, unmount remotes + trash-cli β”‚optional β”‚trash files (default ac… + vlock (Linux), bashlock (macOS), lock(1) (BS…β”‚optional β”‚terminal locker (fallba… + advcpmv (Linux) (integration https://github.…β”‚optional β”‚copy, move progress +  $VISUAL  (else  $EDITOR ),  $PAGER ,  $SHEL…β”‚optional β”‚fallback vi, less, sh diff --git a/ansi/testdata/TestRendererIssues/47.golden b/ansi/testdata/TestRendererIssues/47.golden index 5422c7c3..20b0fd9b 100644 --- a/ansi/testdata/TestRendererIssues/47.golden +++ b/ansi/testdata/TestRendererIssues/47.golden @@ -1,3 +1,3 @@ - Example:                                                    + Example:                                                        diff --git a/ansi/testdata/TestRendererIssues/48.golden b/ansi/testdata/TestRendererIssues/48.golden index a20338ce..ee82302f 100644 --- a/ansi/testdata/TestRendererIssues/48.golden +++ b/ansi/testdata/TestRendererIssues/48.golden @@ -1,17 +1,17 @@ - emoji in text                                                                -                                                                              - πŸ™ ⚑ 🐱 = ❀️                                                                -                                                                              - emoji in header                                                              -                                                                              - ## πŸ™ ⚑ 🐱 = ❀️                                                             -                                                                              - no emoji in code blocks                                                      -                                                                              -   :octopus: :zap: :cat: = :heart:                                            -                                                                              - no emoji in inline code                                                      -                                                                              -  :octopus: :zap: :cat: = :heart:                                             + emoji in text                                                                    +                                                                                  + πŸ™ ⚑ 🐱 = ❀️                                                                    +                                                                                  + emoji in header                                                                  +                                                                                  + ## πŸ™ ⚑ 🐱 = ❀️                                                                 +                                                                                  + no emoji in code blocks                                                          +                                                                                  +   :octopus: :zap: :cat: = :heart:                                                +                                                                                  + no emoji in inline code                                                          +                                                                                  +  :octopus: :zap: :cat: = :heart:                                                 diff --git a/ansi/testdata/TestRendererIssues/60.golden b/ansi/testdata/TestRendererIssues/60.golden index c9c8688a..c6e009b5 100644 --- a/ansi/testdata/TestRendererIssues/60.golden +++ b/ansi/testdata/TestRendererIssues/60.golden @@ -1,28 +1,28 @@ -                                                                              - Library β”‚Version - ─────────────────────────────────────────────────────────────┼────────────── - ESMF https://github.com/esmf-org/esmf β”‚v8.6.1 - FMS https://github.com/NOAA-GFDL/FMS/ β”‚2024.01.02 - netCDF https://github.com/Unidata/netcdf-c β”‚4.9.2 - netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚4.6.1 - netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚4.3.1 - HDF5 https://portal.hdfgroup.org/display/support β”‚1.10.11 - HDF4 https://portal.hdfgroup.org/display/support β”‚4.2.16-2 - GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚v1.16.0 - xgboost https://github.com/dmlc/xgboost β”‚v1.6.0 - libyaml https://github.com/yaml/libyaml.git β”‚0.2.5 - antlr2 https://www.antlr2.org/ β”‚2.7.7 - GSL https://www.gnu.org/software/gsl/ β”‚2.7 - jpeg http://www.ijg.org/ β”‚9e - zlib http://www.zlib.net/ β”‚1.3.1 - szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚2.1.1 - cURL https://curl.haxx.se/ β”‚8.8.0 - UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚2.2.28 - NCO http://nco.sourceforge.net/ β”‚5.2.6 - CDO https://code.mpimet.mpg.de/projects/cdo β”‚2.3.0 - nccmp https://gitlab.com/remikz/nccmp β”‚1.9.1.0 - HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚3.0 - HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚2.0 - SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚5.2.20 +                                                                                  + Library β”‚Version + ───────────────────────────────────────────────────────────────┼──────────────── + ESMF https://github.com/esmf-org/esmf β”‚v8.6.1 + FMS https://github.com/NOAA-GFDL/FMS/ β”‚2024.01.02 + netCDF https://github.com/Unidata/netcdf-c β”‚4.9.2 + netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚4.6.1 + netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚4.3.1 + HDF5 https://portal.hdfgroup.org/display/support β”‚1.10.11 + HDF4 https://portal.hdfgroup.org/display/support β”‚4.2.16-2 + GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚v1.16.0 + xgboost https://github.com/dmlc/xgboost β”‚v1.6.0 + libyaml https://github.com/yaml/libyaml.git β”‚0.2.5 + antlr2 https://www.antlr2.org/ β”‚2.7.7 + GSL https://www.gnu.org/software/gsl/ β”‚2.7 + jpeg http://www.ijg.org/ β”‚9e + zlib http://www.zlib.net/ β”‚1.3.1 + szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚2.1.1 + cURL https://curl.haxx.se/ β”‚8.8.0 + UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚2.2.28 + NCO http://nco.sourceforge.net/ β”‚5.2.6 + CDO https://code.mpimet.mpg.de/projects/cdo β”‚2.3.0 + nccmp https://gitlab.com/remikz/nccmp β”‚1.9.1.0 + HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚3.0 + HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚2.0 + SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚5.2.20 diff --git a/ansi/testdata/TestRendererIssues/79.golden b/ansi/testdata/TestRendererIssues/79.golden new file mode 100644 index 00000000..6d3fa20a --- /dev/null +++ b/ansi/testdata/TestRendererIssues/79.golden @@ -0,0 +1,9 @@ + + Preceding blockquote paragraph                                                   +                                                                                  + β”‚ 1st blockquote paragraph                                                       + β”‚                                                                                + β”‚   quoted code block                                                            + β”‚                                                                                + β”‚ 2nd blockquote paragraph                                                       + diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index 76e2f356..d7f28cdb 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -3,72 +3,15 @@ package main import ( "fmt" + _ "embed" + "github.com/charmbracelet/glamour" ) -const in1 = `# Hello World - -This is a simple example of Markdown rendering with Glamour! -Check out the [other examples](https://github.com/charmbracelet/glamour/tree/master/examples) too. - -## links - -**just bold text** - -_just italic text_ - -**[bold with url within](https://www.example.com)** - -_[italic with url within](https://www.example.com)_ - -[normal](https://www.example.com) - -[url with **bold** within](https://www.example.com) - -[url with _italic_ within](https://www.example.com) - -[**entire url text is bold**](https://www.example.com) - -[aaa _entire url_ aaaa _text is italic_](https://www.example.com) - -**test@example.com** - -https://google.com - -_https://google.com_ - -This is a [link](https://charm.sh). - -## tables - -| h1 | h2 | -|---|---| -| a | b | - -### table with markup and escapes inside - -| a | b | c | -|---|---|---| -|test1|test2|test3| -|pipe \| pipe | 2 | 3 **bold** | -` + "|test1|var a = 1|test3|\n\n## escapes\n" + - "- \\`hi\\`\n" + - `- \\hi -- \*hi -- \_hi -- \{hi\} -- \[hi\] -- \ -- \(hi\) -- \# hi -- \+ hi -- \- hi -- \. hi -- \! hi -- \| hi -` +//go:embed input.md +var input []byte func main() { - out, _ := glamour.Render(in1, "dark") + out, _ := glamour.Render(string(input), "dark") fmt.Print(out) } diff --git a/testdata/TestCapitalization.golden b/testdata/TestCapitalization.golden index 2c684356..fe6e26f0 100644 --- a/testdata/TestCapitalization.golden +++ b/testdata/TestCapitalization.golden @@ -1,7 +1,7 @@ -  EVERYTHING IS UPPERCASE                                                     -                                                                              - ## Everything Is Titled                                                      -                                                                              - ### everything is lowercase                                                  +  EVERYTHING IS UPPERCASE                                                         +                                                                                  + ## Everything Is Titled                                                          +                                                                                  + ### everything is lowercase                                                       diff --git a/testdata/TestRenderHelpers.golden b/testdata/TestRenderHelpers.golden index e598ccaa..73b0e93d 100644 --- a/testdata/TestRenderHelpers.golden +++ b/testdata/TestRenderHelpers.golden @@ -1,40 +1,39 @@ -  Gold                                                                        -                                                                              - Render markdown on the CLI, with pizzazz!                                    -                                                                              - ## 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                                                                     -                                                                              - See cmd/gold /cmd/gold/.                                                     -                                                                              - ## Example Output                                                            -                                                                              - Image: Gold Dark Style β†’                                                     - https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png     -                                                                              - Check out the Gold Style Gallery                                             - https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!  -                                                                              - ## Colors                                                                    -                                                                              - Currently  gold  uses the Aurora ANSI colors                                 - https://godoc.org/github.com/logrusorgru/aurora#Index.                       -                                                                              - ## Development                                                               -                                                                              - Style definitions located in  styles/  can be embedded into the binary by    - running statik https://github.com/rakyll/statik:                             -                                                                              -   statik -f -src styles -include "*.json"                                    -                                                                              - You can re-generate screenshots of all available styles by running           - gallery.sh . This requires  termshot  and  pngcrush  installed on your       - system!                                                                      +  Gold                                                                            +                                                                                  + Render markdown on the CLI, with pizzazz!                                        +                                                                                  + ## 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                                                                         +                                                                                  + See cmd/gold /cmd/gold/.                                                         +                                                                                  + ## Example Output                                                                +                                                                                  + Image: Gold Dark Style β†’                                                         + https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png         +                                                                                  + Check out the Gold Style Gallery                                                 + https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!      +                                                                                  + ## Colors                                                                        +                                                                                  + Currently  gold  uses the Aurora ANSI colors                                     + https://godoc.org/github.com/logrusorgru/aurora#Index.                           +                                                                                  + ## Development                                                                   +                                                                                  + Style definitions located in  styles/  can be embedded into the binary by        + running statik https://github.com/rakyll/statik:                                 +                                                                                  +   statik -f -src styles -include "*.json"                                        +                                                                                  + You can re-generate screenshots of all available styles by running  gallery.sh . + This requires  termshot  and  pngcrush  installed on your system!                diff --git a/testdata/TestTermRenderer.golden b/testdata/TestTermRenderer.golden index e598ccaa..73b0e93d 100644 --- a/testdata/TestTermRenderer.golden +++ b/testdata/TestTermRenderer.golden @@ -1,40 +1,39 @@ -  Gold                                                                        -                                                                              - Render markdown on the CLI, with pizzazz!                                    -                                                                              - ## 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                                                                     -                                                                              - See cmd/gold /cmd/gold/.                                                     -                                                                              - ## Example Output                                                            -                                                                              - Image: Gold Dark Style β†’                                                     - https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png     -                                                                              - Check out the Gold Style Gallery                                             - https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!  -                                                                              - ## Colors                                                                    -                                                                              - Currently  gold  uses the Aurora ANSI colors                                 - https://godoc.org/github.com/logrusorgru/aurora#Index.                       -                                                                              - ## Development                                                               -                                                                              - Style definitions located in  styles/  can be embedded into the binary by    - running statik https://github.com/rakyll/statik:                             -                                                                              -   statik -f -src styles -include "*.json"                                    -                                                                              - You can re-generate screenshots of all available styles by running           - gallery.sh . This requires  termshot  and  pngcrush  installed on your       - system!                                                                      +  Gold                                                                            +                                                                                  + Render markdown on the CLI, with pizzazz!                                        +                                                                                  + ## 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                                                                         +                                                                                  + See cmd/gold /cmd/gold/.                                                         +                                                                                  + ## Example Output                                                                +                                                                                  + Image: Gold Dark Style β†’                                                         + https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png         +                                                                                  + Check out the Gold Style Gallery                                                 + https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!      +                                                                                  + ## Colors                                                                        +                                                                                  + Currently  gold  uses the Aurora ANSI colors                                     + https://godoc.org/github.com/logrusorgru/aurora#Index.                           +                                                                                  + ## Development                                                                   +                                                                                  + Style definitions located in  styles/  can be embedded into the binary by        + running statik https://github.com/rakyll/statik:                                 +                                                                                  +   statik -f -src styles -include "*.json"                                        +                                                                                  + You can re-generate screenshots of all available styles by running  gallery.sh . + This requires  termshot  and  pngcrush  installed on your system!                diff --git a/testdata/TestTermRendererWriter.golden b/testdata/TestTermRendererWriter.golden index e598ccaa..73b0e93d 100644 --- a/testdata/TestTermRendererWriter.golden +++ b/testdata/TestTermRendererWriter.golden @@ -1,40 +1,39 @@ -  Gold                                                                        -                                                                              - Render markdown on the CLI, with pizzazz!                                    -                                                                              - ## 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                                                                     -                                                                              - See cmd/gold /cmd/gold/.                                                     -                                                                              - ## Example Output                                                            -                                                                              - Image: Gold Dark Style β†’                                                     - https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png     -                                                                              - Check out the Gold Style Gallery                                             - https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!  -                                                                              - ## Colors                                                                    -                                                                              - Currently  gold  uses the Aurora ANSI colors                                 - https://godoc.org/github.com/logrusorgru/aurora#Index.                       -                                                                              - ## Development                                                               -                                                                              - Style definitions located in  styles/  can be embedded into the binary by    - running statik https://github.com/rakyll/statik:                             -                                                                              -   statik -f -src styles -include "*.json"                                    -                                                                              - You can re-generate screenshots of all available styles by running           - gallery.sh . This requires  termshot  and  pngcrush  installed on your       - system!                                                                      +  Gold                                                                            +                                                                                  + Render markdown on the CLI, with pizzazz!                                        +                                                                                  + ## 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                                                                         +                                                                                  + See cmd/gold /cmd/gold/.                                                         +                                                                                  + ## Example Output                                                                +                                                                                  + Image: Gold Dark Style β†’                                                         + https://github.com/charmbracelet/gold/raw/master/styles/gallery/dark.png         +                                                                                  + Check out the Gold Style Gallery                                                 + https://github.com/charmbracelet/gold/blob/master/styles/gallery/README.md!      +                                                                                  + ## Colors                                                                        +                                                                                  + Currently  gold  uses the Aurora ANSI colors                                     + https://godoc.org/github.com/logrusorgru/aurora#Index.                           +                                                                                  + ## Development                                                                   +                                                                                  + Style definitions located in  styles/  can be embedded into the binary by        + running statik https://github.com/rakyll/statik:                                 +                                                                                  +   statik -f -src styles -include "*.json"                                        +                                                                                  + You can re-generate screenshots of all available styles by running  gallery.sh . + This requires  termshot  and  pngcrush  installed on your system!                diff --git a/testdata/issues/107.md b/testdata/issues/107.md new file mode 100644 index 00000000..ffa94e32 --- /dev/null +++ b/testdata/issues/107.md @@ -0,0 +1,4 @@ +```ini +[Mount] +Options=reconnect,ServerAliveInterval=15,ServerAliveCountMax=3,noauto,_netdev,allow_other,uid=1000,gid=1000,IdentityFile=/PATH/TO/SSH-KEY/id_rsa,StrictHostKeyChecking=no +``` diff --git a/testdata/issues/172.md b/testdata/issues/172.md new file mode 100644 index 00000000..90990de0 --- /dev/null +++ b/testdata/issues/172.md @@ -0,0 +1,3 @@ + + +> The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. diff --git a/testdata/issues/237.md b/testdata/issues/237.md new file mode 100644 index 00000000..ece6450b --- /dev/null +++ b/testdata/issues/237.md @@ -0,0 +1,3 @@ +> Content Security Policy (CSP) is an added layer of security that helps to detect and mitigate certain types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. These attacks are used for everything from data theft, to site defacement, to malware distribution. +> +> from MDN diff --git a/testdata/issues/79.md b/testdata/issues/79.md new file mode 100644 index 00000000..d5a64b81 --- /dev/null +++ b/testdata/issues/79.md @@ -0,0 +1,9 @@ +Preceding blockquote paragraph + + + +> 1st blockquote paragraph +> +> quoted code block +> +> 2nd blockquote paragraph From ac108c8c4a417c72351613a2efaa86490bdcdba8 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 Jul 2024 17:03:58 -0300 Subject: [PATCH 30/36] fix: build Signed-off-by: Carlos Alexandro Becker --- ansi/elements.go | 7 +++++-- examples/helloworld/main.go | 15 +++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ansi/elements.go b/ansi/elements.go index 2f6670ca..161857ab 100644 --- a/ansi/elements.go +++ b/ansi/elements.go @@ -67,8 +67,11 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element { // Paragraph case ast.KindParagraph: - if node.Parent() != nil && node.Parent().Kind() == ast.KindListItem { - return Element{} + if node.Parent() != nil { + kind := node.Parent().Kind() + if kind == ast.KindListItem { + return Element{} + } } return Element{ Renderer: &ParagraphElement{ diff --git a/examples/helloworld/main.go b/examples/helloworld/main.go index d7f28cdb..53282da3 100644 --- a/examples/helloworld/main.go +++ b/examples/helloworld/main.go @@ -3,15 +3,18 @@ package main import ( "fmt" - _ "embed" - "github.com/charmbracelet/glamour" ) -//go:embed input.md -var input []byte - func main() { - out, _ := glamour.Render(string(input), "dark") + in := `# Hello World + +This is a simple example of Markdown rendering with Glamour! +Check out the [other examples](https://github.com/charmbracelet/glamour/tree/master/examples) too. + +Bye! +` + + out, _ := glamour.Render(in, "dark") fmt.Print(out) } From 33b8894379c4a1bf65c14d4c15396b413ae116ad Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 Jul 2024 17:05:51 -0300 Subject: [PATCH 31/36] chore: gitattributes --- .gitattributes | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..d7bdd748 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.golden linguist-generated From 9f680102d36e1cbcbeea432b495241862cde93b1 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 18 Jul 2024 17:08:08 -0300 Subject: [PATCH 32/36] fix: test opt Signed-off-by: Carlos Alexandro Becker --- ansi/table.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ansi/table.go b/ansi/table.go index 381224e6..e89ce0ff 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -61,7 +61,7 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { st = st.Margin(0, int(*m)) } if row == 0 { - st = st.Bold(true).Width(20) + st = st.Bold(true) } switch e.table.Alignments[col] { From c7ed991fe8c2ac453caa132c74fb483ccc748947 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 22 Jul 2024 10:22:04 -0400 Subject: [PATCH 33/36] fix: stable lipgloss Signed-off-by: Carlos Alexandro Becker --- go.mod | 4 ++-- go.sum | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 1a48dcd0..7dc33b68 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,8 @@ go 1.21 require ( github.com/alecthomas/chroma/v2 v2.14.0 - github.com/charmbracelet/lipgloss v0.12.2-0.20240712161825-87dd58def709 + github.com/charmbracelet/lipgloss v0.12.1 + github.com/charmbracelet/x/ansi v0.1.4 github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 github.com/microcosm-cc/bluemonday v1.0.27 github.com/muesli/reflow v0.3.0 @@ -17,7 +18,6 @@ require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/aymanbagabas/go-udiff v0.2.0 // indirect github.com/aymerick/douceur v0.2.0 // indirect - github.com/charmbracelet/x/ansi v0.1.4 // indirect github.com/dlclark/regexp2 v1.11.0 // indirect github.com/gorilla/css v1.0.1 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect diff --git a/go.sum b/go.sum index 29fc2e11..3a815755 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWp github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk= github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4= -github.com/charmbracelet/lipgloss v0.12.2-0.20240712161825-87dd58def709 h1:xKyYI89iCZPBA4QOBN1WQ8Fqt+iH09K3Ywx0qOKVPUo= -github.com/charmbracelet/lipgloss v0.12.2-0.20240712161825-87dd58def709/go.mod h1:lVwxBOJ0KkZflL9y+DObgGY8V90IZUJ1e6jjZ+PBcnE= +github.com/charmbracelet/lipgloss v0.12.1 h1:/gmzszl+pedQpjCOH+wFkZr/N90Snz40J/NR7A0zQcs= +github.com/charmbracelet/lipgloss v0.12.1/go.mod h1:V2CiwIuhx9S1S1ZlADfOj9HmxeMAORuz5izHb0zGbB8= github.com/charmbracelet/x/ansi v0.1.4 h1:IEU3D6+dWwPSgZ6HBH+v6oUuZ/nVawMiWj5831KfiLM= github.com/charmbracelet/x/ansi v0.1.4/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/charmbracelet/x/exp/golden v0.0.0-20240715153702-9ba8adf781c4 h1:6KzMkQeAF56rggw2NZu1L+TH7j9+DM1/2Kmh7KUxg1I= From 2e84e540d64e3401b13509f6871d95ff5d9ccf29 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 22 Jul 2024 10:35:52 -0400 Subject: [PATCH 34/36] fix: double styles --- ansi/table.go | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/ansi/table.go b/ansi/table.go index e89ce0ff..2c7e4264 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -55,8 +55,7 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { ctx.table.lipgloss = table.New(). Width(width). StyleFunc(func(row, col int) lipgloss.Style { - st := lipgloss.NewStyle(). - MaxWidth(width) + st := lipgloss.NewStyle() if m := ctx.options.Styles.Table.Margin; m != nil { st = st.Margin(0, int(*m)) } @@ -99,33 +98,9 @@ func (e *TableElement) setBorders(ctx RenderContext) { ctx.table.lipgloss.BorderBottom(false) } -func (e *TableElement) setStyles(ctx RenderContext) { - ctx.table.lipgloss.StyleFunc(func(row, col int) lipgloss.Style { - st := lipgloss.NewStyle() - if m := ctx.options.Styles.Table.Margin; m != nil { - st = st.Padding(0, int(*m)) - } - if row == 0 { - st = st.Bold(true) - } - - switch e.table.Alignments[col] { - case astext.AlignCenter: - st = st.Align(lipgloss.Center) - case astext.AlignRight: - st = st.Align(lipgloss.Right) - case astext.AlignLeft: - st = st.Align(lipgloss.Left) - } - - return st - }) -} - func (e *TableElement) Finish(_ io.Writer, ctx RenderContext) error { rules := ctx.options.Styles.Table - e.setStyles(ctx) e.setBorders(ctx) ow := ctx.blockStack.Current().Block From cc0a4106fbfbca4c7dbed879adb746caa58bfc27 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Mon, 22 Jul 2024 16:11:05 -0400 Subject: [PATCH 35/36] fix: tables --- ansi/table.go | 47 ++++++++++--------- ansi/testdata/TestRendererIssues/117.golden | 4 +- ansi/testdata/TestRendererIssues/315.golden | 4 +- ansi/testdata/TestRendererIssues/316.golden | 6 +-- ansi/testdata/TestRendererIssues/44.golden | 12 ++--- ansi/testdata/TestRendererIssues/46_2.golden | 22 ++++----- ansi/testdata/TestRendererIssues/60.golden | 48 ++++++++++---------- dracula.go | 3 -- styles.go | 18 +------- styles/ascii.json | 6 +-- styles/dark.json | 6 +-- styles/dracula.json | 6 +-- styles/light.json | 6 +-- styles/pink.json | 6 +-- styles/tokyo_night.json | 6 +-- 15 files changed, 82 insertions(+), 118 deletions(-) diff --git a/ansi/table.go b/ansi/table.go index 2c7e4264..6ac52948 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -52,30 +52,34 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { renderText(iw, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix) renderText(iw, ctx.options.ColorProfile, style, rules.Prefix) width := int(ctx.blockStack.Width(ctx)) - ctx.table.lipgloss = table.New(). - Width(width). - StyleFunc(func(row, col int) lipgloss.Style { - st := lipgloss.NewStyle() - if m := ctx.options.Styles.Table.Margin; m != nil { - st = st.Margin(0, int(*m)) - } - if row == 0 { - st = st.Bold(true) - } + ctx.table.lipgloss = table.New().Width(width) - switch e.table.Alignments[col] { - case astext.AlignLeft: - st = st.Align(lipgloss.Left) - case astext.AlignCenter: - st = st.Align(lipgloss.Center) - case astext.AlignRight: - st = st.Align(lipgloss.Right) - } + return nil +} + +func (e *TableElement) setStyles(ctx RenderContext) { + ctx.table.lipgloss = ctx.table.lipgloss.StyleFunc(func(row, col int) lipgloss.Style { + st := lipgloss.NewStyle() + if m := ctx.options.Styles.Table.Margin; m != nil { + st = st.Padding(0, int(*m)) + } else { + st = st.Padding(0, 1) + } + if row == 0 { + st = st.Bold(true) + } - return st - }) + switch e.table.Alignments[col] { + case astext.AlignLeft: + st = st.Align(lipgloss.Left) + case astext.AlignCenter: + st = st.Align(lipgloss.Center) + case astext.AlignRight: + st = st.Align(lipgloss.Right) + } - return nil + return st + }) } func (e *TableElement) setBorders(ctx RenderContext) { @@ -101,6 +105,7 @@ func (e *TableElement) setBorders(ctx RenderContext) { func (e *TableElement) Finish(_ io.Writer, ctx RenderContext) error { rules := ctx.options.Styles.Table + e.setStyles(ctx) e.setBorders(ctx) ow := ctx.blockStack.Current().Block diff --git a/ansi/testdata/TestRendererIssues/117.golden b/ansi/testdata/TestRendererIssues/117.golden index 22637fc2..8474cb62 100644 --- a/ansi/testdata/TestRendererIssues/117.golden +++ b/ansi/testdata/TestRendererIssues/117.golden @@ -1,6 +1,6 @@                                                                                  - cmd β”‚descr + cmd β”‚ descr ──────────────────────────────────────┼───────────────────────────────────────── -  glow config  β”‚open glow config +  glow config  β”‚ open glow config diff --git a/ansi/testdata/TestRendererIssues/315.golden b/ansi/testdata/TestRendererIssues/315.golden index e7ca1fc2..de1039ea 100644 --- a/ansi/testdata/TestRendererIssues/315.golden +++ b/ansi/testdata/TestRendererIssues/315.golden @@ -1,6 +1,6 @@                                                                                  - Expression β”‚Value β”‚Type + Expression β”‚ Value β”‚ Type ────────────────────────────────────────────┼──────────────────┼──────────────── -  (1 >= 26) || (12 >= 6) {.java} β”‚s β”‚a +  (1 >= 26) || (12 >= 6) {.java} β”‚ s β”‚ a diff --git a/ansi/testdata/TestRendererIssues/316.golden b/ansi/testdata/TestRendererIssues/316.golden index 131cba89..57ec09f8 100644 --- a/ansi/testdata/TestRendererIssues/316.golden +++ b/ansi/testdata/TestRendererIssues/316.golden @@ -1,7 +1,7 @@                                                                                  - Aβ”‚B + A β”‚ B ──────────────────────────────────────────────────────────┼───────────────────── - Here https://example.comβ”‚hello - https://autolink.com https://autolink.comβ”‚world + Here https://example.com β”‚ hello + https://autolink.com https://autolink.com β”‚ world diff --git a/ansi/testdata/TestRendererIssues/44.golden b/ansi/testdata/TestRendererIssues/44.golden index cf12febd..97f3c5a0 100644 --- a/ansi/testdata/TestRendererIssues/44.golden +++ b/ansi/testdata/TestRendererIssues/44.golden @@ -1,9 +1,9 @@                                                                                  - Distri…β”‚Architectures β”‚Package - ───────┼───────────────────────────────────┼──────────────────────────────────── - Debian β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… - RedHat β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… - OpenSU…β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… - Ubuntu β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… + Distr… β”‚ Architectures β”‚ Package + ────────┼───────────────────────────────────┼─────────────────────────────────── + Debian β”‚  amd64 ,  arm64 ,  armel ,  i386… β”‚  deb  https://github.com/twpayne… + RedHat β”‚  aarch64 ,  armhfp ,  i686 ,  pp… β”‚  rpm  https://github.com/twpayne… + OpenS… β”‚  aarch64 ,  armhfp ,  i686 ,  pp… β”‚  rpm  https://github.com/twpayne… + Ubuntu β”‚  amd64 ,  arm64 ,  armel ,  i386… β”‚  deb  https://github.com/twpayne… diff --git a/ansi/testdata/TestRendererIssues/46_2.golden b/ansi/testdata/TestRendererIssues/46_2.golden index 62aa4370..265bc3e2 100644 --- a/ansi/testdata/TestRendererIssues/46_2.golden +++ b/ansi/testdata/TestRendererIssues/46_2.golden @@ -1,14 +1,14 @@                                                                                  - Dependency β”‚Installa…β”‚Operation - ─────────────────────────────────────────────┼─────────┼──────────────────────── - xdg-open (Linux), open(1) (macOS), cygstart …β”‚base β”‚desktop opener - file, coreutils (cp, mv, rm), xargs β”‚base β”‚file type, copy, move a… - tar, (un)zip [atool/bsdtar for more formats] β”‚base β”‚create, list, extract t… - archivemount, fusermount(3) β”‚optional β”‚mount, unmount archives - sshfs, rclone https://rclone.org/, fusermoun…β”‚optional β”‚mount, unmount remotes - trash-cli β”‚optional β”‚trash files (default ac… - vlock (Linux), bashlock (macOS), lock(1) (BS…β”‚optional β”‚terminal locker (fallba… - advcpmv (Linux) (integration https://github.…β”‚optional β”‚copy, move progress -  $VISUAL  (else  $EDITOR ),  $PAGER ,  $SHEL…β”‚optional β”‚fallback vi, less, sh + Dependency β”‚ Install… β”‚ Operation + ───────────────────────────────────────────┼──────────┼───────────────────────── + xdg-open (Linux), open(1) (macOS), cygst… β”‚ base β”‚ desktop opener + file, coreutils (cp, mv, rm), xargs β”‚ base β”‚ file type, copy, move … + tar, (un)zip [atool/bsdtar for more form… β”‚ base β”‚ create, list, extract … + archivemount, fusermount(3) β”‚ optional β”‚ mount, unmount archives + sshfs, rclone https://rclone.org/, fuser… β”‚ optional β”‚ mount, unmount remotes + trash-cli β”‚ optional β”‚ trash files (default a… + vlock (Linux), bashlock (macOS), lock(1)… β”‚ optional β”‚ terminal locker (fallb… + advcpmv (Linux) (integration https://git… β”‚ optional β”‚ copy, move progress +  $VISUAL  (else  $EDITOR ),  $PAGER ,  $… β”‚ optional β”‚ fallback vi, less, sh diff --git a/ansi/testdata/TestRendererIssues/60.golden b/ansi/testdata/TestRendererIssues/60.golden index c6e009b5..73f6755d 100644 --- a/ansi/testdata/TestRendererIssues/60.golden +++ b/ansi/testdata/TestRendererIssues/60.golden @@ -1,28 +1,28 @@                                                                                  - Library β”‚Version + Library β”‚ Version ───────────────────────────────────────────────────────────────┼──────────────── - ESMF https://github.com/esmf-org/esmf β”‚v8.6.1 - FMS https://github.com/NOAA-GFDL/FMS/ β”‚2024.01.02 - netCDF https://github.com/Unidata/netcdf-c β”‚4.9.2 - netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚4.6.1 - netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚4.3.1 - HDF5 https://portal.hdfgroup.org/display/support β”‚1.10.11 - HDF4 https://portal.hdfgroup.org/display/support β”‚4.2.16-2 - GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚v1.16.0 - xgboost https://github.com/dmlc/xgboost β”‚v1.6.0 - libyaml https://github.com/yaml/libyaml.git β”‚0.2.5 - antlr2 https://www.antlr2.org/ β”‚2.7.7 - GSL https://www.gnu.org/software/gsl/ β”‚2.7 - jpeg http://www.ijg.org/ β”‚9e - zlib http://www.zlib.net/ β”‚1.3.1 - szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚2.1.1 - cURL https://curl.haxx.se/ β”‚8.8.0 - UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚2.2.28 - NCO http://nco.sourceforge.net/ β”‚5.2.6 - CDO https://code.mpimet.mpg.de/projects/cdo β”‚2.3.0 - nccmp https://gitlab.com/remikz/nccmp β”‚1.9.1.0 - HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚3.0 - HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚2.0 - SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚5.2.20 + ESMF https://github.com/esmf-org/esmf β”‚ v8.6.1 + FMS https://github.com/NOAA-GFDL/FMS/ β”‚ 2024.01.02 + netCDF https://github.com/Unidata/netcdf-c β”‚ 4.9.2 + netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚ 4.6.1 + netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚ 4.3.1 + HDF5 https://portal.hdfgroup.org/display/support β”‚ 1.10.11 + HDF4 https://portal.hdfgroup.org/display/support β”‚ 4.2.16-2 + GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚ v1.16.0 + xgboost https://github.com/dmlc/xgboost β”‚ v1.6.0 + libyaml https://github.com/yaml/libyaml.git β”‚ 0.2.5 + antlr2 https://www.antlr2.org/ β”‚ 2.7.7 + GSL https://www.gnu.org/software/gsl/ β”‚ 2.7 + jpeg http://www.ijg.org/ β”‚ 9e + zlib http://www.zlib.net/ β”‚ 1.3.1 + szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚ 2.1.1 + cURL https://curl.haxx.se/ β”‚ 8.8.0 + UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚ 2.2.28 + NCO http://nco.sourceforge.net/ β”‚ 5.2.6 + CDO https://code.mpimet.mpg.de/projects/cdo β”‚ 2.3.0 + nccmp https://gitlab.com/remikz/nccmp β”‚ 1.9.1.0 + HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚ 3.0 + HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚ 2.0 + SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚ 5.2.20 diff --git a/dracula.go b/dracula.go index 0b0eb56a..cf0a7dfc 100644 --- a/dracula.go +++ b/dracula.go @@ -208,9 +208,6 @@ var DraculaStyleConfig = ansi.StyleConfig{ StyleBlock: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, }, - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), }, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n🠢 ", diff --git a/styles.go b/styles.go index 840a4732..1d735ebe 100644 --- a/styles.go +++ b/styles.go @@ -110,11 +110,7 @@ var ( Margin: uintPtr(defaultMargin), }, }, - Table: ansi.StyleTable{ - CenterSeparator: stringPtr("+"), - ColumnSeparator: stringPtr("|"), - RowSeparator: stringPtr("-"), - }, + Table: ansi.StyleTable{}, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n* ", }, @@ -324,9 +320,6 @@ var ( StyleBlock: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, }, - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), }, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n🠢 ", @@ -536,9 +529,6 @@ var ( StyleBlock: ansi.StyleBlock{ StylePrimitive: ansi.StylePrimitive{}, }, - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), }, DefinitionDescription: ansi.StylePrimitive{ BlockPrefix: "\n🠢 ", @@ -642,11 +632,7 @@ var ( Suffix: " ", }, }, - Table: ansi.StyleTable{ - CenterSeparator: stringPtr("β”Ό"), - ColumnSeparator: stringPtr("β”‚"), - RowSeparator: stringPtr("─"), - }, + Table: ansi.StyleTable{}, DefinitionList: ansi.StyleBlock{}, DefinitionTerm: ansi.StylePrimitive{}, DefinitionDescription: ansi.StylePrimitive{ diff --git a/styles/ascii.json b/styles/ascii.json index 048a7298..be13b917 100644 --- a/styles/ascii.json +++ b/styles/ascii.json @@ -72,11 +72,7 @@ "code_block": { "margin": 2 }, - "table": { - "center_separator": "+", - "column_separator": "|", - "row_separator": "-" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/dark.json b/styles/dark.json index 0ba3bd5a..47bf0afc 100644 --- a/styles/dark.json +++ b/styles/dark.json @@ -180,11 +180,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/dracula.json b/styles/dracula.json index ef0d2927..4bbcd497 100644 --- a/styles/dracula.json +++ b/styles/dracula.json @@ -177,11 +177,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/light.json b/styles/light.json index ced0b54b..08533592 100644 --- a/styles/light.json +++ b/styles/light.json @@ -179,11 +179,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/pink.json b/styles/pink.json index 10070eb5..39a7f9ec 100644 --- a/styles/pink.json +++ b/styles/pink.json @@ -79,11 +79,7 @@ "background_color": "236" }, "code_block": {}, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { diff --git a/styles/tokyo_night.json b/styles/tokyo_night.json index 801278c8..82dd7755 100644 --- a/styles/tokyo_night.json +++ b/styles/tokyo_night.json @@ -169,11 +169,7 @@ } } }, - "table": { - "center_separator": "β”Ό", - "column_separator": "β”‚", - "row_separator": "─" - }, + "table": {}, "definition_list": {}, "definition_term": {}, "definition_description": { From 342f07bc6d83daea40d321d242607b24c2ce9c59 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Tue, 23 Jul 2024 16:13:42 -0400 Subject: [PATCH 36/36] fix: tables --- ansi/table.go | 8 ++-- ansi/testdata/TestRenderer/table.golden | 6 +-- ansi/testdata/TestRenderer/table_align.golden | 6 +-- ansi/testdata/TestRendererIssues/117.golden | 4 +- ansi/testdata/TestRendererIssues/315.golden | 4 +- ansi/testdata/TestRendererIssues/316.golden | 6 +-- ansi/testdata/TestRendererIssues/44.golden | 12 ++--- ansi/testdata/TestRendererIssues/46_2.golden | 22 ++++----- ansi/testdata/TestRendererIssues/60.golden | 48 +++++++++---------- 9 files changed, 57 insertions(+), 59 deletions(-) diff --git a/ansi/table.go b/ansi/table.go index 6ac52948..e8d26f13 100644 --- a/ansi/table.go +++ b/ansi/table.go @@ -59,11 +59,9 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error { func (e *TableElement) setStyles(ctx RenderContext) { ctx.table.lipgloss = ctx.table.lipgloss.StyleFunc(func(row, col int) lipgloss.Style { - st := lipgloss.NewStyle() + st := lipgloss.NewStyle().Inline(true) if m := ctx.options.Styles.Table.Margin; m != nil { st = st.Padding(0, int(*m)) - } else { - st = st.Padding(0, 1) } if row == 0 { st = st.Bold(true) @@ -71,11 +69,11 @@ func (e *TableElement) setStyles(ctx RenderContext) { switch e.table.Alignments[col] { case astext.AlignLeft: - st = st.Align(lipgloss.Left) + st = st.Align(lipgloss.Left).PaddingRight(0) case astext.AlignCenter: st = st.Align(lipgloss.Center) case astext.AlignRight: - st = st.Align(lipgloss.Right) + st = st.Align(lipgloss.Right).PaddingLeft(0) } return st diff --git a/ansi/testdata/TestRenderer/table.golden b/ansi/testdata/TestRenderer/table.golden index 37e29920..eae2429a 100644 --- a/ansi/testdata/TestRenderer/table.golden +++ b/ansi/testdata/TestRenderer/table.golden @@ -1,5 +1,5 @@ - Label β”‚ Value β”‚ URL +Label β”‚Value β”‚URL ──────────────────┼────────────────┼──────────────────────────────────────────── - First β”‚ foo β”‚ https://charm.sh https://charm.sh - Second β”‚ bar β”‚ https://charm.sh https://charm.sh +First β”‚foo β”‚https://charm.sh https://charm.sh +Second β”‚bar β”‚https://charm.sh https://charm.sh diff --git a/ansi/testdata/TestRenderer/table_align.golden b/ansi/testdata/TestRenderer/table_align.golden index 430eead2..4507bc62 100644 --- a/ansi/testdata/TestRenderer/table_align.golden +++ b/ansi/testdata/TestRenderer/table_align.golden @@ -1,5 +1,5 @@ - Label β”‚ Value β”‚ URL +Label β”‚ Value β”‚ URL ──────────────────┼────────────────┼──────────────────────────────────────────── - First β”‚ foo β”‚ charm.sh - Second β”‚ bar β”‚ https://charm.sh https://charm.sh +First β”‚ foo β”‚ charm.sh +Second β”‚ bar β”‚ https://charm.sh https://charm.sh diff --git a/ansi/testdata/TestRendererIssues/117.golden b/ansi/testdata/TestRendererIssues/117.golden index 8474cb62..22637fc2 100644 --- a/ansi/testdata/TestRendererIssues/117.golden +++ b/ansi/testdata/TestRendererIssues/117.golden @@ -1,6 +1,6 @@                                                                                  - cmd β”‚ descr + cmd β”‚descr ──────────────────────────────────────┼───────────────────────────────────────── -  glow config  β”‚ open glow config +  glow config  β”‚open glow config diff --git a/ansi/testdata/TestRendererIssues/315.golden b/ansi/testdata/TestRendererIssues/315.golden index de1039ea..e7ca1fc2 100644 --- a/ansi/testdata/TestRendererIssues/315.golden +++ b/ansi/testdata/TestRendererIssues/315.golden @@ -1,6 +1,6 @@                                                                                  - Expression β”‚ Value β”‚ Type + Expression β”‚Value β”‚Type ────────────────────────────────────────────┼──────────────────┼──────────────── -  (1 >= 26) || (12 >= 6) {.java} β”‚ s β”‚ a +  (1 >= 26) || (12 >= 6) {.java} β”‚s β”‚a diff --git a/ansi/testdata/TestRendererIssues/316.golden b/ansi/testdata/TestRendererIssues/316.golden index 57ec09f8..131cba89 100644 --- a/ansi/testdata/TestRendererIssues/316.golden +++ b/ansi/testdata/TestRendererIssues/316.golden @@ -1,7 +1,7 @@                                                                                  - A β”‚ B + Aβ”‚B ──────────────────────────────────────────────────────────┼───────────────────── - Here https://example.com β”‚ hello - https://autolink.com https://autolink.com β”‚ world + Here https://example.comβ”‚hello + https://autolink.com https://autolink.comβ”‚world diff --git a/ansi/testdata/TestRendererIssues/44.golden b/ansi/testdata/TestRendererIssues/44.golden index 97f3c5a0..cf12febd 100644 --- a/ansi/testdata/TestRendererIssues/44.golden +++ b/ansi/testdata/TestRendererIssues/44.golden @@ -1,9 +1,9 @@                                                                                  - Distr… β”‚ Architectures β”‚ Package - ────────┼───────────────────────────────────┼─────────────────────────────────── - Debian β”‚  amd64 ,  arm64 ,  armel ,  i386… β”‚  deb  https://github.com/twpayne… - RedHat β”‚  aarch64 ,  armhfp ,  i686 ,  pp… β”‚  rpm  https://github.com/twpayne… - OpenS… β”‚  aarch64 ,  armhfp ,  i686 ,  pp… β”‚  rpm  https://github.com/twpayne… - Ubuntu β”‚  amd64 ,  arm64 ,  armel ,  i386… β”‚  deb  https://github.com/twpayne… + Distri…β”‚Architectures β”‚Package + ───────┼───────────────────────────────────┼──────────────────────────────────── + Debian β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… + RedHat β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… + OpenSU…β”‚ aarch64 ,  armhfp ,  i686 ,  ppc6…β”‚ rpm  https://github.com/twpayne/ch… + Ubuntu β”‚ amd64 ,  arm64 ,  armel ,  i386 ,…β”‚ deb  https://github.com/twpayne/ch… diff --git a/ansi/testdata/TestRendererIssues/46_2.golden b/ansi/testdata/TestRendererIssues/46_2.golden index 265bc3e2..62aa4370 100644 --- a/ansi/testdata/TestRendererIssues/46_2.golden +++ b/ansi/testdata/TestRendererIssues/46_2.golden @@ -1,14 +1,14 @@                                                                                  - Dependency β”‚ Install… β”‚ Operation - ───────────────────────────────────────────┼──────────┼───────────────────────── - xdg-open (Linux), open(1) (macOS), cygst… β”‚ base β”‚ desktop opener - file, coreutils (cp, mv, rm), xargs β”‚ base β”‚ file type, copy, move … - tar, (un)zip [atool/bsdtar for more form… β”‚ base β”‚ create, list, extract … - archivemount, fusermount(3) β”‚ optional β”‚ mount, unmount archives - sshfs, rclone https://rclone.org/, fuser… β”‚ optional β”‚ mount, unmount remotes - trash-cli β”‚ optional β”‚ trash files (default a… - vlock (Linux), bashlock (macOS), lock(1)… β”‚ optional β”‚ terminal locker (fallb… - advcpmv (Linux) (integration https://git… β”‚ optional β”‚ copy, move progress -  $VISUAL  (else  $EDITOR ),  $PAGER ,  $… β”‚ optional β”‚ fallback vi, less, sh + Dependency β”‚Installa…β”‚Operation + ─────────────────────────────────────────────┼─────────┼──────────────────────── + xdg-open (Linux), open(1) (macOS), cygstart …β”‚base β”‚desktop opener + file, coreutils (cp, mv, rm), xargs β”‚base β”‚file type, copy, move a… + tar, (un)zip [atool/bsdtar for more formats] β”‚base β”‚create, list, extract t… + archivemount, fusermount(3) β”‚optional β”‚mount, unmount archives + sshfs, rclone https://rclone.org/, fusermoun…β”‚optional β”‚mount, unmount remotes + trash-cli β”‚optional β”‚trash files (default ac… + vlock (Linux), bashlock (macOS), lock(1) (BS…β”‚optional β”‚terminal locker (fallba… + advcpmv (Linux) (integration https://github.…β”‚optional β”‚copy, move progress +  $VISUAL  (else  $EDITOR ),  $PAGER ,  $SHEL…β”‚optional β”‚fallback vi, less, sh diff --git a/ansi/testdata/TestRendererIssues/60.golden b/ansi/testdata/TestRendererIssues/60.golden index 73f6755d..c6e009b5 100644 --- a/ansi/testdata/TestRendererIssues/60.golden +++ b/ansi/testdata/TestRendererIssues/60.golden @@ -1,28 +1,28 @@                                                                                  - Library β”‚ Version + Library β”‚Version ───────────────────────────────────────────────────────────────┼──────────────── - ESMF https://github.com/esmf-org/esmf β”‚ v8.6.1 - FMS https://github.com/NOAA-GFDL/FMS/ β”‚ 2024.01.02 - netCDF https://github.com/Unidata/netcdf-c β”‚ 4.9.2 - netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚ 4.6.1 - netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚ 4.3.1 - HDF5 https://portal.hdfgroup.org/display/support β”‚ 1.10.11 - HDF4 https://portal.hdfgroup.org/display/support β”‚ 4.2.16-2 - GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚ v1.16.0 - xgboost https://github.com/dmlc/xgboost β”‚ v1.6.0 - libyaml https://github.com/yaml/libyaml.git β”‚ 0.2.5 - antlr2 https://www.antlr2.org/ β”‚ 2.7.7 - GSL https://www.gnu.org/software/gsl/ β”‚ 2.7 - jpeg http://www.ijg.org/ β”‚ 9e - zlib http://www.zlib.net/ β”‚ 1.3.1 - szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚ 2.1.1 - cURL https://curl.haxx.se/ β”‚ 8.8.0 - UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚ 2.2.28 - NCO http://nco.sourceforge.net/ β”‚ 5.2.6 - CDO https://code.mpimet.mpg.de/projects/cdo β”‚ 2.3.0 - nccmp https://gitlab.com/remikz/nccmp β”‚ 1.9.1.0 - HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚ 3.0 - HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚ 2.0 - SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚ 5.2.20 + ESMF https://github.com/esmf-org/esmf β”‚v8.6.1 + FMS https://github.com/NOAA-GFDL/FMS/ β”‚2024.01.02 + netCDF https://github.com/Unidata/netcdf-c β”‚4.9.2 + netCDF Fortran https://github.com/Unidata/netcdf-fortran β”‚4.6.1 + netCDF C++ https://github.com/Unidata/netcdf-cxx4 β”‚4.3.1 + HDF5 https://portal.hdfgroup.org/display/support β”‚1.10.11 + HDF4 https://portal.hdfgroup.org/display/support β”‚4.2.16-2 + GFE https://github.com/Goddard-Fortran-Ecosystem/GFE β”‚v1.16.0 + xgboost https://github.com/dmlc/xgboost β”‚v1.6.0 + libyaml https://github.com/yaml/libyaml.git β”‚0.2.5 + antlr2 https://www.antlr2.org/ β”‚2.7.7 + GSL https://www.gnu.org/software/gsl/ β”‚2.7 + jpeg http://www.ijg.org/ β”‚9e + zlib http://www.zlib.net/ β”‚1.3.1 + szip https://support.hdfgroup.org/doc_resource/SZIP/ β”‚2.1.1 + cURL https://curl.haxx.se/ β”‚8.8.0 + UDUNITS2 https://github.com/GMAO-SI-Team/UDUNITS-2.git β”‚2.2.28 + NCO http://nco.sourceforge.net/ β”‚5.2.6 + CDO https://code.mpimet.mpg.de/projects/cdo β”‚2.3.0 + nccmp https://gitlab.com/remikz/nccmp β”‚1.9.1.0 + HDF-EOS2 https://wiki.earthdata.nasa.gov/display/DAS β”‚3.0 + HDF-EOS5 https://wiki.earthdata.nasa.gov/display/DAS β”‚2.0 + SDP Toolkit https://wiki.earthdata.nasa.gov/display/DAS β”‚5.2.20