nodes

package
v1.0.0-alpha1 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var IframeAllowlist = []string{
	"carto.com",
	"codepen.io",
	"dartlang.org",
	"dartpad.dev",
	"demo.arcade.software",
	"github.com",
	"glitch.com",
	"google.com",
	"google.dev",
	"observablehq.com",
	"repl.it",
	"stackblitz.com",
	"vimeo.com",
	"web.dev",
}

iframe allowlist - set of domains allow to embed iframes in a codelab. TODO make this configurable somehow

Functions

func EmptyNodes

func EmptyNodes(nodes []Node) bool

EmptyNodes returns true if all of nodes are empty.

func IsHeader

func IsHeader(t NodeType) bool

IsHeader returns true if t is one of header types.

func IsInline

func IsInline(t NodeType) bool

IsInline returns true if t is an inline node type.

func IsItemsList

func IsItemsList(t NodeType) bool

IsItemsList returns true if t is one of ItemsListNode types.

Types

type ButtonNode

type ButtonNode struct {
	Raise    bool
	Color    bool
	Download bool
	Content  *ListNode
	// contains filtered or unexported fields
}

ButtonNode represents a button, e.g. "Download Zip".

func NewButtonNode

func NewButtonNode(raise, color, download bool, n ...Node) *ButtonNode

TODO this is a long arg signature. Maybe use an options type? NewButtonNode creates a new button with optional content nodes n.

func (*ButtonNode) Block

func (b *ButtonNode) Block() interface{}

func (*ButtonNode) Empty

func (bn *ButtonNode) Empty() bool

Empty returns true if its content is empty.

func (*ButtonNode) Env

func (b *ButtonNode) Env() []string

func (*ButtonNode) MutateBlock

func (b *ButtonNode) MutateBlock(v interface{})

func (*ButtonNode) MutateEnv

func (b *ButtonNode) MutateEnv(e []string)

func (*ButtonNode) MutateType

func (b *ButtonNode) MutateType(t NodeType)

Default implementation is a no op.

func (*ButtonNode) Type

func (b *ButtonNode) Type() NodeType

type CodeNode

type CodeNode struct {
	Term  bool
	Lang  string
	Value string
	// contains filtered or unexported fields
}

CodeNode is either a source code snippet or a terminal output. TODO is there any room to consolidate Term and Lang?

func NewCodeNode

func NewCodeNode(v string, term bool, lang string) *CodeNode

NewCodeNode creates a new Node of type NodeCode. Use term argument to specify a terminal output.

func (*CodeNode) Block

func (b *CodeNode) Block() interface{}

func (*CodeNode) Empty

func (cn *CodeNode) Empty() bool

Empty returns true if cn.Value is zero, exluding space runes.

func (*CodeNode) Env

func (b *CodeNode) Env() []string

func (*CodeNode) MutateBlock

func (b *CodeNode) MutateBlock(v interface{})

func (*CodeNode) MutateEnv

func (b *CodeNode) MutateEnv(e []string)

func (*CodeNode) MutateType

func (b *CodeNode) MutateType(t NodeType)

Default implementation is a no op.

func (*CodeNode) Type

func (b *CodeNode) Type() NodeType

type GridCell

type GridCell struct {
	Colspan int
	Rowspan int
	Content *ListNode
}

GridCell is a cell of GridNode.

type GridNode

type GridNode struct {
	Rows [][]*GridCell
	// contains filtered or unexported fields
}

TODO define a convenience type for row GridNode is a 2d matrix.

func NewGridNode

func NewGridNode(rows ...[]*GridCell) *GridNode

NewGridNode creates a new grid with optional content.

func (*GridNode) Block

func (b *GridNode) Block() interface{}

func (*GridNode) Empty

func (gn *GridNode) Empty() bool

Empty returns true when every cell has empty content.

func (*GridNode) Env

func (b *GridNode) Env() []string

func (*GridNode) MutateBlock

func (b *GridNode) MutateBlock(v interface{})

func (*GridNode) MutateEnv

func (b *GridNode) MutateEnv(e []string)

func (*GridNode) MutateType

func (b *GridNode) MutateType(t NodeType)

Default implementation is a no op.

func (*GridNode) Type

func (b *GridNode) Type() NodeType

type HeaderNode

type HeaderNode struct {
	Level   int
	Content *ListNode
	// contains filtered or unexported fields
}

HeaderNode is any regular header, a checklist header, or an FAQ header.

func NewHeaderNode

func NewHeaderNode(level int, n ...Node) *HeaderNode

NewHeaderNode creates a new HeaderNode with optional content nodes n.

func (*HeaderNode) Block

func (b *HeaderNode) Block() interface{}

