hooks.go (3080B)
1 // Copyright 2019 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 hooks
15
16 import (
17 "io"
18
19 "github.com/gohugoio/hugo/common/hugio"
20 "github.com/gohugoio/hugo/common/text"
21 "github.com/gohugoio/hugo/common/types/hstring"
22 "github.com/gohugoio/hugo/identity"
23 "github.com/gohugoio/hugo/markup/internal/attributes"
24 )
25
26 var _ AttributesOptionsSliceProvider = (*attributes.AttributesHolder)(nil)
27
28 type AttributesProvider interface {
29 Attributes() map[string]any
30 }
31
32 type LinkContext interface {
33 Page() any
34 Destination() string
35 Title() string
36 Text() hstring.RenderedString
37 PlainText() string
38 }
39
40 type CodeblockContext interface {
41 AttributesProvider
42 text.Positioner
43 Options() map[string]any
44 Type() string
45 Inner() string
46 Ordinal() int
47 Page() any
48 }
49
50 type AttributesOptionsSliceProvider interface {
51 AttributesSlice() []attributes.Attribute
52 OptionsSlice() []attributes.Attribute
53 }
54
55 type LinkRenderer interface {
56 RenderLink(w io.Writer, ctx LinkContext) error
57 identity.Provider
58 }
59
60 type CodeBlockRenderer interface {
61 RenderCodeblock(w hugio.FlexiWriter, ctx CodeblockContext) error
62 identity.Provider
63 }
64
65 type IsDefaultCodeBlockRendererProvider interface {
66 IsDefaultCodeBlockRenderer() bool
67 }
68
69 // HeadingContext contains accessors to all attributes that a HeadingRenderer
70 // can use to render a heading.
71 type HeadingContext interface {
72 // Page is the page containing the heading.
73 Page() any
74 // Level is the level of the header (i.e. 1 for top-level, 2 for sub-level, etc.).
75 Level() int
76 // Anchor is the HTML id assigned to the heading.
77 Anchor() string
78 // Text is the rendered (HTML) heading text, excluding the heading marker.
79 Text() hstring.RenderedString
80 // PlainText is the unrendered version of Text.
81 PlainText() string
82
83 // Attributes (e.g. CSS classes)
84 AttributesProvider
85 }
86
87 // HeadingRenderer describes a uniquely identifiable rendering hook.
88 type HeadingRenderer interface {
89 // Render writes the rendered content to w using the data in w.
90 RenderHeading(w io.Writer, ctx HeadingContext) error
91 identity.Provider
92 }
93
94 // ElementPositionResolver provides a way to resolve the start Position
95 // of a markdown element in the original source document.
96 // This may be both slow and approximate, so should only be
97 // used for error logging.
98 type ElementPositionResolver interface {
99 ResolvePosition(ctx any) text.Position
100 }
101
102 type RendererType int
103
104 const (
105 LinkRendererType RendererType = iota + 1
106 ImageRendererType
107 HeadingRendererType
108 CodeBlockRendererType
109 )
110
111 type GetRendererFunc func(t RendererType, id any) any