#20: fix duplicated macro expansion

This commit is contained in:
Stanislav Seletskiy 2020-04-13 16:56:06 +03:00
parent f437e47039
commit b7709aa344

View File

@ -22,13 +22,13 @@ var reMacroDirective = regexp.MustCompile(
`(?s)` + // dot capture newlines `(?s)` + // dot capture newlines
/**/ `<!--\s*Macro:\s*(?P<expr>[^\n]+)\n` + /**/ `<!--\s*Macro:\s*(?P<expr>[^\n]+)\n` +
/* */ `\s*Template:\s*(?P<template>\S+)\s*` + /* */ `\s*Template:\s*(?P<template>\S+)\s*` +
/* */ `(\n(?P<config>.*?))?-->`, /* */ `(?P<config>\n.*?)?-->`,
) )
type Macro struct { type Macro struct {
Regexp *regexp.Regexp Regexp *regexp.Regexp
Template *template.Template Template *template.Template
Config map[string]interface{} Config string
} }
func (macro *Macro) Apply( func (macro *Macro) Apply(
@ -39,14 +39,22 @@ func (macro *Macro) Apply(
content = macro.Regexp.ReplaceAllFunc( content = macro.Regexp.ReplaceAllFunc(
content, content,
func(match []byte) []byte { func(match []byte) []byte {
config := macro.configure( config := map[string]interface{}{}
macro.Config,
macro.Regexp.FindSubmatch(match), err = yaml.Unmarshal([]byte(macro.Config), &config)
) if err != nil {
err = karma.Format(
err,
"unable to unmarshal macros config template",
)
}
var buffer bytes.Buffer var buffer bytes.Buffer
err = macro.Template.Execute(&buffer, config) err = macro.Template.Execute(&buffer, macro.configure(
config,
macro.Regexp.FindSubmatch(match),
))
if err != nil { if err != nil {
err = karma.Format( err = karma.Format(
err, err,
@ -144,17 +152,7 @@ func ExtractMacros(
return nil return nil
} }
err = yaml.Unmarshal([]byte(config), &macro.Config) macro.Config = config
if err != nil {
err = facts.
Describe("config", string(config)).
Format(
err,
"unable to unmarshal template data config",
)
return nil
}
log.Tracef( log.Tracef(
facts.Describe("config", macro.Config), facts.Describe("config", macro.Config),