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> --> <!-- 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 Templates can accept configuration data in YAML format which immediately
follows the `Include` tag: follows the `Include` and `Delims` tag, if present:
```markdown ```markdown
<!-- Include: <path> <!-- Include: <path>

View File

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

View File

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