cnm-go: contnet.org/lib/cnm-go Index | Files | Directories

package cnm

import "contnet.org/lib/cnm-go"

Package cnm implements CNM document parsing and composition.

Index

Package Files

content.go document.go parse.go selector.go simpletext.go token.go

Variables

var TopLevel = &TokenBlock{
    TokenLine: TokenLine{
        Indentation: -1,
        RawLine:     "",
        LineNo:      0,
    },
    Parent: nil,
    Name:   "",
    Args:   nil,
}

TopLevel represents the top-level block.

func CollapseWhitespace Uses

func CollapseWhitespace(raw string) string

CollapseWhitespace collapses sequences of non-escaped whitespace in raw CNM simple text into single spaces.

func Escape Uses

func Escape(s string) string

Escape escapes whitespace, backslash and and U+0000 within s.

func EscapeNonspace Uses

func EscapeNonspace(s string) string

EscapeNonspace works like Escape, except it does not escape spaces, tabs and line feeds.

func EscapeSpace Uses

func EscapeSpace(s string) string

EscapeSpace works like Escape, except it only escapes spaces, tabs and line feeds.

func JoinEscape Uses

func JoinEscape(ss []string) string

JoinEscape escapes each argument using simple text escape sequences and then joins them with spaces.

func ParseSimpleText Uses

func ParseSimpleText(raw string) string

ParseSimpleText parses raw as simple text (collapses whitespace and resolves escape sequences).

func RegisterTextContentParser Uses

func RegisterTextContentParser(name string, parser TextContentParser)

RegisterTextContentParser registers a new text content parser for a format.

func SplitUnescape Uses

func SplitUnescape(s string) []string

SplitUnescape splits the string s by whitespace, then unescapes simple text escape sequences.

func Unescape Uses

func Unescape(s string) string

Unescape resolves escape sequences in simple text.

func WriteIndent Uses

func WriteIndent(w io.Writer, s string, depth int) error

WriteIndent writes an indented line into a writer.

Writes depth tab characters, the string s and a newline. If s is blank, no indentation is used. s should not contain newlines.

type Block Uses

type Block interface {
    // Name returns the name of the block.
    Name() string

    // Args returns the block arguments.
    Args() []string

    WriteIndent(w io.Writer, n int) error
}

Block represents an arbitrary CNM within the "content" top-level block.

func SelectIndex Uses

func SelectIndex(block Block, indices ...int) Block

SelectIndex selects a child block based on an index path.

Note that this is *not* the same as a CNM section index selector.

type ContainerBlock Uses

type ContainerBlock interface {
    Block

    // Children returns the child blocks.
    Children() []Block

    // AppendChild adds a new child block to the end of the list of children.
    AppendChild(Block)
}

ContainerBlock represents a content block that contains only other blocks.

type ContentBlock Uses

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

ContentBlock represents a block that holds other content blocks.

func NewContentBlock Uses

func NewContentBlock(name string, args ...string) *ContentBlock

NewContentBlock creates a new ContentBlock with a name and argument.

func (*ContentBlock) AppendChild Uses

func (cb *ContentBlock) AppendChild(block Block)

AppendChild adds a new child block to the end of the list of children.

func (*ContentBlock) Args Uses

func (cb *ContentBlock) Args() []string

Args returns the block arguments.

func (*ContentBlock) Children Uses

func (cb *ContentBlock) Children() []Block

Children returns the block's child blocks.

func (*ContentBlock) Name Uses

func (cb *ContentBlock) Name() string

Name returns the block's name.

func (*ContentBlock) WriteIndent Uses

func (cb *ContentBlock) WriteIndent(w io.Writer, n int) error

WriteIndent writes the block header and its children indented by n tabs.

type Document Uses

type Document struct {
    // Title is the document title (top-level "title" block).
    Title string

    // Links is a list of document-level hyperlinks (top-level "links" block).
    Links []Link

    // Site is a sitemap (top-level "site" block).
    Site Site

    // Content is the document content (top-level "content" block).
    Content *ContentBlock
}

