code

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultRedactionPatterns = []RedactionPattern{

	{regexp.MustCompile(`` +
		`(?i)` +
		`"` +
		`(?P<value>` +
		`(?:` +
		`sk_(?:live|test)_[\w]{24,}|` +
		`sk-[\w]{32,}|` +
		`gh[porsu]_[\w]{36,}|` +
		`AKIA[\w]{16}|` +
		`eyJ[\w-_=]+\.eyJ[\w-_=]+\.[\w-_.+/=]+` +
		`)` +
		`)` +
		`"`), "Known Secret Format"},

	{regexp.MustCompile(`` +
		`(?i)` +
		`(?:\w+)?://` +
		`[^:]+:` +
		`(?P<value>[^@\s]+)` +
		`@`), "URL Password"},

	{regexp.MustCompile(`` +
		`(?im)` +
		`(?:^|\s|"|,)` +
		`\s*` +
		`(?:")?` +
		`[\w]*` +
		`(?:` +
		`key|token|secret|pass(?:word)?|pwd|auth|cred` +
		`)` +
		`[\w]*` +
		`(?:")?\s*` +
		`(?:[:=]|:=)\s*` +
		`(?:"|\x60)` +
		`(?P<value>` +
		`(?:[^"\x60]|(?:\n|\\n)[^"\x60]*)*?` +
		`)` +
		`(?:"|\x60)`,
	), "Sensitive Variable"},
}

DefaultRedactionPatterns contains common patterns for sensitive information

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 LoadCustomThemes

func LoadCustomThemes() error

LoadCustomThemes loads all custom themes from the embedded themes directory

func PrintTokens

func PrintTokens(code string) error

PrintTokens prints all tokens for debugging

func ShouldRedact

func ShouldRedact(pos int, ranges []RedactionRange) bool

ShouldRedact returns true if the given position in the text should be redacted

Types

type CodeRenderer

type CodeRenderer struct {
	Code  string
	Style *CodeStyle
}

func DefaultRenderer

func DefaultRenderer(input string) *CodeRenderer

func NewRenderer

func NewRenderer(input string, style *CodeStyle) *CodeRenderer

func (*CodeRenderer) Render

func (r *CodeRenderer) Render() (image.Image, error)

func (*CodeRenderer) WithFont

func (r *CodeRenderer) WithFont(font *fonts.Font) *CodeRenderer

func (*CodeRenderer) WithFontName

func (r *CodeRenderer) WithFontName(name string, style *fonts.FontStyle) *CodeRenderer

func (*CodeRenderer) WithFontSize

func (r *CodeRenderer) WithFontSize(size float64) *CodeRenderer

func (*CodeRenderer) WithLanguage

func (r *CodeRenderer) WithLanguage(language string) *CodeRenderer

func (*CodeRenderer) WithLineHeight

func (r *CodeRenderer) WithLineHeight(height float64) *CodeRenderer

func (*CodeRenderer) WithLineHighlightRange

func (r *CodeRenderer) WithLineHighlightRange(start, end int) *CodeRenderer

func (*CodeRenderer) WithLineNumberPadding

func (r *CodeRenderer) WithLineNumberPadding(padding int) *CodeRenderer

func (*CodeRenderer) WithLineNumbers

func (r *CodeRenderer) WithLineNumbers(show bool) *CodeRenderer

func (*CodeRenderer) WithLineRange

func (r *CodeRenderer) WithLineRange(start, end int) *CodeRenderer

func (*CodeRenderer) WithManualRedaction

func (r *CodeRenderer) WithManualRedaction(x, y, width, height int) *CodeRenderer

func (*CodeRenderer) WithMaxWidth

func (r *CodeRenderer) WithMaxWidth(width int) *CodeRenderer

func (*CodeRenderer) WithMinWidth

func (r *CodeRenderer) WithMinWidth(width int) *CodeRenderer

func (*CodeRenderer) WithPadding

func (r *CodeRenderer) WithPadding(left, right, top, bottom int) *CodeRenderer

