| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package tables |
| 15 |
|
| 16 |
import ( |
| 17 |
"github.com/gohugoio/hugo/common/herrors" |
| 18 |
"github.com/gohugoio/hugo/common/types/hstring" |
| 19 |
"github.com/gohugoio/hugo/markup/converter/hooks" |
| 20 |
"github.com/gohugoio/hugo/markup/goldmark/internal/render" |
| 21 |
"github.com/gohugoio/hugo/markup/internal/attributes" |
| 22 |
"github.com/yuin/goldmark" |
| 23 |
"github.com/yuin/goldmark/ast" |
| 24 |
gast "github.com/yuin/goldmark/extension/ast" |
| 25 |
"github.com/yuin/goldmark/renderer" |
| 26 |
"github.com/yuin/goldmark/util" |
| 27 |
) |
| 28 |
|
| 29 |
type ( |
| 30 |
ext struct{} |
| 31 |
htmlRenderer struct{} |
| 32 |
) |
| 33 |
|
| 34 |
func New() goldmark.Extender { |
| 35 |
return &ext{} |
| 36 |
} |
| 37 |
|
| 38 |
func (e *ext) Extend(m goldmark.Markdown) { |
| 39 |
m.Renderer().AddOptions(renderer.WithNodeRenderers( |
| 40 |
util.Prioritized(newHTMLRenderer(), 100), |
| 41 |
)) |
| 42 |
} |
| 43 |
|
| 44 |
func newHTMLRenderer() renderer.NodeRenderer { |
| 45 |
r := &htmlRenderer{} |
| 46 |
return r |
| 47 |
} |
| 48 |
|
| 49 |
func (r *htmlRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { |
| 50 |
reg.Register(gast.KindTable, r.renderTable) |
| 51 |
reg.Register(gast.KindTableHeader, r.renderHeaderOrRow) |
| 52 |
reg.Register(gast.KindTableRow, r.renderHeaderOrRow) |
| 53 |
reg.Register(gast.KindTableCell, r.renderCell) |
| 54 |
} |
| 55 |
|
| 56 |
func (r *htmlRenderer) renderTable(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { |
| 57 |
ctx := w.(*render.Context) |
| 58 |
if entering { |
| 59 |
// This will be modified below. |
| 60 |
table := &hooks.Table{} |
| 61 |
ctx.PushValue(gast.KindTable, table) |
| 62 |
return ast.WalkContinue, nil |
| 63 |
} |
| 64 |
|
| 65 |
v := ctx.PopValue(gast.KindTable) |
| 66 |
if v == nil { |
| 67 |
panic("table not found") |
| 68 |
} |
| 69 |
|
| 70 |
table := v.(*hooks.Table) |
| 71 |
|
| 72 |
renderer := ctx.RenderContext().GetRenderer(hooks.TableRendererType, nil) |
| 73 |
if renderer == nil { |
| 74 |
panic("table hook renderer not found") |
| 75 |
} |
| 76 |
|
| 77 |
ordinal := ctx.GetAndIncrementOrdinal(gast.KindTable) |
| 78 |
|
| 79 |
tctx := tableContext{ |
| 80 |
BaseContext: render.NewBaseContext(ctx, renderer, n, source, ordinal), |
| 81 |
AttributesHolder: attributes.New(n.Attributes(), attributes.AttributesOwnerGeneral), |
| 82 |
tHead: table.THead, |
| 83 |
tBody: table.TBody, |
| 84 |
} |
| 85 |
|
| 86 |
cr := renderer.(hooks.TableRenderer) |
| 87 |
|
| 88 |
err := cr.RenderTable( |
| 89 |
ctx.RenderContext().Ctx, |
| 90 |
w, |
| 91 |
tctx, |
| 92 |
) |
| 93 |
if err != nil { |
| 94 |
return ast.WalkContinue, herrors.NewFileErrorFromPos(err, tctx.Position()) |
| 95 |
} |
| 96 |
|
| 97 |
return ast.WalkContinue, nil |
| 98 |
} |
| 99 |
|
| 100 |
func (r *htmlRenderer) peekTable(ctx *render.Context) *hooks.Table { |
| 101 |
v := ctx.PeekValue(gast.KindTable) |
| 102 |
if v == nil { |
| 103 |
panic("table not found") |
| 104 |
} |
| 105 |
return v.(*hooks.Table) |
| 106 |
} |
| 107 |
|
| 108 |
func (r *htmlRenderer) renderCell(w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) { |
| 109 |
ctx := w.(*render.Context) |
| 110 |
|
| 111 |
if entering { |
| 112 |
// Store the current pos so we can capture the rendered text. |
| 113 |
ctx.PushPos(ctx.Buffer.Len()) |
| 114 |
return ast.WalkContinue, nil |
| 115 |
} |
| 116 |
|
| 117 |
n := node.(*gast.TableCell) |
| 118 |
|
| 119 |
text := ctx.PopRenderedString() |
| 120 |
|
| 121 |
table := r.peekTable(ctx) |
| 122 |
|
| 123 |
var alignment string |
| 124 |
switch n.Alignment { |
| 125 |
case gast.AlignLeft: |
| 126 |
alignment = "left" |
| 127 |
case gast.AlignRight: |
| 128 |
alignment = "right" |
| 129 |
case gast.AlignCenter: |
| 130 |
alignment = "center" |
| 131 |
default: |
| 132 |
alignment = "" |
| 133 |
} |
| 134 |
|
| 135 |
cell := hooks.TableCell{Text: hstring.HTML(text), Alignment: alignment} |
| 136 |
|
| 137 |
if node.Parent().Kind() == gast.KindTableHeader { |
| 138 |
table.THead[len(table.THead)-1] = append(table.THead[len(table.THead)-1], cell) |
| 139 |
} else { |
| 140 |
table.TBody[len(table.TBody)-1] = append(table.TBody[len(table.TBody)-1], cell) |
| 141 |
} |
| 142 |
|
| 143 |
return ast.WalkContinue, nil |
| 144 |
} |
| 145 |
|
| 146 |
func (r *htmlRenderer) renderHeaderOrRow(w util.BufWriter, source []byte, n ast.Node, entering bool) (ast.WalkStatus, error) { |
| 147 |
ctx := w.(*render.Context) |
| 148 |
table := r.peekTable(ctx) |
| 149 |
if entering { |
| 150 |
if n.Kind() == gast.KindTableHeader { |
| 151 |
table.THead = append(table.THead, hooks.TableRow{}) |
| 152 |
} else { |
| 153 |
table.TBody = append(table.TBody, hooks.TableRow{}) |
| 154 |
} |
| 155 |
return ast.WalkContinue, nil |
| 156 |
} |
| 157 |
|
| 158 |
return ast.WalkContinue, nil |
| 159 |
} |
| 160 |
|
| 161 |
type tableContext struct { |
| 162 |
hooks.BaseContext |
| 163 |
*attributes.AttributesHolder |
| 164 |
|
| 165 |
tHead []hooks.TableRow |
| 166 |
tBody []hooks.TableRow |
| 167 |
} |
| 168 |
|
| 169 |
func (c tableContext) THead() []hooks.TableRow { |
| 170 |
return c.tHead |
| 171 |
} |
| 172 |
|
| 173 |
func (c tableContext) TBody() []hooks.TableRow { |
| 174 |
return c.tBody |
| 175 |
} |