func (*HeaderNode) Empty

func (hn *HeaderNode) Empty() bool

Empty returns true if header content is empty.

func (*HeaderNode) Env

func (b *HeaderNode) Env() []string

func (*HeaderNode) MutateBlock

func (b *HeaderNode) MutateBlock(v interface{})

func (*HeaderNode) MutateEnv

func (b *HeaderNode) MutateEnv(e []string)

func (*HeaderNode) MutateType

func (hn *HeaderNode) MutateType(t NodeType)

MutateType sets the header's node type if the given type is a header type.

func (*HeaderNode) Type

func (b *HeaderNode) Type() NodeType

type IframeNode

type IframeNode struct {
	URL string
	// contains filtered or unexported fields
}

IframeNode is an embeddes iframe.

func NewIframeNode

func NewIframeNode(url string) *IframeNode

NewIframeNode creates a new embedded iframe.

func (*IframeNode) Block

func (b *IframeNode) Block() interface{}

func (*IframeNode) Empty

func (iframe *IframeNode) Empty() bool

Empty returns true if iframe's URL field is empty.

func (*IframeNode) Env

func (b *IframeNode) Env() []string

func (*IframeNode) MutateBlock

func (b *IframeNode) MutateBlock(v interface{})

func (*IframeNode) MutateEnv

func (b *IframeNode) MutateEnv(e []string)

func (*IframeNode) MutateType

func (b *IframeNode) MutateType(t NodeType)

Default implementation is a no op.

func (*IframeNode) Type

func (b *IframeNode) Type() NodeType

type ImageNode

type ImageNode struct {
	Src   string
	Width float32
	Alt   string
	Title string
	Bytes []byte
	// contains filtered or unexported fields
}

ImageNode represents a single image.

func ImageNodes

func ImageNodes(nodes []Node) []*ImageNode

ImageNodes extracts everything except NodeImage nodes, recursively. TODO rename

func NewImageNode

func NewImageNode(opts NewImageNodeOptions) *ImageNode

NewImageNode creates a new ImageNode with the given options. TODO this API is inconsistent with button

func (*ImageNode) Block

func (b *ImageNode) Block() interface{}

func (*ImageNode) Empty

func (in *ImageNode) Empty() bool

Empty returns true if its Src is zero, excluding space runes.

func (*ImageNode) Env

func (b *ImageNode) Env() []string

func (*ImageNode) MutateBlock

func (b *ImageNode) MutateBlock(v interface{})

func (*ImageNode) MutateEnv

func (b *ImageNode) MutateEnv(e []string)

func (*ImageNode) MutateType

func (b *ImageNode) MutateType(t NodeType)

Default implementation is a no op.

func (*ImageNode) Type

func (b *ImageNode) Type() NodeType

type ImportNode

type ImportNode struct {
	URL     string
	Content *ListNode
	// contains filtered or unexported fields
}

ImportNode indicates a remote resource available at ImportNode.URL.

func ImportNodes

func ImportNodes(nodes []Node) []*ImportNode

ImportNodes extracts everything except NodeImport nodes, recursively.

func NewImportNode

func NewImportNode(url string) *ImportNode

NewImportNode creates a new Node of type NodeImport, with initialized ImportNode.Content.

func (*ImportNode) Block

func (b *ImportNode) Block() interface{}

func (*ImportNode) Empty

func (in *ImportNode) Empty() bool

Empty returns the result of in.Content.Empty method.

func (*ImportNode) Env

func (b *ImportNode) Env() []string

func (*ImportNode) MutateBlock

func (in *ImportNode) MutateBlock(v interface{})

MutateBlock mutates both in's block marker and that of in.Content.

func (*ImportNode) MutateEnv

func (b *ImportNode) MutateEnv(e []string)

func (*ImportNode) MutateType

func (b *ImportNode) MutateType(t NodeType)

Default implementation is a no op.

func (*ImportNode) Type

func (b *ImportNode) Type() NodeType

type InfoboxKind

type InfoboxKind string

InfoboxKind defines kind type for InfoboxNode.

const (
	InfoboxPositive InfoboxKind = "special"
	InfoboxNegative InfoboxKind = "warning"
)

InfoboxNode variants.

type InfoboxNode

type InfoboxNode struct {
	Kind    InfoboxKind
	Content *ListNode
	// contains filtered or unexported fields
}

InfoboxNode is any regular header, a checklist header, or an FAQ header.

func NewInfoboxNode

func NewInfoboxNode(k InfoboxKind, n ...Node) *InfoboxNode

NewInfoboxNode creates a new infobox node with specified kind and optional content.

func (*InfoboxNode) Block

func (b *InfoboxNode) Block() interface{}

func (*InfoboxNode) Empty

