highlight_go: github.com/shurcooL/highlight_go Index | Examples | Files

package highlight_go

import "github.com/shurcooL/highlight_go"

Package highlight_go provides a syntax highlighter for Go, using go/scanner.

Index

Examples

Package Files

main.go

func Annotate Uses

func Annotate(src []byte, a syntaxhighlight.Annotator) (annotate.Annotations, error)

Code:

src := []byte(`package main

import "fmt"

func main() {
	fmt.Println("Hey there, Go.")
}
`)

// debugAnnotator implements syntaxhighlight.Annotator and prints the parameters it's given.
p := debugAnnotator{Annotator: syntaxhighlight.HTMLAnnotator(syntaxhighlight.DefaultHTMLConfig)}

anns, err := highlight_go.Annotate(src, p)
if err != nil {
    log.Fatalln(err)
}

sort.Sort(anns)

b, err := annotate.Annotate(src, anns, template.HTMLEscape)
if err != nil {
    log.Fatalln(err)
}

fmt.Println(string(b))

Output:

Annotate(0, syntaxhighlight.Keyword, "package")
Annotate(8, syntaxhighlight.Plaintext, "main")
Annotate(14, syntaxhighlight.Keyword, "import")
Annotate(21, syntaxhighlight.String, "\"fmt\"")
Annotate(28, syntaxhighlight.Keyword, "func")
Annotate(33, syntaxhighlight.Plaintext, "main")
Annotate(37, syntaxhighlight.Plaintext, "(")
Annotate(38, syntaxhighlight.Plaintext, ")")
Annotate(40, syntaxhighlight.Plaintext, "{")
Annotate(43, syntaxhighlight.Plaintext, "fmt")
Annotate(46, syntaxhighlight.Plaintext, ".")
Annotate(47, syntaxhighlight.Plaintext, "Println")
Annotate(54, syntaxhighlight.Plaintext, "(")
Annotate(55, syntaxhighlight.String, "\"Hey there, Go.\"")
Annotate(71, syntaxhighlight.Plaintext, ")")
Annotate(73, syntaxhighlight.Plaintext, "}")
<span class="kwd">package</span> <span class="pln">main</span>

<span class="kwd">import</span> <span class="str">&#34;fmt&#34;</span>

<span class="kwd">func</span> <span class="pln">main</span><span class="pln">(</span><span class="pln">)</span> <span class="pln">{</span>
	<span class="pln">fmt</span><span class="pln">.</span><span class="pln">Println</span><span class="pln">(</span><span class="str">&#34;Hey there, Go.&#34;</span><span class="pln">)</span>
<span class="pln">}</span>

func Print Uses

func Print(src []byte, w io.Writer, p syntaxhighlight.Printer) error

Code:

src := []byte(`package main

import "fmt"

func main() {
	fmt.Println("Hey there, Go.")
}
`)

// debugPrinter implements syntaxhighlight.Printer and prints the parameters it's given.
p := debugPrinter{Printer: syntaxhighlight.HTMLPrinter(syntaxhighlight.DefaultHTMLConfig)}

var buf bytes.Buffer
highlight_go.Print(src, &buf, p)

io.Copy(os.Stdout, &buf)

Output:

Print(syntaxhighlight.Keyword, "package")
Print(syntaxhighlight.Whitespace, " ")
Print(syntaxhighlight.Plaintext, "main")
Print(syntaxhighlight.Whitespace, "\n\n")
Print(syntaxhighlight.Keyword, "import")
Print(syntaxhighlight.Whitespace, " ")
Print(syntaxhighlight.String, "\"fmt\"")
Print(syntaxhighlight.Whitespace, "\n\n")
Print(syntaxhighlight.Keyword, "func")
Print(syntaxhighlight.Whitespace, " ")
Print(syntaxhighlight.Plaintext, "main")
Print(syntaxhighlight.Plaintext, "(")
Print(syntaxhighlight.Plaintext, ")")
Print(syntaxhighlight.Whitespace, " ")
Print(syntaxhighlight.Plaintext, "{")
Print(syntaxhighlight.Whitespace, "\n\t")
Print(syntaxhighlight.Plaintext, "fmt")
Print(syntaxhighlight.Plaintext, ".")
Print(syntaxhighlight.Plaintext, "Println")
Print(syntaxhighlight.Plaintext, "(")
Print(syntaxhighlight.String, "\"Hey there, Go.\"")
Print(syntaxhighlight.Plaintext, ")")
Print(syntaxhighlight.Whitespace, "\n")
Print(syntaxhighlight.Plaintext, "}")
Print(syntaxhighlight.Whitespace, "\n")
<span class="kwd">package</span> <span class="pln">main</span>

<span class="kwd">import</span> <span class="str">&#34;fmt&#34;</span>

<span class="kwd">func</span> <span class="pln">main</span><span class="pln">(</span><span class="pln">)</span> <span class="pln">{</span>
	<span class="pln">fmt</span><span class="pln">.</span><span class="pln">Println</span><span class="pln">(</span><span class="str">&#34;Hey there, Go.&#34;</span><span class="pln">)</span>
<span class="pln">}</span>

Code:

src := []byte("  package    main      \n\t\n")

highlight_go.Print(src, ioutil.Discard, debugPrinter{Printer: syntaxhighlight.HTMLPrinter(syntaxhighlight.DefaultHTMLConfig)})

Output:

Print(syntaxhighlight.Whitespace, "  ")
Print(syntaxhighlight.Keyword, "package")
Print(syntaxhighlight.Whitespace, "    ")
Print(syntaxhighlight.Plaintext, "main")
Print(syntaxhighlight.Whitespace, "      \n\t\n")

func TokenKind Uses

func TokenKind(tok token.Token, lit string) syntaxhighlight.Kind

TokenKind returns a syntaxhighlight token kind value for the given tok and lit.

Package highlight_go imports 5 packages (graph) and is imported by 4 packages. Updated 2017-05-30. Refresh now. Tools for package owners.