render

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2020 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const MaxRenderAttempts = 15
View Source
const SHELL_DISABLED_PLACEHOLDER = "replace-me"

Variables

View Source
var CAMEL_CASE_REGEX = regexp.MustCompile(
	"(^([[:lower:]]|[[:digit:]])+)|" +

		"([[:upper:]]*([[:lower:]]|[[:digit:]]|$)*)") // Handle normal camel case

This regex can be used to split CamelCase strings into "words". That is, given a string like FooBarBaz, you can use this regex to split it into an array ["Foo", "Bar", "Baz"]. It also handles lower camel case, which is the same as camel case, except it starts with a lower case word, such as fooBarBaz.

To capture lowercase camel case, we just look for words that consist of lower case letters and digits at the start of the string. To capture all other camel case, we look for "words" that start with one or more consecutive upper case letters followed by one or more lower case letters or digits.

View Source
var ENV_VAR_REGEX = regexp.MustCompile("^ENV:(.+?)=(.*)$")
View Source
var NoArgsPassedToShellHelper = fmt.Errorf("The shell helper requires at least one argument")
View Source
var PUNCTUATION_OR_WHITESPACE_REGEX = regexp.MustCompile("([[:space:]]|[[:punct:]])+")
View Source
var SNIPPET_MARKER_REGEX = regexp.MustCompile("boilerplate-snippet:\\s*(.+?)(?:\\s|$)")
View Source
var WHITESPACE_REGEX = regexp.MustCompile("[[:space:]]+")

Functions

func CreateTemplateHelpers

func CreateTemplateHelpers(templatePath string, opts *options.BoilerplateOptions, tmpl *template.Template) template.FuncMap

Create a map of custom template helpers exposed by boilerplate

func PathRelativeToTemplate

func PathRelativeToTemplate(templatePath string, filePath string) string

Returns the given filePath relative to the given templatePath. If filePath is already an absolute path, returns it unchanged.

Example:

pathRelativeToTemplate("/foo/bar/template-file.txt, "../src/code.java")

Returns: "/foo/src/code.java"

func RenderTemplateFromString added in v0.3.1

func RenderTemplateFromString(templatePath string, templateContents string, variables map[string]interface{}, opts *options.BoilerplateOptions) (string, error)

Render the template at templatePath, with contents templateContents, using the Go template engine, passing in the given variables as data.

func RenderTemplateRecursively

func RenderTemplateRecursively(templatePath string, templateContents string, variables map[string]interface{}, opts *options.BoilerplateOptions) (string, error)

Render the template at templatePath, with contents templateContents, using the Go template engine, passing in the given variables as data. If the rendered result contains more Go templating syntax, render it again, and repeat this process recursively until there is no more rendering to be done.

The main use case for this is to allow boilerplate variables to reference other boilerplate variables. This can obviously lead to an infinite loop. The proper way to prevent that would be to parse Go template syntax and build a dependency graph, but that is way too complicated. Therefore, we use hacky solution: render the template multiple times. If it is the same as the last time you rendered it, that means no new interpolations were processed, so we're done. If it changes, that means more interpolations are being processed, so keep going, up to a maximum number of render attempts.

func RenderTemplateWithPartials added in v0.3.1

func RenderTemplateWithPartials(templatePath string, partials []string, variables map[string]interface{}, opts *options.BoilerplateOptions) (string, error)

RenderTemplateWithPartials renders the template at templatePath with the contents of the root template (the template named by the user on the command line) as well as all of the partials matched by the provided globs using the Go template engine, passing in the given variables as data.

func RenderVariable

func RenderVariable(variable interface{}, variables map[string]interface{}, opts *options.BoilerplateOptions) (interface{}, error)

Variable values are allowed to use Go templating syntax (e.g. to reference other variables), so here, we render those templates and return a new map of variables that are fully resolved.

func RenderVariables

func RenderVariables(variables map[string]interface{}, opts *options.BoilerplateOptions) (map[string]interface{}, error)

Variable values are allowed to use Go templating syntax (e.g. to reference other variables), so this function loops over each variable value, renders each one, and returns a new map of rendered variables.

Types

type InvalidSnippetArguments

type InvalidSnippetArguments []string

func (InvalidSnippetArguments) Error

func (args InvalidSnippetArguments) Error() string

type InvalidTypeForMethodArgument

type InvalidTypeForMethodArgument struct {
	MethodName   string
	ExpectedType string
	ActualType   string
}

func (InvalidTypeForMethodArgument) Error

type SnippetNotFound

type SnippetNotFound string

func (SnippetNotFound) Error

func (snippetName SnippetNotFound) Error() string

type SnippetNotTerminated

type SnippetNotTerminated string

func (SnippetNotTerminated) Error

func (snippetName SnippetNotTerminated) Error() string

type TemplateContainsInfiniteLoop

type TemplateContainsInfiniteLoop struct {
	TemplatePath     string
	TemplateContents string
	RenderAttempts   int
}

func (TemplateContainsInfiniteLoop) Error

type TemplateHelper

type TemplateHelper func(templatePath string, opts *options.BoilerplateOptions, args ...string) (string, error)

All boilerplate template helpers implement this signature. They get the path of the template they are rendering as the first arg, the Boilerplate Options as the second arg, and then any arguments the user passed when calling the helper.

Jump to

Keyboard shortcuts

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