syntax

package module
v1.12.10 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: BSD-3-Clause Imports: 10 Imported by: 4

README

Syntax

Go package for syntax highlighting + the hicat utility (a bit like syntax highlighted cat).

Installing hicat

The hicat utility can be installed with:

go install github.com/xyproto/syntax/cmd/hicat@latest
General info
  • License: BSD 3-Clause
  • Version: 1.12.10

Documentation

Overview

Package syntax provides syntax highlighting for source code, using the same approach as Orbiton. It tokenizes input with text/scanner, classifies tokens into kinds, and wraps them in color tags that can be converted to ANSI escape codes by the vt package. Theme selection is supported via the O_THEME (or THEME) environment variable.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTextConfig = TextConfig{
	AndOr:         "red",
	AngleBracket:  "red",
	AssemblyEnd:   "lightyellow",
	Class:         "white",
	Comment:       "darkgray",
	Decimal:       "red",
	Dollar:        "white",
	Keyword:       "red",
	Literal:       "white",
	Mut:           "magenta",
	Plaintext:     "white",
	Private:       "red",
	Protected:     "red",
	Public:        "red",
	Punctuation:   "red",
	Self:          "magenta",
	Star:          "white",
	Static:        "lightyellow",
	String:        "lightwhite",
	Tag:           "white",
	TextAttrName:  "white",
	TextAttrValue: "white",
	TextTag:       "white",
	Type:          "white",
	Whitespace:    "",
}

DefaultTextConfig provides class names that match the color names of textoutput tags: https://github.com/xyproto/textoutput

View Source
var (

	// Keywords contains the default syntax highlighting keywords
	Keywords = map[string]struct{}{}/* 190 elements not displayed */

)

Functions

func AddAndRemoveKeywords

func AddAndRemoveKeywords(addAndDel ...[]string)

AddAndRemoveKeywords first adds and then removes keywords.

func AddKeywords

func AddKeywords(kws []string)

AddKeywords adds the given keywords so that they will be syntax highlighted.

func AddKeywordsAsUppercase

func AddKeywordsAsUppercase(xs []string)

AddKeywordsAsUppercase adds uppercased versions of the given keywords.

func AdjustKeywords

func AdjustKeywords(m mode.Mode)

AdjustKeywords contains per-language adjustments to highlighting of keywords

func Annotate

func Annotate(src []byte, a Annotator, m mode.Mode) (annotate.Annotations, error)

Annotate tokenizes src and returns annotations for mode m.

func AsText

func AsText(src []byte, m mode.Mode, options ...Option) ([]byte, error)

AsText converts source code into a Text-highlighted version. It accepts optional configuration parameters to control rendering.

func CatBytes

func CatBytes(sourceCodeData []byte, o *vt.TextOutput) error

CatBytes highlights sourceCodeData and writes it to stdout via the given TextOutput.

func ClearKeywords

func ClearKeywords()

ClearKeywords resets the global Keywords map.

func NewScanner

func NewScanner(src []byte) *scanner.Scanner

NewScanner is a helper that takes a []byte src, wraps it in a reader and creates a Scanner.

func NewScannerReader

func NewScannerReader(src io.Reader) *scanner.Scanner

NewScannerReader takes a reader src and creates a Scanner.

func Print

func Print(s *scanner.Scanner, w io.Writer, p Printer, m mode.Mode) error

Print scans tokens from s, using Printer p for mode m.

func RemoveKeywords

func RemoveKeywords(kws []string)

RemoveKeywords removes keywords that should not be syntax highlighted.

func SetDefaultTextConfigFromEnv

func SetDefaultTextConfigFromEnv()

SetDefaultTextConfigFromEnv will update DefaultTextConfig based on O_THEME.

func SetKeywords

func SetKeywords(addAndDel ...[]string)

SetKeywords clears, then adds/removes keywords.

Types

type Annotator

type Annotator interface {
	Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
}

Annotator produces syntax highlighting annotations.

type Kind

type Kind uint8

Kind represents a syntax highlighting kind (class) which will be assigned to tokens. A syntax highlighting scheme (style) maps text style properties to each token kind.

const (
	Whitespace Kind = iota
	AndOr
	AngleBracket
	AssemblyEnd
	Class
	Comment
	Decimal
	Dollar
	Literal
	Keyword
	Mut
	Plaintext
	Private
	Protected
	Public
	Punctuation
	Self
	Star
	Static
	String
	Tag
	TextAttrName
	TextAttrValue
	TextTag
	Type
)

