hype

package module
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 34 Imported by: 0

README

Release Go Build Status Go Reference Go Report Card Slack


Hype

Hype is a content generation tool that use traditional Markdown syntax, and allows it to be extended for almost any use to create dynamic, rich, automated output that is easily maintainable and reusable.

Hype follows the same principals that we use for coding:

  • packages (keep relevant content in small, reusable packages, with all links relative to the package)
  • reuse - write your documentation once (even in your code), and use everywhere (blog, book, github repo, etc)
  • partials/includes - support including documents into a larger document (just like code!)
  • validation - like tests, but validate all your code samples are valid (or not if that is what you expect).
  • asset validation - ensure local assets like images, etc actually exist

Created with Hype

This README was created with hype. Here was the command we used to create it:

From the .hype directory, run:

hype export -format=markdown -f hype.md > ../README.md

You can also use a github action to automatically update your README as well.


Quick Start Guide

For more in depth examples, you can read our quick start guide here.

The Basics

This is the syntax to include a code sample in your document:

<code src="src/hello/main.go" snippet="example"></code>

The above code snippet does the following:

  • Includes the code snippet specified in the source code
  • Validates that the code compiles

Here is the source file:

package main

import "fmt"

// snippet: example
func main() {
 fmt.Println("Hello World")
}

// snippet: example

Notice the use of the snippet comment. The format for the comment is:

// snippet: <snippet_name_here>

You must have a beginning and an ending snippet for the code to work.

The output of including that tag will be as follows:

func main() {
	fmt.Println("Hello World")
}

source: docs/quickstart/src/hello/main.go:example

A snippet is not required in your code tag. The default behavior of a code tag is to include the entire source file.

If we leave the tag out, it will result in the following code being included:

package main

import "fmt"

func main() {
	fmt.Println("Hello World")
}

source: docs/quickstart/src/hello/main.go

Notice that none of the snippet comments were in the output? This is because hype recognizes them as directives for the document, and will not show them in the actual output.

Go Specific Commands

There are a number of Go specific commands you can run as well. Anything from executing the code and showing the output, to including go doc (from the standard library or your own source code), etc.

Let's look at how we use the go tag.

Here is the source code of the Go file we are going to include. Notice the use of the snippet comments to identify the area of code we want included. We'll see how to specify that in the next section when we include it in our markdown.

Running Go Code

The following command will include the go source code, run it, and include the output of the program as well:

<go src="src/hello" run="."></go>

Here is the result that will be included in your document from the above command:

$ go run .

Hello World

--------------------------------------------------------------------------------
Go Version: go1.25.3

Running and Showing the Code

If you want to both run and show the code with the same tag, you can add the code attribute to the tag:

<go src="src/hello" run="." code="main.go"></go>

Now the source code is includes, as well as the output of the program:

package main

import "fmt"

func main() {
	fmt.Println("Hello World")
}

source: docs/quickstart/src/hello/main.go


$ go run .

Hello World

--------------------------------------------------------------------------------
Go Version: go1.25.3

Snippets with Go

You can also specify the snippet in a go tag as well. The result is that it will only include the code snippet in the included source:

<go src="src/hello" run="." code="main.go#example"></go>

You can see now that only the snippet is included, but the output is still the same:

func main() {
	fmt.Println("Hello World")
}

source: docs/quickstart/src/hello/main.go#example:example


$ go run .

Hello World

--------------------------------------------------------------------------------
Go Version: go1.25.3

Invalid Code

What if you want to include an example of code that does not compile? We still want the code to be parsed and included, even though the code doesn't compile. For this, we can state the expected output of the program.

<go src="src/broken" run="." code="main.go#example" exit="1"></go>

The result now includes the snippet, and the error output from trying to compile the invalid source code.

func main() {
	fmt.Prin("Hello World")
}

source: docs/quickstart/src/broken/main.go#example:example


$ go run .

# github.com/gopherguides/hype/.
./main.go:7:6: undefined: fmt.Prin

--------------------------------------------------------------------------------
Go Version: go1.25.3

GoDoc

While there are a number of godoc commands that will allow you to put your documentation from your code directly into your articles as well. Here are some of the commands.

Here is the basic usage first:

<go doc="-short context"></go>

Here is the output for the above command:

$ go doc -short context

var Canceled = errors.New("context canceled")
var DeadlineExceeded error = deadlineExceededError{}
func AfterFunc(ctx Context, f func()) (stop func() bool)
func Cause(c Context) error
func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
func WithCancelCause(parent Context) (ctx Context, cancel CancelCauseFunc)
func WithDeadline(parent Context, d time.Time) (Context, CancelFunc)
func WithDeadlineCause(parent Context, d time.Time, cause error) (Context, CancelFunc)
func WithTimeout(parent Context, timeout time.Duration) (Context, CancelFunc)
func WithTimeoutCause(parent Context, timeout time.Duration, cause error) (Context, CancelFunc)
type CancelCauseFunc func(cause error)
type CancelFunc func()
type Context interface{ ... }
    func Background() Context
    func TODO() Context
    func WithValue(parent Context, key, val any) Context
    func WithoutCancel(parent Context) Context

--------------------------------------------------------------------------------
Go Version: go1.25.3

