diff options
Diffstat (limited to 'markup/goldmark/tables')
-rw-r--r-- | markup/goldmark/tables/tables.go | 175 | ||||
-rw-r--r-- | markup/goldmark/tables/tables_integration_test.go | 181 |
2 files changed, 356 insertions, 0 deletions
diff --git a/markup/goldmark/tables/tables.go b/markup/goldmark/tables/tables.go new file mode 100644 index 000000000..61c9b893f --- /dev/null +++ b/markup/goldmark/tables/tables.go @@ -0,0 +1,175 @@ +// Copyright 2024 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tables + +import ( + "github.com/gohugoio/hugo/common/herrors" + "github.com/gohugoio/hugo/common/types/hstring" + "github.com/gohugoio/hugo/markup/converter/hooks" + "github.com/gohugoio/hugo/markup/goldmark/internal/render" + "github.com/gohugoio/hugo/markup/internal/attributes" + "github.com/yuin/goldmark" + "github.com/yuin/goldmark/ast" + gast "github.com/yuin/goldmark/extension/ast" + "github.com/yuin/goldmark/renderer" + "github.com/yuin/goldmark/util" +) + +type ( + ext struct{} + htmlRenderer struct{} +) + +func New() goldmark.Extender { + return &ext{} +} + +func (e *ext) Extend(m goldmark.Markdown) { + m.Renderer().AddOptions(renderer.WithNodeRenderers( + util.Prioritized(newHTMLRenderer(), 100), + )) +} + +func newHTMLRenderer() renderer.NodeRenderer { + r := &htmlRenderer{} + return r +} + +func (r *htmlRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { + reg.Register(gast.KindTable, r.renderTable) + reg.Register(gast.KindTableHeader, r.renderHeaderOrRow) + reg.Register(gast.KindTableRow, r.renderHeaderOrRow) + reg.Register(gast.KindTableCell, r.renderCell) +} + +func (r *htmlRenderer) renderTable(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { + ctx := w.(*render.Context) + if entering { + // This will be modified below. + table := &hooks.Table{} + ctx.PushValue(gast.KindTable, table) + return ast.WalkContinue, nil + } + + v := ctx.PopValue(gast.KindTable) + if v == nil { + panic("table not found") + } + + table := v.(*hooks.Table) + + renderer := ctx.RenderContext().GetRenderer(hooks.TableRendererType, nil) + if renderer == nil { + panic("table hook renderer not found") + } + + ordinal := ctx.GetAndIncrementOrdinal(gast.KindTable) + + tctx := &tableContext{ + BaseContext: render.NewBaseContext(ctx, renderer, n, source, nil, ordinal), + AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral), + tHead: table.THead, + tBody: table.TBody, + } + + cr := renderer.(hooks.TableRenderer) + + err := cr.RenderTable( + ctx.RenderContext().Ctx, + w, + tctx, + ) + if err != nil { + return ast.WalkContinue, herrors.NewFileErrorFromPos(err, tctx.Position()) + } + + return ast.WalkContinue, nil +} + +func (r *htmlRenderer) peekTable(ctx *render.Context) *hooks.Table { + v := ctx.PeekValue(gast.KindTable) + if v == nil { + panic("table not found") + } + return v.(*hooks.Table) +} + +func (r *htmlRenderer) renderCell(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { + ctx := w.(*render.Context) + + if entering { + // Store the current pos so we can capture the rendered text. + ctx.PushPos(ctx.Buffer.Len()) + return ast.WalkContinue, nil + } + + n := node.(*gast.TableCell) + + text := ctx.PopRenderedString() + + table := r.peekTable(ctx) + + var alignment string + switch n.Alignment { + case gast.AlignLeft: + alignment = "left" + case gast.AlignRight: + alignment = "right" + case gast.AlignCenter: + alignment = "center" + default: + alignment = "left" + } + + cell := hooks.TableCell{Text: hstring.RenderedString(text), Alignment: alignment} + + if node.Parent().Kind() == gast.KindTableHeader { + table.THead[len(table.THead)-1] = append(table.THead[len(table.THead)-1], cell) + } else { + table.TBody[len(table.TBody)-1] = append(table.TBody[len(table.TBody)-1], cell) + } + + return ast.WalkContinue, nil +} + +func (r *htmlRenderer) renderHeaderOrRow(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { + ctx := w.(*render.Context) + table := r.peekTable(ctx) + if entering { + if n.Kind() == gast.KindTableHeader { + table.THead = append(table.THead, hooks.TableRow{}) + } else { + table.TBody = append(table.TBody, hooks.TableRow{}) + } + return ast.WalkContinue, nil + } + + return ast.WalkContinue, nil +} + +type tableContext struct { + hooks.BaseContext + *attributes.AttributesHolder + + tHead []hooks.TableRow + tBody []hooks.TableRow +} + +func (c *tableContext) THead() []hooks.TableRow { + return c.tHead +} + +func (c *tableContext) TBody() []hooks.TableRow { + return c.tBody +} diff --git a/markup/goldmark/tables/tables_integration_test.go b/markup/goldmark/tables/tables_integration_test.go new file mode 100644 index 000000000..8055265c8 --- /dev/null +++ b/markup/goldmark/tables/tables_integration_test.go @@ -0,0 +1,181 @@ +// Copyright 2024 The Hugo Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package tables_test + +import ( + "testing" + + "github.com/gohugoio/hugo/hugolib" +) + +func TestTableHook(t *testing.T) { + t.Parallel() + files := ` +-- hugo.toml -- +[markup.goldmark.parser.attribute] +block = true +title = true +-- content/p1.md -- +## Table 1 + +| Item | In Stock | Price | +| :---------------- | :------: | ----: | +| Python Hat | True | 23.99 | +| SQL Hat | True | 23.99 | +| Codecademy Tee | False | 19.99 | +| Codecademy Hoodie | False | 42.99 | +{.foo foo="bar"} + +## Table 2 + +| Month | Savings | +| -------- | ------- | +| January | $250 | +| February | $80 | +| March | $420 | + +-- layouts/_default/single.html -- +{{ .Content }} +-- layouts/_default/_markup/render-table.html -- +Attributes: {{ .Attributes }}| +{{ template "print" (dict "what" (printf "table-%d-thead" $.Ordinal) "rows" .THead) }} +{{ template "print" (dict "what" (printf "table-%d-tbody" $.Ordinal) "rows" .TBody) }} +{{ define "print" }} + {{ .what }}:{{ range $i, $a := .rows }} {{ $i }}:{{ range $j, $b := . }} {{ $j }}: {{ .Alignment }}: {{ .Text }}|{{ end }}{{ end }}$ +{{ end }} + +` + b := hugolib.Test(t, files) + + b.AssertFileContent("public/p1/index.html", + "Attributes: map[class:foo foo:bar]|", + "table-0-thead: 0: 0: left: Item| 1: center: In Stock| 2: right: Price|$", + "table-0-tbody: 0: 0: left: Python Hat| 1: center: True| 2: right: 23.99| 1: 0: left: SQL Hat| 1: center: True| 2: right: 23.99| 2: 0: left: Codecademy Tee| 1: center: False| 2: right: 19.99| 3: 0: left: Codecademy Hoodie| 1: center: False| 2: right: 42.99|$", + ) + + b.AssertFileContent("public/p1/index.html", + "table-1-thead: 0: 0: left: Month| 1: left: Savings|$", + "table-1-tbody: 0: 0: left: January| 1: left: $250| 1: 0: left: February| 1: left: $80| 2: 0: left: March| 1: left: $420|$", + ) +} + +func TestTableDefault(t *testing.T) { + t.Parallel() + files := ` +-- hugo.toml -- +[markup.goldmark.parser.attribute] +block = true +title = true +-- content/p1.md -- + +## Table 1 + +| Item | In Stock | Price | +| :---------------- | :------: | ----: | +| Python Hat | True | 23.99 | +| SQL Hat | True | 23.99 | +| Codecademy Tee | False | 19.99 | +| Codecademy Hoodie | False | 42.99 | +{.foo} + + +-- layouts/_default/single.html -- +Summary: {{ .Summary }} +Content: {{ .Content }} + +` + b := hugolib.Test(t, files) + + b.AssertFileContent("public/p1/index.html", "<table class=\"foo\">") +} + +// Issue 12811. +func TestTableDefaultRSSAndHTML(t *testing.T) { + t.Parallel() + files := ` +-- hugo.toml -- +[outputFormats] + [outputFormats.rss] + weight = 30 + [outputFormats.html] + weight = 20 +-- content/_index.md -- +--- +title: "Home" +output: ["rss", "html"] +--- + +| Item | In Stock | Price | +| :---------------- | :------: | ----: | +| Python Hat | True | 23.99 | +| SQL Hat | True | 23.99 | +| Codecademy Tee | False | 19.99 | +| Codecademy Hoodie | False | 42.99 | + +{{< foo >}} + +-- layouts/index.html -- +Content: {{ .Content }} +-- layouts/index.xml -- +Content: {{ .Content }} +-- layouts/shortcodes/foo.xml -- +foo xml +-- layouts/shortcodes/foo.html -- +foo html + +` + b := hugolib.Test(t, files) + + b.AssertFileContent("public/index.xml", "<table>") + b.AssertFileContent("public/index.html", "<table>") +} + +func TestTableDefaultRSSOnly(t *testing.T) { + t.Parallel() + files := ` +-- hugo.toml -- +[outputs] + home = ['rss'] + section = ['rss'] + taxonomy = ['rss'] + term = ['rss'] + page = ['rss'] +disableKinds = ["taxonomy", "term", "page", "section"] +-- content/_index.md -- +--- +title: "Home" +--- + +## Table 1 + +| Item | In Stock | Price | +| :---------------- | :------: | ----: | +| Python Hat | True | 23.99 | +| SQL Hat | True | 23.99 | +| Codecademy Tee | False | 19.99 | +| Codecademy Hoodie | False | 42.99 | + + + + + +-- layouts/index.xml -- +Content: {{ .Content }} + + +` + b := hugolib.Test(t, files) + + b.AssertFileContent("public/index.xml", "<table>") +} |