converter.go (3733B)
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 converter
15
16 import (
17 "bytes"
18
19 "github.com/gohugoio/hugo/common/hexec"
20 "github.com/gohugoio/hugo/common/loggers"
21 "github.com/gohugoio/hugo/config"
22 "github.com/gohugoio/hugo/identity"
23 "github.com/gohugoio/hugo/markup/converter/hooks"
24 "github.com/gohugoio/hugo/markup/highlight"
25 "github.com/gohugoio/hugo/markup/markup_config"
26 "github.com/gohugoio/hugo/markup/tableofcontents"
27 "github.com/spf13/afero"
28 )
29
30 // ProviderConfig configures a new Provider.
31 type ProviderConfig struct {
32 MarkupConfig markup_config.Config
33
34 Cfg config.Provider // Site config
35 ContentFs afero.Fs
36 Logger loggers.Logger
37 Exec *hexec.Exec
38 highlight.Highlighter
39 }
40
41 // ProviderProvider creates converter providers.
42 type ProviderProvider interface {
43 New(cfg ProviderConfig) (Provider, error)
44 }
45
46 // Provider creates converters.
47 type Provider interface {
48 New(ctx DocumentContext) (Converter, error)
49 Name() string
50 }
51
52 // NewProvider creates a new Provider with the given name.
53 func NewProvider(name string, create func(ctx DocumentContext) (Converter, error)) Provider {
54 return newConverter{
55 name: name,
56 create: create,
57 }
58 }
59
60 type newConverter struct {
61 name string
62 create func(ctx DocumentContext) (Converter, error)
63 }
64
65 func (n newConverter) New(ctx DocumentContext) (Converter, error) {
66 return n.create(ctx)
67 }
68
69 func (n newConverter) Name() string {
70 return n.name
71 }
72
73 var NopConverter = new(nopConverter)
74
75 type nopConverter int
76
77 func (nopConverter) Convert(ctx RenderContext) (Result, error) {
78 return &bytes.Buffer{}, nil
79 }
80
81 func (nopConverter) Supports(feature identity.Identity) bool {
82 return false
83 }
84
85 // Converter wraps the Convert method that converts some markup into
86 // another format, e.g. Markdown to HTML.
87 type Converter interface {
88 Convert(ctx RenderContext) (Result, error)
89 Supports(feature identity.Identity) bool
90 }
91
92 // Result represents the minimum returned from Convert.
93 type Result interface {
94 Bytes() []byte
95 }
96
97 // DocumentInfo holds additional information provided by some converters.
98 type DocumentInfo interface {
99 AnchorSuffix() string
100 }
101
102 // TableOfContentsProvider provides the content as a ToC structure.
103 type TableOfContentsProvider interface {
104 TableOfContents() tableofcontents.Root
105 }
106
107 // AnchorNameSanitizer tells how a converter sanitizes anchor names.
108 type AnchorNameSanitizer interface {
109 SanitizeAnchorName(s string) string
110 }
111
112 // Bytes holds a byte slice and implements the Result interface.
113 type Bytes []byte
114
115 // Bytes returns itself
116 func (b Bytes) Bytes() []byte {
117 return b
118 }
119
120 // DocumentContext holds contextual information about the document to convert.
121 type DocumentContext struct {
122 Document any // May be nil. Usually a page.Page
123 DocumentID string
124 DocumentName string
125 Filename string
126 }
127
128 // RenderContext holds contextual information about the content to render.
129 type RenderContext struct {
130 // Src is the content to render.
131 Src []byte
132
133 // Whether to render TableOfContents.
134 RenderTOC bool
135
136 // GerRenderer provides hook renderers on demand.
137 GetRenderer hooks.GetRendererFunc
138 }
139
140 var FeatureRenderHooks = identity.NewPathIdentity("markup", "renderingHooks")