github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/markup/converter/converter.go (about)

     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 // Site 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  	Supports(feature identity.Identity) bool
    93  }
    94  
    95  // ParseRenderer is an optional interface.
    96  // The Goldmark converter implements this, and this allows us
    97  // to extract the ToC without having to render the content.
    98  type ParseRenderer interface {
    99  	Parse(RenderContext) (ResultParse, error)
   100  	Render(RenderContext, any) (ResultRender, error)
   101  }
   102  
   103  // ResultRender represents the minimum returned from Convert and Render.
   104  type ResultRender interface {
   105  	Bytes() []byte
   106  }
   107  
   108  // ResultParse represents the minimum returned from Parse.
   109  type ResultParse interface {
   110  	Doc() any
   111  	TableOfContents() *tableofcontents.Fragments
   112  }
   113  
   114  // DocumentInfo holds additional information provided by some converters.
   115  type DocumentInfo interface {
   116  	AnchorSuffix() string
   117  }
   118  
   119  // TableOfContentsProvider provides the content as a ToC structure.
   120  type TableOfContentsProvider interface {
   121  	TableOfContents() *tableofcontents.Fragments
   122  }
   123  
   124  // AnchorNameSanitizer tells how a converter sanitizes anchor names.
   125  type AnchorNameSanitizer interface {
   126  	SanitizeAnchorName(s string) string
   127  }
   128  
   129  // Bytes holds a byte slice and implements the Result interface.
   130  type Bytes []byte
   131  
   132  // Bytes returns itself
   133  func (b Bytes) Bytes() []byte {
   134  	return b
   135  }
   136  
   137  // DocumentContext holds contextual information about the document to convert.
   138  type DocumentContext struct {
   139  	Document     any // May be nil. Usually a page.Page
   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  	// Whether to render TableOfContents.
   154  	RenderTOC bool
   155  
   156  	// GerRenderer provides hook renderers on demand.
   157  	GetRenderer hooks.GetRendererFunc
   158  }
   159  
   160  var FeatureRenderHooks = identity.NewPathIdentity("markup", "renderingHooks")