Document represents a CNM document.

func NewDocument Uses

func NewDocument() *Document

NewDocument creates a new Document.

func ParseDocument Uses

func ParseDocument(r io.Reader) (doc *Document, err error)

ParseDocument parses a CNM document from r.

func (*Document) Args Uses

func (doc *Document) Args() []string

Args returns a nil slice (top-level block context has no args).

func (*Document) Name Uses

func (doc *Document) Name() string

Name returns an empty string (top-level block context has no name).

func (*Document) Select Uses

func (doc *Document) Select(selector string) (*Document, error)

Select selects content from the document based on an extended CNM selector.

See ContNet draft/cnm-selector for more information about content selectors.

This function may create copies of the document and all container content blocks, but includes non-container blocks untouched in the copy.

func (*Document) SelectBlock Uses

func (doc *Document) SelectBlock(selector string) (indices []int, block Block, err error)

SelectBlock selects section blocks from the document with a selector query.

Returns the block index path within content blocks, the target block and, if the query was invalid, an error. All are nil when no block was found with a valid selector.

See ContNet draft/cnm-selector for more information.

func (*Document) SelectIndex Uses

func (doc *Document) SelectIndex(indices ...int) (block Block)

SelectIndex selects a content block based on an index path.

For example, indices=[]int{1, 4, 2} selects doc.Content.Children()[1].Children()[4].Children(2) if it exists, otherwise returns nil. Note that this is *not* the same as a CNM index selector.

func (*Document) Write Uses

func (doc *Document) Write(w io.Writer) error

Write writes the document to w.

func (*Document) WriteIndent Uses

func (doc *Document) WriteIndent(w io.Writer, n int) error

WriteIndent writes the document to w.

n must be 0 or this function will panic.

type EmbedBlock Uses

type EmbedBlock struct {
    // Type is the content type (first word of block argument).
    Type string

    // URL is the content URL (second word of the block argument).
    URL string

    // Description is the content description (block body as simple text).
    Description string
}

EmbedBlock represents an "embed" content block.

func NewEmbedBlock Uses

func NewEmbedBlock(typ, url, desc string) *EmbedBlock

NewEmbedBlock creates a new EmbedBlock.

func (*EmbedBlock) Args Uses

func (e *EmbedBlock) Args() []string

Args returns the block argument (type and URL).

func (*EmbedBlock) Name Uses

func (e *EmbedBlock) Name() string

Name returns the block name "embed".

func (*EmbedBlock) WriteIndent Uses

func (e *EmbedBlock) WriteIndent(w io.Writer, n int) error

WriteIndent writes the embed block header and contents indented by n tabs.

type HeaderBlock Uses

type HeaderBlock struct {
    ContentBlock
}

HeaderBlock represents a "header" table block.

func NewHeaderBlock Uses

func NewHeaderBlock() *HeaderBlock

NewHeaderBlock creates a new HeaderBlock.

type Link struct {
    // URL is the hyperlink URL.
    URL string

    // Name is the hyperlink text.
    Name string

    // Description is the description of the hyperlink.
    Description string
}

Link represents a document-level hyperlink in the "links" top-level block.

func (Link) WriteIndent Uses

func (link Link) WriteIndent(w io.Writer, n int) error

WriteIndent writes the link URL, name and description indented by n tabs.

type ListBlock Uses

type ListBlock struct {
    ContentBlock
}

ListBlock represents a "list" content block.

func NewListBlock Uses

func NewListBlock(ordered bool) *ListBlock

NewListBlock creates a new ListBlock.

If the ordered parameter is true, the list is created in "ordered" mode.

func (*ListBlock) Ordered Uses

func (b *ListBlock) Ordered() bool

Ordered returns true if the list is in ordered mode (first word of the block argument is "ordered").

type Parser Uses

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

Parser parses a CNM document by lines.

