xdoc

package module
v0.0.0-...-82d0c9f Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2021 License: MIT Imports: 6 Imported by: 0

README

xdoc

Structured Document Content Schema implementation

Implemented Elements

Page <page />
Title <title />
Paragraph <para />

Schema

XML Schema

JSON Schema

Implementing a new Element

  • A new element should satisfy interface Element at least

  • It's mapping entry should be in schema map in schema.go, along with it's constructor ctor function.

  • In tree.go, add its entry in switch inside marshal and unmarshal.

  • Any data directly related to element should be its struct attribute. Example -

    type Text struct {
      Text string
      Bold bool
    } 
    
  • With Children - If element supports children, it should embed Children with proper json and xml tags for their correct marshalling and unmarshalling. Example -

    type Para struct {
      Children `json:"children" xml:",any"`
    }
    
  • Custom element specific Marshal/Unmarshal - To customize the JSON and XML marshalling and unmarshalling, in new element implement/satisfy any or all of -

    • json.Marshaller
    • json.Unmarshaller
    • xml.Marshaller
    • xml.Unmarshaller

    If element doesn't satisfy any, the default behaviour is implied, which is purely based on struct tags in struct fields (json or xml)

TODOs

  • Implement Elements
    • H1
    • H2
    • H3
    • Blockquote
    • Panel
    • Image

Documentation

Index

Constants

View Source
const (
	TypeNone       = ""
	TypePage       = "page"
	TypePara       = "p"
	TypeText       = "text"
	TypeLink       = "link"
	TypeBlockquote = "blockquote"
	TypeTitle      = "title"
	TypeH1         = "h1"
	TypeH2         = "h2"
	TypeH3         = "h3"
	TypeList       = "list"
	TypeImage      = "image"
	TypePanel      = "panel"
)

Variables

View Source
var (
	MaxIteration      = 1000
	MaxRecursionDepth = 1000

	SkipLevel              = errors.New("skip level")
	StopWalk               = errors.New("stop walk")
	ErrMaxIter             = errors.New("max iteration reached")
	ErrMaxRecursion        = errors.New("max recursion depth reached")
	ErrInconsistentTree    = errors.New("inconsistent tree")
	ErrInvalidNode         = errors.New("invalid node")
	ErrNodeAlreadyAttached = errors.New("node already attached")
	ErrNodeNotFound        = errors.New("node not found")
	ErrParentNodeNotFound  = errors.New("parent node not found")
	ErrChildNotAllowed     = errors.New("child not allowed")
)
View Source
var (
	ErrInvalidElementType = errors.New("invalid element type")
)

Functions

func GetValidParentsFor

func GetValidParentsFor(n string) (parents []string)

GetValidParentsFor returns slice of parents which can contain `n` as child

func InStrList

func InStrList(str string, list []string) bool

func IsValidNode

func IsValidNode(n string) bool

func IsValidParent

func IsValidParent(parent, child string) bool

IsValidParent returns true if parent is valid node to contain given child

func PanicIfErr

func PanicIfErr(e error)

func ToJSON

func ToJSON(n *Node) ([]byte, error)

func ToXML

func ToXML(n *Node) ([]byte, error)

Types

type ChildContainer

type ChildContainer interface {
	GetChildren() []*Node
	Len() int
	// contains filtered or unexported methods
}

type Children

type Children []*Node

func (*Children) GetChildren

func (c *Children) GetChildren() []*Node

func (*Children) Len

func (c *Children) Len() int

type Element

type Element interface {
	GetType() string
	CanContain(Element) bool
}

func NewElementForType

func NewElementForType(typ string) (Element, error)
type Link struct {
	Href     string `json:"href,omitempty" xml:"href,attr,omitempty"`
	Title    string `json:"title,omitempty" xml:"title,attr,omitempty"`
	Children `json:"children" xml:",any"`
}
func NewLink() *Link

func (*Link) CanContain

func (l *Link) CanContain(e Element) bool

func (Link) GetType

func (Link) GetType() string

type Node

type Node struct {
	Element
	// contains filtered or unexported fields
}

func FromJSON

func FromJSON(data []byte) (n *Node, err error)

func FromXML

func FromXML(data []byte) (n *Node, err error)

func NewElementNode

func NewElementNode(el Element) *Node

func NewNode

func NewNode() *Node

func (*Node) AppendChild

func (n *Node) AppendChild(child ...*Node) error

func (Node) FirstChild

func (n Node) FirstChild() *Node

func (*Node) GetElement

func (n *Node) GetElement() Element

func (Node) HasParent

func (n Node) HasParent() bool

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

func (Node) IsRoot

func (n Node) IsRoot() bool

func (Node) LastChild

func (n Node) LastChild() *Node

func (*Node) MarshalJSON

func (n *Node) MarshalJSON() ([]byte, error)

func (*Node) MarshalXML

func (n *Node) MarshalXML(e *xml.Encoder, start xml.StartElement) error

func (Node) NumChild

func (n Node) NumChild() int

func (Node) Parent

func (n Node) Parent() *Node

func (*Node) UnmarshalJSON

func (n *Node) UnmarshalJSON(data []byte) error

func (*Node) UnmarshalXML

func (n *Node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error

func (*Node) WithChild

func (n *Node) WithChild(child *Node) (this *Node)

type NodeInfo

type NodeInfo struct {
	Type         string
	AllowedChild []string
	Ctor         ctor
}

type Page

type Page struct {
	Version  int    `json:"version,string" xml:"version,attr"`
	Lang     string `json:"lang" xml:"lang,attr"`
	Children `json:"children" xml:",any"`
}

Page node

func NewPage

func NewPage() *Page

func (*Page) CanContain

func (p *Page) CanContain(e Element) bool

func (*Page) GetType

func (p *Page) GetType() string

func (*Page) WithLang

func (p *Page) WithLang(l string) *Page

func (*Page) WithVersion

func (p *Page) WithVersion(v int) *Page

type Paragraph

type Paragraph struct {
	Children `json:"children" xml:",any"`
}

Paragraph node

func NewParagraph

func NewParagraph() *Paragraph

func (*Paragraph) CanContain

func (p *Paragraph) CanContain(e Element) bool

func (*Paragraph) GetType

func (p *Paragraph) GetType() string

type Text

type Text struct {
	Text          string `json:"text" xml:",chardata"`
	Code          bool   `json:"code,omitempty" xml:"code,attr,omitempty"`
	Bold          bool   `json:"bold,omitempty" xml:"bold,attr,omitempty"`
	Italics       bool   `json:"italics,omitempty" xml:"italics,attr,omitempty"`
	Underline     bool   `json:"underline,omitempty" xml:"underline,attr,omitempty"`
	Strikethrough bool   `json:"strikethrough,omitempty" xml:"strikethrough,attr,omitempty"`
}

func NewText

func NewText() *Text

func (*Text) CanContain

func (t *Text) CanContain(e Element) bool

func (*Text) GetType

func (t *Text) GetType() string

func (*Text) WithBold

func (t *Text) WithBold(bold bool) *Text

func (*Text) WithText

func (t *Text) WithText(txt string) *Text

type TextValuer

type TextValuer interface {
	GetText() string
}

type Title

type Title struct {
	Children `json:"children" xml:",any"`
}

Title node

func NewTitle

func NewTitle() *Title

func (*Title) CanContain

func (t *Title) CanContain(e Element) bool

func (*Title) GetType

func (t *Title) GetType() string

func (*Title) WithText

func (t *Title) WithText(s string) *Title

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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