gomd

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: AGPL-3.0 Imports: 6 Imported by: 0

README

gomd

Last release Test coverage GoDoc License

gomd package contains the core functionality for parsing and building a markup document tree using a pluggable engine system.


This package contains:

  • The Engine interface, that allows to bring support for any markup language for tagging and building the content tree.
  • The Runner struct, that allows to run an engine by tagging, cleaning tags and building the content tree using that engine.
  • The ./types package:
    • ./types/tag.go: Includes the definition of Tag struct, that holds pieces of content of any text that are remarkable for a extension builder. They will be generated during the tagging phase.
    • ./types/extension.go: Definition of Extension interface and related types.
  • Others helper subpackages:
    • ./iterator: Contains the Iterator[T], a generic thread-safe iterator for slices generic items, that provides methods for navigating through a collection of items, iterating over them or filtering based on certain conditions.
    • ./tree: Contains the Node structure, that allows to represent a document as a hierarchical tree of pieces of content. It not just enable to build complex document structures, but also to navigate trough parent-sibling-child relationships.

The engine based system allows for extensions to provide custom tagging rules and node builders, enabling flexible and extensible markup processing. The process involves two main steps:

  1. Tagging: The input content is read and processed line by line and character by character to generate a series of tags based on the rules defined by the engine extensions.
  2. Building: The generated tags are then used to construct a tree structure representing the markup document, using node builders provided by the engine extensions.

Engines

Engines are sets of definitions that allows to developers to bring support for any markup language, even those designed by they.

Each engine should implement the Engine interface to provide its own collection of types.Extensions and DefaultTag() function definition:

  • Extensions(): Returns the list of available extensions for this engine.
  • DefaultTag(): Returns the default tag that will be used as a fallback if no extension taggers is used.

Each extension should implement the types.Extension interface to provide its own parsing rules and node builders:

  • CharTaggingRuleSet(): Returns a set of character-level tagging rules.
  • LineTaggingRuleSet(): Returns a set of line-level tagging rules.
  • TagCleaner(): Returns a function to clean or modify the generated tags.
  • NodeBuilders(): Returns a set of node builders for constructing the tree.
Engine example

Basic example of a extension workflow:

  1. The Engine definition includes some types.Extension that includes its own tagging rules and node builders.

  2. In the tagging phase, the Runner applies the line and character tagging rules to the input content to generate tags. The extension's rules will receive lines and characters to tag accordingly. These rules should check if the content receives matches their criteria and return the appropriate tags, or skip if not.

  3. After tagging, the Runner continues with clearing, by applying the TagCleaner function from the extension to clean or modify the generated tags as needed. It could be used to remove redundant tags, merge adjacent tags, or perform any other necessary adjustments before building the tree.

  4. In the building phase, the Runner uses the node builders provided by the extension to construct the tree structure from the cleaned tags. Each node builder will attempt to decode tags into tree nodes based on the extension's logic. The extension can use the BuildTree function to generate child subtrees recursively using the extension builders. It should skip those tags that it cannot decode.

For more information about custom Engine's definition, follow the Engine example guide.

Documentation

Overview

gomd package contains the core functionality for parsing and building a markup document tree using a pluggable engine system. It defines the Engine interface, the Runner struct for executing the parsing and building process, and functions for tagging lines, cleaning tags, and constructing a tree from tags. The plugin system allows for extensions to provide custom parsing rules and node builders, enabling flexible and extensible markup processing. The process involves two main steps:

  1. Tagging: The input content is read and processed line by line and character by character to generate a series of tags based on the rules defined by the extensions.
  2. Building: The generated tags are then used to construct a tree structure representing the markup document, using node builders provided by the extensions.

Each extension should implement the Extension interface to provide its own parsing rules and node builders:

  • CharTaggingRuleSet: Returns a set of character-level tagging rules.
  • LineTaggingRuleSet: Returns a set of line-level tagging rules.
  • TagCleaner: Returns a function to clean or modify the generated tags.
  • NodeBuilders: Returns a set of node builders for constructing the tree.