You can also be more specific.

<go doc="-short context.WithCancel"></go>

Here is the output for the above command:

$ go doc -short context.WithCancel

func WithCancel(parent Context) (ctx Context, cancel CancelFunc)
    WithCancel returns a derived context that points to the parent context but
    has a new Done channel. The returned context's Done channel is closed when
    the returned cancel function is called or when the parent context's Done
    channel is closed, whichever happens first.

    Canceling this context releases resources associated with it, so code should
    call cancel as soon as the operations running in this Context complete.

--------------------------------------------------------------------------------
Go Version: go1.25.3

For more examples, see the hype repo.

Arbitrary Commands

You can also use the cmd tag and the exec attribute to run arbitrary commands and include them in your documentation. Here is the command to run the tree command and include it in our documentation:

<cmd exec="tree" src="."></cmd>

Here is the output:

$ tree

.
├── hype.md
├── includes.md
└── src
    ├── broken
    │   └── main.go
    └── hello
        └── main.go

4 directories, 4 files

The Export Command

There are several options for running the hype command. Most notable is the export option:

`$ hype export -h

Usage of hype: -f string optional file name to preview, if not provided, defaults to hype.md (default "hype.md") -format string content type to export to: markdown, html (default "markdown") -timeout duration timeout for execution, defaults to 30 seconds (30s) (default 5s) -v enable verbose output for debugging

Usage: hype export [options]

Examples: hype export -format html hype export -f README.md -format html hype export -f README.md -format markdown -timeout=10s `

This allows you to see your compiled document either as a single markdown, or as an html document that you can preview in the browser.

Including Markdown

To include a markdown file, use the include tag. This will run that markdown file through the hype.Parser being used and append the results to the current document.

The paths specified in the src attribute of the include are relative to the markdown file they are used in. This allows you to move entire directory structures around in your project without having to change references within the documents themselves.

The following code will parse the code/code.md and sourceable/sourceable.md documents and append them to the end of the document they were included in.

<include src="code/code.md"></include>

<include src="sourceable/sourceable.md"></include>

source: docs/quickstart/includes.md


README Source

You can view the source for this entire readme in the .hype directory.

Here is the current structure that we are using to create this readme:

$ tree ./docs

./docs
├── badges.md
├── license.md
└── quickstart
    ├── hype.md
    ├── includes.md
    └── src
        ├── broken
        │   └── main.go
        └── hello
            └── main.go

5 directories, 6 files

Using Github Actions to update your README

This repo uses the action to keep the README up to date.

Requirements

For this action to work, you need to either configure your repo with specific permissions, or use a personal access token.

Repo Permissions

You need to give permission to your GitHub Actions to create a pull request in your GitHub repo settings (Settings -> Actions -> General).

Under Workflow Permissions

  • Check Allow GitHub Actions to create and approve pull requests.
  • Check Read and write permissions
Personal Access Token

Alternately, you can use tokens to give permission to your action.

It is recommend to use a GitHub Personnal Acces Token like: ${{secrets.PAT}} instead of using ${{secrets.GITHUB_TOKEN}} in GitHub Actions.

The Action

The current action is set to only generate the readme on a pull request and commit it back to that same pull request. You can modify this to your own needs.

name: Generate README with Hype
on:
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      pull-requests: write
    steps:
      - uses: actions/checkout@v4
        with:
          ref: ${{github.event.pull_request.head.ref}}
          repository: ${{github.event.pull_request.head.repo.full_name}}
      - name: Set up Go
        uses: actions/setup-go@v4
        with:
          go-version-file: 'go.mod'
          cache-dependency-path: subdir/go.sum
      - name: Install hype
        run: go install github.com/gopherguides/hype/cmd/hype@latest
      - name: Run hype
        run: hype export -format=markdown -f hype.md -o README.md
      - name: Check for changes
        id: git-check
        run: |
          git diff --quiet README.md || echo "changed=true" >> $GITHUB_OUTPUT
      - name: Commit README changes if any
        if: steps.git-check.outputs.changed == 'true'
        run: |
          git config --local user.email "github-actions[bot]@users.noreply.github.com"
          git config --local user.name "github-actions[bot]"
          git add README.md
          git commit -m "Update README.md with latest Hype changes"
          git push

source: .github/workflows/hype.yml


Issues

There are several issues that still need to be worked on. Please see the issues tab if you are interested in helping.


License

MIT License

Copyright © 2025 Gopher Guides LLC

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Index

Constants

View Source
const (
	ErrNilFigure = ErrIsNil("figure")
)
View Source
const (
	TIME_FORMAT = "2006-01-02 15:04:05 -0700 MST"
)

Variables

This section is empty.

Functions

func AttrMatches

func AttrMatches(ats *Attributes, query map[string]string) bool

AttrMatches returns true if the given keys and values in the query map are present, and equal, in the given attributes. A `*` matches any value.

func ByType

func ByType[T Node](nodes Nodes) []T

func DefaultElements

func DefaultElements() map[Atom]ParseElementFn

DefaultElements returns a map of all the default element parser functions. For example, `include`, `body`, `code`, etc.

func FindTitle

func FindTitle(nodes Nodes) string

