parser

package
v0.2.546 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLegacyFileFormat = errors.New("legacy file format - run templ migrate")
View Source
var ErrNonSpaceCharacter = errors.New("non space character found")
View Source
var ErrTemplateNotFound = errors.New("template not found")

Functions

func ExpressionOf

func ExpressionOf(p parse.Parser[string]) parse.Parser[Expression]

func StripType

func StripType[T any](p parse.Parser[T]) parse.Parser[any]

StripType takes the parser and throws away the return value.

Types

type Attribute

type Attribute interface {
	// Write out the string.
	Write(w io.Writer, indent int) error
}

type BoolConstantAttribute

type BoolConstantAttribute struct {
	Name string
}

<hr noshade/>

func (BoolConstantAttribute) String

func (bca BoolConstantAttribute) String() string

func (BoolConstantAttribute) Write

func (bca BoolConstantAttribute) Write(w io.Writer, indent int) error

type BoolExpressionAttribute

type BoolExpressionAttribute struct {
	Name       string
	Expression Expression
}

noshade={ templ.Bool(...) }

func (BoolExpressionAttribute) String

func (ea BoolExpressionAttribute) String() string

func (BoolExpressionAttribute) Write

func (ea BoolExpressionAttribute) Write(w io.Writer, indent int) error

type CSSProperty

type CSSProperty interface {
	IsCSSProperty() bool
	Write(w io.Writer, indent int) error
}

CSSProperty is a CSS property and value pair.

type CSSTemplate

type CSSTemplate struct {
	Name       Expression
	Properties []CSSProperty
}

CSS definition.

css Name() {
  color: #ffffff;
  background-color: { constants.BackgroundColor };
  background-image: url('./somewhere.png');
}

func (CSSTemplate) IsTemplateFileNode

func (css CSSTemplate) IsTemplateFileNode() bool

func (CSSTemplate) Write

func (css CSSTemplate) Write(w io.Writer, indent int) error

type CallTemplateExpression

type CallTemplateExpression struct {
	// Expression returns a template to execute.
	Expression Expression
}

CallTemplateExpression can be used to create and render a template using data. {! Other(p.First, p.Last) } or it can be used to render a template parameter. {! v }

func (CallTemplateExpression) IsNode

func (cte CallTemplateExpression) IsNode() bool

func (CallTemplateExpression) Write

func (cte CallTemplateExpression) Write(w io.Writer, indent int) error

type CaseExpression

type CaseExpression struct {
	Expression  Expression
	Children    []Node
	Diagnostics []Diagnostic
}

case "Something":

type ChildrenExpression

type ChildrenExpression struct{}

ChildrenExpression can be used to rended the children of a templ element. { children ... }

func (ChildrenExpression) IsNode

func (ChildrenExpression) IsNode() bool

func (ChildrenExpression) Write

func (ChildrenExpression) Write(w io.Writer, indent int) error

type ConditionalAttribute

type ConditionalAttribute struct {
	Expression Expression
	Then       []Attribute
	Else       []Attribute
}
<a href="test" \
	if active {
   class="isActive"
 }

func (ConditionalAttribute) String

func (ca ConditionalAttribute) String() string

func (ConditionalAttribute) Write

func (ca ConditionalAttribute) Write(w io.Writer, indent int) error

type ConstantAttribute

type ConstantAttribute struct {
	Name        string
	Value       string
	SingleQuote bool
}

href=""

func (ConstantAttribute) String

func (ca ConstantAttribute) String() string

func (ConstantAttribute) Write

func (ca ConstantAttribute) Write(w io.Writer, indent int) error

type ConstantCSSProperty

type ConstantCSSProperty struct {
	Name  string
	Value string
}

color: #ffffff;

func (ConstantCSSProperty) IsCSSProperty

func (c ConstantCSSProperty) IsCSSProperty() bool

func (ConstantCSSProperty) String

func (c ConstantCSSProperty) String(minified bool) string

func (ConstantCSSProperty) Write

func (c ConstantCSSProperty) Write(w io.Writer, indent int) error

type Diagnostic

type Diagnostic struct {
	Message string
	Range   Range
}

Diagnostic for template file.

type DocType

type DocType struct {
	Value string
}

<!DOCTYPE html>

func (DocType) IsNode

func (dt DocType) IsNode() bool

func (DocType) Write

func (dt DocType) Write(w io.Writer, indent int) error

type Element

type Element struct {
	Name           string
	Attributes     []Attribute
	IndentAttrs    bool
	Children       []Node
	IndentChildren bool
	TrailingSpace  TrailingSpace
	Diagnostics    []Diagnostic
}

<a .../> or <div ...>...</div>

func (Element) IsBlockElement

func (e Element) IsBlockElement() bool

func (Element) IsNode

func (e Element) IsNode() bool

func (Element) Trailing

func (e Element) Trailing() TrailingSpace

func (Element) Validate

func (e Element) Validate() (msgs []string, ok bool)

