Skip to content

Commit

Permalink
Add svg template function.
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmysawczuk committed Feb 4, 2021
1 parent 78f4561 commit 3d1ebe3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,19 @@ func (p *pipeline) run() error {
}
defer out.Close()

t := tmpl.New()

switch p.format {
case "html":
if err := tmpl.New().WriteHTML(out, in, p.minify); err != nil {
if err := t.WriteHTML(out, in, p.minify); err != nil {
return errors.Wrapf(err, "write html (in: %s)", p.inpath)
}
case "json":
if err := tmpl.New().WriteJSON(out, in, p.minify); err != nil {
if err := t.WriteJSON(out, in, p.minify); err != nil {
return errors.Wrapf(err, "write json (in: %s)", p.inpath)
}
default:
if err := tmpl.New().WriteText(out, in); err != nil {
if err := t.WriteText(out, in); err != nil {
return errors.Wrapf(err, "write text (in: %s)", p.inpath)
}
}
Expand Down
1 change: 1 addition & 0 deletions tmpl/tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (t *Tmpl) funcs() map[string]interface{} {
"safeJS": tmplfunc.SafeJS,
"seq": tmplfunc.Seq,
"sub": tmplfunc.Sub,
"svg": tmplfunc.SVG,
"timeIn": tmplfunc.TimeIn,
}
}
Expand Down
11 changes: 11 additions & 0 deletions tmpl/tmplfunc/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tmplfunc

import (
"bytes"
"html/template"
"io"
"os"

Expand All @@ -22,3 +23,13 @@ func File(path string) (string, error) {

return buf.String(), nil
}

// SVG reads the file at the provided path and returns its contents under the assumption that it's an HTML-safe string.
func SVG(path string) (template.HTML, error) {
contents, err := File(path)
if err != nil {
return "", err
}

return template.HTML(contents), nil
}

0 comments on commit 3d1ebe3

Please sign in to comment.