| 1 | # syntaxhighlight |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | The main [`AsHTML(src []byte) ([]byte, error)`](https://sourcegraph.com/sourcegraph.com/sourcegraph/syntaxhighlight@master/.GoPackage/sourcegraph.com/sourcegraph/syntaxhighlight/.def/AsHTML) function outputs HTML that uses the same CSS classes as [google-code-prettify](https://code.google.com/p/google-code-prettify/), so any stylesheets for that should also work with this package. |
| 6 | |
| 7 | **[Documentation on Sourcegraph](https://sourcegraph.com/github.com/sourcegraph/syntaxhighlight)** |
| 8 | |
| 9 | [](https://travis-ci.org/sourcegraph/syntaxhighlight) |
| 10 | [](https://sourcegraph.com/github.com/sourcegraph/syntaxhighlight) |
| 11 | |
| 12 | ## Installation |
| 13 | |
| 14 | ``` |
| 15 | go get -u github.com/sourcegraph/syntaxhighlight |
| 16 | ``` |
| 17 | First you should install the golang evironment, you can download it [here](https://golang.org/dl) or you can follow the [getting started](https://golang.org/doc/install) |
| 18 | |
| 19 | Remember you should set the environment variables correctly (GOPATH and PATH) |
| 20 | |
| 21 | ## Example usage |
| 22 | |
| 23 | The function [`AsHTML(src []byte, options ...Option) ([]byte, error)`](https://sourcegraph.com/sourcegraph.com/sourcegraph/syntaxhighlight@master/.GoPackage/sourcegraph.com/sourcegraph/syntaxhighlight/.def/AsHTML) returns an HTML-highlighted version of `src`. The input source code can be in any language; the lexer is language independent. An `OrderedList()` option can be passed to produce an `<ol>...</ol>`-wrapped list to display line numbers. |
| 24 | |
| 25 | ```go |
| 26 | package syntaxhighlight_test |
| 27 | |
| 28 | import ( |
| 29 | "fmt" |
| 30 | "os" |
| 31 | |
| 32 | "github.com/sourcegraph/syntaxhighlight" |
| 33 | ) |
| 34 | |
| 35 | func Example() { |
| 36 | src := []byte(` |
| 37 | /* hello, world! */ |
| 38 | var a = 3; |
| 39 | |
| 40 | // b is a cool function |
| 41 | function b() { |
| 42 | return 7; |
| 43 | }`) |
| 44 | |
| 45 | highlighted, err := syntaxhighlight.AsHTML(src) |
| 46 | if err != nil { |
| 47 | fmt.Println(err) |
| 48 | os.Exit(1) |
| 49 | } |
| 50 | |
| 51 | fmt.Println(string(highlighted)) |
| 52 | |
| 53 | // Output: |
| 54 | // <span class="com">/* hello, world! */</span> |
| 55 | // <span class="kwd">var</span> <span class="pln">a</span> <span class="pun">=</span> <span class="dec">3</span><span class="pun">;</span> |
| 56 | // |
| 57 | // <span class="com">// b is a cool function</span> |
| 58 | // <span class="kwd">function</span> <span class="pln">b</span><span class="pun">(</span><span class="pun">)</span> <span class="pun">{</span> |
| 59 | // <span class="kwd">return</span> <span class="dec">7</span><span class="pun">;</span> |
| 60 | // <span class="pun">}</span> |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | ## Contributors |
| 65 | |
| 66 | * [Quinn Slack](https://sourcegraph.com/sqs) |
| 67 | |
| 68 | Contributions are welcome! Submit a pull request on GitHub. |