Validate that no invalid expressions have been used.

func (Element) Write

func (e Element) Write(w io.Writer, indent int) error

type ElseIfExpression

type ElseIfExpression struct {
	Expression  Expression
	Then        []Node
	Diagnostics []Diagnostic
}

type Expression

type Expression struct {
	Value string
	Range Range
}

Expression containing Go code.

func NewExpression

func NewExpression(value string, from, to parse.Position) Expression

NewExpression creates a Go expression.

type ExpressionAttribute

type ExpressionAttribute struct {
	Name       string
	Expression Expression
}

href={ ... }

func (ExpressionAttribute) String

func (ea ExpressionAttribute) String() string

func (ExpressionAttribute) Write

func (ea ExpressionAttribute) Write(w io.Writer, indent int) (err error)

type ExpressionCSSProperty

type ExpressionCSSProperty struct {
	Name  string
	Value StringExpression
}

background-color: { constants.BackgroundColor };

func (ExpressionCSSProperty) IsCSSProperty

func (c ExpressionCSSProperty) IsCSSProperty() bool

func (ExpressionCSSProperty) Write

func (c ExpressionCSSProperty) Write(w io.Writer, indent int) error

type ForExpression

type ForExpression struct {
	Expression  Expression
	Children    []Node
	Diagnostics []Diagnostic
}
for i, v := range p.Addresses {
  {! Address(v) }
}

func (ForExpression) IsNode

func (fe ForExpression) IsNode() bool

func (ForExpression) Write

func (fe ForExpression) Write(w io.Writer, indent int) error

type GoComment

type GoComment struct {
	Contents  string
	Multiline bool
}

GoComment.

func (GoComment) IsNode

func (c GoComment) IsNode() bool

func (GoComment) Write

func (c GoComment) Write(w io.Writer, indent int) error

type HTMLComment

type HTMLComment struct {
	Contents string
}

HTMLComment.

func (HTMLComment) IsNode

func (c HTMLComment) IsNode() bool

func (HTMLComment) Write

func (c HTMLComment) Write(w io.Writer, indent int) error

type HTMLTemplate

type HTMLTemplate struct {
	Diagnostics []Diagnostic
	Expression  Expression
	Children    []Node
}

HTMLTemplate definition.

templ Name(p Parameter) {
  if ... {
      <Element></Element>
  }
}

func (HTMLTemplate) IsTemplateFileNode

func (t HTMLTemplate) IsTemplateFileNode() bool

func (HTMLTemplate) Write

func (t HTMLTemplate) Write(w io.Writer, indent int) error

type IfExpression

type IfExpression struct {
	Expression  Expression
	Then        []Node
	ElseIfs     []ElseIfExpression
	Else        []Node
	Diagnostics []Diagnostic
}

if p.Type == "test" && p.thing { }

func (IfExpression) IsNode

func (n IfExpression) IsNode() bool

func (IfExpression) Write

func (n IfExpression) Write(w io.Writer, indent int) error

type Node

type Node interface {
	IsNode() bool
	// Write out the string.
	Write(w io.Writer, indent int) error
}

A Node appears within a template, e.g. an StringExpression, Element, IfExpression etc.

type Nodes

type Nodes struct {
	Diagnostics []Diagnostic
	Nodes       []Node
}

type Package

type Package struct {
	Expression Expression
}

func (Package) Write

func (p Package) Write(w io.Writer, indent int) error

type Position

type Position struct {
	Index int64
	Line  uint32
	Col   uint32
}

Source mapping to map from the source code of the template to the in-memory representation.

func NewPosition

func NewPosition(index int64, line, col uint32) Position

NewPosition initialises a position.

func (Position) String

func (p Position) String() string

type Range

type Range struct {
	From Position
	To   Position
}

Range of text within a file.

type RawElement

type RawElement struct {
	Name       string
	Attributes []Attribute
	Contents   string
}

func (RawElement) IsNode

func (e RawElement) IsNode() bool

func (RawElement) Write

func (e RawElement) Write(w io.Writer, indent int) error

type ScriptTemplate

type ScriptTemplate struct {
	Name       Expression
	Parameters Expression
	Value      string
}

ScriptTemplate is a script block.

func (ScriptTemplate) IsTemplateFileNode

func (s ScriptTemplate) IsTemplateFileNode() bool

func (ScriptTemplate) Write

func (s ScriptTemplate) Write(w io.Writer, indent int) error

type SourceMap

type SourceMap struct {
	SourceLinesToTarget map[uint32]map[uint32]Position
	TargetLinesToSource map[uint32]map[uint32]Position
}

func NewSourceMap

func NewSourceMap() *SourceMap

NewSourceMap creates a new lookup to map templ source code to items in the parsed template.

func (*SourceMap) Add

func (sm *SourceMap) Add(src Expression, tgt Range) (updatedFrom Position)

Add an item to the lookup.

func (*SourceMap) SourcePositionFromTarget

