moonrockz/notebooks/dib
Types
Cell
type Cell derive(Eq, @moonbitlang/core/debug.Debug)An immutable source cell with a kernel name. Sources normalize line endings and trim only empty boundary lines. Notebook construction resolves kernel aliases; a standalone cell may still contain an alias.
Cell::Cell
fn Cell::Cell(source : String, kernel~ : KernelName) -> CellNormalize LF, CRLF and CR to LF and remove empty boundary lines. Retain whitespace-only and internal blank lines. Registry membership is checked by Notebook construction; selector conflicts are checked on writing.
Cell::directives
fn Cell::directives(self : Cell) -> ReadOnlyArray[Directive]Return column-zero #! lines in source order as immutable directive views. Indented lines are excluded. Kernel selectors consumed during parsing are cell properties and do not appear in parsed cell source.
Cell::equal
fn Cell::equal(Cell, Cell) -> BoolCell::kernel
fn Cell::kernel(self : Cell) -> KernelNameReturn the cell's kernel name. Notebook construction canonicalizes registered aliases; a standalone Cell retains the supplied name.
Cell::kind
fn Cell::kind(self : Cell) -> @moonrockz/notebooks.CellKindClassify markdown and md cells as Markdown and other kernels as Code.
Cell::metadata
fn Cell::metadata(_self : Cell) -> @moonrockz/notebooks.Metadata?DIB has no stored MIME bundles, so capability inspection uses the shared default of None. It also has no cell-local metadata container.
Cell::mime_capability
fn Cell::mime_capability(_self : Cell) -> &@moonrockz/notebooks.MimeTyped?DIB has no stored MIME bundles, so capability inspection uses the shared default of None. It also has no cell-local metadata container.
Cell::not_equal
fn Cell::not_equal(x : Cell, y : Cell) -> BoolCell::source
fn Cell::source(self : Cell) -> StringReturn normalized cell source without its kernel selector.
Cell::to_repr
fn Cell::to_repr(Cell) -> @moonbitlang/core/debug.Reprimpl CellView for Cell
CellView::kind
fn kind(self : Cell) -> @moonrockz/notebooks.CellKindCellView::source
fn source(self : Cell) -> String?Directive
type Directive derive(Eq, @moonbitlang/core/debug.Debug)An immutable column-zero #! line retained in cell source, with a name, unparsed arguments, exact normalized line text and one-based source line. This view does not execute commands or interpret quoted arguments.
Directive::arguments
fn Directive::arguments(self : Directive) -> StringReturn the remaining command text with leading whitespace removed. Arguments remain uninterpreted; quoting is not parsed.
Directive::equal
fn Directive::equal(Directive, Directive) -> BoolDirective::line
fn Directive::line(self : Directive) -> IntReturn the one-based line relative to normalized cell source, not the line number in the original DIB document.
Directive::name
fn Directive::name(self : Directive) -> StringReturn the command token after #!, stopping at the first space or tab.
Directive::not_equal
fn Directive::not_equal(x : Directive, y : Directive) -> BoolDirective::text
fn Directive::text(self : Directive) -> StringReturn the complete #! source line, without a newline terminator.
Directive::to_repr
fn Directive::to_repr(Directive) -> @moonbitlang/core/debug.ReprKernelInfo
type KernelInfo derive(Eq, @moonbitlang/core/debug.Debug)An immutable registration describing a canonical kernel name, its aliases and an optional language label. Names alone do not execute or install a kernel.
KernelInfo::KernelInfo
fn KernelInfo::KernelInfo(name : KernelName, aliases? : ReadOnlyArray[KernelName], language? : String?) -> KernelInfoDescribe a kernel using validated names. Defaults to no aliases and no language label. Registry construction checks name and alias collisions.
KernelInfo::aliases
fn KernelInfo::aliases(self : KernelInfo) -> ReadOnlyArray[KernelName]Return immutable case-sensitive aliases for this kernel.
KernelInfo::equal
fn KernelInfo::equal(KernelInfo, KernelInfo) -> BoolKernelInfo::language
fn KernelInfo::language(self : KernelInfo) -> String?Return the optional language label supplied by the registration.
KernelInfo::name
fn KernelInfo::name(self : KernelInfo) -> KernelNameReturn this registration's canonical name.
KernelInfo::not_equal
fn KernelInfo::not_equal(x : KernelInfo, y : KernelInfo) -> BoolKernelInfo::to_repr
fn KernelInfo::to_repr(KernelInfo) -> @moonbitlang/core/debug.ReprKernelName
type KernelName derive(Eq, @moonbitlang/core/debug.Debug)A validated kernel name or alias. Names are case-sensitive and nonblank; they cannot start with #, contain line breaks or name a metadata header.
KernelName::KernelName
fn KernelName::KernelName(value : String) -> KernelName raise FormatErrorValidate a case-sensitive kernel name or alias. Raise InvalidKernelName for blank names, leading #, line breaks or reserved metadata selectors. This validates spelling, not membership in a kernel registry.
test "doc: resolve a kernel alias" {
let registry = @dib.KernelRegistry::standard()
let name = @dib.KernelName("csharp")
@expect.expect(registry.resolve("c#")).to_equal(Some(name))
}
KernelName::equal
fn KernelName::equal(KernelName, KernelName) -> BoolKernelName::not_equal
fn KernelName::not_equal(x : KernelName, y : KernelName) -> BoolKernelName::to_repr
fn KernelName::to_repr(KernelName) -> @moonbitlang/core/debug.ReprKernelName::to_string
fn KernelName::to_string(self : KernelName) -> StringReturn the name or alias exactly as supplied.
KernelRegistry
type KernelRegistry derive(Eq, @moonbitlang/core/debug.Debug)An immutable execution environment used to distinguish cell selectors from source directives. Lookups map canonical names and aliases to canonical KernelName values. Parsing also supplies the Markdown pseudo-kernel.
KernelRegistry::KernelRegistry
fn KernelRegistry::KernelRegistry(kernels : ReadOnlyArray[KernelInfo], default_kernel~ : KernelName) -> KernelRegistry raise FormatErrorBuild a registry and resolve its required default name or alias. Raise DuplicateKernelName for any repeated name/alias, including within one registration, or UnknownKernel if the default cannot be resolved.
KernelRegistry::default_kernel
fn KernelRegistry::default_kernel(self : KernelRegistry) -> KernelNameReturn the default kernel's canonical name.
KernelRegistry::equal
fn KernelRegistry::equal(KernelRegistry, KernelRegistry) -> BoolKernelRegistry::kernels
fn KernelRegistry::kernels(self : KernelRegistry) -> ReadOnlyArray[KernelInfo]Return immutable registrations in their stored order.
KernelRegistry::not_equal
fn KernelRegistry::not_equal(x : KernelRegistry, y : KernelRegistry) -> BoolKernelRegistry::resolve
fn KernelRegistry::resolve(self : KernelRegistry, name : String) -> KernelName?Resolve an exact, case-sensitive name or alias to its canonical name. Return None for an unregistered name.
KernelRegistry::standard
fn KernelRegistry::standard() -> KernelRegistryReturn the standard execution-kernel registrations with C# as default. Includes C#, F#, PowerShell, JavaScript, HTML, SQL, KQL, HTTP, value and Mermaid. Parsing and Notebook construction also add Markdown as needed.
KernelRegistry::to_repr
fn KernelRegistry::to_repr(KernelRegistry) -> @moonbitlang/core/debug.ReprMetadataOutput
pub(all) enum MetadataOutput {
Auto
Json
Ion
} derive(Eq, @moonbitlang/core/debug.Debug)DIB output keeps JSON when it preserves the metadata, otherwise uses Ion. Explicit Json requests reject metadata that cannot round-trip through JSON.
MetadataOutput::equal
fn MetadataOutput::equal(MetadataOutput, MetadataOutput) -> BoolMetadataOutput::not_equal
fn MetadataOutput::not_equal(x : MetadataOutput, y : MetadataOutput) -> BoolMetadataOutput::to_repr
fn MetadataOutput::to_repr(MetadataOutput) -> @moonbitlang/core/debug.ReprNode
pub(all) enum Node {
Notebook(Notebook)
Cell(Cell)
Directive(Directive)
} derive(Eq, @moonbitlang/core/debug.Debug)A concrete DIB traversal node. Notebook children are cells in source order; cell children are retained directives in source order. Directives are leaves. Metadata and kernel registrations use their own accessors.
Node::at
fn Node::at(self : Node, path : @moonrockz/notebooks.NodePath) -> Node?Resolve a shared child-index path relative to this node, returning None for any invalid index. Cell child indices refer to retained directives.
Node::children
fn Node::children(self : Node) -> ReadOnlyArray[Node]Return immutable cells or directives in source order. Directive leaves have no children. Kernel selectors are properties of cells, not children.
Node::cursor
fn Node::cursor(self : Node) -> @moonrockz/notebooks.Cursor[Node]Establish a new navigation root at this node. Use an existing cursor to retain its original paths and ancestors.
Node::equal
fn Node::equal(Node, Node) -> BoolNode::kind
fn Node::kind(self : Node) -> @moonrockz/notebooks.NodeKindClassify this concrete node for shared traversal. Directive kinds retain the command name, while concrete nodes retain the complete directive.
Node::metadata
fn Node::metadata(self : Node) -> @moonrockz/notebooks.Metadata?Return this node's own metadata. Some(empty) retains an empty container; None means this node has none. Ancestor metadata is never inherited.
Node::not_equal
fn Node::not_equal(x : Node, y : Node) -> BoolNode::source
fn Node::source(self : Node) -> String?Return normalized cell source or a directive's source line. Return None for a notebook node.
Node::to_repr
fn Node::to_repr(Node) -> @moonbitlang/core/debug.Reprimpl Node for Node
Node::children
fn children(self : Node) -> ReadOnlyArray[Node]Node::kind
fn kind(self : Node) -> @moonrockz/notebooks.NodeKindNode::metadata
fn metadata(self : Node) -> @moonrockz/notebooks.Metadata?Node::source
fn source(self : Node) -> String?Notebook
type Notebook derive(Eq, @moonbitlang/core/debug.Debug)An immutable DIB source notebook with ordered cells, metadata and the kernel registry needed to interpret its selectors. DIB stores no outputs.
Notebook::Notebook
fn Notebook::Notebook(cells : ReadOnlyArray[Cell], metadata? : @moonrockz/notebooks.Metadata, registry? : KernelRegistry) -> Notebook raise FormatErrorBuild a notebook with empty metadata and the standard registry by default. Interpret metadata registrations and canonicalize each cell's kernel alias. Raise FormatError for invalid registration metadata, duplicate kernel names or unregistered cell kernels. Empty cells remain until serialization.
Notebook::cell
fn Notebook::cell(self : Notebook, index : Int) -> Cell?Look up a zero-based cell index. Return None for negative or out-of-range indices.
Notebook::cells
fn Notebook::cells(self : Notebook) -> ReadOnlyArray[Cell]Return immutable cells in source order, including constructed empty cells.
Notebook::cells_for_kernel
fn Notebook::cells_for_kernel(self : Notebook, name : String) -> ReadOnlyArray[Cell]Resolve a canonical name or alias, then select matching cells in source order. Return an empty immutable array for an unknown name. Markdown is a selectable pseudo-kernel.
Notebook::cursor
fn Notebook::cursor(self : Notebook) -> @moonrockz/notebooks.Cursor[Node]Start a typed DIB cursor at this notebook with depth zero and path $.
Notebook::equal
fn Notebook::equal(Notebook, Notebook) -> BoolNotebook::fold
fn[A] Notebook::fold(self : Notebook, init : A, f : (A, @moonrockz/notebooks.Cursor[Node]) -> A) -> AThread an accumulator through this notebook tree in pre-order, supplying typed DIB cursors. Use walk for pruning or early termination.
Notebook::metadata
fn Notebook::metadata(self : Notebook) -> @moonrockz/notebooks.MetadataReturn immutable document metadata, retaining custom namespaces.
Notebook::not_equal
fn Notebook::not_equal(x : Notebook, y : Notebook) -> BoolNotebook::registry
fn Notebook::registry(self : Notebook) -> KernelRegistryReturn the effective registry, including metadata declarations and Markdown. Retain it when rereading custom environment-dependent selectors.
Notebook::root
fn Notebook::root(self : Notebook) -> NodeWrap this notebook as the concrete root accepted by shared traversal.
Notebook::to_repr
fn Notebook::to_repr(Notebook) -> @moonbitlang/core/debug.ReprNotebook::to_string
fn Notebook::to_string(self : Notebook, metadata_output? : MetadataOutput) -> String raise FormatErrorSerialize normalized DIB source using canonical kernel selectors. Omit empty cells and retain document metadata. Auto output uses JSON when it preserves the metadata, otherwise Ion. Explicit Json rejects data loss. Emit a header when needed to preserve later metadata directives as source. Raise UnrepresentableSource if embedded registered selectors would split a cell. Custom kernels supplied only by the environment require the same registry on subsequent reads; the writer does not inject those registrations.
Errors
FormatError
pub(all) suberror FormatError {
InvalidMetadata(line~ : Int, message~ : String)
InvalidKernelName(String)
DuplicateKernelName(String)
UnknownKernel(String)
UnrepresentableSource(cell~ : Int, line~ : Int)
} derive(Eq, @moonbitlang/core/debug.Debug)A DIB metadata, kernel registration or serialization failure. UnrepresentableSource identifies a zero-based cell index and a one-based line within its normalized source. Metadata line numbers are one-based.
FormatError::equal
fn FormatError::equal(FormatError, FormatError) -> BoolFormatError::not_equal
fn FormatError::not_equal(x : FormatError, y : FormatError) -> BoolFormatError::to_repr
fn FormatError::to_repr(FormatError) -> @moonbitlang/core/debug.ReprType aliases
Control
using @moonrockz/notebooks { type Control }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
Cursor
using @moonrockz/notebooks { type Cursor }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
Metadata
using @moonrockz/notebooks { type Metadata }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
NodePath
using @moonrockz/notebooks { type NodePath }
Shared navigation types. Explicit cursor annotations use Cursor[Node].
Values
parse
fn parse(source : String, registry? : KernelRegistry) -> Notebook raise FormatErrorRead a DIB source notebook without executing it. Registered, exact column-zero selectors start cells; other directives remain source. Read the first exact metadata section and apply its registrations. #!meta and #!meta json read JSON; #!meta yaml, #!meta toml and #!meta ion are extensions. Normalize line endings, trim empty boundary lines and omit empty sections; an empty document yields one empty cell. Accept a leading BOM. Raise FormatError for invalid metadata or conflicting kernel registrations.
walk
fn walk(root : Node, enter : (Node) -> @moonrockz/notebooks.Control, leave? : (Node) -> Unit) -> UnitWalk concrete DIB nodes in depth-first pre-order with the shared engine. Use walk_cursor when callbacks need path or ancestor context.
walk_cursor
fn walk_cursor(root : Node, enter : (@moonrockz/notebooks.Cursor[Node]) -> @moonrockz/notebooks.Control, leave? : (@moonrockz/notebooks.Cursor[Node]) -> Unit) -> UnitUse the shared engine to walk typed DIB positions from a new root. SkipChildren still receives leave; Stop cancels pending leave calls.