okami.mom/glamour

git clone https://git.nkp.pet/okami.mom/glamour

nekomimi.petadd WithExtensions and WithElement so callers can parse and render their own nodes4046973

main
1.0 KiB48 linesraw
1package ansi
2
3import (
4	"html"
5	"strings"
6
7	"github.com/microcosm-cc/bluemonday"
8)
9
10// RenderContext holds the current rendering options and state.
11type RenderContext struct {
12	options Options
13
14	blockStack *BlockStack
15	table      *TableElement
16
17	stripper *bluemonday.Policy
18}
19
20// NewRenderContext returns a new RenderContext.
21func NewRenderContext(options Options) RenderContext {
22	return RenderContext{
23		options:    options,
24		blockStack: &BlockStack{},
25		table:      &TableElement{},
26		stripper:   bluemonday.StrictPolicy(),
27	}
28}
29
30// Width is the width left for content inside the current block.
31func (ctx RenderContext) Width() int {
32	return int(ctx.blockStack.Width(ctx))
33}
34
35// Styles is the style configuration being rendered with.
36func (ctx RenderContext) Styles() StyleConfig {
37	return ctx.options.Styles
38}
39
40// SanitizeHTML sanitizes HTML content.
41func (ctx RenderContext) SanitizeHTML(s string, trimSpaces bool) string {
42	s = ctx.stripper.Sanitize(s)
43	if trimSpaces {
44		s = strings.TrimSpace(s)
45	}
46
47	return html.UnescapeString(s)
48}