Basic example of a extension workflow:

  1. The extension defines its tagging rules and node builders.
  2. In the tagging phase, the Runner applies the line and character tagging rules to the input content to generate tags. The extension's rules will receive lines and characters to tag accordingly. These rules should check if the content receives matches their criteria and return the appropriate tags, or skip if not.
  3. After tagging, the Runner applies the TagCleaner function from the extension to clean or modify the generated tags as needed. It could be used to remove redundant tags, merge adjacent tags, or perform any other necessary adjustments before building the tree.
  4. In the building phase, the Runner uses the node builders provided by the extension to construct the tree structure from the cleaned tags. Each node builder will attempt to decode tags into tree nodes based on the extension's logic. The extension can use the BuildTree function to generate child subtrees recursively using the extension builders. It should skip those tags that it cannot decode.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildChilds added in v0.0.2

func BuildChilds(
	node *tree.Node,
	decoders types.NodeBuildersSet[*types.Tag],
	tags []*types.Tag,
) (*tree.Node, error)

BuildChilds constructs a tree.Node from the provided root node, decoders, and tags. It iterates over the tags and applies the decoders to build the tree under the root node. It returns an error if any decoding fails or if tags cannot be decoded.

func EncodeChilds added in v0.0.2

func EncodeChilds(node *tree.Node, encoders types.NodeEncodersSet) ([]byte, error)

EncodeChilds encodes the child nodes of the provided tree.Node using the provided encoders. It returns a byte slice representing the encoded content or an error if encoding fails or if no encoder is found for a child node.

Types

type Engine

type Engine interface {
	Extensions() []types.Extension
	DefaultTag([]byte) *types.Tag
}

Engine defines the interface for a markup engine. It provides methods to retrieve extensions and the default tag.

type Runner

type Runner struct {
	Engine
}

Runner is responsible for running the tagging and building process using a given Engine.

func Run

func Run(e Engine) (*Runner, error)

Run creates a new Runner with the provided Engine.

func (*Runner) CharTaggingRuleSet

func (r *Runner) CharTaggingRuleSet() types.CharTaggingRuleSet

func (*Runner) Decode

func (r *Runner) Decode(in io.Reader) (*tree.Node, error)

Decode reads from the provided io.Reader, tags the content, and builds a tree.Node representation of the content. It returns an error if reading, tagging, or building fails.

func (*Runner) DecodeBytes

func (r *Runner) DecodeBytes(content []byte) (*tree.Node, error)

DecodeBytes tags the provided byte content and builds a tree.Node representation of the content. It returns an error if tagging or building fails.

func (*Runner) Encode added in v0.0.2

func (r *Runner) Encode(node *tree.Node) ([]byte, error)

Encode encodes the provided tree.Node into a byte slice. It returns an error if encoding fails or if the node is not a root node. It uses current extension node encoders to perform the encoding.

func (*Runner) LineTaggingRuleSet

func (r *Runner) LineTaggingRuleSet() types.LineTaggingRuleSet

func (*Runner) NodeBuilders

func (r *Runner) NodeBuilders() types.NodeBuildersSet[*types.Tag]

func (*Runner) NodeEncoders added in v0.0.2

func (r *Runner) NodeEncoders() types.NodeEncodersSet

Directories

Path Synopsis
cmd
markdow2json command
engines
iterator package contains a generic thread-safe iterator for slices of any type T. It provides methods for navigating through a collection of items, iterating over them or filtering based on certain conditions.
iterator package contains a generic thread-safe iterator for slices of any type T. It provides methods for navigating through a collection of items, iterating over them or filtering based on certain conditions.
tree package contains the Node structure and related types and methods that allows to represent a document as a tree of nodes ("pieces") of content.
tree package contains the Node structure and related types and methods that allows to represent a document as a tree of nodes ("pieces") of content.

Jump to

Keyboard shortcuts

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