soyjs

package
v0.0.0-...-c50f1b6 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package soyjs compiles Soy to javascript.

It fulfills the same interface as the javascript produced by the official Soy compiler and should work as a drop-in replacement. https://developers.google.com/closure/templates/docs/javascript_usage

It is presently alpha quality. See ../TODO for unimplemented features.

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("file not found")
View Source
var Funcs = make(map[string]Func, len(funcs))

Funcs contains the available Soy functions. Callers may add custom functions to this map.

View Source
var PrintDirectives = map[string]PrintDirective{
	"insertWordBreaks":  {"soy.$$insertWordBreaks", true},
	"changeNewlineToBr": {"soy.$$changeNewlineToBr", true},
	"truncate":          {"soy.$$truncate", false},
	"id":                {"", true},
	"noAutoescape":      {"", true},
	"escapeHtml":        {"soy.$$escapeHtml", true},
	"escapeUri":         {"soy.$$escapeUri", true},
	"escapeJsString":    {"soy.$$escapeJsString", true},
	"bidiSpanWrap":      {"soy.$$bidiSpanWrap", false},
	"bidiUnicodeWrap":   {"soy.$$bidiUnicodeWrap", false},
	"json":              {"JSON.stringify", true},
}

PrintDirectives are the builtin print directives. Callers may add their own print directives to this map.

Functions

func ES6Identifier

func ES6Identifier(s string) string

ES6Identifier creates an ES6 compatible function name without periods. It replaces all periods, which usually denominate namespaces in soy, with a double underscore. For example, from the file

{namespace say}
{template .hello_world}
Hello World
{/template}

when ES6Identifier is called on

say.hello_world

it will return

say__hello_world

func Write

func Write(out io.Writer, node ast.Node, options Options) (err error)

Write writes the javascript represented by the given node to the given writer. The first error encountered is returned.

Types

type ES5Formatter

type ES5Formatter struct{}

ES5Formatter implements the JSFormatter interface and creates Javascript files following the ES5 Javascript format (without imports)

func (ES5Formatter) Call

func (f ES5Formatter) Call(name string) (string, string)

Call returns two values, the name of the template to save in the called functions map, and a string that is written into the imports - for ES5, there are no imports

func (ES5Formatter) Directive

func (f ES5Formatter) Directive(dir PrintDirective) string

Directive takes in a PrintDirective and returns a string that is written into the imports - for ES5, there are no imports

func (ES5Formatter) Function

func (f ES5Formatter) Function(fn Func) string

Function takes in a Func and returns a string that is written into the imports - for ES5, there are no imports

func (ES5Formatter) Template

func (f ES5Formatter) Template(name string) (string, string)

Template returns two values, the name of the template to save in the defined functions map, and how the function should be defined. For ES5, the function is not exported, but defined globally

type ES6Formatter

type ES6Formatter struct{}

ES6Formatter implements the JSFormatter interface and creates Javascript files following the ES6 Javascript format (with imports)

func (ES6Formatter) Call

func (f ES6Formatter) Call(name string) (string, string)

Call returns two values, the name of the template to save in the called functions map, and a string that is written into the imports

func (ES6Formatter) Directive

func (f ES6Formatter) Directive(dir PrintDirective) string

Directive takes in a PrintDirective and returns a string that is written into the imports

func (ES6Formatter) Function

func (f ES6Formatter) Function(fn Func) string

Function takes in a Func and returns a string that is written into the imports

func (ES6Formatter) Template

func (f ES6Formatter) Template(name string) (string, string)

Template returns two values, the name of the template to save in the defined functions map, and how the function should be defined. For ES6, the function is not defined globally, but exported

type Func

type Func struct {
	Name            string
	Apply           func(js JSWriter, args []ast.Node)
	ValidArgLengths []int
}

Func represents a Soy function that may invoked within a template.

type Generator

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

Generator provides an interface to a template registry capable of generating javascript to execute the embodied templates. The generated javascript requires lib/soyutils.js to already have been loaded.

func NewGenerator

func NewGenerator(registry *template.Registry) *Generator

NewGenerator returns a new javascript generator capable of producing javascript for the templates contained in the given registry.

func (*Generator) WriteFile

func (gen *Generator) WriteFile(out io.Writer, filename string) error

WriteFile generates javascript corresponding to the Soy file of the given name.

type JSFormatter

type JSFormatter interface {
	// Template returns two values, the name of the template to save
	// in the defined functions map, and how the function should be defined.
	Template(name string) (string, string)
	// Call returns two values, the name of the template to save
	// in the called functions map, and a string that is written
	// into the imports
	Call(name string) (string, string)
	// Directive takes in a PrintDirective and returns a string
	// that is written into the imports
	Directive(PrintDirective) string
	// Function takes in a Func and returns a string
	// that is written into the imports
	Function(Func) string
}

The JSFormatter interface allows for callers to choose which version of Javascript they would like soyjs to output. To maintain backwards compatibility, if no JSFormatter is specified in the Options, soyjs will default to the ES5Formatter implemented in exec.go

type JSWriter

type JSWriter interface {
	// Write writes the given arguments into the generated javascript.  It is
	// recommended to only pass strings and ast.Nodes to Write. Other types
	// are printed using their default string representation (fmt.Sprintf("%v")).
	Write(...interface{})
}

JSWriter is provided to functions to write to the generated javascript.

type Options

type Options struct {
	Messages  soymsg.Bundle
	Formatter JSFormatter
}

Options for js source generation. When no Formatter is defined, soyjs will default to ES5Formatter from exec.go

type PrintDirective

type PrintDirective struct {
	Name             string
	CancelAutoescape bool
}

PrintDirective represents a transformation applied when printing a value.

Jump to

Keyboard shortcuts

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