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 ParseFragment ¶
ParseFragment parses an HTML fragment (like a template partial).
func ParseReader ¶
ParseReader parses HTML from an io.Reader.
type Node ¶
Node wraps html.Node with source location and traversal helpers.
func (*Node) TextContent ¶
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 ¶
OriginalPosition converts a position in processed content to original position.