Example #1
0
func compileTemplate(data []byte, name string) *structure.Helper {
	baseHelper := structure.Helper{Name: name, Arguments: nil, Unescaped: false, Position: 0, Block: []byte{}, Children: nil, Function: getFunction(name)}
	allHelpers := make([]structure.Helper, 0)
	data, allHelpers = findHelper(data, allHelpers)
	baseHelper.Block = data
	baseHelper.Children = allHelpers
	// Handle extend helpers
	for index, child := range baseHelper.Children {
		if child.Name == "body" {
			baseHelper.BodyHelper = &baseHelper.Children[index] //TODO: This handles only one body helper per hbs file. That is a potential bug source, but no theme should be using more than one per file anyway.
		}
	}
	return &baseHelper
}
Example #2
0
func createHelper(helperName []byte, unescaped bool, startPos int, block []byte, children []structure.Helper, elseHelper *structure.Helper) *structure.Helper {
	var helper *structure.Helper
	// Check for =arguments
	twoPartArgumentResult := twoPartArgumentChecker.FindAllSubmatch(helperName, -1)
	twoPartArguments := make([][]byte, 0)
	for _, arg := range twoPartArgumentResult {
		if len(arg) == 3 {
			twoPartArguments = append(twoPartArguments, bytes.Join(arg[1:], []byte("=")))
			//remove =argument from helper name
			helperName = bytes.Replace(helperName, arg[0], []byte(""), 1)
		}
	}
	// Separate arguments (e.g. 'if @blog.title')
	tags := bytes.Fields(helperName)
	for index, tag := range tags {
		//remove "" around tag if present
		quoteTagResult := quoteTagChecker.FindSubmatch(tag)
		if len(quoteTagResult) != 0 {
			// Get the string inside the quotes (3rd element in array)
			tag = quoteTagResult[2]
		}
		//TODO: This may have to change if the first argument is surrounded by quotes
		if index == 0 {
			helper = makeHelper(string(tag), unescaped, startPos, block, children)
		} else {
			// Handle whitespaces in arguments
			helper.Arguments = append(helper.Arguments, *makeHelper(string(tag), unescaped, 0, []byte{}, nil))
		}
	}
	if len(twoPartArguments) != 0 {
		for _, arg := range twoPartArguments {
			// Check for quotes in the =argument (has beem omitted from the check above)
			quoteTagResult := quoteTagChecker.FindSubmatch(arg)
			if len(quoteTagResult) != 0 {
				// Join poth parts, this time without the quotes
				arg = bytes.Join([][]byte{quoteTagResult[1], quoteTagResult[2]}, []byte(""))
			}
			helper.Arguments = append(helper.Arguments, *makeHelper(string(arg), unescaped, 0, []byte{}, nil))
		}
	}
	if elseHelper != nil {
		helper.Arguments = append(helper.Arguments, *elseHelper)
	}
	return helper
}