github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/markup/converter/hooks/hooks.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 hooks
    15  
    16  import (
    17  	"io"
    18  
    19  	"github.com/gohugoio/hugo/common/hugio"
    20  	"github.com/gohugoio/hugo/identity"
    21  	"github.com/gohugoio/hugo/markup/internal/attributes"
    22  )
    23  
    24  var _ AttributesOptionsSliceProvider = (*attributes.AttributesHolder)(nil)
    25  
    26  type AttributesProvider interface {
    27  	Attributes() map[string]interface{}
    28  }
    29  
    30  type LinkContext interface {
    31  	Page() interface{}
    32  	Destination() string
    33  	Title() string
    34  	Text() string
    35  	PlainText() string
    36  }
    37  
    38  type CodeblockContext interface {
    39  	AttributesProvider
    40  	Options() map[string]interface{}
    41  	Lang() string
    42  	Code() string
    43  	Ordinal() int
    44  	Page() interface{}
    45  }
    46  
    47  type AttributesOptionsSliceProvider interface {
    48  	AttributesSlice() []attributes.Attribute
    49  	OptionsSlice() []attributes.Attribute
    50  }
    51  
    52  type LinkRenderer interface {
    53  	RenderLink(w io.Writer, ctx LinkContext) error
    54  	identity.Provider
    55  }
    56  
    57  type CodeBlockRenderer interface {
    58  	RenderCodeblock(w hugio.FlexiWriter, ctx CodeblockContext) error
    59  	identity.Provider
    60  }
    61  
    62  // HeadingContext contains accessors to all attributes that a HeadingRenderer
    63  // can use to render a heading.
    64  type HeadingContext interface {
    65  	// Page is the page containing the heading.
    66  	Page() interface{}
    67  	// Level is the level of the header (i.e. 1 for top-level, 2 for sub-level, etc.).
    68  	Level() int
    69  	// Anchor is the HTML id assigned to the heading.
    70  	Anchor() string
    71  	// Text is the rendered (HTML) heading text, excluding the heading marker.
    72  	Text() string
    73  	// PlainText is the unrendered version of Text.
    74  	PlainText() string
    75  
    76  	// Attributes (e.g. CSS classes)
    77  	AttributesProvider
    78  }
    79  
    80  // HeadingRenderer describes a uniquely identifiable rendering hook.
    81  type HeadingRenderer interface {
    82  	// Render writes the rendered content to w using the data in w.
    83  	RenderHeading(w io.Writer, ctx HeadingContext) error
    84  	identity.Provider
    85  }
    86  
    87  type RendererType int
    88  
    89  const (
    90  	LinkRendererType RendererType = iota + 1
    91  	ImageRendererType
    92  	HeadingRendererType
    93  	CodeBlockRendererType
    94  )
    95  
    96  type GetRendererFunc func(t RendererType, id interface{}) interface{}