Supported highlighting kinds

type Option

type Option func(*TextConfig)

Option is a type of the function that can modify one or more of the options in the TextConfig structure.

type Printer

type Printer interface {
	Print(w io.Writer, kind Kind, tokText string) error
}

Printer implements an interface to render highlighted output (see TextPrinter for the implementation of this interface).

type TextAnnotator

type TextAnnotator TextConfig

TextAnnotator wraps TextConfig to implement Annotator.

func (TextAnnotator) Annotate

func (a TextAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)

Annotate returns an annotation for the given token.

type TextConfig

type TextConfig struct {
	AndOr         string
	AngleBracket  string
	AssemblyEnd   string
	Class         string
	Comment       string
	Decimal       string
	Dollar        string
	Keyword       string
	Literal       string
	Mut           string
	Plaintext     string
	Private       string
	Protected     string
	Public        string
	Punctuation   string
	Self          string
	Star          string
	Static        string
	String        string
	Tag           string
	TextAttrName  string
	TextAttrValue string
	TextTag       string
	Type          string
	Whitespace    string
}

TextConfig holds the Text class configuration to be used by annotators when highlighting code.

func LightTextConfigByName

func LightTextConfigByName(name string) TextConfig

LightTextConfigByName will return the TextConfig for the given theme name, preferring the light variant when one exists.

func NewDarkBlueEditTextConfig

func NewDarkBlueEditTextConfig() TextConfig

NewDarkBlueEditTextConfig returns the TextConfig for the dark "blueedit" theme.

func NewDarkVSTextConfig

func NewDarkVSTextConfig() TextConfig

NewDarkVSTextConfig returns the TextConfig for the dark "vs" theme.

func NewDefaultTextConfig

func NewDefaultTextConfig() TextConfig

NewDefaultTextConfig returns the TextConfig for the default Orbiton theme.

func NewLightBlueEditTextConfig

func NewLightBlueEditTextConfig() TextConfig

NewLightBlueEditTextConfig returns the TextConfig for the light "blueedit" theme.

func NewLightVSTextConfig

func NewLightVSTextConfig() TextConfig

NewLightVSTextConfig returns the TextConfig for the light "vs" theme.

func NewLitmusTextConfig

func NewLitmusTextConfig() TextConfig

NewLitmusTextConfig returns the TextConfig for the "litmus" theme.

func NewNoColorTextConfig

func NewNoColorTextConfig() TextConfig

NewNoColorTextConfig returns an empty TextConfig with no colors.

func NewOrbTextConfig

func NewOrbTextConfig() TextConfig

NewOrbTextConfig returns the TextConfig for the "orb" theme.

func NewPinetreeTextConfig

func NewPinetreeTextConfig() TextConfig

NewPinetreeTextConfig returns the TextConfig for the "pinetree" theme.

func NewRedBlackTextConfig

func NewRedBlackTextConfig() TextConfig

NewRedBlackTextConfig returns the TextConfig for the "redblack" theme.

func NewSynthwaveTextConfig

func NewSynthwaveTextConfig() TextConfig

NewSynthwaveTextConfig returns the TextConfig for the "synthwave" theme.

func NewTealTextConfig

func NewTealTextConfig() TextConfig

NewTealTextConfig returns the TextConfig for the "teal" theme.

func NewZuluTextConfig

func NewZuluTextConfig() TextConfig

NewZuluTextConfig returns the TextConfig for the "zulu" theme.

func TextConfigByName

func TextConfigByName(name string) TextConfig

TextConfigByName returns the TextConfig for the given theme name. If the name is not recognized, the default TextConfig is returned.

func TextConfigFromEnv

func TextConfigFromEnv() TextConfig

TextConfigFromEnv will return the TextConfig selected by the O_THEME (or default) environment variable, falling back to the default. If NO_COLOR is set, an empty TextConfig (no colors) is returned. If O_LIGHT is set, light theme variants are preferred.

func (TextConfig) GetClass

func (c TextConfig) GetClass(kind Kind) string

GetClass returns the set class for a given token Kind.

type TextPrinter

type TextPrinter TextConfig

TextPrinter implements Printer interface and is used to produce Text-based highligher.

func (TextPrinter) Print

func (p TextPrinter) Print(w io.Writer, kind Kind, tokText string) error

Print is the function that emits highlighted source code using <color>...<off> wrapper tags.

Directories

Path Synopsis
cmd
hicat command

Jump to

Keyboard shortcuts

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