func NewParser Uses

func NewParser(r io.Reader) *Parser

NewParser creates a new Parser that reads from r.

func (*Parser) Block Uses

func (p *Parser) Block() Token

Block parses the current line in block mode.

Returns a TokenBlock if the line was not empty, otherwise TokenEmptyLine. In block mode, a line is empty even if its indentation exceeds the block content indentation, as long as it only contains tab characters.

Next() must have been called before calling Block().

func (*Parser) Empty Uses

func (p *Parser) Empty() bool

Empty returns true if the current line is empty.

func (*Parser) Indent Uses

func (p *Parser) Indent() int

Indent returns the indentation of the current line.

Returns -1 if no line has been read yet.

func (*Parser) Line Uses

func (p *Parser) Line() int

Line returns the number of the last parsed line in the document, starting with 1 after the first line.

func (*Parser) Next Uses

func (p *Parser) Next() error

Next retrieves the next line.

func (*Parser) RawText Uses

func (p *Parser) RawText() Token

RawText parses the current line as raw text.

Returns a TokenRawText if the line was not empty, otherwise TokenEmptyLine.

Next() must have been called before calling RawText().

func (*Parser) SimpleText Uses

func (p *Parser) SimpleText() Token

SimpleText parses the current line as simple text.

Returns a TokenSimpleText if the line was not empty, otherwise TokenEmptyLine.

Next() must have been called before calling SimpleText().

type RawBlock Uses

type RawBlock struct {
    // Syntax is the syntax of the block contents (first word of block argument)
    Syntax string

    // Contents is the raw content.
    Contents TextRawContents
}

RawBlock represents a "raw" content block.

func NewRawBlock Uses

func NewRawBlock(syntax, contents string) *RawBlock

NewRawBlock creates a new RawBlock.

func (*RawBlock) Args Uses

func (r *RawBlock) Args() []string

Args returns the block's arguments (syntax).

func (*RawBlock) Name Uses

func (r *RawBlock) Name() string

Name returns the block name "raw".

func (*RawBlock) WriteIndent Uses

func (r *RawBlock) WriteIndent(w io.Writer, n int) error

WriteIndent writes the raw content indented by n tabs.

type RowBlock Uses

type RowBlock struct {
    ContentBlock
}

RowBlock represents a "row" table block.

func NewRowBlock Uses

func NewRowBlock() *RowBlock

NewRowBlock creates a new RowBlock.

type SectionBlock Uses

type SectionBlock struct {
    ContentBlock
}

SectionBlock represents a "section" content block.

func NewSectionBlock Uses

func NewSectionBlock(title string) *SectionBlock

NewSectionBlock creates a new SectionBlock with a title.

func (*SectionBlock) Title Uses

func (b *SectionBlock) Title() string

Title returns the section block's title.

type Site Uses

type Site struct {
    // Path is the node's path fragment.
    Path string

    // Name is the node's name.
    Name string

    // Children are the nodes below this node.
    Children []Site
}

Site represents a node in the sitemap in the "site" top-level block.

func (Site) WriteIndent Uses

func (site Site) WriteIndent(w io.Writer, n int) error

WriteIndent writes the sitemap indented by n tabs.

type TableBlock Uses

type TableBlock struct {
    ContentBlock
}

TableBlock represents a "table" content block.

func NewTableBlock Uses

func NewTableBlock() *TableBlock

NewTableBlock creates a new TableBlock.

type TextBlock Uses

type TextBlock struct {
    // Format is the text format (first word of the block argument).
    Format string
    // Contents are the text contents.
    Contents TextContents
}

TextBlock represents a "text" content block.

func NewTextBlock Uses

func NewTextBlock(format string, contents TextContents) *TextBlock

NewTextBlock creates a new TextBlock containing arbitrary text contents.

func NewTextPlainBlock Uses

func NewTextPlainBlock(paragraphs []string) *TextBlock