FindTitle finds the title element in the given HTML document. If no title element is found, the first h1 element is returned. If no h1 element is found, `"Untitled"` is returned.

func FirstByType

func FirstByType[T Node](nodes Nodes) (T, bool)

func GoVersion

func GoVersion() string

func IsEmptyNode

func IsEmptyNode(node Node) bool

func Language

func Language(ats *Attributes, lang string) string

Language tries to determine the language of the given set of attributes.

  • "language" is the first attr tested.
  • "language-*" is the second attr tested. (e.g. "language-go", "language-js")
  • "lang" is the third attr tested.

func RestripeFigureIDs

func RestripeFigureIDs(nodes Nodes, fn IDGenerator) error

RestripeFigureIDs will rewrite all of the figure IDs, and they're references, using the given IDGenerator.

func WrapNodeErr

func WrapNodeErr(n Node, err error) error

Types

type Atom

type Atom = atomx.Atom

type Atomable

type Atomable interface {
	Atom() Atom
}

type AtomableNode

type AtomableNode interface {
	Node
	Atomable
}

func ByAtom

func ByAtom[T ~string](nodes Nodes, want ...T) []AtomableNode

func FirstByAtom

func FirstByAtom[T ~string](nodes Nodes, want ...T) (AtomableNode, bool)

type AttrNode

type AttrNode interface {
	Node
	Attrs() *Attributes
}

func ByAttrs

func ByAttrs(nodes Nodes, query map[string]string) []AttrNode

func FirstByAttrs

func FirstByAttrs(nodes Nodes, query map[string]string) (AttrNode, bool)

type Attributes

type Attributes = syncx.Map[string, string]

func ConvertHTMLAttrs

func ConvertHTMLAttrs(attrs []html.Attribute) (*Attributes, error)

ConvertHTMLAttrs converts a slice of HTML attributes to a new Attributes type.

type Body

type Body struct {
	*Element
}

Body is a container for all the elements in a document.

func NewBody

func NewBody(el *Element) (*Body, error)

NewBody creates a new Body.

func (*Body) AsPage

func (b *Body) AsPage() *Page

AsPage returns the body as a Page.

func (*Body) MarshalJSON

func (b *Body) MarshalJSON() ([]byte, error)

type Cmd

type Cmd struct {
	*Element

	Args         []string
	Env          []string
	ExpectedExit int
	Timeout      time.Duration
	// contains filtered or unexported fields
}

Cmd is a tag representing a command to be executed.

func NewCmd

func NewCmd(el *Element) (*Cmd, error)

func (*Cmd) Execute

func (c *Cmd) Execute(ctx context.Context, doc *Document) error

Execute the command.

func (*Cmd) MD

func (c *Cmd) MD() string

func (*Cmd) MarshalJSON

func (c *Cmd) MarshalJSON() ([]byte, error)

func (*Cmd) Result

func (c *Cmd) Result() *CmdResult

Result returns the result of executing the command.

type CmdError

type CmdError struct {
	clam.RunError
	Filename string
}

func (CmdError) As

func (ce CmdError) As(target any) bool

func (CmdError) Error

func (ce CmdError) Error() string

func (CmdError) Is

func (ce CmdError) Is(target error) bool

func (CmdError) MarshalJSON

func (ce CmdError) MarshalJSON() ([]byte, error)

type CmdResult

type CmdResult struct {
	*Element
	*clam.Result
}

CmdResult is the result of executing a command.

func NewCmdResult

func NewCmdResult(p *Parser, c *Cmd, res *clam.Result) (*CmdResult, error)

func (*CmdResult) MD

func (c *CmdResult) MD() string

func (*CmdResult) MarshalJSON

func (c *CmdResult) MarshalJSON() ([]byte, error)

type Comment

type Comment string

func (Comment) Children

func (tn Comment) Children() Nodes

func (Comment) MarshalJSON

func (c Comment) MarshalJSON() ([]byte, error)

func (Comment) String

func (tn Comment) String() string

func (Comment) Text

func (tn Comment) Text() string

type Document

type Document struct {
	fs.FS
	sync.RWMutex

	ID        string
	Nodes     Nodes
	Parser    *Parser // Parser used to create the document
	Root      string
	SectionID int
	Snippets  Snippets
	Title     string
	Filename  string
}

func (*Document) Body

func (doc *Document) Body() (*Body, error)

func (*Document) Children

func (doc *Document) Children() Nodes

func (*Document) Execute

func (doc *Document) Execute(ctx context.Context) (err error)

Execute the Document with the given context. Any child nodes that implement the PreExecuter, ExecutableNode, or PostExecuter interfaces will be executed.

func (*Document) Format

func (doc *Document) Format(f fmt.State, verb rune)

func (*Document) MD

func (doc *Document) MD() string

func (*Document) MarshalJSON

func (doc *Document) MarshalJSON() ([]byte, error)

func (*Document) Pages

func (doc *Document) Pages() ([]*Page, error)

func (*Document) String

func (doc *Document) String() string

type Documents

type Documents []*Document

func (Documents) Execute

func (docs Documents) Execute(ctx context.Context) error

type Element

type Element struct {
	*Attributes
	sync.RWMutex

	HTMLNode *html.Node
	Nodes    Nodes
	Parent   Node
	Filename string // only set when Parser.ParseFile() is used
}

