sml

package
v4.24.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2017 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package sml of the Tideland Go Library provides a very simple markup language using a kind of LISP like notation with curly braces.

The tag only consists out of the chars 'a' to 'z', '0' to '9' and '-'. Also several parts of the tag can be separated by colons. The package contains a kind of DOM as well as a parser and a processor. The latter is used e.g. for printing SML documents.

Index

Constants

View Source
const (
	ErrBuilder = iota + 1
	ErrReader
	ErrNoRootProcessor
	ErrRegisteredPlugin
)

Error codes of the SML package.

Variables

This section is empty.

Functions

func IsBuilderError

func IsBuilderError(err error) bool

IsBuilderError checks for an error during node building.

func IsNoRootProcessorError

func IsNoRootProcessorError(err error) bool

IsNoRootProcessorError checks for an unregistered root processor.

func IsReaderError

func IsReaderError(err error) bool

IsReaderError checks for an error during SML text reading.

func IsRegisteredPluginError

func IsRegisteredPluginError(err error) bool

IsRegisteredPluginError checks for the error of an already registered plugin.

func ReadSML

func ReadSML(reader io.Reader, builder Builder) error

ReadSML parses a SML document and uses the passed builder for the callbacks.

func ValidateTag

func ValidateTag(tag string) ([]string, error)

ValidateTag checks if a tag is valid. Only the chars 'a' to 'z', '0' to '9', '-' and ':' are accepted. It also transforms it to lowercase and splits the parts at the colons.

func WriteSML

func WriteSML(node Node, ctx *WriterContext) error

WriteSML uses one WriterProcessor and possible more as plugins to write the SML node to the passed writer. The prettyPrint flag controls if the writing is in a more beautiful formatted way.

Types

type Builder

type Builder interface {
	// BeginTagNode is called when new tag node begins.
	BeginTagNode(tag string) error

	// EndTagNode is called when the tag node ends.
	EndTagNode() error

	// TextNode is called for each text data.
	TextNode(text string) error

	// RawNode is called for each raw data.
	RawNode(raw string) error

	// Comment is called for each comment data inside a node.
	CommentNode(comment string) error
}

Builder defines the callbacks for the reader to handle parts of the document.

type KeyStringValueTreeBuilder

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

KeyStringValueTreeBuilder implements Builder to parse a file and create a KeyStringValueTree.

func NewKeyStringValueTreeBuilder

func NewKeyStringValueTreeBuilder() *KeyStringValueTreeBuilder

NewKeyStringValueTreeBuilder return a new nnode builder.

func (*KeyStringValueTreeBuilder) BeginTagNode

func (tb *KeyStringValueTreeBuilder) BeginTagNode(tag string) error

BeginTagNode implements the Builder interface.

func (*KeyStringValueTreeBuilder) CommentNode

func (tb *KeyStringValueTreeBuilder) CommentNode(comment string) error

CommentNode implements the Builder interface.

func (*KeyStringValueTreeBuilder) EndTagNode

func (tb *KeyStringValueTreeBuilder) EndTagNode() error

EndTagNode implements the Builder interface.

func (*KeyStringValueTreeBuilder) RawNode

func (tb *KeyStringValueTreeBuilder) RawNode(raw string) error

RawNode implements the Builder interface.

func (*KeyStringValueTreeBuilder) TextNode

func (tb *KeyStringValueTreeBuilder) TextNode(text string) error

TextNode implements the Builder interface.

func (*KeyStringValueTreeBuilder) Tree

Tree returns the created tree.

type Node

type Node interface {
	// Tag returns the tag in case of a tag node, otherwise nil.
	Tag() []string

	// Len returns the length of a text or the number of subnodes,
	// depending on the concrete type of the node.
	Len() int

	// ProcessWith is called for the processing of this node.
	ProcessWith(p Processor) error

	// String returns a simple string representation of the node.
	String() string
}

Node represents the common interface of all nodes (tags and text).

type NodeBuilder

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

NodeBuilder creates a node structure.

func NewNodeBuilder

func NewNodeBuilder() *NodeBuilder

NewNodeBuilder return a new nnode builder.

func (*NodeBuilder) BeginTagNode

func (nb *NodeBuilder) BeginTagNode(tag string) error

BeginTagNode implements the Builder interface.

func (*NodeBuilder) CommentNode

func (nb *NodeBuilder) CommentNode(comment string) error

CommentNode implements the Builder interface.

func (*NodeBuilder) EndTagNode

func (nb *NodeBuilder) EndTagNode() error

EndTagNode implements the Builder interface.

func (*NodeBuilder) RawNode

func (nb *NodeBuilder) RawNode(raw string) error

RawNode implements the Builder interface.

func (*NodeBuilder) Root

func (nb *NodeBuilder) Root() (Node, error)

Root returns the root node of the read document.

func (*NodeBuilder) TextNode

func (nb *NodeBuilder) TextNode(text string) error

TextNode implements the Builder interface.

type Processor

type Processor interface {
	// OpenTag is called if a tag is opened.
	OpenTag(tag []string) error

	// CloseTag is called if a tag is closed.
	CloseTag(tag []string) error

	// Text is called for each text data inside a node.
	Text(text string) error

	// Raw is called for each raw data inside a node.
	Raw(raw string) error

	// Comment is called for each comment data inside a node.
	Comment(comment string) error
}

Processor represents any type able to process a node structure. Four callbacks then will be called with the according data.

type WriterContext

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

WriterContext controls the parameters of a writing.

func NewWriterContext

func NewWriterContext(wp WriterProcessor, w io.Writer, pretty bool, indentStr string) *WriterContext

NewWriterContext creates a new writer context.

func (*WriterContext) Register

func (ctx *WriterContext) Register(tag string, wp WriterProcessor) error

Register adds a writer processor which will be responsible for processing if the tag is matching.

func (*WriterContext) Writef

func (ctx *WriterContext) Writef(format string, args ...interface{}) error

Writef writes a formatted string to the writer.

type WriterProcessor

type WriterProcessor interface {
	// WriterProcessor is a processor itself.
	Processor

	// SetContext sets the writer context.
	SetContext(ctx *WriterContext)
}

WriterProcessor a processor

func NewStandardSMLWriter

func NewStandardSMLWriter() WriterProcessor

NewStandardSMLWriter creates a new writer for a ML document in standard notation.

func NewXMLWriter

func NewXMLWriter(rawTag string) WriterProcessor

NewXMLWriter creates a new writer for a ML document in XML notation.

type WriterProcessors

type WriterProcessors map[string]WriterProcessor

WriterProcessors is a map of processors that can be plugged into the used SML writer. The key is the first part of each tag.

Jump to

Keyboard shortcuts

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