Skip to content

Commit

Permalink
small style fixes related to templates
Browse files Browse the repository at this point in the history
  • Loading branch information
kovetskiy committed Oct 19, 2022
1 parent 7834573 commit 1ebb29e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
18 changes: 12 additions & 6 deletions pkg/mark/includes/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 16 additions & 5 deletions pkg/mark/macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}

Expand All @@ -143,27 +143,38 @@ func ExtractMacros(
err,
"unable to unmarshal macros config template",
)

return nil
}

body, ok := cfg[template[1:]].(string)
if !ok {
err = fmt.Errorf(
"the template config doesn't have '%s' field",
template[1:],
)

return nil
}
body, _ := cfg[template[1:]].(string)

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)
Expand Down

0 comments on commit 1ebb29e

Please sign in to comment.