func NewEl

func NewEl[T ~string](at T, parent Node) *Element

func (*Element) Atom

func (el *Element) Atom() Atom

func (*Element) Attrs

func (el *Element) Attrs() *Attributes

func (*Element) Children

func (el *Element) Children() Nodes

func (*Element) EndTag

func (el *Element) EndTag() string

EndTag returns the end tag for the element. For example, for an element with an Atom of "div", the end tag would be "</div>".

func (*Element) FileName

func (el *Element) FileName() string

FileName returns the filename of the element. This is only set when Parser.ParseFile() is used.

func (*Element) Format

func (el *Element) Format(f fmt.State, verb rune)

func (*Element) HTML

func (el *Element) HTML() *html.Node

func (*Element) JSONMap

func (el *Element) JSONMap() (map[string]any, error)

func (*Element) MD

func (el *Element) MD() string

func (*Element) MarshalJSON

func (el *Element) MarshalJSON() ([]byte, error)

func (*Element) Set

func (el *Element) Set(k string, v string) error

func (*Element) StartTag

func (el *Element) StartTag() string

StartTag returns the start tag for the element. For example, for an element with an Atom of "div", the start tag would be "<div>".

func (*Element) String

func (el *Element) String() string

String returns StartTag() + Children().String() + EndTag()

func (*Element) ValidAttr

func (el *Element) ValidAttr(k string) (string, error)

func (*Element) WrapErr

func (el *Element) WrapErr(err error) error

type EmptyableNode

type EmptyableNode interface {
	IsEmptyNode() bool
}

type ErrAttrEmpty

type ErrAttrEmpty string

func (ErrAttrEmpty) Error

func (e ErrAttrEmpty) Error() string

type ErrAttrNotFound

type ErrAttrNotFound string

func (ErrAttrNotFound) Error

func (e ErrAttrNotFound) Error() string

type ErrIsNil

type ErrIsNil string

func (ErrIsNil) Error

func (e ErrIsNil) Error() string

type ExecutableNode

type ExecutableNode interface {
	Node
	Execute(ctx context.Context, d *Document) error
}

type ExecuteError

type ExecuteError struct {
	Err      error
	Contents []byte
	Document *Document
	Filename string
	Root     string
}

func (ExecuteError) As

func (pe ExecuteError) As(target any) bool

func (ExecuteError) Error

func (pe ExecuteError) Error() string

func (ExecuteError) Is

func (pe ExecuteError) Is(target error) bool

func (ExecuteError) MarshalJSON

func (pe ExecuteError) MarshalJSON() ([]byte, error)

func (ExecuteError) String

func (pe ExecuteError) String() string

func (ExecuteError) Unwrap

func (pe ExecuteError) Unwrap() error

type ExecuteFn

type ExecuteFn func(ctx context.Context, d *Document) error

func (ExecuteFn) Execute

func (fn ExecuteFn) Execute(ctx context.Context, d *Document) error

type FencedCode

type FencedCode struct {
	*Element
}

func NewFencedCode

func NewFencedCode(el *Element) (*FencedCode, error)

func (*FencedCode) Lang

func (code *FencedCode) Lang() string

func (*FencedCode) MD

func (code *FencedCode) MD() string

func (*FencedCode) MarshalJSON

func (code *FencedCode) MarshalJSON() ([]byte, error)

type Figcaption

type Figcaption struct {
	*Element
}

func NewFigcaption

func NewFigcaption(el *Element) (*Figcaption, error)

func (*Figcaption) MD

func (fc *Figcaption) MD() string

func (*Figcaption) MarshalJSON

func (fc *Figcaption) MarshalJSON() ([]byte, error)

type Figure

type Figure struct {
	*Element

	Pos       int
	SectionID int
	// contains filtered or unexported fields
}

func NewFigure

func NewFigure(p *Parser, el *Element) (*Figure, error)
func (f *Figure) Link() string

func (*Figure) MD

func (f *Figure) MD() string

func (*Figure) MarshalJSON

func (f *Figure) MarshalJSON() ([]byte, error)

func (*Figure) Name

func (f *Figure) Name() string

func (*Figure) Style

func (f *Figure) Style() string

Style returns type of the figure. ex: "figure", "table", "listing", ...

type HTMLNode

type HTMLNode interface {
	Node
	HTML() *html.Node
}

type Heading

type Heading struct {
	*Element
	// contains filtered or unexported fields
}

func NewHeading

func NewHeading(el *Element) (*Heading, error)

func (*Heading) Format

func (h *Heading) Format(f fmt.State, verb rune)

func (*Heading) Level

func (h *Heading) Level() int

func (*Heading) MD

func (h *Heading) MD() string

func (*Heading) MarshalJSON

func (h *Heading) MarshalJSON() ([]byte, error)

type IDGenerator

type IDGenerator func(i int, fig *Figure) (string, error)

type Image

type Image struct {
	*Element
}

func NewImage

func NewImage(el *Element) (*Image, error)

func (*Image) Execute

func (i *Image) Execute(ctx context.Context, doc *Document) error

func (*Image) MD

func (i *Image) MD() string