func (ib *InfoboxNode) Empty() bool

Empty returns true if ib content is empty.

func (*InfoboxNode) Env

func (b *InfoboxNode) Env() []string

func (*InfoboxNode) MutateBlock

func (b *InfoboxNode) MutateBlock(v interface{})

func (*InfoboxNode) MutateEnv

func (b *InfoboxNode) MutateEnv(e []string)

func (*InfoboxNode) MutateType

func (b *InfoboxNode) MutateType(t NodeType)

Default implementation is a no op.

func (*InfoboxNode) Type

func (b *InfoboxNode) Type() NodeType

type ItemsListNode

type ItemsListNode struct {
	ListType string
	Start    int
	Items    []*ListNode
	// contains filtered or unexported fields
}

ItemsListNode containts sets of ListNode. Non-zero ListType indicates an ordered list.

func NewItemsListNode

func NewItemsListNode(typ string, start int) *ItemsListNode

NewItemsListNode creates a new ItemsListNode of type NodeItemsList, which defaults to an unordered list. Provide a positive start to make this a numbered list. NodeItemsCheck and NodeItemsFAQ are always unnumbered.

func (*ItemsListNode) Block

func (b *ItemsListNode) Block() interface{}

func (*ItemsListNode) Empty

func (il *ItemsListNode) Empty() bool

Empty returns true if every item has empty content.

func (*ItemsListNode) Env

func (b *ItemsListNode) Env() []string

func (*ItemsListNode) MutateBlock

func (b *ItemsListNode) MutateBlock(v interface{})

func (*ItemsListNode) MutateEnv

func (b *ItemsListNode) MutateEnv(e []string)

func (*ItemsListNode) MutateType

func (il *ItemsListNode) MutateType(t NodeType)

MutateType sets the items list's node type if the given type is an items list type.

func (*ItemsListNode) NewItem

func (il *ItemsListNode) NewItem(nodes ...Node) *ListNode

NewItem creates a new ListNode and adds it to il.Items.

func (*ItemsListNode) Type

func (b *ItemsListNode) Type() NodeType

type ListNode

type ListNode struct {
	Nodes []Node
	// contains filtered or unexported fields
}

ListNode contains other nodes.

func NewListNode

func NewListNode(nodes ...Node) *ListNode

NewListNode creates a new Node of type NodeList.

func (*ListNode) Append

func (l *ListNode) Append(n ...Node)

TODO remove Append appends nodes n to the end of l.Nodes slice.

func (*ListNode) Block

func (b *ListNode) Block() interface{}

func (*ListNode) Empty

func (l *ListNode) Empty() bool

Empty returns true if all l.Nodes are empty.

func (*ListNode) Env

func (b *ListNode) Env() []string

func (*ListNode) MutateBlock

func (b *ListNode) MutateBlock(v interface{})

func (*ListNode) MutateEnv

func (b *ListNode) MutateEnv(e []string)

func (*ListNode) MutateType

func (b *ListNode) MutateType(t NodeType)

Default implementation is a no op.

func (*ListNode) Type

func (b *ListNode) Type() NodeType

type NewImageNodeOptions

type NewImageNodeOptions struct {
	Src   string
	Width float32
	Alt   string
	Title string
	Bytes []byte
}

type NewTextNodeOptions

type NewTextNodeOptions struct {
	Bold   bool
	Italic bool
	Code   bool
	Value  string
}

type Node

type Node interface {
	// Type returns node type.
	Type() NodeType
	// MutateType changes node type where possible.
	// Only changes within this same category are allowed.
	// For instance, items list or header nodes can change their types
	// to another kind of items list or header.
	MutateType(NodeType)
	// Block returns a source reference of the node.
	Block() interface{}
	// MutateBlock updates source reference of the node.
	MutateBlock(interface{})
	// Empty returns true if the node has no content.
	Empty() bool
	// Env returns node environment
	Env() []string
	// MutateEnv replaces current node environment tags with env.
	MutateEnv(env []string)
}

Node is an interface common to all node types.

type NodeType

type NodeType uint32

NodeType is type for parsed codelab nodes tree.

const (
	NodeInvalid     NodeType = 1 << iota
	NodeList                 // A node which contains a list of other nodes
	NodeGrid                 // Table
	NodeText                 // Simple node with a string as the value
	NodeCode                 // Source code or console (terminal) output
	NodeInfobox              // An aside box for notes or warnings
	NodeSurvey               // Sets of grouped questions
	NodeURL                  // Represents elements such as <a href="...">
	NodeImage                // Image
	NodeButton               // Button
	NodeItemsList            // Set of NodeList items
	NodeItemsCheck           // Special kind of NodeItemsList, checklist
	NodeItemsFAQ             // Special kind of NodeItemsList, FAQ
	NodeHeader               // A header text node
	NodeHeaderCheck          // Special kind of header, checklist
	NodeHeaderFAQ            // Special kind of header, FAQ
	NodeYouTube              // YouTube video
	NodeIframe               // Embedded iframe
	NodeImport               // A node which holds content imported from another resource
)

