URI: 
       hooks.go - hugo - [fork] hugo port for 9front
  HTML git clone https://git.drkhsh.at/hugo.git
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
   DIR README
   DIR LICENSE
       ---
       hooks.go (6561B)
       ---
            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         "context"
           18         "io"
           19 
           20         "github.com/gohugoio/hugo/common/hugio"
           21         "github.com/gohugoio/hugo/common/text"
           22         "github.com/gohugoio/hugo/common/types/hstring"
           23         "github.com/gohugoio/hugo/markup/internal/attributes"
           24 )
           25 
           26 var _ AttributesOptionsSliceProvider = (*attributes.AttributesHolder)(nil)
           27 
           28 type AttributesProvider interface {
           29         // Attributes passed in from Markdown (e.g. { attrName1=attrValue1 attrName2="attr Value 2" }).
           30         Attributes() map[string]any
           31 }
           32 
           33 // LinkContext is the context passed to a link render hook.
           34 type LinkContext interface {
           35         PageProvider
           36 
           37         // The link URL.
           38         Destination() string
           39 
           40         // The link title attribute.
           41         Title() string
           42 
           43         // The rendered (HTML) text.
           44         Text() hstring.HTML
           45 
           46         // The plain variant of Text.
           47         PlainText() string
           48 }
           49 
           50 // ImageLinkContext is the context passed to a image link render hook.
           51 type ImageLinkContext interface {
           52         LinkContext
           53 
           54         // Returns true if this is a standalone image and the config option
           55         // markup.goldmark.parser.wrapStandAloneImageWithinParagraph is disabled.
           56         IsBlock() bool
           57 
           58         // Zero-based ordinal for all the images in the current document.
           59         Ordinal() int
           60 }
           61 
           62 // CodeblockContext is the context passed to a code block render hook.
           63 type CodeblockContext interface {
           64         BaseContext
           65         AttributesProvider
           66 
           67         // Chroma highlighting processing options. This will only be filled if Type is a known Chroma Lexer.
           68         Options() map[string]any
           69 
           70         // The type of code block. This will be the programming language, e.g. bash, when doing code highlighting.
           71         Type() string
           72 
           73         // The text between the code fences.
           74         Inner() string
           75 }
           76 
           77 // TableContext is the context passed to a table render hook.
           78 type TableContext interface {
           79         BaseContext
           80         AttributesProvider
           81 
           82         THead() []TableRow
           83         TBody() []TableRow
           84 }
           85 
           86 // BaseContext is the base context used in most render hooks.
           87 type BaseContext interface {
           88         text.Positioner
           89         PageProvider
           90 
           91         // Zero-based ordinal for all elements of this kind in the current document.
           92         Ordinal() int
           93 }
           94 
           95 // BlockquoteContext is the context passed to a blockquote render hook.
           96 type BlockquoteContext interface {
           97         BaseContext
           98 
           99         AttributesProvider
          100 
          101         // The blockquote text.
          102         // If type is "alert", this will be the alert text.
          103         Text() hstring.HTML
          104 
          105         /// Returns the blockquote type, one of "regular" and "alert".
          106         // Type "alert" indicates that this is a GitHub type alert.
          107         Type() string
          108 
          109         // The GitHub alert type converted to lowercase, e.g. "note".
          110         // Only set if Type is "alert".
          111         AlertType() string
          112 
          113         // The alert title.
          114         // Currently only relevant for Obsidian alerts.
          115         // GitHub does not suport alert titles and will not render alerts with titles.
          116         AlertTitle() hstring.HTML
          117 
          118         // The alert sign,  "+" or "-" or "" used to indicate folding.
          119         // Currently only relevant for Obsidian alerts.
          120         // GitHub does not suport alert signs and will not render alerts with signs.
          121         AlertSign() string
          122 }
          123 
          124 type PositionerSourceTargetProvider interface {
          125         // For internal use.
          126         PositionerSourceTarget() []byte
          127 }
          128 
          129 // PassThroughContext is the context passed to a passthrough render hook.
          130 type PassthroughContext interface {
          131         BaseContext
          132         AttributesProvider
          133 
          134         // Currently one of "inline" or "block".
          135         Type() string
          136 
          137         // The inner content of the passthrough element, excluding the delimiters.
          138         Inner() string
          139 }
          140 
          141 type AttributesOptionsSliceProvider interface {
          142         AttributesSlice() []attributes.Attribute
          143         OptionsSlice() []attributes.Attribute
          144 }
          145 
          146 type LinkRenderer interface {
          147         RenderLink(cctx context.Context, w io.Writer, ctx LinkContext) error
          148 }
          149 
          150 type CodeBlockRenderer interface {
          151         RenderCodeblock(cctx context.Context, w hugio.FlexiWriter, ctx CodeblockContext) error
          152 }
          153 
          154 type BlockquoteRenderer interface {
          155         RenderBlockquote(cctx context.Context, w hugio.FlexiWriter, ctx BlockquoteContext) error
          156 }
          157 
          158 type TableRenderer interface {
          159         RenderTable(cctx context.Context, w hugio.FlexiWriter, ctx TableContext) error
          160 }
          161 
          162 type PassthroughRenderer interface {
          163         RenderPassthrough(cctx context.Context, w io.Writer, ctx PassthroughContext) error
          164 }
          165 
          166 type IsDefaultCodeBlockRendererProvider interface {
          167         IsDefaultCodeBlockRenderer() bool
          168 }
          169 
          170 // HeadingContext contains accessors to all attributes that a HeadingRenderer
          171 // can use to render a heading.
          172 type HeadingContext interface {
          173         PageProvider
          174         // Level is the level of the header (i.e. 1 for top-level, 2 for sub-level, etc.).
          175         Level() int
          176         // Anchor is the HTML id assigned to the heading.
          177         Anchor() string
          178         // Text is the rendered (HTML) heading text, excluding the heading marker.
          179         Text() hstring.HTML
          180         // PlainText is the unrendered version of Text.
          181         PlainText() string
          182 
          183         // Attributes (e.g. CSS classes)
          184         AttributesProvider
          185 }
          186 
          187 type PageProvider interface {
          188         // Page is the page being rendered.
          189         Page() any
          190 
          191         // PageInner may be different than Page when .RenderShortcodes is in play.
          192         // The main use case for this is to include other pages' markdown into the current page
          193         // but resolve resources and pages relative to the original.
          194         PageInner() any
          195 }
          196 
          197 // HeadingRenderer describes a uniquely identifiable rendering hook.
          198 type HeadingRenderer interface {
          199         // RenderHeading writes the rendered content to w using the data in w.
          200         RenderHeading(cctx context.Context, w io.Writer, ctx HeadingContext) error
          201 }
          202 
          203 // ElementPositionResolver provides a way to resolve the start Position
          204 // of a markdown element in the original source document.
          205 // This may be both slow and approximate, so should only be
          206 // used for error logging.
          207 type ElementPositionResolver interface {
          208         ResolvePosition(ctx any) text.Position
          209 }
          210 
          211 type RendererType int
          212 
          213 const (
          214         LinkRendererType RendererType = iota + 1
          215         ImageRendererType
          216         HeadingRendererType
          217         CodeBlockRendererType
          218         PassthroughRendererType
          219         BlockquoteRendererType
          220         TableRendererType
          221 )
          222 
          223 type GetRendererFunc func(t RendererType, id any) any
          224 
          225 type TableCell struct {
          226         Text      hstring.HTML
          227         Alignment string // left, center, or right
          228 }
          229 
          230 type TableRow []TableCell
          231 
          232 type Table struct {
          233         THead []TableRow
          234         TBody []TableRow
          235 }