github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/markup/goldmark/internal/render/context.go (about)

     1  // Copyright 2022 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 render
    15  
    16  import (
    17  	"bytes"
    18  	"math/bits"
    19  
    20  	"github.com/gohugoio/hugo/identity"
    21  	"github.com/gohugoio/hugo/markup/converter"
    22  )
    23  
    24  type BufWriter struct {
    25  	*bytes.Buffer
    26  }
    27  
    28  const maxInt = 1<<(bits.UintSize-1) - 1
    29  
    30  func (b *BufWriter) Available() int {
    31  	return maxInt
    32  }
    33  
    34  func (b *BufWriter) Buffered() int {
    35  	return b.Len()
    36  }
    37  
    38  func (b *BufWriter) Flush() error {
    39  	return nil
    40  }
    41  
    42  type Context struct {
    43  	*BufWriter
    44  	positions []int
    45  	ContextData
    46  }
    47  
    48  func (ctx *Context) PushPos(n int) {
    49  	ctx.positions = append(ctx.positions, n)
    50  }
    51  
    52  func (ctx *Context) PopPos() int {
    53  	i := len(ctx.positions) - 1
    54  	p := ctx.positions[i]
    55  	ctx.positions = ctx.positions[:i]
    56  	return p
    57  }
    58  
    59  type ContextData interface {
    60  	RenderContext() converter.RenderContext
    61  	DocumentContext() converter.DocumentContext
    62  	AddIdentity(id identity.Provider)
    63  }
    64  
    65  type RenderContextDataHolder struct {
    66  	Rctx converter.RenderContext
    67  	Dctx converter.DocumentContext
    68  	IDs  identity.Manager
    69  }
    70  
    71  func (ctx *RenderContextDataHolder) RenderContext() converter.RenderContext {
    72  	return ctx.Rctx
    73  }
    74  
    75  func (ctx *RenderContextDataHolder) DocumentContext() converter.DocumentContext {
    76  	return ctx.Dctx
    77  }
    78  
    79  func (ctx *RenderContextDataHolder) AddIdentity(id identity.Provider) {
    80  	ctx.IDs.Add(id)
    81  }