Skip to content

Commit

Permalink
introduce inline templates (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
Skeeve authored Jun 7, 2022
1 parent 4c81274 commit 9ec7728
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ for example:
Ticket: ${0} -->
```

Macros can also use inline templates.
Inline templates are templates where the template content
is described in the `<yaml-data>`.
The `Template` value starts with a `#`, followed by the key
used in the `<yaml-data>`.
The key's value must be a string which defines the template's content.

```markdown
<!-- Macro: <tblbox\s+(.*?)\s*>
Template: #inline
title: ${1}
inline: |
<table>
<thead><tr><th>{{ .title }}</th></tr></thead>
<tbody><tr><td>
-->
<!-- Macro: </tblbox>
Template: #also_inline
also_inline: |
</td></tr></tbody></table>
-->
<tblbox with a title>
and some
content
</tblbox>
```


### Code Blocks

If you have long code blocks, you can make them collapsible with the [Code Block Macro]:
Expand Down
31 changes: 27 additions & 4 deletions pkg/mark/macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,36 @@ func ExtractMacros(
macro Macro
)

macro.Template, err = includes.LoadTemplate(base, template, templates)
if err != nil {
err = karma.Format(err, "unable to load template")
if strings.HasPrefix(template, "#") {
cfg := map[string]interface{}{}

return nil
err = yaml.Unmarshal([]byte(config), &cfg)
if err != nil {
err = karma.Format(
err,
"unable to unmarshal macros config template",
)
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 9ec7728

Please sign in to comment.