format

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package format reads and writes notebooks.

A notebook is a Markdown document. Its fenced code blocks whose info string's first word is "go", "ruby" or "julia" are code cells, in Go, Ruby or Julia; everything between them is prose, kept byte for byte. GitHub and any Markdown viewer show a notebook as a document, diffs are diffs of text, and no output is ever stored in the file: results live in the kernel's value store and in exports.

# Spectrum

```go
x := ndarray.Linspace(0, 1, 1024)
```

Fences follow CommonMark: three or more backticks or tildes, indented by at most three spaces, closed by a fence of the same character at least as long. A code block in another language is prose.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	Kind Kind

	// Source is the cell's text: the Markdown of a prose cell, the Go code of
	// a code cell (without its fences, ending in a newline unless empty).
	Source string

	// Line is the 1-based line of the document where Source starts.
	Line int

	// Info is a code cell's info string after its language (its options),
	// trimmed.
	Info string

	// Lang is a code cell's language, the first word of its info string:
	// "go", "ruby" or "julia".
	Lang string
	// contains filtered or unexported fields
}

Cell is one cell of a notebook.

func NewCode

func NewCode(src string) Cell

NewCode returns a Go code cell holding src, for building a notebook in code.

func NewCodeIn added in v0.3.0

func NewCodeIn(lang, src string) Cell

NewCodeIn returns a code cell in the language lang ("go", "ruby", "julia") holding src.

func (*Cell) SetSource added in v0.2.0

func (c *Cell) SetSource(src string)

SetSource replaces a cell's source. When a line of the new source would close a code cell's fence, the fence is rebuilt when the notebook is written — backticks, longer than any run in the source, info kept — so the cell still reads back whole.

type Kind

type Kind uint8

Kind tells prose from code.

const (
	// Prose is Markdown text.
	Prose Kind = iota
	// Code is a code cell, in Go or in Ruby (see Lang).
	Code
)

type Notebook

type Notebook struct {
	Cells []Cell
}

Notebook is a parsed notebook.

func Parse

func Parse(src string) *Notebook

Parse splits a Markdown document into cells. It never fails: any text is a notebook, perhaps of prose only. An unclosed go fence runs to the end of the document, as CommonMark says.

func (*Notebook) String

func (nb *Notebook) String() string

String writes the notebook back as Markdown. For a parsed notebook it is the original text, byte for byte. A code cell made by NewCode gets a backtick fence long enough for its source.

Jump to

Keyboard shortcuts

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