mobiledoc

package module
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: May 10, 2022 License: MIT Imports: 11 Imported by: 0

README

mobiledoc

Test GoDoc Release

Go package for handling the mobiledoc format.

Installation

Get the package using the go tool:

$ go get -u github.com/256dpi/mobiledoc

License

The MIT License (MIT)

Copyright (c) 2018 Joël Gähwiler

Documentation

Index

Constants

View Source
const Version = "0.3.1"

Version specifies the mobiledoc version.

Variables

View Source
var DefaultImageSection = func(source string) bool {
	return len(source) > 0
}

DefaultImageSection defines the default image section validator.

View Source
var DefaultListSections = []string{"ul", "ol"}

DefaultListSections defines the default list sections.

View Source
var DefaultMarkupSections = []string{"aside", "blockquote", "h1", "h2", "h3", "h4", "h5", "h6", "p"}

DefaultMarkupSections defines the default markup sections.

DefaultMarkups defines the default expected markups with the tag as the key and a map of attributes and validator functions.

Functions

func LinkValidator added in v0.1.1

func LinkValidator(attributes Map) bool

LinkValidator validates the href attribute.

func NoAttributesValidator added in v0.1.1

func NoAttributesValidator(attributes Map) bool

NoAttributesValidator returns true if the provided attributes are empty.

Types

type Atom

type Atom struct {
	Name    string
	Text    string
	Payload Map
}

Atom is a single atom.

type Card

type Card struct {
	Name    string
	Payload Map
}

Card is a single card.

type Document

type Document struct {
	Version  string
	Markups  []Markup
	Atoms    []Atom
	Cards    []Card
	Sections []Section
}

Document is a mobiledoc.

func ConvertText added in v0.3.0

func ConvertText(str string) Document

ConvertText will convert a basic text with no formatting but newlines to a document.

func Parse

func Parse(doc Map) (Document, error)

Parse will parse the specified raw structure into a document.

func (*Document) IsZero added in v0.3.1

func (d *Document) IsZero() bool

IsZero returns true if the document is nil or empty.

func (*Document) MarshalBSONValue added in v0.3.1

func (d *Document) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue implements the bson.ValueMarshaler interface.

func (Document) MarshalJSON added in v0.3.0

func (d Document) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Document) UnmarshalBSON added in v0.3.0

func (d *Document) UnmarshalBSON(bytes []byte) error

UnmarshalBSON implements the bson.Unmarshaler interface.

func (*Document) UnmarshalJSON added in v0.3.0

func (d *Document) UnmarshalJSON(bytes []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type HTMLRenderer

type HTMLRenderer struct {
	Atoms map[string]func(*bufio.Writer, string, Map) error
	Cards map[string]func(*bufio.Writer, Map) error
}

HTMLRenderer implements a basic HTML renderer.

func NewHTMLRenderer

func NewHTMLRenderer() *HTMLRenderer

NewHTMLRenderer creates a new HTMLRenderer.

func (*HTMLRenderer) Render

func (r *HTMLRenderer) Render(w io.Writer, doc Document) error

Render will render the document to the provided writer.

type List

type List = []interface{}

List is a general purpose list.

type Map

type Map = map[string]interface{}

Map is a general purpose map.

func Compile added in v0.3.0

func Compile(doc Document) (Map, error)

Compile will compile provided document into its raw structure.

type Marker

type Marker struct {
	Type          MarkerType
	OpenMarkups   []*Markup
	ClosedMarkups int
	Text          string
	Atom          *Atom
}

Marker is a single marker.

type MarkerType

type MarkerType int

MarkerType defines a marker type.

const (
	TextMarker MarkerType = 0
	AtomMarker MarkerType = 1
)

The available marker identifiers.

type Markup

type Markup struct {
	Tag        string
	Attributes Map
}

Markup is a single markup.

type Section

type Section struct {
	Type    SectionType
	Tag     string
	Markers []Marker
	Source  string
	Items   [][]Marker
	Card    *Card
}

Section is a single section.

type SectionType

type SectionType int

SectionType defines a section type.

const (
	MarkupSection SectionType = 1
	ImageSection  SectionType = 2
	ListSection   SectionType = 3
	CardSection   SectionType = 10
)

The available section identifiers.

type TextRenderer added in v0.3.1

type TextRenderer struct {
	Atoms map[string]func(*bufio.Writer, string, Map) error
	Cards map[string]func(*bufio.Writer, Map) error
}

TextRenderer implements a basic text renderer.

func NewTextRenderer added in v0.3.1

func NewTextRenderer() *TextRenderer

NewTextRenderer creates a new TextRenderer.

func (*TextRenderer) Render added in v0.3.1

func (r *TextRenderer) Render(w io.Writer, doc Document) error

Render will render the document to the provided writer.

type Validator

type Validator struct {
	// Markups defines the allowed markups with the name as key and a
	// attributes validator function.
	Markups map[string]func(attributes Map) bool

	// Whether to allow unknown atoms and cards.
	UnknownAtoms bool
	UnknownCards bool

	// Atoms defines the allowed atoms with the name as the key and a validator
	// function.
	Atoms map[string]func(name string, payload Map) bool

	// Cards defines the allowed cards with the name as the key and a validator
	// function.
	Cards map[string]func(payload Map) bool

	// MarkupSections defines the allowed markup sections.
	MarkupSections []string

	// ListSections defines the allowed list sections.
	ListSections []string

	// ImageSection defines whether the image section is allowed when a source
	// validator is set.
	ImageSection func(source string) bool
}

Validator validates a mobiledoc.

func NewDefaultValidator added in v0.1.1

func NewDefaultValidator() *Validator

NewDefaultValidator creates a validator that validates the default mobiledoc standard excluding atoms and cards.

func NewEmptyValidator added in v0.1.1

func NewEmptyValidator() *Validator

NewEmptyValidator creates an empty validator.

func NewFormatValidator added in v0.3.0

func NewFormatValidator() *Validator

NewFormatValidator creates a validator that validates the default mobiledoc standard including atoms and cards.

func (*Validator) Validate

func (v *Validator) Validate(doc Document) error

Validate will walk the specified mobiledoc and check if it is valid.

Jump to

Keyboard shortcuts

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