Codelab node kinds.

type SurveyGroup

type SurveyGroup struct {
	Name    string
	Options []string
}

SurveyGroup contains group name/question and possible answers.

type SurveyNode

type SurveyNode struct {
	ID     string
	Groups []*SurveyGroup
	// contains filtered or unexported fields
}

SurveyNode contains groups of questions. Each group name is the Survey key.

func NewSurveyNode

func NewSurveyNode(id string, groups ...*SurveyGroup) *SurveyNode

NewSurveyNode creates a new survey node with optional questions. If survey is nil, a new empty map will be created. TODO is "map" above a mistake, or should the code below contain a map?

func (*SurveyNode) Block

func (b *SurveyNode) Block() interface{}

func (*SurveyNode) Empty

func (sn *SurveyNode) Empty() bool

Empty returns true if each group has 0 options.

func (*SurveyNode) Env

func (b *SurveyNode) Env() []string

func (*SurveyNode) MutateBlock

func (b *SurveyNode) MutateBlock(v interface{})

func (*SurveyNode) MutateEnv

func (b *SurveyNode) MutateEnv(e []string)

func (*SurveyNode) MutateType

func (b *SurveyNode) MutateType(t NodeType)

Default implementation is a no op.

func (*SurveyNode) Type

func (b *SurveyNode) Type() NodeType

type TextNode

type TextNode struct {
	Bold   bool
	Italic bool
	Code   bool
	Value  string
	// contains filtered or unexported fields
}

TextNode is a simple node containing text as a string value.

func NewTextNode

func NewTextNode(opts NewTextNodeOptions) *TextNode

NewTextNode creates a new Node of type NodeText.

func (*TextNode) Block

func (b *TextNode) Block() interface{}

func (*TextNode) Empty

func (tn *TextNode) Empty() bool

Empty returns true if tn.Value is zero, excluding space runes.

func (*TextNode) Env

func (b *TextNode) Env() []string

func (*TextNode) MutateBlock

func (b *TextNode) MutateBlock(v interface{})

func (*TextNode) MutateEnv

func (b *TextNode) MutateEnv(e []string)

func (*TextNode) MutateType

func (b *TextNode) MutateType(t NodeType)

Default implementation is a no op.

func (*TextNode) Type

func (b *TextNode) Type() NodeType

type URLNode

type URLNode struct {
	URL     string
	Name    string
	Target  string
	Content *ListNode
	// contains filtered or unexported fields
}

URLNode represents elements such as <a href="...">

func NewURLNode

func NewURLNode(url string, n ...Node) *URLNode

NewURLNode creates a new Node of type NodeURL with optional content n.

func (*URLNode) Block

func (b *URLNode) Block() interface{}

func (*URLNode) Empty

func (un *URLNode) Empty() bool

Empty returns true if un content is empty.

func (*URLNode) Env

func (b *URLNode) Env() []string

func (*URLNode) MutateBlock

func (b *URLNode) MutateBlock(v interface{})

func (*URLNode) MutateEnv

func (b *URLNode) MutateEnv(e []string)

func (*URLNode) MutateType

func (b *URLNode) MutateType(t NodeType)

Default implementation is a no op.

func (*URLNode) Type

func (b *URLNode) Type() NodeType

type YouTubeNode

type YouTubeNode struct {
	VideoID string
	// contains filtered or unexported fields
}

YouTubeNode is a YouTube video.

func NewYouTubeNode

func NewYouTubeNode(vid string) *YouTubeNode

NewYouTubeNode creates a new YouTube video node.

func (*YouTubeNode) Block

func (b *YouTubeNode) Block() interface{}

func (*YouTubeNode) Empty

func (yt *YouTubeNode) Empty() bool

Empty returns true if yt's VideoID field is zero.

func (*YouTubeNode) Env

func (b *YouTubeNode) Env() []string

func (*YouTubeNode) MutateBlock

func (b *YouTubeNode) MutateBlock(v interface{})

func (*YouTubeNode) MutateEnv

func (b *YouTubeNode) MutateEnv(e []string)

func (*YouTubeNode) MutateType

func (b *YouTubeNode) MutateType(t NodeType)

Default implementation is a no op.

func (*YouTubeNode) Type

func (b *YouTubeNode) Type() NodeType

Jump to

Keyboard shortcuts

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