syntaxhighlight

package module
v0.0.0-...-bd320f5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2017 License: BSD-3-Clause Imports: 9 Imported by: 0

README

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.

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/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

Examples

Constants

This section is empty.

Variables

View Source
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",
	Whitespace:    "",
}

DefaultHTMLConfig provides class names that match those of google-code-prettify (https://code.google.com/p/google-code-prettify/).

Functions

func Annotate

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

func AsHTML

func AsHTML(src []byte, options ...Option) ([]byte, error)

AsHTML converts source code into an HTML-highlighted version; It accepts optional configuration parameters to control rendering (see OrderedList as one example)

func NewScanner

func NewScanner(src []byte) *scanner.Scanner

NewScanner is a helper that takes a []byte src, wraps it in a reader and creates a Scanner.

func NewScannerReader

func NewScannerReader(src io.Reader) *scanner.Scanner

NewScannerReader takes a reader src and creates a Scanner.

func Print

func Print(s *scanner.Scanner, w io.Writer, p Printer) error

Types

type Annotator

type Annotator interface {
	Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
}

type HTMLAnnotator

type HTMLAnnotator HTMLConfig

func (HTMLAnnotator) Annotate

func (a HTMLAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)

type HTMLConfig

type HTMLConfig struct {
	String        string
	Keyword       string
	Comment       string
	Type          string
	Literal       string
	Punctuation   string
	Plaintext     string
	Tag           string
	HTMLTag       string
	HTMLAttrName  string
	HTMLAttrValue string
	Decimal       string
	Whitespace    string

	AsOrderedList bool
}

HTMLConfig holds the HTML class configuration to be used by annotators when highlighting code.

func (HTMLConfig) Class

func (c HTMLConfig) Class(kind Kind) string

Class returns the set class for a given token Kind.

type HTMLPrinter

type HTMLPrinter HTMLConfig

HTMLPrinter implements Printer interface and is used to produce HTML-based highligher

func (HTMLPrinter) Print

func (p HTMLPrinter) Print(w io.Writer, kind Kind, tokText string) error

Print is the function that emits highlighted source code using <span class="...">...</span> wrapper tags

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.

const (
	Whitespace Kind = iota
	String
	Keyword
	Comment
	Type
	Literal
	Punctuation
	Plaintext
	Tag
	HTMLTag
	HTMLAttrName
	HTMLAttrValue
	Decimal
)

A set of supported highlighting kinds

func (Kind) GoString

func (i Kind) GoString() string

type Option

type Option func(options *HTMLConfig)

Option is a type of the function that can modify one or more of the options in the HTMLConfig structure.

func OrderedList

func OrderedList() Option

OrderedList allows you to format the output as an ordered list to have line numbers in the output.

Example: AsHTML(input, OrderedList())

type Printer

type Printer interface {
	Print(w io.Writer, kind Kind, tokText string) error
}

Printer implements an interface to render highlighted output (see HTMLPrinter for the implementation of this interface)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL