syntax

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAvailableLanguages

func GetAvailableLanguages(aliases bool) []string

GetAvailableLanguages returns a list of all supported languages

func GetAvailableStyles

func GetAvailableStyles() []string

GetAvailableStyles returns a list of all available syntax highlighting styles

func GetLanguageByAlias

func GetLanguageByAlias(alias string) string

GetLanguageByAlias returns the canonical language name for a given alias

func GetLanguageInfo

func GetLanguageInfo() map[string][]string

GetLanguageInfo returns a map of language names to their aliases

func PrintTokens

func PrintTokens(code string) error

PrintTokens prints all tokens for debugging

Types

type HighlightOptions

type HighlightOptions struct {
	Style            string // Name of the chroma style to use
	Language         string // Optional: Language to use for highlighting (e.g., "go", "python")
	TabWidth         int    // Number of spaces per tab
	ShowLineNums     bool   // Whether to show line numbers
	HighlightedLines []int  // Lines that should be highlighted
	ShowPrompt       bool   // Whether to show a terminal prompt
	PromptCommand    string // The command to show in the prompt
	UseANSI          bool   // If true, parse ANSI escape sequences instead of using Chroma
}

HighlightOptions contains options for syntax highlighting

func DefaultOptions

func DefaultOptions() *HighlightOptions

DefaultOptions returns the default highlight options

type HighlightedCode

type HighlightedCode struct {
	Lines           []Line      // The lines of code with their tokens
	BackgroundColor color.Color // Background color for the code block
	GutterColor     color.Color // Color for the gutter (line numbers background)
	LineNumberColor color.Color // Color for line numbers
	HighlightColor  color.Color // Color for highlighted lines

	HighlightedLines []int // Lines that should be highlighted
}

HighlightedCode represents syntax highlighted code ready for rendering

func Highlight

func Highlight(code string, opts *HighlightOptions) (*HighlightedCode, error)

Highlight performs syntax highlighting on the given code

func ParseANSI added in v0.6.0

func ParseANSI(text string, opts *HighlightOptions) (*HighlightedCode, error)

ParseANSI parses text with ANSI escape sequences and returns HighlightedCode

func (*HighlightedCode) RenderToImage

func (h *HighlightedCode) RenderToImage(config *RenderConfig) (image.Image, error)

RenderToImage converts highlighted code to an image

type Line

type Line struct {
	Tokens    []Token // The tokens in this line
	Highlight bool    // Whether this line should be highlighted
}

Line represents a single line of highlighted code

type RenderConfig

type RenderConfig struct {
	FontSize      float64
	LineHeight    float64
	PaddingLeft   int
	PaddingRight  int
	PaddingTop    int
	PaddingBottom int
	FontFace      font.Face
	Font          *truetype.Font
	Background    image.Image
	TabWidth      int // Width of tab characters in spaces
	MinWidth      int // Minimum width in pixels (0 means no minimum)
	MaxWidth      int // Maximum width in pixels (0 means no limit)

	// Line number settings
	ShowLineNumbers   bool
	LineNumberColor   color.Color
	LineNumberPadding int         // Padding on either side of line numbers in pixels
	LineNumberBg      color.Color // Background color for line numbers
	StartLineNumber   int         // Line number to start from
	EndLineNumber     int         // Line number to end at

	// Line highlighting settings
	LineHighlightColor color.Color // Color for highlighted lines
}

RenderConfig holds configuration for rendering highlighted code to an image

func DefaultConfig

func DefaultConfig() *RenderConfig

DefaultConfig returns a default rendering configuration

func (*RenderConfig) Clone

func (c *RenderConfig) Clone() *RenderConfig

Clone creates a deep copy of the RenderConfig

func (*RenderConfig) GetBackground

func (c *RenderConfig) GetBackground() image.Image

func (*RenderConfig) GetEndLineNumber

func (c *RenderConfig) GetEndLineNumber() int

func (*RenderConfig) GetFontFace added in v0.3.0

func (c *RenderConfig) GetFontFace() font.Face

GetFontFace returns the current font face

func (*RenderConfig) GetLineHeight

func (c *RenderConfig) GetLineHeight() float64

Getters and setters for RenderConfig

func (*RenderConfig) GetLineHighlightColor

func (c *RenderConfig) GetLineHighlightColor() color.Color

func (*RenderConfig) GetLineNumberBg

func (c *RenderConfig) GetLineNumberBg() color.Color