func (*Image) MarshalJSON

func (i *Image) MarshalJSON() ([]byte, error)

type Include

type Include struct {
	*Element
	// contains filtered or unexported fields
}

func NewInclude

func NewInclude(p *Parser, el *Element) (*Include, error)

func (*Include) MD

func (inc *Include) MD() string

func (*Include) MarshalJSON

func (inc *Include) MarshalJSON() ([]byte, error)

func (*Include) PostParse

func (inc *Include) PostParse(p *Parser, d *Document, err error) error

func (*Include) String

func (inc *Include) String() string

type InlineCode

type InlineCode struct {
	*Element
}

func NewInlineCode

func NewInlineCode(el *Element) (*InlineCode, error)

func (*InlineCode) MD

func (code *InlineCode) MD() string

func (*InlineCode) MarshalJSON

func (code *InlineCode) MarshalJSON() ([]byte, error)

func (*InlineCode) String

func (code *InlineCode) String() string

type LI

type LI struct {
	*Element

	Type string
}

func (*LI) MD

func (li *LI) MD() string

func (*LI) MarshalJSON

func (li *LI) MarshalJSON() ([]byte, error)
type Link struct {
	*Element
}
func NewLink(el *Element) (*Link, error)

func (*Link) Href

func (l *Link) Href() (string, error)

func (*Link) MD

func (l *Link) MD() string

func (*Link) MarshalJSON

func (l *Link) MarshalJSON() ([]byte, error)

type MDNode

type MDNode interface {
	Node
	MD() string
}

type Metadata

type Metadata struct {
	Element *Element
	syncx.Map[string, string]
}

func NewMetadata

func NewMetadata(el *Element) (*Metadata, error)

func (*Metadata) Children

func (md *Metadata) Children() Nodes

func (*Metadata) IsEmptyNode

func (md *Metadata) IsEmptyNode() bool

func (*Metadata) MarshalJSON

func (md *Metadata) MarshalJSON() ([]byte, error)

func (*Metadata) PostParse

func (md *Metadata) PostParse(p *Parser, d *Document, err error) error

type Node

type Node interface {
	Children() Nodes
}

type Nodes

type Nodes []Node

func GenerateToC

func GenerateToC(p *Parser, nodes Nodes) (Nodes, error)

func NewAttrCode

func NewAttrCode(p *Parser, el *Element) (Nodes, error)

func NewBodyNodes

func NewBodyNodes(p *Parser, el *Element) (Nodes, error)

NewBodyNodes implements the ParseElementFn type

func NewCmdNodes

func NewCmdNodes(p *Parser, el *Element) (Nodes, error)

func NewCodeNodes

func NewCodeNodes(p *Parser, el *Element) (Nodes, error)

NewCodeNodes implements the ParseElementFn type

func NewFencedCodeNodes

func NewFencedCodeNodes(p *Parser, el *Element) (Nodes, error)

func NewFigcaptionNodes

func NewFigcaptionNodes(p *Parser, el *Element) (Nodes, error)

func NewFigureNodes

func NewFigureNodes(p *Parser, el *Element) (Nodes, error)

func NewGoDocLinkNodes

func NewGoDocLinkNodes(p *Parser, el *Element) (Nodes, error)

func NewGolangNodes

func NewGolangNodes(p *Parser, el *Element) (Nodes, error)

func NewGolangs

func NewGolangs(p *Parser, el *Element) (Nodes, error)

func NewHeadingNodes

func NewHeadingNodes(p *Parser, el *Element) (Nodes, error)

func NewImageNodes

func NewImageNodes(p *Parser, el *Element) (Nodes, error)

func NewIncludeNodes

func NewIncludeNodes(p *Parser, el *Element) (Nodes, error)

NewIncludeNodes implements the ParseElementFn type

func NewInlineCodeNodes

func NewInlineCodeNodes(p *Parser, el *Element) (Nodes, error)

func NewLINodes

func NewLINodes(p *Parser, el *Element) (Nodes, error)

func NewLinkNodes

func NewLinkNodes(p *Parser, el *Element) (Nodes, error)

func NewMetadataNodes

func NewMetadataNodes(p *Parser, el *Element) (Nodes, error)

func NewNowNodes

func NewNowNodes(p *Parser, el *Element) (Nodes, error)

func NewOLNodes

func NewOLNodes(p *Parser, el *Element) (Nodes, error)

func NewPageNodes

func NewPageNodes(p *Parser, el *Element) (Nodes, error)

func NewParagraphNodes

func NewParagraphNodes(p *Parser, el *Element) (Nodes, error)

func NewRefNodes

func NewRefNodes(p *Parser, el *Element) (Nodes, error)

func NewSourceCodeNodes

func NewSourceCodeNodes(p *Parser, el *Element) (Nodes, error)

func NewTDNodes

func NewTDNodes(p *Parser, el *Element) (Nodes, error)

func NewTHNodes

func NewTHNodes(p *Parser, el *Element) (Nodes, error)

func NewTHeadNodes

func NewTHeadNodes(p *Parser, el *Element) (Nodes, error)

func NewTRNodes

func NewTRNodes(p *Parser, el *Element) (Nodes, error)

func NewTableNodes

