- Go 100%
|
All checks were successful
/ job_go_tests (push) Successful in 30s
* New encoders API and new test encoder * Basic implementation using Example engine * Text extension files in Example engine divided * Guide extended to include encoders Reviewed-on: kacuatro/gomd#10 Co-authored-by: Lucas Menendez <hi@lucasmenendez.me> Co-committed-by: Lucas Menendez <hi@lucasmenendez.me> |
||
|---|---|---|
| .badges | ||
| .forgejo/workflows | ||
| cmd/markdow2json | ||
| engines | ||
| iterator | ||
| test | ||
| tree | ||
| types | ||
| engine.go | ||
| engine_test.go | ||
| go.mod | ||
| LICENSE | ||
| README.md | ||
gomd
gomd package contains the core functionality for parsing and building a markup document tree using a pluggable engine system.
This package contains:
- The
Engineinterface, that allows to bring support for any markup language for tagging and building the content tree. - The
Runnerstruct, that allows to run an engine by tagging, cleaning tags and building the content tree using that engine. - The
./typespackage:./types/tag.go: Includes the definition ofTagstruct, 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 ofExtensioninterface and related types.
- Others helper subpackages:
./iterator: Contains theIterator[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 theNodestructure, 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:
- 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.
- 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:
-
The
Enginedefinition includes sometypes.Extensionthat includes its own tagging rules and node builders. -
In the tagging phase, the
Runnerapplies 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. -
After tagging, the
Runnercontinues with clearing, by applying theTagCleanerfunction 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. -
In the building phase, the
Runneruses 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.