2019-08-16 09:55:03 -04: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 08:00:58 -05:00
|
|
|
"bytes"
|
2023-02-25 03:24:59 -05:00
|
|
|
"context"
|
2020-02-18 08:00:58 -05:00
|
|
|
|
2021-12-12 06:11:11 -05:00
|
|
|
"github.com/gohugoio/hugo/common/hexec"
|
2019-08-16 09:55:03 -04:00
|
|
|
"github.com/gohugoio/hugo/common/loggers"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
2019-11-27 07:42:36 -05:00
|
|
|
"github.com/gohugoio/hugo/identity"
|
|
|
|
"github.com/gohugoio/hugo/markup/converter/hooks"
|
2022-02-17 07:04:00 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/highlight"
|
2019-11-06 14:10:47 -05:00
|
|
|
"github.com/gohugoio/hugo/markup/markup_config"
|
|
|
|
"github.com/gohugoio/hugo/markup/tableofcontents"
|
2019-08-16 09:55:03 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ProviderConfig configures a new Provider.
|
|
|
|
type ProviderConfig struct {
|
2023-01-04 12:24:36 -05:00
|
|
|
Conf config.AllProvider // Site config
|
2019-08-16 09:55:03 -04:00
|
|
|
ContentFs afero.Fs
|
2020-10-21 05:17:48 -04:00
|
|
|
Logger loggers.Logger
|
2021-12-12 06:11:11 -05:00
|
|
|
Exec *hexec.Exec
|
2022-02-17 07:04:00 -05:00
|
|
|
highlight.Highlighter
|
2019-08-16 09:55:03 -04:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (p ProviderConfig) MarkupConfig() markup_config.Config {
|
|
|
|
return p.Conf.GetConfigSection("markup").(markup_config.Config)
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
// ProviderProvider creates converter providers.
|
|
|
|
type ProviderProvider interface {
|
2019-08-16 09:55:03 -04:00
|
|
|
New(cfg ProviderConfig) (Provider, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provider creates converters.
|
|
|
|
type Provider interface {
|
|
|
|
New(ctx DocumentContext) (Converter, error)
|
2019-11-06 14:10:47 -05: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 09:55:03 -04:00
|
|
|
}
|
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
func (n newConverter) New(ctx DocumentContext) (Converter, error) {
|
|
|
|
return n.create(ctx)
|
|
|
|
}
|
2019-08-16 09:55:03 -04:00
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
func (n newConverter) Name() string {
|
|
|
|
return n.name
|
2019-08-16 09:55:03 -04:00
|
|
|
}
|
|
|
|
|
2020-02-18 08:00:58 -05:00
|
|
|
var NopConverter = new(nopConverter)
|
|
|
|
|
|
|
|
type nopConverter int
|
|
|
|
|
2023-02-24 01:23:10 -05:00
|
|
|
func (nopConverter) Convert(ctx RenderContext) (ResultRender, error) {
|
2020-02-18 08:00:58 -05:00
|
|
|
return &bytes.Buffer{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (nopConverter) Supports(feature identity.Identity) bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:55:03 -04:00
|
|
|
// Converter wraps the Convert method that converts some markup into
|
|
|
|
// another format, e.g. Markdown to HTML.
|
|
|
|
type Converter interface {
|
2023-02-24 01:23:10 -05:00
|
|
|
Convert(ctx RenderContext) (ResultRender, error)
|
2019-08-16 09:55:03 -04:00
|
|
|
}
|
|
|
|
|
2023-02-24 01:23:10 -05: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 09:55:03 -04:00
|
|
|
Bytes() []byte
|
|
|
|
}
|
|
|
|
|
2023-02-24 01:23:10 -05:00
|
|
|
// ResultParse represents the minimum returned from Parse.
|
|
|
|
type ResultParse interface {
|
|
|
|
Doc() any
|
|
|
|
TableOfContents() *tableofcontents.Fragments
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:55:03 -04:00
|
|
|
// DocumentInfo holds additional information provided by some converters.
|
|
|
|
type DocumentInfo interface {
|
|
|
|
AnchorSuffix() string
|
|
|
|
}
|
|
|
|
|
2019-11-06 14:10:47 -05:00
|
|
|
// TableOfContentsProvider provides the content as a ToC structure.
|
|
|
|
type TableOfContentsProvider interface {
|
2023-02-11 10:20:24 -05:00
|
|
|
TableOfContents() *tableofcontents.Fragments
|
2019-11-06 14:10:47 -05:00
|
|
|
}
|
|
|
|
|
2020-01-04 05:28:19 -05:00
|
|
|
// AnchorNameSanitizer tells how a converter sanitizes anchor names.
|
|
|
|
type AnchorNameSanitizer interface {
|
|
|
|
SanitizeAnchorName(s string) string
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:55:03 -04: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 05:01:47 -04:00
|
|
|
Document any // May be nil. Usually a page.Page
|
|
|
|
DocumentID string
|
|
|
|
DocumentName string
|
|
|
|
Filename string
|
2019-08-16 09:55:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RenderContext holds contextual information about the content to render.
|
|
|
|
type RenderContext struct {
|
2023-02-25 03:24:59 -05:00
|
|
|
// Ctx is the context.Context for the current Page render.
|
|
|
|
Ctx context.Context
|
|
|
|
|
2022-02-26 06:52:06 -05:00
|
|
|
// Src is the content to render.
|
|
|
|
Src []byte
|
|
|
|
|
|
|
|
// Whether to render TableOfContents.
|
2022-02-17 07:04:00 -05:00
|
|
|
RenderTOC bool
|
|
|
|
|
2022-02-26 06:52:06 -05:00
|
|
|
// GerRenderer provides hook renderers on demand.
|
2022-02-17 07:04:00 -05:00
|
|
|
GetRenderer hooks.GetRendererFunc
|
2019-08-16 09:55:03 -04:00
|
|
|
}
|