func NewTableNodes(p *Parser, el *Element) (Nodes, error)

func NewToCNodes

func NewToCNodes(p *Parser, el *Element) (Nodes, error)

func NewULNodes

func NewULNodes(p *Parser, el *Element) (Nodes, error)

func NewVarNodes

func NewVarNodes(p *Parser, el *Element) (Nodes, error)

func ToNodes

func ToNodes[T Node](list []T) Nodes

func (Nodes) Children

func (list Nodes) Children() Nodes

func (Nodes) Delete

func (list Nodes) Delete(node Node) Nodes

func (Nodes) Execute

func (list Nodes) Execute(wg WaitGrouper, ctx context.Context, d *Document) (err error)

func (Nodes) Format

func (list Nodes) Format(f fmt.State, verb rune)

func (Nodes) MD

func (list Nodes) MD() string

func (Nodes) PostExecute

func (list Nodes) PostExecute(ctx context.Context, d *Document, err error) error

func (Nodes) PostParse

func (list Nodes) PostParse(p *Parser, d *Document, err error) error

func (Nodes) PreExecute

func (list Nodes) PreExecute(ctx context.Context, d *Document) error

func (Nodes) String

func (list Nodes) String() string

type Now

type Now struct {
	*Element
}

func (*Now) Execute

func (now *Now) Execute(ctx context.Context, doc *Document) error

func (*Now) MarshalJSON

func (now *Now) MarshalJSON() ([]byte, error)

type OL

type OL struct {
	*Element
}

func (*OL) MD

func (ol *OL) MD() string

func (*OL) MarshalJSON

func (ol *OL) MarshalJSON() ([]byte, error)

type Page

type Page struct {
	Title string
	*Element
}

func NewPage

func NewPage(el *Element) (*Page, error)

func (*Page) Body

func (page *Page) Body() (*Body, error)

func (*Page) MD

func (page *Page) MD() string

func (*Page) MarshalJSON

func (page *Page) MarshalJSON() ([]byte, error)

func (*Page) PostParse

func (page *Page) PostParse(p *Parser, d *Document, err error) error

type Paragraph

type Paragraph struct {
	*Element
}

func (*Paragraph) IsEmptyNode

func (p *Paragraph) IsEmptyNode() bool

func (*Paragraph) MD

func (p *Paragraph) MD() string

func (*Paragraph) MarshalJSON

func (p *Paragraph) MarshalJSON() ([]byte, error)

type ParseElementFn

type ParseElementFn func(p *Parser, el *Element) (Nodes, error)

type ParseError

type ParseError struct {
	Err      error
	Filename string
	Root     string
	Contents []byte
}

func (ParseError) As

func (pe ParseError) As(target any) bool

func (ParseError) Error

func (pe ParseError) Error() string

func (ParseError) Is

func (pe ParseError) Is(target error) bool

func (ParseError) MarshalJSON

func (pe ParseError) MarshalJSON() ([]byte, error)

func (ParseError) Unwrap

func (pe ParseError) Unwrap() error

type Parser

type Parser struct {
	fs.FS

	DisablePages bool
	DocIDGen     func() (string, error) // default: uuid.NewV4().String()
	Filename     string                 // only set when Parser.ParseFile() is used
	NodeParsers  map[Atom]ParseElementFn
	NowFn        func() time.Time // default: time.Now()
	PreParsers   PreParsers
	Root         string
	Section      int
	Vars         syncx.Map[string, any]
	Contents     []byte // a copy of the contents being parsed - set just before parsing
	// contains filtered or unexported fields
}

func NewParser

func NewParser(cab fs.FS) *Parser

NewParser returns a fully initialized Parser. This includes the Markdown pre-parser and the default node parsers.

func (*Parser) MarshalJSON

func (p *Parser) MarshalJSON() ([]byte, error)

func (*Parser) Now

func (p *Parser) Now() time.Time

func (*Parser) Parse

func (p *Parser) Parse(r io.Reader) (doc *Document, err error)

func (*Parser) ParseExecute

func (p *Parser) ParseExecute(ctx context.Context, r io.Reader) (*Document, error)

func (*Parser) ParseExecuteFile

func (p *Parser) ParseExecuteFile(ctx context.Context, name string) (*Document, error)

func (*Parser) ParseExecuteFolder

func (p *Parser) ParseExecuteFolder(ctx context.Context, name string) (Documents, error)

func (*Parser) ParseExecuteFragment

func (p *Parser) ParseExecuteFragment(ctx context.Context, r io.Reader) (Nodes, error)

func (*Parser) ParseFile

func (p *Parser) ParseFile(name string) (doc *Document, err error)

ParseFile parses the given file from Parser.FS. If successful a *Document is returned. The returned *Document is NOT yet executed.

func (*Parser) ParseFolder

func (p *Parser) ParseFolder(name string) (doc Documents, err error)

func (*Parser) ParseFragment

func (p *Parser) ParseFragment(r io.Reader) (Nodes, error)

func (*Parser) ParseHTMLNode

func (p *Parser) ParseHTMLNode(node *html.Node, parent Node) (Node, error)

func (*Parser) Sub

func (p *Parser) Sub(dir string) (*Parser, error)

type PostExecuteError

