treeificator

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2025 License: MIT Imports: 6 Imported by: 0

README

Treeificator

Go Coverage license Go Documentation GoDoc

A small module to turn a string into a tree of nodes based on a list of informers.

How to use

First import the module go get git.mstar.dev/mstar/treeificator

Then define the informers for the beginning and end of each node type you want

type PInformer struct{}

// The prefix starting a block of type PInformer
func (p *PInformer) GetPrefix() string {
  return "<p>"
}

// The suffix ending a block of type PInformer
func (p *PInformer) GetSuffix() string {
  return "</p>"
}

// The name of PInformer
func (p *PInformer) GetName() string {
  return "p-element"
}

And finally parse any string

import "git.mstar.dev/mstar/treeificator"

treeRoot := treeificator.Marshal(yourString, &PInformer{})

See documentation for more details

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RenderNodeTree added in v1.1.0

func RenderNodeTree(root Node) string

Types

type DefaultInformer

type DefaultInformer struct{}

Defa

func (*DefaultInformer) GetName

func (defaultinformer *DefaultInformer) GetName() string

func (*DefaultInformer) GetPrefix

func (defaultinformer *DefaultInformer) GetPrefix() string

func (*DefaultInformer) GetSuffix

func (defaultinformer *DefaultInformer) GetSuffix() string

type Informer

type Informer interface {
	// Get the prefix starting the informer's node type
	GetPrefix() string
	// Get the suffix starting the informer's node type
	GetSuffix() string
	// Get the name of the node type.
	// Each name (in lowercase) may only exist once. The parser will enforce this.
	// No guaranteed order for which informer will be used if multiple with the same name are in use.
	// The name "default" is occupied by the built-in default informer for just strings
	GetName() string
}

Informers inform the parser about the prefix and suffix that make up a node

type Node

type Node struct {
	// What kind of node this is
	NodeType Informer
	// The elements in this node, ordered by appearance
	Elements []NodeElement
}

func Marshal

func Marshal(raw string, informers ...Informer) Node

func (*Node) Unmarshal

func (n *Node) Unmarshal() string

Unmarshal a node into the string it got constructed from

type NodeElement

type NodeElement struct {
	Text *string
	Node *Node
}

An element in a node. Either a string or another node. Leaf elements will always be a string. Root element will always be a [DefaultInformaer].

func (*NodeElement) Unmarshal

func (e *NodeElement) Unmarshal() string

Unmarshal an element into the string it got constructed from

Jump to

Keyboard shortcuts

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