Documentation ¶
Overview ¶
Package syntaxhighlight provides syntax highlighting for code. It currently uses a language-independent lexer and performs decently on JavaScript, Java, Ruby, Python, Go, and C.
Package syntaxhighlight provides syntax highlighting for code. It currently uses a language-independent lexer and performs decently on JavaScript, Java, Ruby, Python, Go, and C.
Example ¶
package main import ( "fmt" "os" "github.com/sourcegraph/syntaxhighlight" ) func main() { src := []byte(` /* hello, world! */ var a = 3; // b is a cool function function b() { return 7; }`) highlighted, err := syntaxhighlight.AsHTML(src) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(string(highlighted)) }
Output: <span class="com">/* hello, world! */</span> <span class="kwd">var</span> <span class="pln">a</span> <span class="pun">=</span> <span class="dec">3</span><span class="pun">;</span> <span class="com">// b is a cool function</span> <span class="kwd">function</span> <span class="pln">b</span><span class="pun">(</span><span class="pun">)</span> <span class="pun">{</span> <span class="kwd">return</span> <span class="dec">7</span><span class="pun">;</span> <span class="pun">}</span>
Index ¶
- Constants
- Variables
- func Annotate(src []byte, a Annotator) (annotate.Annotations, error)
- func AsANSI(src []byte) ([]byte, error)
- func AsHTML(src []byte) ([]byte, error)
- func NewScanner(src []byte) *scanner.Scanner
- func NewScannerReader(src io.Reader) *scanner.Scanner
- func Print(s *scanner.Scanner, w io.Writer, p Printer) error
- type Annotator
- type AnsiAnnotator
- type AnsiConfig
- type AnsiPrinter
- type HTMLAnnotator
- type HTMLConfig
- type HTMLPrinter
- type Kind
- type Printer
Examples ¶
Constants ¶
const ( ResetAll = "\033[0m" Bold = "\033[1m" Dim = "\033[2m" Underlined = "\033[4m" Blink = "\033[5m" Reverse = "\033[7m" Hidden = "\033[8m" ResetBold = "\033[21m" ResetDim = "\033[22m" ResetUnderlined = "\033[24m" ResetBlink = "\033[25m" ResetReverse = "\033[27m" ResetHidden = "\033[28m" Default = "\033[39m" Black = "\033[30m" Red = "\033[31m" Green = "\033[32m" Yellow = "\033[33m" Blue = "\033[34m" Magenta = "\033[35m" Cyan = "\033[36m" LightGray = "\033[37m" DarkGray = "\033[90m" LightRed = "\033[91m" LightGreen = "\033[92m" LightYellow = "\033[93m" LightBlue = "\033[94m" LightMagenta = "\033[95m" LightCyan = "\033[96m" White = "\033[97m" BackgroundDefault = "\033[49m" BackgroundBlack = "\033[40m" BackgroundRed = "\033[41m" BackgroundGreen = "\033[42m" BackgroundYellow = "\033[43m" BackgroundBlue = "\033[44m" BackgroundMagenta = "\033[45m" BackgroundCyan = "\033[46m" BackgroundLightGray = "\033[47m" BackgroundDarkGray = "\033[100m" BackgroundLightRed = "\033[101m" BackgroundLightGreen = "\033[102m" BackgroundLightYellow = "\033[103m" BackgroundLightBlue = "\033[104m" BackgroundLightMagenta = "\033[105m" BackgroundLightCyan = "\033[106m" BackgroundWhite = "\033[107m" )
Variables ¶
var DefaultAnsiConfig = AnsiConfig{ String: LightBlue, Keyword: Bold + LightCyan, Comment: Yellow, Type: White, Literal: LightYellow, Punctuation: DarkGray, Plaintext: DarkGray, Tag: LightGray, HTMLTag: LightGray, HTMLAttrName: Blue, HTMLAttrValue: Blue, Decimal: LightCyan, Whitespace: "", }
DefaultHTMLConfig's class names match those of google-code-prettify (https://code.google.com/p/google-code-prettify/).
var DefaultHTMLConfig = HTMLConfig{ String: "str", Keyword: "kwd", Comment: "com", Type: "typ", Literal: "lit", Punctuation: "pun", Plaintext: "pln", Tag: "tag", HTMLTag: "htm", HTMLAttrName: "atn", HTMLAttrValue: "atv", Decimal: "dec", Whitespace: "", }
DefaultHTMLConfig's class names match those of google-code-prettify (https://code.google.com/p/google-code-prettify/).
Functions ¶
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.
Types ¶
type AnsiAnnotator ¶
type AnsiAnnotator AnsiConfig
func (AnsiAnnotator) Annotate ¶
func (a AnsiAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
type AnsiConfig ¶
type AnsiConfig struct { String string Keyword string Comment string Type string Literal string Punctuation string Plaintext string Tag string HTMLTag string HTMLAttrName string HTMLAttrValue string Decimal string Whitespace string }
AnsiConfig holds the ANSI class configuration to be used by annotators when highlighting code.
func (AnsiConfig) Class ¶
func (c AnsiConfig) Class(kind Kind) string
Class returns the set class for a given token Kind.
type AnsiPrinter ¶
type AnsiPrinter AnsiConfig
type HTMLAnnotator ¶
type HTMLAnnotator HTMLConfig
func (HTMLAnnotator) Annotate ¶
func (a HTMLAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
type HTMLConfig ¶
type HTMLConfig struct { String string Keyword string Comment string Type string Literal string Punctuation string Plaintext string Tag string HTMLTag string HTMLAttrName string HTMLAttrValue string Decimal string Whitespace string }
HTMLConfig holds the HTML class configuration to be used by annotators when highlighting code.
func (HTMLConfig) Class ¶
func (c HTMLConfig) Class(kind Kind) string
Class returns the set class for a given token Kind.
type HTMLPrinter ¶
type HTMLPrinter HTMLConfig