NewTextPlainBlock creates a new TextBlock containing TextPlainContents.

func NewTextPreBlock Uses

func NewTextPreBlock(text string) *TextBlock

NewTextPreBlock creates a new TextBlock containing TextPreContents.

func (*TextBlock) Args Uses

func (t *TextBlock) Args() []string

Args returns the block's arguments (format).

func (*TextBlock) Name Uses

func (t *TextBlock) Name() string

Name returns the block name "text".

func (*TextBlock) WriteIndent Uses

func (t *TextBlock) WriteIndent(w io.Writer, n int) error

WriteIndent writes the block header and its content indented by n tabs.

type TextContentParser Uses

type TextContentParser func(p *Parser, block *TokenBlock) (TextContents, error)

TextContentParser parses text content in a text block.

func GetTextContentParser Uses

func GetTextContentParser(name string) TextContentParser

GetTextContentParser retrieves a text content parser or nil if it doesn't exist.

type TextContents Uses

type TextContents interface {
    WriteIndent(w io.Writer, n int) error
}

TextContents represents the textual contents of a text block.

type TextPlainContents Uses

type TextPlainContents struct {
    // Paragraphs is a list of simple text paragraphs.
    Paragraphs []string
}

TextPlainContents represents a list of simple text paragraphs.

func (TextPlainContents) WriteIndent Uses

func (t TextPlainContents) WriteIndent(w io.Writer, n int) error

WriteIndent writes the plain text content indented by n tabs.

type TextPreContents Uses

type TextPreContents struct {
    // Text is the preformatted content.
    Text string
}

TextPreContents represents preformatted contents of a text block.

func (TextPreContents) WriteIndent Uses

func (t TextPreContents) WriteIndent(w io.Writer, n int) error

WriteIndent writes the preformatted text content indented by n tabs.

type TextRawContents Uses

type TextRawContents struct {
    // Text is the raw content.
    Text string
}

TextRawContents represents raw contents of a text or raw block.

func (TextRawContents) WriteIndent Uses

func (t TextRawContents) WriteIndent(w io.Writer, n int) error

WriteIndent writes the raw content indented by n tabs.

type Token Uses

type Token interface {
    Indent() int
    Raw() string
    Line() int
}

Token represents a parsed line in a CNM document.

type TokenBlock Uses

type TokenBlock struct {
    TokenLine
    // Parent is the parent block
    Parent *TokenBlock
    // Name is the block name.
    Name string
    // Args are the block arguments, split by whitespace and then parsed as
    // simple text.
    Args []string
}

TokenBlock represents a block header line.

type TokenEmptyLine Uses

type TokenEmptyLine struct {
    TokenLine
}

TokenEmptyLine represents an empty line.

A line is empty as long as it contains up to as many tab characters as the line's indentation and nothing else.

type TokenLine Uses

type TokenLine struct {
    Indentation int
    RawLine     string
    LineNo      int
}

TokenLine represents an arbitrary CNM line.

func (*TokenLine) Indent Uses

func (t *TokenLine) Indent() int

Indent returns the indentation of the parsed line.

func (*TokenLine) Line Uses

func (t *TokenLine) Line() int

Line returns the line number in the document, starting from 1.

func (*TokenLine) Raw Uses

func (t *TokenLine) Raw() string

Raw returns the original unparsed line.

type TokenRawText Uses

type TokenRawText struct {
    TokenLine
    // Text is the raw contents of the line with the indentation removed.
    Text string
}

TokenRawText represents a non-empty line with unparsed contents.

type TokenSimpleText Uses

type TokenSimpleText struct {
    TokenLine
    // Text is the line contents parsed as simple text.
    Text string
}

TokenSimpleText represents a line of simple text.

Directories

PathSynopsis
cnmfmtPackage cnmfmt provides parsing and composition for CNMfmt formatting.

Package cnm imports 10 packages (graph) and is imported by 1 packages. Updated 2017-09-23. Refresh now. Tools for package owners.