2019-08-16 13:55:03 +00:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
2020-02-18 13:00:58 +00:00
|
|
|
"bytes"
|
2023-02-25 08:24:59 +00:00
|
|
|
"context"
|
2020-02-18 13:00:58 +00:00
|
|
|
|
2021-12-12 11:11:11 +00:00
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
2019-08-16 13:55:03 +00:00
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
2019-11-27 12:42:36 +00:00
|
|
|
"github.com/gohugoio/hugo/identity"
|
|
|
|
"github.com/gohugoio/hugo/markup/converter/hooks"
|
2022-02-17 12:04:00 +00:00
|
|
|
"github.com/gohugoio/hugo/markup/highlight"
|
2019-11-06 19:10:47 +00:00
|
|
|
"github.com/gohugoio/hugo/markup/markup_config"
|
|
|
|
"github.com/gohugoio/hugo/markup/tableofcontents"
|
2019-08-16 13:55:03 +00:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProviderConfig configures a new Provider.
|
|
|
|
type ProviderConfig struct {
|
2023-01-04 17:24:36 +00:00
|
|
|
Conf config.AllProvider // Site config
|
2019-08-16 13:55:03 +00:00
|
|
|
ContentFs afero.Fs
|
2020-10-21 09:17:48 +00:00
|
|
|
Logger loggers.Logger
|
2021-12-12 11:11:11 +00:00
|
|
|
Exec *hexec.Exec
|
2022-02-17 12:04:00 +00:00
|
|
|
highlight.Highlighter
|
2019-08-16 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:24:36 +00:00
|
|
|
func (p ProviderConfig) MarkupConfig() markup_config.Config {
|
|
|
|
return p.Conf.GetConfigSection("markup").(markup_config.Config)
|
|
|
|
}
|
|
|
|
|
2019-11-06 19:10:47 +00:00
|
|
|
// ProviderProvider creates converter providers.
|
|
|
|
type ProviderProvider interface {
|
2019-08-16 13:55:03 +00:00
|
|
|
New(cfg ProviderConfig) (Provider, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provider creates converters.
|
|
|
|
type Provider interface {
|
|
|
|
New(ctx DocumentContext) (Converter, error)
|
2019-11-06 19:10:47 +00:00
|
|
|
Name() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewProvider creates a new Provider with the given name.
|
|
|
|
func NewProvider(name string, create func(ctx DocumentContext) (Converter, error)) Provider {
|
|
|
|
return newConverter{
|
|
|
|
name: name,
|
|
|
|
create: create,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type newConverter struct {
|
|
|
|
name string
|
|
|
|
create func(ctx DocumentContext) (Converter, error)
|
2019-08-16 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 19:10:47 +00:00
|
|
|
func (n newConverter) New(ctx DocumentContext) (Converter, error) {
|
|
|
|
return n.create(ctx)
|
|
|
|
}
|
2019-08-16 13:55:03 +00:00
|
|
|
|
2019-11-06 19:10:47 +00:00
|
|
|
func (n newConverter) Name() string {
|
|
|
|
return n.name
|
2019-08-16 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 13:00:58 +00:00
|
|
|
var NopConverter = new(nopConverter)
|
|
|
|
|
|
|
|
type nopConverter int
|
|
|
|
|
2023-02-24 06:23:10 +00:00
|
|
|
func (nopConverter) Convert(ctx RenderContext) (ResultRender, error) {
|
2020-02-18 13:00:58 +00:00
|
|
|
return &bytes.Buffer{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopConverter) Supports(feature identity.Identity) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-16 13:55:03 +00:00
|
|
|
// Converter wraps the Convert method that converts some markup into
|
|
|
|
// another format, e.g. Markdown to HTML.
|
|
|
|
type Converter interface {
|
2023-02-24 06:23:10 +00:00
|
|
|
Convert(ctx RenderContext) (ResultRender, error)
|
2019-11-27 12:42:36 +00:00
|
|
|
Supports(feature identity.Identity) bool
|
2019-08-16 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
2023-02-24 06:23:10 +00:00
|
|
|
// ParseRenderer is an optional interface.
|
|
|
|
// The Goldmark converter implements this, and this allows us
|
|
|
|
// to extract the ToC without having to render the content.
|
|
|
|
type ParseRenderer interface {
|
|
|
|
Parse(RenderContext) (ResultParse, error)
|
|
|
|
Render(RenderContext, any) (ResultRender, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResultRender represents the minimum returned from Convert and Render.
|
|
|
|
type ResultRender interface {
|
2019-08-16 13:55:03 +00:00
|
|
|
Bytes() []byte
|
|
|
|
}
|
|
|
|
|
2023-02-24 06:23:10 +00:00
|
|
|
// ResultParse represents the minimum returned from Parse.
|
|
|
|
type ResultParse interface {
|
|
|
|
Doc() any
|
|
|
|
TableOfContents() *tableofcontents.Fragments
|
|
|
|
}
|
|
|
|
|
2019-08-16 13:55:03 +00:00
|
|
|
// DocumentInfo holds additional information provided by some converters.
|
|
|
|
type DocumentInfo interface {
|
|
|
|
AnchorSuffix() string
|
|
|
|
}
|
|
|
|
|
2019-11-06 19:10:47 +00:00
|
|
|
// TableOfContentsProvider provides the content as a ToC structure.
|
|
|
|
type TableOfContentsProvider interface {
|
2023-02-11 15:20:24 +00:00
|
|
|
TableOfContents() *tableofcontents.Fragments
|
2019-11-06 19:10:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 10:28:19 +00:00
|
|
|
// AnchorNameSanitizer tells how a converter sanitizes anchor names.
|
|
|
|
type AnchorNameSanitizer interface {
|
|
|
|
SanitizeAnchorName(s string) string
|
|
|
|
}
|
|
|
|
|
2019-08-16 13:55:03 +00:00
|
|
|
// Bytes holds a byte slice and implements the Result interface.
|
|
|
|
type Bytes []byte
|
|
|
|
|
|
|
|
// Bytes returns itself
|
|
|
|
func (b Bytes) Bytes() []byte {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// DocumentContext holds contextual information about the document to convert.
|
|
|
|
type DocumentContext struct {
|
2022-05-28 09:01:47 +00:00
|
|
|
Document any // May be nil. Usually a page.Page
|
|
|
|
DocumentID string
|
|
|
|
DocumentName string
|
|
|
|
Filename string
|
2019-08-16 13:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenderContext holds contextual information about the content to render.
|
|
|
|
type RenderContext struct {
|
2023-02-25 08:24:59 +00:00
|
|
|
// Ctx is the context.Context for the current Page render.
|
|
|
|
Ctx context.Context
|
|
|
|
|
2022-02-26 11:52:06 +00:00
|
|
|
// Src is the content to render.
|
|
|
|
Src []byte
|
|
|
|
|
|
|
|
// Whether to render TableOfContents.
|
2022-02-17 12:04:00 +00:00
|
|
|
RenderTOC bool
|
|
|
|
|
2022-02-26 11:52:06 +00:00
|
|
|
// GerRenderer provides hook renderers on demand.
|
2022-02-17 12:04:00 +00:00
|
|
|
GetRenderer hooks.GetRendererFunc
|
2019-08-16 13:55:03 +00:00
|
|
|
}
|
2019-11-27 12:42:36 +00:00
|
|
|
|
2020-12-02 12:23:25 +00:00
|
|
|
var FeatureRenderHooks = identity.NewPathIdentity("markup", "renderingHooks")
|