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 ¶
- Variables
- func AddAndRemoveKeywords(addAndDel ...[]string)
- func AddKeywords(kws []string)
- func AddKeywordsAsUppercase(xs []string)
- func AdjustKeywords(m mode.Mode)
- func Annotate(src []byte, a Annotator, m mode.Mode) (annotate.Annotations, error)
- func AsText(src []byte, m mode.Mode, options ...Option) ([]byte, error)
- func CatBytes(sourceCodeData []byte, o *vt.TextOutput) error
- func ClearKeywords()
- func NewScanner(src []byte) *scanner.Scanner
- func NewScannerReader(src io.Reader) *scanner.Scanner
- func Print(s *scanner.Scanner, w io.Writer, p Printer, m mode.Mode) error
- func RemoveKeywords(kws []string)
- func SetKeywords(addAndDel ...[]string)
- type Annotator
- type Kind
- type Option
- type Printer
- type TextAnnotator
- type TextConfig
- type TextPrinter
Constants ¶
This section is empty.
Variables ¶
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
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 ¶
AdjustKeywords contains per-language adjustments to highlighting of keywords
func AsText ¶
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 NewScanner ¶
NewScanner is a helper that takes a []byte src, wraps it in a reader and creates a Scanner.
func NewScannerReader ¶
NewScannerReader takes a reader src and creates a Scanner.
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 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.
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 ¶
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
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 (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.