fix 142 redone for current master (#199)

This commit is contained in:
Stephan Hradek 2022-06-07 10:16:24 +02:00 committed by GitHub
parent ac2132b9c6
commit 39dfdec099
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 8 deletions

View File

@ -68,8 +68,28 @@ to the template relative to current working dir, e.g.:
<!-- Include: <path> -->
```
Optionally the delimiters can be defined:
```markdown
<!-- Include: <path>
Delims: "<<", ">>"
-->
```
Or they can be switched off to disable processing:
```markdown
<!-- Include: <path>
Delims: none
-->
```
**Note:** Switching delimiters off really simply changes
them to ASCII characters "\x00" and "\x01" which, usually
should not occure in a template.
Templates can accept configuration data in YAML format which immediately
follows the `Include` tag:
follows the `Include` and `Delims` tag, if present:
```markdown
<!-- Include: <path>

View File

@ -16,13 +16,20 @@ import (
)
// <!-- Include: <template path>
// (Delims: (none | "<left>","<right>"))?
// <optional yaml data> -->
var reIncludeDirective = regexp.MustCompile(
`(?s)<!--\s*Include:\s*(?P<template>.+?)\s*(\n(?P<config>.*?))?-->`)
`(?s)` +
`<!--\s*Include:\s*(?P<template>.+?)\s*` +
`(?:\n\s*Delims:\s*(?:(none|"(?P<left>.*?)"\s*,\s*"(?P<right>.*?)")))?\s*` +
`(?:\n(?P<config>.*?))?-->`,
)
func LoadTemplate(
base string,
path string,
left string,
right string,
templates *template.Template,
) (*template.Template, error) {
var (
@ -52,7 +59,7 @@ func LoadTemplate(
[]byte("\n"),
)
templates, err = templates.New(name).Parse(string(body))
templates, err = templates.New(name).Delims(left, right).Parse(string(body))
if err != nil {
err = facts.Format(
err,
@ -104,12 +111,15 @@ func ProcessIncludes(
groups := reIncludeDirective.FindSubmatch(spec)
var (
path, config = string(groups[1]), groups[2]
path, none, left, right, config = string(groups[1]), string(groups[2]), string(groups[3]), string(groups[4]), groups[5]
data = map[string]interface{}{}
facts = karma.Describe("path", path)
)
if none == "none" {
left = "\x00"
right = "\x01"
}
err = yaml.Unmarshal(config, &data)
if err != nil {
err = facts.
@ -124,7 +134,7 @@ func ProcessIncludes(
log.Tracef(vardump(facts, data), "including template %q", path)
templates, err = LoadTemplate(base, path, templates)
templates, err = LoadTemplate(base, path, left, right, templates)
if err != nil {
err = facts.Format(err, "unable to load template")

View File

@ -155,7 +155,7 @@ func ExtractMacros(
return nil
}
} else {
macro.Template, err = includes.LoadTemplate(base, template, templates)
macro.Template, err = includes.LoadTemplate(base, template, "{{", "}}", templates)
if err != nil {
err = karma.Format(err, "unable to load template")