Git Product home page Git Product logo

syntaxhighlight's Introduction

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.

syntaxhighlight's People

Contributors

beyang avatar dmitshur avatar gbbr avatar hariharan-uno avatar iafan avatar luisantonioig avatar sqs avatar yinwang0 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

syntaxhighlight's Issues

Merge r3.0 into master

@gbbr, I just updated branch r3.0 to compile against the latest nodb branch of go-sourcegraph. When that is merged, can you merge r3.0 into master?

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!

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).

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.

Too many incidental dependencies.

This issue is largely documented downstream at shurcooL/go#19 (comment), 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.

Support for <ol> to have line numbers

Would you be interested in accepting iafan@0da6819 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.

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.