func (*RenderConfig) GetLineNumberColor

func (c *RenderConfig) GetLineNumberColor() color.Color

func (*RenderConfig) GetLineNumberPadding

func (c *RenderConfig) GetLineNumberPadding() int

func (*RenderConfig) GetMaxWidth

func (c *RenderConfig) GetMaxWidth() int

func (*RenderConfig) GetMinWidth

func (c *RenderConfig) GetMinWidth() int

func (*RenderConfig) GetMonospaceWidth

func (c *RenderConfig) GetMonospaceWidth(charCount int) int

GetMonospaceWidth calculates the width needed for a given number of characters

func (*RenderConfig) GetPaddingBottom added in v0.4.0

func (c *RenderConfig) GetPaddingBottom() int

func (*RenderConfig) GetPaddingLeft added in v0.4.0

func (c *RenderConfig) GetPaddingLeft() int

func (*RenderConfig) GetPaddingRight added in v0.4.0

func (c *RenderConfig) GetPaddingRight() int

func (*RenderConfig) GetPaddingTop added in v0.4.0

func (c *RenderConfig) GetPaddingTop() int

func (*RenderConfig) GetShowLineNumbers

func (c *RenderConfig) GetShowLineNumbers() bool

Line number settings

func (*RenderConfig) GetStartLineNumber

func (c *RenderConfig) GetStartLineNumber() int

func (*RenderConfig) GetTabWidth

func (c *RenderConfig) GetTabWidth() int

func (*RenderConfig) SetBackground

func (c *RenderConfig) SetBackground(bg image.Image) *RenderConfig

func (*RenderConfig) SetEndLineNumber

func (c *RenderConfig) SetEndLineNumber(line int) *RenderConfig

func (*RenderConfig) SetFontFace added in v0.3.0

func (c *RenderConfig) SetFontFace(face font.Face, f *truetype.Font, size float64) *RenderConfig

SetFontFace sets both the font face and underlying TrueType font

func (*RenderConfig) SetLineHeight

func (c *RenderConfig) SetLineHeight(height float64) *RenderConfig

func (*RenderConfig) SetLineHighlightColor

func (c *RenderConfig) SetLineHighlightColor(col color.Color) *RenderConfig

func (*RenderConfig) SetLineNumberBg

func (c *RenderConfig) SetLineNumberBg(bg color.Color) *RenderConfig

func (*RenderConfig) SetLineNumberColor

func (c *RenderConfig) SetLineNumberColor(col color.Color) *RenderConfig

func (*RenderConfig) SetLineNumberPadding

func (c *RenderConfig) SetLineNumberPadding(padding int) *RenderConfig

func (*RenderConfig) SetMaxWidth

func (c *RenderConfig) SetMaxWidth(width int) *RenderConfig

func (*RenderConfig) SetMinWidth

func (c *RenderConfig) SetMinWidth(width int) *RenderConfig

func (*RenderConfig) SetPaddingBottom added in v0.4.0

func (c *RenderConfig) SetPaddingBottom(padding int) *RenderConfig

func (*RenderConfig) SetPaddingLeft added in v0.4.0

func (c *RenderConfig) SetPaddingLeft(padding int) *RenderConfig

func (*RenderConfig) SetPaddingRight added in v0.4.0

func (c *RenderConfig) SetPaddingRight(padding int) *RenderConfig

func (*RenderConfig) SetPaddingTop added in v0.4.0

func (c *RenderConfig) SetPaddingTop(padding int) *RenderConfig

func (*RenderConfig) SetShowLineNumbers

func (c *RenderConfig) SetShowLineNumbers(show bool) *RenderConfig

func (*RenderConfig) SetStartLineNumber

func (c *RenderConfig) SetStartLineNumber(line int) *RenderConfig

func (*RenderConfig) SetTabWidth

func (c *RenderConfig) SetTabWidth(width int) *RenderConfig

func (*RenderConfig) WithFont

func (c *RenderConfig) WithFont(ttfData []byte) (*RenderConfig, error)

WithFont is a convenience method to set the font face from TTF data

type Style

type Style struct {
	Name  string
	Style *chroma.Style
}

Style represents a syntax highlighting style

type Token

type Token struct {
	Text   string      // The text content
	Color  color.Color // The color to render the token in
	Bold   bool        // Whether to render the token in bold
	Italic bool        // Whether to render the token in italic
}

Token represents a syntax highlighted token with its style information

Jump to

Keyboard shortcuts

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