Documentation
¶
Overview ¶
Package erubi is a pure-Go (no cgo) reimplementation of the erubi gem — Rails' default ERB template engine and the modern, frozen-string successor to erubis. It is a template-to-Ruby-source COMPILER: given an ERB template it emits the exact Ruby source that erubi's Erubi::Engine#src would emit, matching the erubi gem (1.13.1, Ruby 4.0.5) byte-for-byte, so that a downstream Ruby evaluation (e.g. go-embedded-ruby/rbgo, feeding actionview's rendering seam) produces identical output.
What it is NOT: the final eval of the compiled source against a binding needs a Ruby interpreter and is deliberately left to the consumer. This package compiles; the host evaluates. The escape helper ::Erubi.h that the emitted source calls is likewise a runtime helper the host supplies; HTMLEscape below documents and mirrors its semantics for parity and testing.
The package faithfully ports erubi's lib/erubi.rb Engine#initialize: the <% %> (execute), <%= %> (output, auto-escaped when Escape), <%== %> (the inverted-escape output), <%# %> (comment) and <%% %%> literal tags, the trim rules for <% %>/<%- -%>, the frozen-string-literal handling, the chained-append optimization, the begin/ensure wrapping, and the preamble/postamble/regexp/ literal-delimiter/escape-function overrides. CaptureEndEngine ports lib/erubi/capture_end.rb (the <%|= ... %> ... <%| end %> block-capture variant Rails uses for form_with-style helpers).
Index ¶
Constants ¶
This section is empty.
Variables ¶
var CaptureEndRegexp = regexp.MustCompile(`(?s)<%(\|?={1,2}|-|#|%|\|)?(.*?)([-=])?%>([ \t]*\r?\n)?`)
CaptureEndRegexp is the scanner used by NewCaptureEndEngine, mirroring the regexp Erubi::CaptureEndEngine installs, adding the <%| , <%|= and <%|== tags.
var DefaultRegexp = regexp.MustCompile(`(?s)<%(={1,2}|-|#|%)?(.*?)([-=])?%>([ \t]*\r?\n)?`)
DefaultRegexp is the scanner used by NewEngine, mirroring Erubi::Engine::DEFAULT_REGEXP. Ruby's /m flag (dot matches newline) maps to Go's (?s) flag.
Functions ¶
func HTMLEscape ¶
HTMLEscape mirrors ::Erubi.h (ERB::Escape#html_escape): it replaces the five HTML-significant characters with their entity references (note "'" becomes "'"). The compiled source emitted by this package calls ::Erubi.h at render time; this function documents and reproduces that helper's semantics so a host binding (or a test) can supply an identical escape.
Types ¶
type CaptureEndEngine ¶
type CaptureEndEngine struct {
*Engine
}
CaptureEndEngine is the compiled result of the capture-block ERB variant, mirroring Erubi::CaptureEndEngine. It supports capturing blocks via the <%|= %> and <%|== %> tags, explicitly ended with a <%| end %> block — the form Rails uses for form_with-style capture helpers. It embeds *Engine, so Src, Filename and BufVar are available directly.
func NewCaptureEndEngine ¶
func NewCaptureEndEngine(input string, opts Options) *CaptureEndEngine
NewCaptureEndEngine compiles input into a CaptureEndEngine, mirroring Erubi::CaptureEndEngine.new. It installs CaptureEndRegexp (unless overridden by Options.Regexp) so the <%| , <%|= and <%|== tags are recognized, and honors the EscapeCapture and YieldReturnsBuffer options. It panics with *InvalidIndicatorError only if a caller-supplied Regexp yields an unknown indicator.
type Engine ¶
type Engine struct {
// contains filtered or unexported fields
}
Engine is the compiled result for a template, mirroring Erubi::Engine. Its Src is the frozen Ruby source that, when eval'd against a binding, renders the template identically to the erubi gem.
func NewEngine ¶
NewEngine compiles input into an Engine, mirroring Erubi::Engine.new. It panics with *InvalidIndicatorError only if a caller-supplied Options.Regexp yields an unknown tag indicator.
func (*Engine) BufVar ¶
BufVar returns the buffer variable name used by the compiled source (erubi's #bufvar).
type InvalidIndicatorError ¶
type InvalidIndicatorError struct{ Indicator string }
InvalidIndicatorError is what NewEngine/NewCaptureEndEngine panics with when a custom Regexp yields a tag indicator the engine does not know how to handle, mirroring the ArgumentError raised by Erubi::Engine#handle. It is only reachable with a caller-supplied Regexp; the built-in regexps never produce an unknown indicator.
func (*InvalidIndicatorError) Error ¶
func (e *InvalidIndicatorError) Error() string
type Options ¶
type Options struct {
// Escape makes <%= %> auto-HTML-escape (via EscapeFunc) and inverts the
// <%= %> / <%== %> semantics. nil falls back to EscapeHTML, then false.
Escape *bool
// EscapeHTML is the lower-priority alias for Escape (erubi's :escape_html).
EscapeHTML *bool
// Trim enables erubi's whitespace-trim rules for <% %> / <%- -%> and the
// comment tag. nil means the erubi default, true.
Trim *bool
// FreezeTemplateLiterals suffixes each literal text chunk with .freeze.
// nil means the erubi default, true.
FreezeTemplateLiterals *bool
// Freeze prepends a "# frozen_string_literal: true" magic comment.
Freeze bool
// ChainAppends chains buffer appends (_buf << a << b) for speed.
ChainAppends bool
// Ensure wraps the template in begin/ensure restoring the previous BufVar.
Ensure bool
// EscapeFunc overrides the escape function name (erubi default resolves to
// "::Erubi.h", or "__erubi.h" with a hoisted "__erubi = ::Erubi;" when
// Escape is set). Empty means use that default.
EscapeFunc string
// BufVar names the buffer variable. Empty defaults to OutVar, then "_buf".
BufVar string
// OutVar is the lower-priority alias for BufVar (erubi's :outvar).
OutVar string
// BufVal is the buffer's initial value expression. Empty defaults to
// "::String.new".
BufVal string
// Filename is the template filename, reported by Engine.Filename.
Filename string
// Preamble overrides the emitted preamble. Empty defaults to
// "<BufVar> = <BufVal>;".
Preamble string
// Postamble overrides the emitted postamble. Empty defaults to
// "<BufVar>.to_s\n".
Postamble string
// LiteralPrefix is emitted for an escaped opening delimiter (the <%% tag).
// Empty defaults to "<%".
LiteralPrefix string
// LiteralPostfix is emitted for an escaped closing delimiter. Empty defaults
// to "%>".
LiteralPostfix string
// Src is the initial source string the compiled output is appended to.
Src string
// Regexp overrides the scanning regexp. nil uses DefaultRegexp (or
// CaptureEndRegexp for NewCaptureEndEngine).
Regexp *regexp.Regexp
// EscapeCapture (CaptureEndEngine only) makes <%|= %> escape by default and
// <%|== %> not. nil defaults to the resolved Escape value.
EscapeCapture *bool
// YieldReturnsBuffer (CaptureEndEngine only) makes <%| %> insert the buffer
// as an expression so the block yields the buffer.
YieldReturnsBuffer bool
}
Options configures NewEngine and NewCaptureEndEngine, mirroring the keyword arguments accepted by Erubi::Engine.new and Erubi::CaptureEndEngine.new.
The tri-state boolean options are pointers so that "unset" is distinguishable from an explicit false, exactly as a missing Ruby hash key differs from a present false value. Use Bool to set them, e.g. Options{Trim: erubi.Bool(false)}. String options treat the zero value "" as "unset" (falling back to the erubi default), matching erubi's `properties[:x] || default` idiom.
