gosyntect

package module
v0.0.0-...-3fb5b35 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 10 Imported by: 3

README

gosyntect

gosyntect is a Go HTTP client for syntect_server, a Rust HTTP server which syntax highlights code.

Installation

go get -u github.com/sourcegraph/gosyntect/cmd/gosyntect

Usage

usage: gosyntect <server> <theme> <file.go>

example:
	gosyntect http://localhost:9238 'InspiredGitHub' gosyntect.go

API

client := gosyntect.New("http://localhost:9238")
resp, err := cl.Highlight(&gosyntect.Query{
	Extension: "go",
	Theme:     "InspiredGitHub",
	Code:      string(theGoCode),
})
if err != nil {
	log.Fatal(err)
}
fmt.Println(resp.Data) // prints highlighted HTML

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidTheme is returned when the Query.Theme is not a valid theme.
	ErrInvalidTheme = errors.New("invalid theme")

	// ErrRequestTooLarge is returned when the request is too large for syntect_server to handle (e.g. file is too large to highlight).
	ErrRequestTooLarge = errors.New("request too large")

	// ErrPanic occurs when syntect_server panics while highlighting code. This
	// most often occurs when Syntect does not support e.g. an obscure or
	// relatively unused sublime-syntax feature and as a result panics.
	ErrPanic = errors.New("syntect panic while highlighting")

	// ErrHSSWorkerTimeout occurs when syntect_server's wrapper,
	// http-server-stabilizer notices syntect_server is taking too long to
	// serve a request, has most likely gotten stuck, and as such has been
	// restarted. This occurs rarely on certain files syntect_server cannot yet
	// handle for some reason.
	ErrHSSWorkerTimeout = errors.New("HSS worker timeout while serving request")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents a client connection to a syntect_server.

func New

func New(syntectServer string) *Client

New returns a client connection to a syntect_server.

func (*Client) Highlight

func (c *Client) Highlight(ctx context.Context, q *Query) (*Response, error)

Highlight performs a query to highlight some code.

type Query

type Query struct {
	// Extension is deprecated: use Filepath instead.
	Extension string `json:"extension"`

	// Filepath is the file path of the code. It can be the full file path, or
	// just the name and extension.
	//
	// See: https://github.com/sourcegraph/syntect_server#supported-file-extensions
	Filepath string `json:"filepath"`

	// Theme is the color theme to use for highlighting.
	// If CSS is true, theme is ignored.
	//
	// See https://github.com/sourcegraph/syntect_server#embedded-themes
	Theme string `json:"theme"`

	// Code is the literal code to highlight.
	Code string `json:"code"`

	// CSS causes results to be returned in HTML table format with CSS class
	// names annotating the spans rather than inline styles.
	CSS bool `json:"css"`

	// LineLengthLimit is the maximum length of line that will be highlighted if set.
	// Defaults to no max if zero.
	// If CSS is false, LineLengthLimit is ignored.
	LineLengthLimit int `json:"line_length_limit,omitempty"`

	// StabilizeTimeout, if non-zero, overrides the default syntect_server
	// http-server-stabilizer timeout of 10s. This is most useful when a user
	// is requesting to highlight a very large file and is willing to wait
	// longer, but it is important this not _always_ be a long duration because
	// the worker's threads could get stuck at 100% CPU for this amount of
	// time if the user's request ends up being a problematic one.
	StabilizeTimeout time.Duration `json:"-"`

	// Tracer, if not nil, will be used to record opentracing spans associated with the query.
	Tracer opentracing.Tracer
}

Query represents a code highlighting query to the syntect_server.

type Response

type Response struct {
	// Data is the actual highlighted HTML version of Query.Code.
	Data string

	// Plaintext indicates whether or not a syntax could not be found for the
	// file and instead it was rendered as plain text.
	Plaintext bool
}

Response represents a response to a code highlighting query.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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