ansi

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 26 Imported by: 45

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TemplateFuncMap = template.FuncMap{
		"Left": func(values ...interface{}) string {
			s := values[0].(string)
			n := values[1].(int)
			if n > len(s) {
				n = len(s)
			}

			return s[:n]
		},
		"Matches": func(values ...interface{}) bool {
			ok, _ := regexp.MatchString(values[1].(string), values[0].(string))
			return ok
		},
		"Mid": func(values ...interface{}) string {
			s := values[0].(string)
			l := values[1].(int)
			if l > len(s) {
				l = len(s)
			}

			if len(values) > 2 {
				r := values[2].(int)
				if r > len(s) {
					r = len(s)
				}
				return s[l:r]
			}
			return s[l:]
		},
		"Right": func(values ...interface{}) string {
			s := values[0].(string)
			n := len(s) - values[1].(int)
			if n < 0 {
				n = 0
			}

			return s[n:]
		},
		"Last": func(values ...interface{}) string {
			return values[0].([]string)[len(values[0].([]string))-1]
		},

		"Compare":      strings.Compare,
		"Contains":     strings.Contains,
		"ContainsAny":  strings.ContainsAny,
		"Count":        strings.Count,
		"EqualFold":    strings.EqualFold,
		"HasPrefix":    strings.HasPrefix,
		"HasSuffix":    strings.HasSuffix,
		"Index":        strings.Index,
		"IndexAny":     strings.IndexAny,
		"Join":         strings.Join,
		"LastIndex":    strings.LastIndex,
		"LastIndexAny": strings.LastIndexAny,
		"Repeat":       strings.Repeat,
		"Replace":      strings.Replace,
		"Split":        strings.Split,
		"SplitAfter":   strings.SplitAfter,
		"SplitAfterN":  strings.SplitAfterN,
		"SplitN":       strings.SplitN,
		"Title":        strings.Title,
		"ToLower":      strings.ToLower,
		"ToTitle":      strings.ToTitle,
		"ToUpper":      strings.ToUpper,
		"Trim":         strings.Trim,
		"TrimLeft":     strings.TrimLeft,
		"TrimPrefix":   strings.TrimPrefix,
		"TrimRight":    strings.TrimRight,
		"TrimSpace":    strings.TrimSpace,
		"TrimSuffix":   strings.TrimSuffix,
	}
)

TemplateFuncMap contains a few useful template helpers.

Functions

This section is empty.

Types

type ANSIRenderer

type ANSIRenderer struct {
	// contains filtered or unexported fields
}

ANSIRenderer renders markdown content as ANSI escaped sequences.

func NewRenderer

func NewRenderer(options Options) *ANSIRenderer

NewRenderer returns a new ANSIRenderer with style and options set.

func (*ANSIRenderer) NewElement

func (tr *ANSIRenderer) NewElement(node ast.Node, source []byte) Element

NewElement returns the appropriate render Element for a given node.

func (*ANSIRenderer) RegisterFuncs

func (r *ANSIRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer)

RegisterFuncs implements NodeRenderer.RegisterFuncs.

type BaseElement

type BaseElement struct {
	Token  string
	Prefix string
	Suffix string
	Style  StylePrimitive
}

BaseElement renders a styled primitive element.

func (*BaseElement) Render

func (e *BaseElement) Render(w io.Writer, ctx RenderContext) error

func (*BaseElement) StyleOverrideRender added in v0.8.0

func (e *BaseElement) StyleOverrideRender(w io.Writer, ctx RenderContext, style StylePrimitive) error

type BlockElement

type BlockElement struct {
	Block   *bytes.Buffer
	Style   StyleBlock
	Margin  bool
	Newline bool
}

BlockElement provides a render buffer for children of a block element. After all children have been rendered into it, it applies indentation and margins around them and writes everything to the parent rendering buffer.

func (*BlockElement) Finish

func (e *BlockElement) Finish(w io.Writer, ctx RenderContext) error

func (*BlockElement) Render

func (e *BlockElement) Render(w io.Writer, ctx RenderContext) error

type BlockStack

type BlockStack []BlockElement

BlockStack is a stack of block elements, used to calculate the current indentation & margin level during the rendering process.

func (BlockStack) Current

func (s BlockStack) Current() BlockElement

Current returns the current BlockElement.

func (BlockStack) Indent

func (s BlockStack) Indent() uint

Indent returns the current indentation level of all elements in the stack.

func (*BlockStack) Len

func (s *BlockStack) Len() int

Len returns the length of the stack.

func (BlockStack) Margin

func (s BlockStack) Margin() uint

Margin returns the current margin level of all elements in the stack.

func (BlockStack) Parent

func (s BlockStack) Parent() BlockElement

Parent returns the current BlockElement's parent.

func (*BlockStack) Pop

func (s *BlockStack) Pop()

Pop removes the last item on the stack.

func (*BlockStack) Push

func (s *BlockStack) Push(e BlockElement)

Push appends an item to the stack.

func (BlockStack) Width

func (s BlockStack) Width(ctx RenderContext) uint

Width returns the available rendering width.

func (BlockStack) With

With returns a StylePrimitive that inherits the current BlockElement's style.

type Chroma

type Chroma struct {
	Text                StylePrimitive `json:"text,omitempty"`
	Error               StylePrimitive `json:"error,omitempty"`
	Comment             StylePrimitive `json:"comment,omitempty"`
	CommentPreproc      StylePrimitive `json:"comment_preproc,omitempty"`
	Keyword             StylePrimitive `json:"keyword,omitempty"`
	KeywordReserved     StylePrimitive `json:"keyword_reserved,omitempty"`
	KeywordNamespace    StylePrimitive `json:"keyword_namespace,omitempty"`
	KeywordType         StylePrimitive `json:"keyword_type,omitempty"`
	Operator            StylePrimitive `json:"operator,omitempty"`
	Punctuation         StylePrimitive `json:"punctuation,omitempty"`
	Name                StylePrimitive `json:"name,omitempty"`
	NameBuiltin         StylePrimitive `json:"name_builtin,omitempty"`
	NameTag             StylePrimitive `json:"name_tag,omitempty"`
	NameAttribute       StylePrimitive `json:"name_attribute,omitempty"`
	NameClass           StylePrimitive `json:"name_class,omitempty"`
	NameConstant        StylePrimitive `json:"name_constant,omitempty"`
	NameDecorator       StylePrimitive `json:"name_decorator,omitempty"`
	NameException       StylePrimitive `json:"name_exception,omitempty"`
	NameFunction        StylePrimitive `json:"name_function,omitempty"`
	NameOther           StylePrimitive `json:"name_other,omitempty"`
	Literal             StylePrimitive `json:"literal,omitempty"`
	LiteralNumber       StylePrimitive `json:"literal_number,omitempty"`
	LiteralDate         StylePrimitive `json:"literal_date,omitempty"`
	LiteralString       StylePrimitive `json:"literal_string,omitempty"`
	LiteralStringEscape StylePrimitive `json:"literal_string_escape,omitempty"`
	GenericDeleted      StylePrimitive `json:"generic_deleted,omitempty"`
	GenericEmph         StylePrimitive `json:"generic_emph,omitempty"`
	GenericInserted     StylePrimitive `json:"generic_inserted,omitempty"`
	GenericStrong       StylePrimitive `json:"generic_strong,omitempty"`
	GenericSubheading   StylePrimitive `json:"generic_subheading,omitempty"`
	Background          StylePrimitive `json:"background,omitempty"`
}

Chroma holds all the chroma settings.

type CodeBlockElement

type CodeBlockElement struct {
	Code     string
	Language string
}

A CodeBlockElement is used to render code blocks.

func (*CodeBlockElement) Render

func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error

type CodeSpanElement added in v0.8.0

type CodeSpanElement struct {
	Text  string
	Style StylePrimitive
}

A CodeSpanElement is used to render codespan.

func (*CodeSpanElement) Render added in v0.8.0

func (e *CodeSpanElement) Render(w io.Writer, ctx RenderContext) error

type Element

type Element struct {
	Entering string
	Exiting  string
	Renderer ElementRenderer
	Finisher ElementFinisher
}

An Element is used to instruct the renderer how to handle individual markdown nodes.

type ElementFinisher

type ElementFinisher interface {
	Finish(w io.Writer, ctx RenderContext) error
}

ElementFinisher is called when leaving a markdown node.

type ElementRenderer

type ElementRenderer interface {
	Render(w io.Writer, ctx RenderContext) error
}

ElementRenderer is called when entering a markdown node.

type EmphasisElement added in v0.8.0

type EmphasisElement struct {
	Children []ElementRenderer
	Level    int
}

A EmphasisElement is used to render emphasis.

func (*EmphasisElement) Render added in v0.8.0

func (e *EmphasisElement) Render(w io.Writer, ctx RenderContext) error