func (*CodeRenderer) WithRedactionBlurRadius

func (r *CodeRenderer) WithRedactionBlurRadius(radius float64) *CodeRenderer

func (*CodeRenderer) WithRedactionEnabled

func (r *CodeRenderer) WithRedactionEnabled(enabled bool) *CodeRenderer

func (*CodeRenderer) WithRedactionPattern

func (r *CodeRenderer) WithRedactionPattern(pattern string, name string) *CodeRenderer

func (*CodeRenderer) WithRedactionStyle

func (r *CodeRenderer) WithRedactionStyle(style RedactionStyle) *CodeRenderer

func (*CodeRenderer) WithStyle

func (r *CodeRenderer) WithStyle(style *CodeStyle) *CodeRenderer

func (*CodeRenderer) WithTabWidth

func (r *CodeRenderer) WithTabWidth(width int) *CodeRenderer

func (*CodeRenderer) WithTheme

func (r *CodeRenderer) WithTheme(theme string) *CodeRenderer

type CodeStyle

type CodeStyle struct {
	Theme               string              // The chroma syntax theme to use
	Language            string              // The language to highlight
	Font                *fonts.Font         // The font to use
	FontSize            float64             // The font size in points
	LineHeight          float64             // The line height multiplier
	PaddingLeft         int                 // Padding between the code and the left edge
	PaddingRight        int                 // Padding between the code and the right edge
	PaddingTop          int                 // Padding between the code and the top edge
	PaddingBottom       int                 // Padding between the code and the bottom edge
	LineNumberPadding   int                 // Padding between line numbers and code
	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)
	ShowLineNumbers     bool                // Whether to show line numbers
	LineRanges          []content.LineRange // Ranges of lines to render
	LineHighlightRanges []content.LineRange // Ranges of lines to highlight
	RedactionConfig     *RedactionConfig    // Redaction configuration
}

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
	CommentColor     color.Color // Color for comments
	HighlightedLines []int       // Lines that should be highlighted
}

HighlightedCode represents syntax highlighted code ready for rendering

func Highlight

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

Highlight performs syntax highlighting on the given code

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 RedactionArea

type RedactionArea struct {
	X      int
	Y      int
	Width  int
	Height int
}

RedactionArea represents an area to be redacted in the image

type RedactionConfig

type RedactionConfig struct {
	Enabled          bool
	Style            RedactionStyle
	Patterns         []RedactionPattern
	BlurRadius       float64
	ManualRedactions []RedactionArea
}

RedactionConfig holds configuration for the redaction feature

func NewRedactionConfig

func NewRedactionConfig() *RedactionConfig

NewRedactionConfig creates a new redaction configuration with default settings

func (*RedactionConfig) AddManualRedaction

func (rc *RedactionConfig) AddManualRedaction(x, y, width, height int)

AddManualRedaction adds a manual redaction area

type RedactionPattern

type RedactionPattern struct {
	Pattern *regexp.Regexp
	Name    string
}

RedactionPattern represents a pattern to match for redaction

type RedactionRange

type RedactionRange struct {
	StartIndex int
	EndIndex   int
	Pattern    string // Name of the pattern that matched
}

RedactionRange represents a range of text that should be redacted

func FindRedactionRanges

func FindRedactionRanges(config *RedactionConfig, text string) []RedactionRange

FindRedactionRanges analyzes the text and returns ranges that should be redacted

type RedactionStyle

type RedactionStyle string

RedactionStyle represents the style of redaction to apply

const (
	// RedactionStyleBlock replaces text with block characters
	RedactionStyleBlock RedactionStyle = "block"
	// RedactionStyleBlur applies a blur effect to the text
	RedactionStyleBlur RedactionStyle = "blur"
)

type Token

type Token struct {
	Text      string
	Color     color.Color
	Bold      bool
	Italic    bool
	Underline bool
	NoItalic  bool
}

Token represents a syntax highlighted token

Jump to

Keyboard shortcuts

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