type PostExecuteError struct {
	Err          error
	Document     *Document
	Filename     string
	OrigErr      error
	Root         string
	PostExecuter any
}

func (PostExecuteError) As

func (e PostExecuteError) As(target any) bool

func (PostExecuteError) Error

func (e PostExecuteError) Error() string

func (PostExecuteError) Is

func (e PostExecuteError) Is(target error) bool

func (PostExecuteError) MarshalJSON

func (pee PostExecuteError) MarshalJSON() ([]byte, error)

func (PostExecuteError) Unwrap

func (e PostExecuteError) Unwrap() error

type PostExecuteFn

type PostExecuteFn func(ctx context.Context, d *Document, err error) error

func (PostExecuteFn) PostExecute

func (fn PostExecuteFn) PostExecute(ctx context.Context, d *Document, err error) error

type PostExecuter

type PostExecuter interface {
	PostExecute(ctx context.Context, d *Document, err error) error
}

type PostParseError

type PostParseError struct {
	Err        error
	Document   *Document
	Filename   string
	OrigErr    error
	Root       string
	PostParser any
}

func (PostParseError) As

func (ppe PostParseError) As(target any) bool

func (PostParseError) Error

func (ppe PostParseError) Error() string

func (PostParseError) Is

func (ppe PostParseError) Is(target error) bool

func (PostParseError) MarshalJSON

func (ppe PostParseError) MarshalJSON() ([]byte, error)

func (PostParseError) Unwrap

func (ppe PostParseError) Unwrap() error

type PostParseFn

type PostParseFn func(p *Parser, d *Document, err error) error

func (PostParseFn) PostParse

func (fn PostParseFn) PostParse(p *Parser, d *Document, err error) error

type PostParser

type PostParser interface {
	PostParse(p *Parser, d *Document, err error) error
}

type PreExecuteError

type PreExecuteError struct {
	Err         error
	Document    *Document
	Filename    string
	Root        string
	PreExecuter any
}

func (PreExecuteError) As

func (pee PreExecuteError) As(target any) bool

func (PreExecuteError) Error

func (pee PreExecuteError) Error() string

func (PreExecuteError) Is

func (pee PreExecuteError) Is(target error) bool

func (PreExecuteError) MarshalJSON

func (pee PreExecuteError) MarshalJSON() ([]byte, error)

func (PreExecuteError) Unwrap

func (pee PreExecuteError) Unwrap() error

type PreExecuteFn

type PreExecuteFn func(ctx context.Context, d *Document) error

func (PreExecuteFn) PreExecute

func (fn PreExecuteFn) PreExecute(ctx context.Context, d *Document) error

type PreExecuter

type PreExecuter interface {
	PreExecute(ctx context.Context, d *Document) error
}

type PreParseError

type PreParseError struct {
	Err       error
	Contents  []byte
	Filename  string
	Root      string
	PreParser any
}

func (PreParseError) As

func (e PreParseError) As(target any) bool

func (PreParseError) Error

func (e PreParseError) Error() string

func (PreParseError) Is

func (e PreParseError) Is(target error) bool

func (PreParseError) MarshalJSON

func (e PreParseError) MarshalJSON() ([]byte, error)

func (PreParseError) Unwrap

func (e PreParseError) Unwrap() error

type PreParseFn

type PreParseFn func(p *Parser, r io.Reader) (io.Reader, error)

func GoTemplates

func GoTemplates() PreParseFn

func Markdown

func Markdown() PreParseFn

func VarProcessor

func VarProcessor() PreParseFn

func (PreParseFn) PreParse

func (fn PreParseFn) PreParse(p *Parser, r io.Reader) (io.Reader, error)

type PreParser

type PreParser interface {
	PreParse(p *Parser, r io.Reader) (io.Reader, error)
}

type PreParsers

type PreParsers []PreParser

func (PreParsers) PreParse

func (list PreParsers) PreParse(p *Parser, r io.Reader) (io.Reader, error)

type Ref

type Ref struct {
	*Element
	*Figure
}

func NewRef

func NewRef(el *Element) (*Ref, error)

func (*Ref) MD

func (r *Ref) MD() string

func (*Ref) MarshalJSON

func (r *Ref) MarshalJSON() ([]byte, error)

func (*Ref) PostExecute

func (r *Ref) PostExecute(ctx context.Context, doc *Document, err error) error

type RefProcessor

type RefProcessor struct {
	IDGenerator IDGenerator
	// contains filtered or unexported fields
}

func (*RefProcessor) CurIndex

func (rp *RefProcessor) CurIndex(key string) int

CurIndex will return the current index for the given key.

func (*RefProcessor) NextIndex

func (rp *RefProcessor) NextIndex(key string) int

NextIndex will increment the index for the given key, and return the new index.

func (*RefProcessor) Process

func (rp *RefProcessor) Process(doc *Document) error

func (*RefProcessor) ProcessFigure

func (rp *RefProcessor) ProcessFigure(sectionID int, fig *Figure) error

type Snippet

type Snippet struct {
	Content string `json:"content,omitempty"` // The content of the snippet
	File    string `json:"file,omitempty"`    // the file name of the snippet
	Lang    string `json:"lang,omitempty"`    // the language of the snippet
	Name    string `json:"name,omitempty"`    // the name of the snippet
	Start   int    `json:"start,omitempty"`   // the start line of the snippet
	End     int    `json:"end,omitempty"`     // the end line of the snippet
}

