syntax

package module
v1.14.11 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: BSD-3-Clause Imports: 9 Imported by: 8 modules, 7 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.14.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 (
	DefaultDarkTextConfig  = DefaultTextConfig
	DefaultLightTextConfig = TextConfig{
		AndOr:         "black",
		AngleBracket:  "black",
		AssemblyEnd:   "red",
		Class:         "blue",
		Comment:       "gray",
		CurlyBracket:  "black",
		Decimal:       "darkcyan",
		Dollar:        "red",
		IncludeSystem: "blue",
		Keyword:       "blue",
		Literal:       "darkcyan",
		Mut:           "black",
		Plaintext:     "black",
		Private:       "black",
		Protected:     "black",
		Public:        "black",
		Punctuation:   "black",
		Self:          "darkcyan",
		Star:          "black",
		Static:        "black",
		String:        "red",
		Tag:           "black",
		TextAttrName:  "black",
		TextAttrValue: "black",
		TextTag:       "black",
		Type:          "blue",
		Whitespace:    "",
	}
)
View Source
var (
	DefaultDarkTheme = NewTheme("DefaultDark", false, map[Kind]Style{
		Whitespace:    {},
		Plaintext:     {Foreground: vt.LightGray},
		Keyword:       {Foreground: vt.Red},
		Type:          {Foreground: vt.LightGray},
		String:        {Foreground: vt.White},
		Comment:       {Foreground: vt.DarkGray},
		Decimal:       {Foreground: vt.Red},
		Punctuation:   {Foreground: vt.Red},
		CurlyBracket:  {Foreground: vt.Red},
		IncludeSystem: {Foreground: vt.Red},
	})

	DefaultLightTheme = NewTheme("DefaultLight", true, map[Kind]Style{
		Whitespace:    {},
		Plaintext:     {Foreground: vt.Black},
		Keyword:       {Foreground: vt.Blue},
		Type:          {Foreground: vt.Blue},
		String:        {Foreground: vt.Red},
		Comment:       {Foreground: vt.Gray},
		Decimal:       {Foreground: vt.Cyan},
		Punctuation:   {Foreground: vt.Black},
		CurlyBracket:  {Foreground: vt.Black},
		IncludeSystem: {Foreground: vt.Blue},
	})

	VSLightTheme = NewTheme("VSLight", true, map[Kind]Style{
		Whitespace:    {},
		Plaintext:     {Foreground: vt.TrueColor(0x00, 0x00, 0x00)},
		Keyword:       {Foreground: vt.TrueColor(0x00, 0x00, 0xff)},
		Type:          {Foreground: vt.TrueColor(0x26, 0x7f, 0x99)},
		String:        {Foreground: vt.TrueColor(0xa3, 0x15, 0x15)},
		Comment:       {Foreground: vt.TrueColor(0x00, 0x80, 0x00)},
		Decimal:       {Foreground: vt.TrueColor(0x09, 0x86, 0x58)},
		Punctuation:   {Foreground: vt.TrueColor(0x00, 0x00, 0x00)},
		CurlyBracket:  {Foreground: vt.TrueColor(0x00, 0x00, 0x00)},
		IncludeSystem: {Foreground: vt.TrueColor(0xa3, 0x15, 0x15)},
	})
)
View Source
var DefaultTextConfig = TextConfig{
	AndOr:         "red",
	AngleBracket:  "red",
	AssemblyEnd:   "lightyellow",
	Class:         "white",
	Comment:       "darkgray",
	CurlyBracket:  "red",
	Decimal:       "red",
	Dollar:        "white",
	IncludeSystem: "red",
	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 AsTextWithBackground added in v1.14.11

func AsTextWithBackground(src []byte, m mode.Mode, light bool, options ...Option) ([]byte, error)

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 Highlight added in v1.14.11

func Highlight(src []byte, m mode.Mode, s Styler, w io.Writer) error

func HighlightToAttributes added in v1.14.11

func HighlightToAttributes(src []byte, m mode.Mode, s Styler) []vt.CharAttribute

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 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 DirectPrinter added in v1.14.11

type DirectPrinter struct {
	Scheme Styler
}

func (DirectPrinter) Print added in v1.14.11

func (dp DirectPrinter) Print(w io.Writer, kind Kind, tokText string) error

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
	CurlyBracket
	IncludeSystem
)

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 Palette added in v1.14.11

type Palette [IncludeSystem + 1]Style

func (Palette) Style added in v1.14.11

func (p Palette) Style(k Kind) Style

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 Style added in v1.14.11

type Style struct {
	Foreground vt.AttributeColor
	Background vt.AttributeColor
}

type Styler added in v1.14.11

type Styler interface {
	Style(Kind) Style
}

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
	CurlyBracket  string
	Decimal       string
	Dollar        string
	IncludeSystem 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 NewTextConfig added in v1.14.11

func NewTextConfig(light bool) TextConfig

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.

type Theme added in v1.14.11

type Theme struct {
	Name    string
	Light   bool
	Palette Palette
}

func NewTheme added in v1.14.11

func NewTheme(name string, light bool, mapping map[Kind]Style) Theme

func (Theme) Style added in v1.14.11

func (t Theme) Style(k Kind) Style

Directories

Path Synopsis
cmd
hicat command

Jump to

Keyboard shortcuts

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