Skip to content

Commit

Permalink
#20: fix duplicated macro expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
seletskiy committed Apr 13, 2020
1 parent f437e47 commit b7709aa
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions pkg/mark/macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ var reMacroDirective = regexp.MustCompile(
`(?s)` + // dot capture newlines
/**/ `<!--\s*Macro:\s*(?P<expr>[^\n]+)\n` +
/* */ `\s*Template:\s*(?P<template>\S+)\s*` +
/* */ `(\n(?P<config>.*?))?-->`,
/* */ `(?P<config>\n.*?)?-->`,
)

type Macro struct {
Regexp *regexp.Regexp
Template *template.Template
Config map[string]interface{}
Config string
}

func (macro *Macro) Apply(
Expand All @@ -39,14 +39,22 @@ func (macro *Macro) Apply(
content = macro.Regexp.ReplaceAllFunc(
content,
func(match []byte) []byte {
config := macro.configure(
macro.Config,
macro.Regexp.FindSubmatch(match),
)
config := map[string]interface{}{}

err = yaml.Unmarshal([]byte(macro.Config), &config)
if err != nil {
err = karma.Format(
err,
"unable to unmarshal macros config template",
)
}

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 {
err = karma.Format(
err,
Expand Down Expand Up @@ -144,17 +152,7 @@ func ExtractMacros(
return nil
}

err = yaml.Unmarshal([]byte(config), &macro.Config)
if err != nil {
err = facts.
Describe("config", string(config)).
Format(
err,
"unable to unmarshal template data config",
)

return nil
}
macro.Config = config

log.Tracef(
facts.Describe("config", macro.Config),
Expand Down

0 comments on commit b7709aa

Please sign in to comment.