small style fixes related to templates

This commit is contained in:
Egor Kovetskiy 2022-06-07 14:21:11 +06:00
parent 78345736d5
commit 1ebb29eba0
2 changed files with 28 additions and 11 deletions

View File

@ -20,9 +20,9 @@ import (
// <optional yaml data> -->
var reIncludeDirective = regexp.MustCompile(
`(?s)` +
`<!--\s*Include:\s*(?P<template>.+?)\s*` +
`(?:\n\s*Delims:\s*(?:(none|"(?P<left>.*?)"\s*,\s*"(?P<right>.*?)")))?\s*` +
`(?:\n(?P<config>.*?))?-->`,
`<!--\s*Include:\s*(?P<template>.+?)\s*` +
`(?:\n\s*Delims:\s*(?:(none|"(?P<left>.*?)"\s*,\s*"(?P<right>.*?)")))?\s*` +
`(?:\n(?P<config>.*?))?-->`,
)
func LoadTemplate(
@ -111,15 +111,21 @@ func ProcessIncludes(
groups := reIncludeDirective.FindSubmatch(spec)
var (
path, none, left, right, config = string(groups[1]), string(groups[2]), string(groups[3]), string(groups[4]), groups[5]
data = map[string]interface{}{}
path = string(groups[1])
delimsNone = string(groups[2])
left = string(groups[3])
right = string(groups[4])
config = groups[5]
data = map[string]interface{}{}
facts = karma.Describe("path", path)
)
if none == "none" {
if delimsNone == "none" {
left = "\x00"
right = "\x01"
}
err = yaml.Unmarshal(config, &data)
if err != nil {
err = facts.

View File

@ -130,10 +130,10 @@ func ExtractMacros(
"template",
)
config = regexputil.Subexp(reMacroDirective, groups, "config")
macro Macro
)
var macro Macro
if strings.HasPrefix(template, "#") {
cfg := map[string]interface{}{}
@ -143,27 +143,38 @@ func ExtractMacros(
err,
"unable to unmarshal macros config template",
)
return nil
}
body, _ := cfg[template[1:]].(string)
body, ok := cfg[template[1:]].(string)
if !ok {
err = fmt.Errorf(
"the template config doesn't have '%s' field",
template[1:],
)
return nil
}
macro.Template, err = templates.New(template).Parse(body)
if err != nil {
err = karma.Format(
err,
"unable to parse template",
)
return nil
}
} else {
macro.Template, err = includes.LoadTemplate(base, template, "{{", "}}", templates)
if err != nil {
err = karma.Format(err, "unable to load template")
return nil
}
}
facts := karma.
Describe("template", template).
Describe("expr", expr)