Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align GitHub flavoured Tables #284

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ansi/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element {

// Tables
case astext.KindTable:
te := &TableElement{}
table := node.(*astext.Table)
te := &TableElement{table: table}
return Element{
Entering: "\n",
Renderer: te,
Expand Down
19 changes: 18 additions & 1 deletion ansi/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/muesli/reflow/indent"
"github.com/olekukonko/tablewriter"
astext "github.com/yuin/goldmark/extension/ast"
)

// A TableElement is used to render tables.
Expand All @@ -13,6 +14,7 @@ type TableElement struct {
styleWriter *StyleWriter
header []string
cell []string
table *astext.Table
}

// A TableRowElement is used to render a single row in a table.
Expand Down Expand Up @@ -51,7 +53,22 @@ func (e *TableElement) Render(w io.Writer, ctx RenderContext) error {

renderText(w, ctx.options.ColorProfile, bs.Current().Style.StylePrimitive, rules.BlockPrefix)
renderText(ctx.table.styleWriter, ctx.options.ColorProfile, style, rules.Prefix)
ctx.table.writer = tablewriter.NewWriter(ctx.table.styleWriter)
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
return nil
}

Expand Down
4 changes: 4 additions & 0 deletions styles/examples/table_align.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| Label | Value | URL |
| :----- | :---: | ------: |
| First | foo | charm.sh |
| Second | bar | https://charm.sh |
5 changes: 5 additions & 0 deletions styles/examples/table_align.style
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"table": {
"margin": 4
}
}
5 changes: 5 additions & 0 deletions testdata/table_align.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

LABEL | VALUE | URL
---------+-------+-------------------
First | foo | charm.sh
Second | bar | https://charm.sh
Loading