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.
Example ¶
package main import ( "fmt" "os" "github.com/jingweno/ccat/Godeps/_workspace/src/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 ¶
- Variables
- func Annotate(src []byte, a Annotator) (annotate.Annotations, error)
- func AsHTML(src []byte) ([]byte, error)
- func NewScanner(src []byte) *scanner.Scanner
- func Print(s *scanner.Scanner, w io.Writer, p Printer) error
- type Annotator
- type HTMLAnnotator
- type HTMLConfig
- type HTMLPrinter
- type Kind
- type NilAnnotator
- type Printer
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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", }
DefaultHTMLConfig's class names match those of google-code-prettify (https://code.google.com/p/google-code-prettify/).
Functions ¶
func NewScanner ¶
Types ¶
type HTMLAnnotator ¶
type HTMLAnnotator HTMLConfig
func (HTMLAnnotator) Annotate ¶
func (a HTMLAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
type HTMLConfig ¶
type HTMLPrinter ¶
type HTMLPrinter HTMLConfig
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 NilAnnotator ¶
type NilAnnotator struct { Config HTMLConfig Code *sourcegraph.SourceCode // contains filtered or unexported fields }
NilAnnotator is a special kind of annotator that always returns nil, but stores within itself the snippet of source code that is passed through it as tokens.
This functionality is useful when one wishes to obtain the tokenized source as a data structure, as opposed to an annotated string, allowing full control over rendering and displaying it.
func NewNilAnnotator ¶
func NewNilAnnotator(e *vcsclient.FileWithRange) *NilAnnotator
func (*NilAnnotator) Annotate ¶
func (a *NilAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)