| 1 |
// Copyright 2025 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 render |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"math/bits" |
| 19 |
"strings" |
| 20 |
"sync" |
| 21 |
|
| 22 |
bp "github.com/gohugoio/hugo/bufferpool" |
| 23 |
east "github.com/yuin/goldmark-emoji/ast" |
| 24 |
|
| 25 |
htext "github.com/gohugoio/hugo/common/text" |
| 26 |
"github.com/gohugoio/hugo/tpl" |
| 27 |
|
| 28 |
"github.com/gohugoio/hugo/markup/converter" |
| 29 |
"github.com/gohugoio/hugo/markup/converter/hooks" |
| 30 |
"github.com/yuin/goldmark/ast" |
| 31 |
"github.com/yuin/goldmark/util" |
| 32 |
) |
| 33 |
|
| 34 |
type BufWriter struct { |
| 35 |
*bytes.Buffer |
| 36 |
} |
| 37 |
|
| 38 |
const maxInt = 1<<(bits.UintSize-1) - 1 |
| 39 |
|
| 40 |
func (b *BufWriter) Available() int { |
| 41 |
return maxInt |
| 42 |
} |
| 43 |
|
| 44 |
func (b *BufWriter) Buffered() int { |
| 45 |
return b.Len() |
| 46 |
} |
| 47 |
|
| 48 |
func (b *BufWriter) Flush() error { |
| 49 |
return nil |
| 50 |
} |
| 51 |
|
| 52 |
type Context struct { |
| 53 |
*BufWriter |
| 54 |
ContextData |
| 55 |
positions []int |
| 56 |
pids []uint64 |
| 57 |
ordinals map[ast.NodeKind]int |
| 58 |
values map[ast.NodeKind][]any |
| 59 |
} |
| 60 |
|
| 61 |
func (ctx *Context) GetAndIncrementOrdinal(kind ast.NodeKind) int { |
| 62 |
if ctx.ordinals == nil { |
| 63 |
ctx.ordinals = make(map[ast.NodeKind]int) |
| 64 |
} |
| 65 |
i := ctx.ordinals[kind] |
| 66 |
ctx.ordinals[kind]++ |
| 67 |
return i |
| 68 |
} |
| 69 |
|
| 70 |
func (ctx *Context) PushPos(n int) { |
| 71 |
ctx.positions = append(ctx.positions, n) |
| 72 |
} |
| 73 |
|
| 74 |
func (ctx *Context) PopPos() int { |
| 75 |
i := len(ctx.positions) - 1 |
| 76 |
p := ctx.positions[i] |
| 77 |
ctx.positions = ctx.positions[:i] |
| 78 |
return p |
| 79 |
} |
| 80 |
|
| 81 |
func (ctx *Context) PopRenderedString() string { |
| 82 |
pos := ctx.PopPos() |
| 83 |
text := string(ctx.Bytes()[pos:]) |
| 84 |
ctx.Truncate(pos) |
| 85 |
return text |
| 86 |
} |
| 87 |
|
| 88 |
// PushPid pushes a new page ID to the stack. |
| 89 |
func (ctx *Context) PushPid(pid uint64) { |
| 90 |
ctx.pids = append(ctx.pids, pid) |
| 91 |
} |
| 92 |
|
| 93 |
// PeekPid returns the current page ID without removing it from the stack. |
| 94 |
func (ctx *Context) PeekPid() uint64 { |
| 95 |
if len(ctx.pids) == 0 { |
| 96 |
return 0 |
| 97 |
} |
| 98 |
return ctx.pids[len(ctx.pids)-1] |
| 99 |
} |
| 100 |
|
| 101 |
// PopPid pops the last page ID from the stack. |
| 102 |
func (ctx *Context) PopPid() uint64 { |
| 103 |
if len(ctx.pids) == 0 { |
| 104 |
return 0 |
| 105 |
} |
| 106 |
i := len(ctx.pids) - 1 |
| 107 |
p := ctx.pids[i] |
| 108 |
ctx.pids = ctx.pids[:i] |
| 109 |
return p |
| 110 |
} |
| 111 |
|
| 112 |
func (ctx *Context) PushValue(k ast.NodeKind, v any) { |
| 113 |
if ctx.values == nil { |
| 114 |
ctx.values = make(map[ast.NodeKind][]any) |
| 115 |
} |
| 116 |
ctx.values[k] = append(ctx.values[k], v) |
| 117 |
} |
| 118 |
|
| 119 |
func (ctx *Context) PopValue(k ast.NodeKind) any { |
| 120 |
if ctx.values == nil { |
| 121 |
return nil |
| 122 |
} |
| 123 |
v := ctx.values[k] |
| 124 |
if len(v) == 0 { |
| 125 |
return nil |
| 126 |
} |
| 127 |
i := len(v) - 1 |
| 128 |
r := v[i] |
| 129 |
ctx.values[k] = v[:i] |
| 130 |
return r |
| 131 |
} |
| 132 |
|
| 133 |
func (ctx *Context) PeekValue(k ast.NodeKind) any { |
| 134 |
if ctx.values == nil { |
| 135 |
return nil |
| 136 |
} |
| 137 |
v := ctx.values[k] |
| 138 |
if len(v) == 0 { |
| 139 |
return nil |
| 140 |
} |
| 141 |
return v[len(v)-1] |
| 142 |
} |
| 143 |
|
| 144 |
type ContextData interface { |
| 145 |
RenderContext() converter.RenderContext |
| 146 |
DocumentContext() converter.DocumentContext |
| 147 |
} |
| 148 |
|
| 149 |
type RenderContextDataHolder struct { |
| 150 |
Rctx converter.RenderContext |
| 151 |
Dctx converter.DocumentContext |
| 152 |
} |
| 153 |
|
| 154 |
func (ctx *RenderContextDataHolder) RenderContext() converter.RenderContext { |
| 155 |
return ctx.Rctx |
| 156 |
} |
| 157 |
|
| 158 |
func (ctx *RenderContextDataHolder) DocumentContext() converter.DocumentContext { |
| 159 |
return ctx.Dctx |
| 160 |
} |
| 161 |
|
| 162 |
// GetPageAndPageInner returns the current page and the inner page for the given context. |
| 163 |
func GetPageAndPageInner(rctx *Context) (any, any) { |
| 164 |
p := rctx.DocumentContext().Document |
| 165 |
pid := rctx.PeekPid() |
| 166 |
if pid > 0 { |
| 167 |
if lookup := rctx.DocumentContext().DocumentLookup; lookup != nil { |
| 168 |
if v := rctx.DocumentContext().DocumentLookup(pid); v != nil { |
| 169 |
return p, v |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
return p, p |
| 174 |
} |
| 175 |
|
| 176 |
// NewBaseContext creates a new BaseContext. |
| 177 |
func NewBaseContext(rctx *Context, renderer any, n ast.Node, src []byte, ordinal int) hooks.BaseContext { |
| 178 |
page, pageInner := GetPageAndPageInner(rctx) |
| 179 |
b := &hookBase{ |
| 180 |
page: page, |
| 181 |
pageInner: pageInner, |
| 182 |
ordinal: ordinal, |
| 183 |
} |
| 184 |
|
| 185 |
b.createPos = func() htext.Position { |
| 186 |
if resolver, ok := renderer.(hooks.ElementPositionResolver); ok { |
| 187 |
return resolver.ResolvePosition(rctx.RenderContext(), n.Pos()) |
| 188 |
} |
| 189 |
|
| 190 |
return htext.Position{ |
| 191 |
Filename: rctx.DocumentContext().Filename, |
| 192 |
LineNumber: 1, |
| 193 |
ColumnNumber: 1, |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return b |
| 198 |
} |
| 199 |
|
| 200 |
type hookBase struct { |
| 201 |
page any |
| 202 |
pageInner any |
| 203 |
ordinal int |
| 204 |
|
| 205 |
pos htext.Position |
| 206 |
posInit sync.Once |
| 207 |
createPos func() htext.Position |
| 208 |
} |
| 209 |
|
| 210 |
func (c *hookBase) Page() any { |
| 211 |
return c.page |
| 212 |
} |
| 213 |
|
| 214 |
func (c *hookBase) PageInner() any { |
| 215 |
return c.pageInner |
| 216 |
} |
| 217 |
|
| 218 |
func (c *hookBase) Ordinal() int { |
| 219 |
return c.ordinal |
| 220 |
} |
| 221 |
|
| 222 |
func (c *hookBase) Position() htext.Position { |
| 223 |
c.posInit.Do(func() { |
| 224 |
c.pos = c.createPos() |
| 225 |
}) |
| 226 |
return c.pos |
| 227 |
} |
| 228 |
|
| 229 |
// TextPlain returns a plain text representation of the given node. |
| 230 |
// This will resolve any leftover HTML entities. This will typically be |
| 231 |
// entities inserted by e.g. the typographer extension. |
| 232 |
// Goldmark's Node.Text was deprecated in 1.7.8. |
| 233 |
func TextPlain(n ast.Node, source []byte) string { |
| 234 |
buf := bp.GetBuffer() |
| 235 |
defer bp.PutBuffer(buf) |
| 236 |
|
| 237 |
for c := n.FirstChild(); c != nil; c = c.NextSibling() { |
| 238 |
textPlainTo(c, source, buf) |
| 239 |
} |
| 240 |
return string(util.ResolveEntityNames(buf.Bytes())) |
| 241 |
} |
| 242 |
|
| 243 |
func textPlainTo(c ast.Node, source []byte, buf *bytes.Buffer) { |
| 244 |
if c == nil { |
| 245 |
return |
| 246 |
} |
| 247 |
|
| 248 |
switch c := c.(type) { |
| 249 |
case *ast.RawHTML: |
| 250 |
s := strings.TrimSpace(tpl.StripHTML(string(c.Segments.Value(source)))) |
| 251 |
buf.WriteString(s) |
| 252 |
case *ast.String: |
| 253 |
buf.Write(c.Value) |
| 254 |
case *ast.Text: |
| 255 |
buf.Write(c.Segment.Value(source)) |
| 256 |
if c.HardLineBreak() || c.SoftLineBreak() { |
| 257 |
buf.WriteByte('\n') |
| 258 |
} |
| 259 |
case *east.Emoji: |
| 260 |
buf.WriteString(string(c.ShortName)) |
| 261 |
default: |
| 262 |
textPlainTo(c.FirstChild(), source, buf) |
| 263 |
} |
| 264 |
} |