func (*EmphasisElement) StyleOverrideRender added in v0.8.0

func (e *EmphasisElement) StyleOverrideRender(w io.Writer, ctx RenderContext, style StylePrimitive) error

type HeadingElement

type HeadingElement struct {
	Level int
	First bool
}

A HeadingElement is used to render headings.

func (*HeadingElement) Finish

func (e *HeadingElement) Finish(w io.Writer, ctx RenderContext) error

func (*HeadingElement) Render

func (e *HeadingElement) Render(w io.Writer, ctx RenderContext) error

type ImageElement

type ImageElement struct {
	Text    string
	BaseURL string
	URL     string
	Child   ElementRenderer
}

An ImageElement is used to render images elements.

func (*ImageElement) Render

func (e *ImageElement) Render(w io.Writer, ctx RenderContext) error

type ItemElement

type ItemElement struct {
	IsOrdered   bool
	Enumeration uint
}

An ItemElement is used to render items inside a list.

func (*ItemElement) Render

func (e *ItemElement) Render(w io.Writer, ctx RenderContext) error

type LinkElement

type LinkElement struct {
	BaseURL  string
	URL      string
	Children []ElementRenderer
}

A LinkElement is used to render hyperlinks.

func (*LinkElement) Render

func (e *LinkElement) Render(w io.Writer, ctx RenderContext) error

type MarginWriter

type MarginWriter struct {
	// contains filtered or unexported fields
}

MarginWriter is a Writer that applies indentation and padding around whatever you write to it.

func NewMarginWriter

func NewMarginWriter(ctx RenderContext, w io.Writer, rules StyleBlock) *MarginWriter

NewMarginWriter returns a new MarginWriter.

func (*MarginWriter) Write

func (w *MarginWriter) Write(b []byte) (int, error)

type Options

type Options struct {
	BaseURL          string
	WordWrap         int
	PreserveNewLines bool
	ColorProfile     termenv.Profile
	Styles           StyleConfig
}

Options is used to configure an ANSIRenderer.

type ParagraphElement

type ParagraphElement struct {
	First bool
}

A ParagraphElement is used to render individual paragraphs.

func (*ParagraphElement) Finish

func (e *ParagraphElement) Finish(w io.Writer, ctx RenderContext) error

func (*ParagraphElement) Render

func (e *ParagraphElement) Render(w io.Writer, ctx RenderContext) error

type RenderContext

type RenderContext struct {
	// contains filtered or unexported fields
}

RenderContext holds the current rendering options and state.

func NewRenderContext

func NewRenderContext(options Options) RenderContext

NewRenderContext returns a new RenderContext.

func (RenderContext) SanitizeHTML

func (ctx RenderContext) SanitizeHTML(s string, trimSpaces bool) string

SanitizeHTML sanitizes HTML content.

type StyleBlock

type StyleBlock struct {
	StylePrimitive
	Indent      *uint   `json:"indent,omitempty"`
	IndentToken *string `json:"indent_token,omitempty"`
	Margin      *uint   `json:"margin,omitempty"`
}

StyleBlock holds the basic style settings for block elements.

type StyleCodeBlock

type StyleCodeBlock struct {
	StyleBlock
	Theme  string  `json:"theme,omitempty"`
	Chroma *Chroma `json:"chroma,omitempty"`
}

StyleCodeBlock holds the style settings for a code block.

type StyleConfig

type StyleConfig struct {
	Document   StyleBlock `json:"document,omitempty"`
	BlockQuote StyleBlock `json:"block_quote,omitempty"`
	Paragraph  StyleBlock `json:"paragraph,omitempty"`
	List       StyleList  `json:"list,omitempty"`

	Heading StyleBlock `json:"heading,omitempty"`
	H1      StyleBlock `json:"h1,omitempty"`
	H2      StyleBlock `json:"h2,omitempty"`
	H3      StyleBlock `json:"h3,omitempty"`
	H4      StyleBlock `json:"h4,omitempty"`
	H5      StyleBlock `json:"h5,omitempty"`
	H6      StyleBlock `json:"h6,omitempty"`

	Text           StylePrimitive `json:"text,omitempty"`
	Strikethrough  StylePrimitive `json:"strikethrough,omitempty"`
	Emph           StylePrimitive `json:"emph,omitempty"`
	Strong         StylePrimitive `json:"strong,omitempty"`
	HorizontalRule StylePrimitive `json:"hr,omitempty"`

	Item        StylePrimitive `json:"item,omitempty"`
	Enumeration StylePrimitive `json:"enumeration,omitempty"`
	Task        StyleTask      `json:"task,omitempty"`

	Link     StylePrimitive `json:"link,omitempty"`
	LinkText StylePrimitive `json:"link_text,omitempty"`

	Image     StylePrimitive `json:"image,omitempty"`
	ImageText StylePrimitive `json:"image_text,omitempty"`

	Code      StyleBlock     `json:"code,omitempty"`
	CodeBlock StyleCodeBlock `json:"code_block,omitempty"`

	Table StyleTable `json:"table,omitempty"`

	DefinitionList        StyleBlock     `json:"definition_list,omitempty"`
	DefinitionTerm        StylePrimitive `json:"definition_term,omitempty"`
	DefinitionDescription StylePrimitive `json:"definition_description,omitempty"`

	HTMLBlock StyleBlock `json:"html_block,omitempty"`
	HTMLSpan  StyleBlock `json:"html_span,omitempty"`
}

