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