func (sm *SourceMap) SourcePositionFromTarget(line, col uint32) (src Position, ok bool)

SourcePositionFromTarget looks the source position using the target position.

func (*SourceMap) TargetPositionFromSource

func (sm *SourceMap) TargetPositionFromSource(line, col uint32) (tgt Position, ok bool)

TargetPositionFromSource looks up the target position using the source position.

type SpreadAttributes

type SpreadAttributes struct {
	Expression Expression
}

<a { spread... } />

func (SpreadAttributes) String

func (sa SpreadAttributes) String() string

func (SpreadAttributes) Write

func (sa SpreadAttributes) Write(w io.Writer, indent int) error

type StringExpression

type StringExpression struct {
	Expression Expression
	// TrailingSpace lists what happens after the expression.
	TrailingSpace TrailingSpace
}

StringExpression is used within HTML elements, and for style values. { ... }

func (StringExpression) IsNode

func (se StringExpression) IsNode() bool

func (StringExpression) IsStyleDeclarationValue

func (se StringExpression) IsStyleDeclarationValue() bool

func (StringExpression) Trailing

func (se StringExpression) Trailing() TrailingSpace

func (StringExpression) Write

func (se StringExpression) Write(w io.Writer, indent int) error

type SwitchExpression

type SwitchExpression struct {
	Expression Expression
	Cases      []CaseExpression
}
switch p.Type {
 case "Something":
}

func (SwitchExpression) IsNode

func (se SwitchExpression) IsNode() bool

func (SwitchExpression) Write

func (se SwitchExpression) Write(w io.Writer, indent int) error

type TemplElementExpression

type TemplElementExpression struct {
	// Expression returns a template to execute.
	Expression Expression
	// Children returns the elements in a block element.
	Children    []Node
	Diagnostics []Diagnostic
}

TemplElementExpression can be used to create and render a template using data. @Other(p.First, p.Last) or it can be used to render a template parameter. @v

func (TemplElementExpression) IsNode

func (tee TemplElementExpression) IsNode() bool

func (TemplElementExpression) Write

func (tee TemplElementExpression) Write(w io.Writer, indent int) error

type TemplateFile

type TemplateFile struct {
	// Header contains comments or whitespace at the top of the file.
	Header []TemplateFileGoExpression
	// Package expression.
	Package Package
	// Nodes in the file.
	Nodes []TemplateFileNode
	// Diagnostics contains any errors or warnings.
	Diagnostics []Diagnostic
}

func Parse

func Parse(fileName string) (TemplateFile, error)

func ParseString

func ParseString(template string) (TemplateFile, error)

func (TemplateFile) Write

func (tf TemplateFile) Write(w io.Writer) error

type TemplateFileGoExpression

type TemplateFileGoExpression struct {
	Expression Expression
}

TemplateFileGoExpression within a TemplateFile

func (TemplateFileGoExpression) IsTemplateFileNode

func (exp TemplateFileGoExpression) IsTemplateFileNode() bool

func (TemplateFileGoExpression) Write

func (exp TemplateFileGoExpression) Write(w io.Writer, indent int) error

type TemplateFileNode

type TemplateFileNode interface {
	IsTemplateFileNode() bool
	Write(w io.Writer, indent int) error
}

TemplateFileNode can be a Template, CSS, Script or Go.

type TemplateFileParser

type TemplateFileParser struct {
	DefaultPackage string
}

func NewTemplateFileParser

func NewTemplateFileParser(pkg string) TemplateFileParser

NewTemplateFileParser creates a new TemplateFileParser.

func (TemplateFileParser) Parse

func (p TemplateFileParser) Parse(pi *parse.Input) (tf TemplateFile, ok bool, err error)

type Text

type Text struct {
	// Value is the raw HTML encoded value.
	Value string
	// TrailingSpace lists what happens after the text.
	TrailingSpace TrailingSpace
}

Text node within the document.

func (Text) IsNode

func (t Text) IsNode() bool

func (Text) Trailing

func (t Text) Trailing() TrailingSpace

func (Text) Write

func (t Text) Write(w io.Writer, indent int) error

type TrailingSpace

type TrailingSpace string

TrailingSpace defines the whitespace that may trail behind the close of an element, a text node, or string expression.

const (
	SpaceNone       TrailingSpace = ""
	SpaceHorizontal TrailingSpace = " "
	SpaceVertical   TrailingSpace = "\n"
)

func NewTrailingSpace

func NewTrailingSpace(s string) (ts TrailingSpace, err error)

type Whitespace

type Whitespace struct {
	Value string
}

Whitespace.

func (Whitespace) IsNode

func (ws Whitespace) IsNode() bool

func (Whitespace) Write

func (ws Whitespace) Write(w io.Writer, indent int) error

type WhitespaceTrailer

type WhitespaceTrailer interface {
	Trailing() TrailingSpace
}

Jump to

Keyboard shortcuts

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