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