StyleConfig is used to configure the styling behavior of an ANSIRenderer.

type StyleList

type StyleList struct {
	StyleBlock
	LevelIndent uint `json:"level_indent,omitempty"`
}

StyleList holds the style settings for a list.

type StyleOverriderElementRenderer added in v0.8.0

type StyleOverriderElementRenderer interface {
	StyleOverrideRender(w io.Writer, ctx RenderContext, style StylePrimitive) error
}

type StylePrimitive

type StylePrimitive struct {
	BlockPrefix     string  `json:"block_prefix,omitempty"`
	BlockSuffix     string  `json:"block_suffix,omitempty"`
	Prefix          string  `json:"prefix,omitempty"`
	Suffix          string  `json:"suffix,omitempty"`
	Color           *string `json:"color,omitempty"`
	BackgroundColor *string `json:"background_color,omitempty"`
	Underline       *bool   `json:"underline,omitempty"`
	Bold            *bool   `json:"bold,omitempty"`
	Upper           *bool   `json:"upper,omitempty"`
	Lower           *bool   `json:"lower,omitempty"`
	Title           *bool   `json:"title,omitempty"`
	Italic          *bool   `json:"italic,omitempty"`
	CrossedOut      *bool   `json:"crossed_out,omitempty"`
	Faint           *bool   `json:"faint,omitempty"`
	Conceal         *bool   `json:"conceal,omitempty"`
	Overlined       *bool   `json:"overlined,omitempty"`
	Inverse         *bool   `json:"inverse,omitempty"`
	Blink           *bool   `json:"blink,omitempty"`
	Format          string  `json:"format,omitempty"`
}

StylePrimitive holds all the basic style settings.

type StyleTable

type StyleTable struct {
	StyleBlock
	CenterSeparator *string `json:"center_separator,omitempty"`
	ColumnSeparator *string `json:"column_separator,omitempty"`
	RowSeparator    *string `json:"row_separator,omitempty"`
}

StyleTable holds the style settings for a table.

type StyleTask

type StyleTask struct {
	StylePrimitive
	Ticked   string `json:"ticked,omitempty"`
	Unticked string `json:"unticked,omitempty"`
}

StyleTask holds the style settings for a task item.

type TableCellElement

type TableCellElement struct {
	Children []ElementRenderer
	Head     bool
}

A TableCellElement is used to render a single cell in a row.

func (*TableCellElement) Render

func (e *TableCellElement) Render(_ io.Writer, ctx RenderContext) error

type TableElement

type TableElement struct {
	// contains filtered or unexported fields
}

A TableElement is used to render tables.

func (*TableElement) Finish

func (e *TableElement) Finish(_ io.Writer, ctx RenderContext) error

func (*TableElement) Render

func (e *TableElement) Render(w io.Writer, ctx RenderContext) error

type TableHeadElement

type TableHeadElement struct{}

A TableHeadElement is used to render a table's head element.

func (*TableHeadElement) Finish

func (e *TableHeadElement) Finish(_ io.Writer, ctx RenderContext) error

type TableRowElement

type TableRowElement struct{}

A TableRowElement is used to render a single row in a table.

func (*TableRowElement) Finish

func (e *TableRowElement) Finish(_ io.Writer, ctx RenderContext) error

type TaskElement

type TaskElement struct {
	Checked bool
}

A TaskElement is used to render tasks inside a todo-list.

func (*TaskElement) Render

func (e *TaskElement) Render(w io.Writer, ctx RenderContext) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL