synth

package module
v0.0.0-...-35fdda0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2024 License: MIT Imports: 9 Imported by: 1

README

nb-synth

syntax highlighting for code cells and semi-structured output (JSON/XML) with chroma.

Installation

go get github.com/bevzzz/nb-synth

Usage

Adding rich syntax highlighting to your Jupyter notebooks with synth is simple and intuitive.

Start by importing both libraries to your project:

import (
    "github.com/bevzzz/nb"
    synth "github.com/bevzzz/nb-synth"
)

Then, extend nb converter and convert your Jupyter notebook:

import (
    // synth allows configuring chroma's lexer and formatter
    // with their "native" options, so you will need to import these
    // before adding them to the extension.
    "github.com/alecthomas/chroma"
    chromahtml "github.com/alecthomas/chroma/formatters/html"
)

// Use default configurations.
c := nb.New(
    nb.WithExtensions(
        synth.Highlighting,
    ),
)

// Or, apply options to control lexing, styling, and formatting.
c := nb.New(
    nb.WithExtensions(
        synth.NewHighlighting(
            synth.WithTokenizeOptions(&chroma.TokenizeOptions{
                EnsureLF: true,
            }),
            synth.WithStyle("monokai"),
            synth.WithFormatOptions(
                chromahtml.WithClasses(true),
                chromahtml.WithLineNumbers(true),
            ),
        ),
    ),
)

if err := c.Convert(io.Stdout, b); err != nil {
    panic(err)
}

This package draws a lot from the analogous goldmark-highlighting, which adds syntax highlighting to goldmark's Markdown. If you've had the chance to work with these packages before, synth's structure and APIs will be familiar to you.

Attributions

Repository cover: Synthesizer Vectors by Vecteezy

License

This software is released under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Highlighting = NewHighlighting()

Functions

func NewHighlighting

func NewHighlighting(opts ...Option) nb.Extension

Types

type Config

type Config struct {
	Style           string
	CustomStyle     *chroma.Style
	GuessLanguage   bool
	Coalesce        bool
	FormatOptions   []chromahtml.Option
	TokeniseOptions *chroma.TokeniseOptions
	CSSWriter       io.Writer
}

func NewConfig

func NewConfig() Config

type Option

type Option func(*Config)

func WithCSSWriter

func WithCSSWriter(w io.Writer) Option

func WithCoalesce

func WithCoalesce(v bool) Option

func WithCustomStyle

func WithCustomStyle(s *chroma.Style) Option

func WithFormatOptions

func WithFormatOptions(opts ...chromahtml.Option) Option

func WithGuessLanguage

func WithGuessLanguage(v bool) Option

func WithStyle

func WithStyle(s string) Option

func WithTokenizeOptions

func WithTokenizeOptions(opt *chroma.TokeniseOptions) Option

Jump to

Keyboard shortcuts

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