parser

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package parser provides HTML parsing with Go template syntax support.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	Root     *Node
	Filename string
	// IsTemplateFragment indicates file starts with {{define - a Go template partial
	IsTemplateFragment bool
	// contains filtered or unexported fields
}

Document represents a parsed HTML document with source location tracking.

func Parse

func Parse(filename string, content []byte) (*Document, error)

Parse parses HTML content and returns a Document with line tracking.

func ParseFragment

func ParseFragment(filename string, content []byte) (*Document, error)

ParseFragment parses an HTML fragment (like a template partial).

func ParseReader

func ParseReader(filename string, r io.Reader) (*Document, error)

ParseReader parses HTML from an io.Reader.

func (*Document) Walk

func (d *Document) Walk(fn WalkFunc)

Walk traverses the document tree, calling fn for each node.

type Node

type Node struct {
	*html.Node
	Line     int
	Col      int
	Parent   *Node
	Children []*Node
}

Node wraps html.Node with source location and traversal helpers.

func (*Node) GetAttr

func (n *Node) GetAttr(name string) string

GetAttr returns the value of an attribute, or empty string if not found.

func (*Node) HasAttr

func (n *Node) HasAttr(name string) bool

HasAttr checks if the node has an attribute with the given name.

func (*Node) IsElement

func (n *Node) IsElement(tag string) bool

IsElement returns true if this is an element node with the given tag name.

func (*Node) TextContent

func (n *Node) TextContent() string

TextContent returns the combined text content of the node and descendants.

type Preprocessor

type Preprocessor struct{}

Preprocessor handles Go template syntax in HTML files.

func NewPreprocessor

func NewPreprocessor() *Preprocessor

NewPreprocessor creates a new template preprocessor.

func (*Preprocessor) Process

func (p *Preprocessor) Process(input []byte) ([]byte, *SourceMap, error)

Process replaces Go template syntax with placeholders to produce valid HTML. Returns the processed content and a source map for error location recovery.

Replacement strategies:

  • {{ .Field }} in text content → empty string (preserves structure)
  • {{ .Field }} in attribute values → "tmpl" (keeps attribute valid)
  • {{if}}...{{else}}...{{end}} blocks → content of if-branch kept only
  • {{if}}...{{end}} blocks → content kept
  • {{range}}...{{end}} → single iteration content
  • {{template "name"}} → empty (included template not available)

func (*Preprocessor) ProcessFile

func (p *Preprocessor) ProcessFile(content []byte) ([]byte, *SourceMap, error)

ProcessFile reads a file and processes its template content.

type SourceMap

type SourceMap struct {
	// Original source content
	Original []byte
	// Processed content with templates replaced
	Processed []byte
}

SourceMap tracks the mapping between processed and original source positions. Used to report errors at their original line/column locations.

func (*SourceMap) OriginalPosition

func (sm *SourceMap) OriginalPosition(line, col int) (origLine, origCol int)

OriginalPosition converts a position in processed content to original position.

type WalkFunc

type WalkFunc func(*Node) bool

WalkFunc is called for each node during tree traversal. Return false to stop traversal.

Jump to

Keyboard shortcuts

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