func (Snippet) Children

func (snip Snippet) Children() Nodes

func (Snippet) IsZero

func (snip Snippet) IsZero() bool

func (Snippet) MD

func (snip Snippet) MD() string

func (*Snippet) MarshalJSON

func (snip *Snippet) MarshalJSON() ([]byte, error)

func (Snippet) String

func (snip Snippet) String() string

type Snippets

type Snippets struct {
	// contains filtered or unexported fields
}

func (*Snippets) Add

func (sm *Snippets) Add(ext string, rule string)

func (*Snippets) Get

func (sm *Snippets) Get(name string) (map[string]Snippet, bool)

func (*Snippets) MarshalJSON

func (sm *Snippets) MarshalJSON() ([]byte, error)

func (*Snippets) Parse

func (sm *Snippets) Parse(path string, src []byte) (map[string]Snippet, error)

ParseSnippets will parse the given src and return a map of Snippets.

func (*Snippets) TrimComments

func (sm *Snippets) TrimComments(path string, src []byte) ([]byte, error)

func (*Snippets) UnmarshalJSON

func (sm *Snippets) UnmarshalJSON(data []byte) error

type SourceCode

type SourceCode struct {
	*Element
	Lang    string
	Src     string
	Snippet Snippet
}

func (*SourceCode) Execute

func (code *SourceCode) Execute(ctx context.Context, d *Document) error

func (*SourceCode) MD

func (code *SourceCode) MD() string

func (*SourceCode) MarshalJSON

func (code *SourceCode) MarshalJSON() ([]byte, error)

func (*SourceCode) String

func (code *SourceCode) String() string

type TD

type TD struct {
	*Element
}

func NewTD

func NewTD(p *Parser, el *Element) (*TD, error)

func (*TD) IsEmptyNode

func (td *TD) IsEmptyNode() bool

func (*TD) MarshalJSON

func (td *TD) MarshalJSON() ([]byte, error)

type TH

type TH struct {
	*Element
}

func NewTH

func NewTH(p *Parser, el *Element) (*TH, error)

func (*TH) IsEmptyNode

func (th *TH) IsEmptyNode() bool

func (*TH) MarshalJSON

func (th *TH) MarshalJSON() ([]byte, error)

type THead

type THead struct {
	*Element
}

func NewTHead

func NewTHead(el *Element) (*THead, error)

func (*THead) IsEmptyNode

func (th *THead) IsEmptyNode() bool

func (*THead) MarshalJSON

func (th *THead) MarshalJSON() ([]byte, error)

func (*THead) String

func (th *THead) String() string

type TR

type TR struct {
	*Element
}

func NewTR

func NewTR(el *Element) (*TR, error)

func (*TR) IsEmptyNode

func (tr *TR) IsEmptyNode() bool

func (*TR) MarshalJSON

func (tr *TR) MarshalJSON() ([]byte, error)

type Table

type Table struct {
	*Element
}

func NewTable

func NewTable(p *Parser, el *Element) (*Table, error)

func (*Table) Data

func (tab *Table) Data() (*table.Table, error)

func (*Table) IsEmptyNode

func (tab *Table) IsEmptyNode() bool

func (*Table) MD

func (tab *Table) MD() string

func (*Table) MarshalJSON

func (tab *Table) MarshalJSON() ([]byte, error)

type Tag

type Tag interface {
	Node
	Atomable
	StartTag() string
	EndTag() string
}

type Tags

type Tags []Tag

type Text

type Text string

func (Text) Children

func (tn Text) Children() Nodes

func (Text) Format

func (tn Text) Format(f fmt.State, verb rune)

func (Text) IsEmptyNode

func (tn Text) IsEmptyNode() bool

func (Text) MD

func (tn Text) MD() string

func (Text) MarshalJSON

func (tn Text) MarshalJSON() ([]byte, error)

func (Text) String

func (tn Text) String() string

type ToC

type ToC struct {
	*Element
}

func (*ToC) MD

func (toc *ToC) MD() string

func (*ToC) MarshalJSON

func (toc *ToC) MarshalJSON() ([]byte, error)

func (*ToC) PostExecute

func (toc *ToC) PostExecute(ctx context.Context, doc *Document, err error) error

type UL

type UL struct {
	*Element
}

func (*UL) MD

func (ol *UL) MD() string

func (*UL) MarshalJSON

func (ul *UL) MarshalJSON() ([]byte, error)

type Var

type Var struct {
	*Element

	Key   string
	Value any
}

func NewVarNode

func NewVarNode(p *Parser, el *Element) (*Var, error)

func (*Var) Execute

func (v *Var) Execute(ctx context.Context, doc *Document) error

func (*Var) MarshalJSON

func (v *Var) MarshalJSON() ([]byte, error)

func (*Var) String

func (v *Var) String() string

type WaitGrouper

type WaitGrouper interface {
	Go(fn func() error)
}

Directories

Path Synopsis
cmd
hype command
docs
internal

Jump to

Keyboard shortcuts

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