Go package for syntax highlighting of code

syntaxhighlight

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.

The main AsHTML(src []byte) ([]byte, error) function outputs HTML that uses the same CSS classes as google-code-prettify, so any stylesheets for that should also work with this package.

Documentation on Sourcegraph

Build Status status

Installation

go get -u github.com/sourcegraph/syntaxhighlight

First you should install the golang evironment, you can download it here or you can follow the getting started

Remember you should set the environment variables correctly (GOPATH and PATH)

Example usage

The function AsHTML(src []byte, options ...Option) ([]byte, error) 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.

package syntaxhighlight_test

import (
	"fmt"
	"os"

	"github.com/sourcegraph/syntaxhighlight"
)

func Example() {
	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>
}

Contributors

Contributions are welcome! Submit a pull request on GitHub.

Owner
Sourcegraph
Code search and navigation for teams (self-hosted, OSS)
Sourcegraph
Comments
  • Pass in `io.Reader` instead of `[]byte` to `NewScanner`

    Pass in `io.Reader` instead of `[]byte` to `NewScanner`

    io.Reader is a more generic abstraction for input: it could be a file or it could be a reader of stdin.

    The changes will enable ccat to use the NewScannaer API to create a Scanner. See https://github.com/jingweno/ccat/commit/e7385e67afd8690c1ea677abfc13f2807aadfe05.

  • Support for <ol> to have line numbers

    Support for
      to have line numbers

    Would you be interested in accepting https://github.com/iafan/syntaxhighlight/commit/0da6819d25a1bdbf3c6206cc9ffece21d771bb57 as a PR?

    This commit introduces AsOrderedListHTML function which, in addition to highlighting the source, wraps each line in <li>..</li>, and then adds <ol>..</ol> global wrapper. The resulting highlighted source code now has line numbers.

    Commit comes with full test coverage.

  • Too many incidental dependencies.

    Too many incidental dependencies.

    This issue is largely documented downstream at https://github.com/shurcooL/go/issues/19#issuecomment-97699653, but to quickly summarize:

    After #11 was merged, syntaxhighlight package now indirectly imports an extremely large number of imports that are largely unrelated and unneeded to the core functionality of this package.

    image

    https://godoc.org/github.com/sourcegraph/syntaxhighlight?import-graph&hide=1

    The vast majority of that is coming from the two imports sourcegraph.com/sourcegraph/go-sourcegraph/sourcegraph and sourcegraph.com/sourcegraph/vcsstore/vcsclient which are only needed to pull in the following types:

    Despite those types being small, the packages that contain them are higher level and import many other dependencies.

    We should fix this so that users of syntaxhighlight that only need the core functionality and do not use the NilAnnotator should not need to pull in that many dependencies.

  • Token kinds/classes API could be improved.

    Token kinds/classes API could be improved.

    Hi,

    I'm trying to use the public interface/API of this package externally, but this API could be better:

    const (
        WHITESPACE = iota
        STRING
        KEYWORD
        COMMENT
        TYPE
        LITERAL
        PUNCTUATION
        PLAINTEXT
        TAG
        HTMLTAG
        HTMLATTRNAME
        HTMLATTRVALUE
        DECIMAL
    )
    

    First, it's not idiomatic Go style to use ALLCAPS; these consts should be Whitespace, etc. See style guide section on Mixed Caps. As far as I can tell, it will not cause naming conflicts inside the package.

    Second, these kind values are untyped constants.

    I'd prefer if they were typed, for example syntaxhighlight.Kind, so that I can use that type in a struct in my package.

    According to http://godoc.org/github.com/sourcegraph/syntaxhighlight?importers, the only public importers of this package are all my packages. Do you use the current WHITESPACE, etc. outside this package?

    Do you agree it would be an improvement and would you take a PR that changes the above code to something like this?

    // Kind represents a syntax highlighting class which will be assigned to elements. A style will assign visual properties to each kind.
    type Kind uint8
    
    const (
        Whitespace Kind = iota
        String
        Keyword
        Comment
        Type
        Literal
        Punctuation
        Plaintext
        Tag
        HTMLTag
        HTMLAttrName
        HTMLAttrValue
        Decimal
    )
    

    Also, Keywords probably does not need to be exported, does it? Unless users of this package are meant to be able to make changes to it (if it's meant to be exported, it should be documented, it's currently not).

  • Try to fix Travis.

    Try to fix Travis.

    • I'm guessing that trying to write to benchmark.txt inside travis is likely creating the error due to permissions. Any file changes inside Travis will be lost anyway, so there's no point in doing that.
    • Instead, I just opted to execute the test and benchmarks and show their output in Travis.
    • Also make sure the Go code is gofmted.
    • Use Go 1.3.
  • Css? Newlines?

    Css? Newlines?

    This is more of me not understanding how to use this package, but I'm not finding any css in here. Are we supposed to bring our own?

    For example, compare the way github displays simple.go:

    https://github.com/sourcegraph/syntaxhighlight/blob/master/testdata/simple.go

    Versus the output html of this package for that simple.go file:

    https://rawgithub.com/sourcegraph/syntaxhighlight/master/testdata/simple.go.html

    Are we supposed to create the css ourselves from scratch, or are there any existing ones that can be used?

    Also, all the newlines are missing. Is it because the css is missing or another reason?

    Thanks!

  • Run gofmt -s which simplifies struct definitions

    Run gofmt -s which simplifies struct definitions

    I ran go test and everything passes successfully. But verify that nothing is wrong.

    Note: gofmt -s outputs code which might not be compatible with earlier versions of Go.

  • Fix tests and example.

    Fix tests and example.

    • Update testdata/underscore.go.html to match expected output after input change in b4219f1fb43b89dede6b8a2bc0c03f909cd599f7.
    • Fix example verification to occur (see http://godoc.org/testing#hdr-Examples), and update expected output to match actual output.
    • Copy latest version of example_test.go into README.md so they are in sync.
  • Use text/scanner.

    Use text/scanner.

    When I asked why this branch wasn't merged into master yet, you said the most likely reason was that you simply forgot about it.

    So I'm making this PR for your convenience as a reminder. As far as I can tell, this approach of using text/template should be better overall (more general and simple, handles Unicode, 42% slower).

  • Add an ability to produce an `<ol>...</ol>`-wrapped list to display line numbers

    Add an ability to produce an `
      ...
    `-wrapped list to display line numbers

    This implements #20 using functional parameters:

    // default backward-compatible call
    got, err := AsHTML(input)
    
    // with optional parameter
    got, err := AsHTML(input, OrderedList())
    
  • Add a more direct NewScannerReader func.

    Add a more direct NewScannerReader func.

    If the user has an io.Reader, they can now use NewScannerReader to more directly create a Scanner.

    Previously, they would be forced to either read all bytes from the reader into a byte slice and use that (which then indirectly gets wrapped by a bytes.NewReader), or copy this func in their code.

    Resolves #14. /cc @jingweno

  • Dependency Dashboard

    Dependency Dashboard

    This issue contains a list of Renovate updates and their statuses.

    This repository currently has no open or pending branches.


    • [ ] Check this box to trigger a request for Renovate to run again on this repository
  • Configure Renovate

    Configure Renovate

    Mend Renovate

    Welcome to Renovate! This is an onboarding PR to help you understand and configure settings before regular Pull Requests begin.

    🚦 To activate Renovate, merge this Pull Request. To disable Renovate, simply close this Pull Request unmerged.


    Configuration Summary

    Based on the default config's presets, Renovate will:

    • Start dependency updates only once this onboarding PR is merged
    • Enable Renovate Dependency Dashboard creation.
    • If Renovate detects semantic commits, it will use semantic commit type fix for dependencies and chore for all others.
    • Ignore node_modules, bower_components, vendor and various test/tests directories.
    • Autodetect whether to pin dependencies or maintain ranges.
    • Rate limit PR creation to a maximum of two per hour.
    • Limit to maximum 10 open PRs at any time.
    • Group known monorepo packages together.
    • Use curated list of recommended non-monorepo package groupings.
    • A collection of workarounds for known problems with packages.
    • Run Renovate on following schedule: on the 1st through 7th day of the month

    🔡 Would you like to change the way Renovate is upgrading your dependencies? Simply edit the renovate.json in this branch with your custom config and the list of Pull Requests in the "What to Expect" section below will be updated the next time Renovate runs.


    What to Expect

    It looks like your repository dependencies are already up-to-date and no Pull Requests will be necessary right away.


    ❓ Got questions? Check out Renovate's Docs, particularly the Getting Started section. If you need any further assistance then you can also request help here.


    This PR has been generated by Mend Renovate. View repository job log here.

  • Normalize (consolidate) tokens of the same kind

    Normalize (consolidate) tokens of the same kind

    The way underlying text/scanner package works is it produces two different tokens for combined punctuation like <=, >=, !=, :=, ||, :: and so on. For example, != would be represented in the highlighted output as follows:

    <span class="pun">!</span><span class="pun">=</span>
    

    This is suboptimal in terms of final HTML size, and also prevents font ligatures from being displayed properly (see e.g. Fira Code font).

    This PR implements token normalization so that adjacent tokens of the same kind are merged together. The example above now looks like this:

    <span class="pun">!=</span>
    

    ...and allows for proper ligature rendering.

  • More control over Go lang (and other lang) tagging.

    More control over Go lang (and other lang) tagging.

    I am writing a debugger for Go and came across this project via Dmitri Shuralyov and his use of this which uses go/scanner.

    One improvement I'd like to see is more fined-grained control over what gets tagged. Rather than reinvent stuff when not necessary, I consulted Pygments to see what it has.

    This is from pygments/formatters/terminal.py;

        Whitespace:         ('lightgray',   'darkgray'),
        Comment:            ('lightgray',   'darkgray'),
        Comment.Preproc:    ('teal',        'turquoise'),
        Keyword:            ('darkblue',    'blue'),
        Keyword.Type:       ('teal',        'turquoise'),
        Operator.Word:      ('purple',      'fuchsia'),
        Name.Builtin:       ('teal',        'turquoise'),
        Name.Function:      ('darkgreen',   'green'),
        Name.Namespace:     ('_teal_',      '_turquoise_'),
        Name.Class:         ('_darkgreen_', '_green_'),
        Name.Exception:     ('teal',        'turquoise'),
        Name.Decorator:     ('darkgray',    'lightgray'),
        Name.Variable:      ('darkred',     'red'),
        Name.Constant:      ('darkred',     'red'),
        Name.Attribute:     ('teal',        'turquoise'),
        Name.Tag:           ('blue',        'blue'),
        String:             ('brown',       'brown'),
        Number:             ('darkblue',    'blue'),
    

    There are some things above that aren't relevant to Go (but you might want to add them for other languages), and there are some things that should be added above for go like Rune, possibly Case label and so on. So here is a list below:

    • Whitespace
    • Comment
    • Keyword
    • Type name
    • Built in
    • Function
    • Variable
    • Exported name
    • Number
    • Rune

    Kind is a uint8 so there is plenty of space to add constants to this.

    If you would like me to create a pull request for this, let me know.

    Thanks.

A program to build, run, and restart a Go program on code change

devrun A program to build, run, and restart a Go program on code change. It also supports watching all your Go imports too. So if you change the code

Apr 4, 2022
Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.
  Yet another Go REPL that works nicely. Featured with line editing, code completion, and more.

gore Yet another Go REPL that works nicely. Featured with line editing, code completion, and more. (Screencast taken with cho45/KeyCast) Usage gore Af

Jan 2, 2023
Will autobuild and kill/relaunch the target when you update the code.

Use like rerun github.com/skelterjohn/go.uik/uiktest Usage: rerun [--test] [--build] [--race] [--no-run] <import path> [arg]* For any go executable in

Jul 23, 2022
🔥 Continuous profiling platform — debug performance issues in your code!
🔥  Continuous profiling platform — debug performance issues in your code!

Pyroscope is an open source continuous profiling platform.

Jan 7, 2023
A tool that helps you write code in your favorite IDE: your word processor!
A tool that helps you write code in your favorite IDE: your word processor!

WordIDE Have you ever wondered: How would it feel like to write code in a word processor? Me neither. But after months minutes of planning, I present

Jul 21, 2022
go-pry - an interactive REPL for Go that allows you to drop into your code at any point.
go-pry - an interactive REPL for Go that allows you to drop into your code at any point.

go-pry go-pry - an interactive REPL for Go that allows you to drop into your code at any point. Example Usage Install go-pry go get github.com/d4l3k/g

Dec 24, 2022
Simple tool that updates Visual Studio Code workspace(s) to include Go modules in gopath/src, then launches VSCode if only one modified.

Simple tool that updates Visual Studio Code workspace(s) to include Go modules in gopath/src, then launches VSCode if only one modified.

Jan 27, 2022
📦 Package Node.js applications into executable binaries 📦

caxa ?? Package Node.js applications into executable binaries ?? Support Recurring support on Patreon: https://patreon.com/leafac One-time support on

Jan 6, 2023
Go package for syntax highlighting of code

syntaxhighlight Package syntaxhighlight provides syntax highlighting for code. It currently uses a language-independent lexer and performs decently on

Nov 18, 2022
Improved go doc with terminal syntax highlighting
Improved go doc with terminal syntax highlighting

GopherDoc Improved go doc with terminal syntax highlighting. This is a modification of the original go doc command that adds terminal syntax highlight

Nov 22, 2022
GDScript Syntax Highlighting in GNU Nano

nano-gdscript GDScript Syntax Highlighting in GNU Nano. Updated regularly every minor updates. Contributions are welcomed Installation This is 100% fr

Dec 20, 2022
Kakoune syntax highlighting for the Godot Engine / Godot Scripting Language gdscript
Kakoune syntax highlighting for the Godot Engine / Godot Scripting Language gdscript

gdscript-kak Kakoune syntax highlighting for the Godot Engine / Godot Scripting Language gdscript. Adds basic syntax highlighting to your .gd files fo

Mar 2, 2021
Search for Go code using syntax trees

gogrep GO111MODULE=on go get mvdan.cc/gogrep Search for Go code using syntax trees. Work in progress. gogrep -x 'if $x != nil { return $x, $*_ }' In

Dec 9, 2022
Assembly syntax that makes you feel like you're writing code in a high-level language.

shasm Assembly syntax that makes you feel like you're writing code in a high-level language. Shasm is not an Assembler. Shasm simply compiles Shasm sy

Jun 5, 2021
Syntax-aware grep for PHP code.

phpgrep Syntax-aware grep for PHP code. This repository is used for the library and command-line tool development. A good source for additional utilit

Dec 30, 2022
Syntax-aware Go code search, based on the mvdan/gogrep
Syntax-aware Go code search, based on the mvdan/gogrep

gogrep WIP: this is an attempt to move modified gogrep from the go-ruleguard project, so it can be used outside of the ruleguard as a library. Acknowl

Nov 9, 2022
Snippit - Creates syntax-highlighted code snippets in png or svg format
Snippit - Creates syntax-highlighted code snippets in png or svg format

snippit creates syntax-highlighted code snippets in png or svg format. Installat

Oct 10, 2022
Test your code without writing mocks with ephemeral Docker containers 📦 Setup popular services with just a couple lines of code ⏱️ No bash, no yaml, only code 💻

Gnomock – tests without mocks ??️ Spin up entire dependency stack ?? Setup initial dependency state – easily! ?? Test against actual, close to product

Dec 29, 2022
:triangular_ruler:gofmtmd formats go source code block in Markdown. detects fenced code & formats code using gofmt.
:triangular_ruler:gofmtmd formats go source code block in Markdown. detects fenced code & formats code using gofmt.

gofmtmd gofmtmd formats go source code block in Markdown. detects fenced code & formats code using gofmt. Installation $ go get github.com/po3rin/gofm

Oct 31, 2022
octocov is a tool for collecting code metrics (code coverage, code to test ratio and test execution time).

octocov is a tool for collecting code metrics (code coverage, code to test ratio and test execution time).

Jan 9, 2023