From 16e4510d9d6ab10c15418bd4b79afa45cec0b5be Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Wed, 30 Mar 2022 23:14:32 -0400 Subject: [PATCH] Use Go's embed pkg instead of inline tool This moves the minimum requirements to Go 1.16 to build this project. --- README.md | 6 ++- go.mod | 2 +- render.go | 14 ++---- templates.go | 148 ++--------------------------------------------------------- 4 files changed, 14 insertions(+), 156 deletions(-) diff --git a/README.md b/README.md index 33baad5..d11a289 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ You can also tweak the original template _before_ it generates the final page. 1. Edit this file to your liking, being sure to retain the `{{template ...}}` lines in the file 1. Run `cdr burn` -- it'll generate your page from this template instead of the default! +### Developers + +Requires Go 1.16 and above. + ## Commands ``` @@ -59,4 +63,4 @@ COMMANDS: GLOBAL OPTIONS: --help, -h show help (default: false) --version, -v print the version (default: false) -``` \ No newline at end of file +``` diff --git a/go.mod b/go.mod index 4349668..973d4fa 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/thebaer/cdr -go 1.13 +go 1.16 require ( github.com/dhowden/tag v0.0.0-20201120070457-d52dcb253c63 diff --git a/render.go b/render.go index e8cae2d..80549fc 100644 --- a/render.go +++ b/render.go @@ -1,8 +1,7 @@ -//go:generate inline -o templates.go -p cdr mixtape.tmpl templates/parts.tmpl - package cdr import ( + _ "embed" "html/template" "io" "io/ioutil" @@ -10,17 +9,10 @@ import ( ) func Render(m *Mixtape, w io.Writer) error { - partsRawTmpl, err := ReadAsset("templates/parts.tmpl", false) - if err != nil { - return err - } mixtapeRawTmpl, err := ioutil.ReadFile("mixtape.tmpl") if err != nil { - log.Print("Unable to load local mixtape.tmpl; falling back to default") - mixtapeRawTmpl, err = ReadAsset("mixtape.tmpl", false) - if err != nil { - return err - } + log.Print("Unable to load custom mixtape.tmpl; falling back to default") + mixtapeRawTmpl = defaultMixtapeTmpl } else { log.Print("Generating from local mixtape.tmpl") } diff --git a/templates.go b/templates.go index 08025c1..243dba2 100644 --- a/templates.go +++ b/templates.go @@ -1,149 +1,11 @@ -// Code generated by "inline -o templates.go -p cdr mixtape.tmpl templates/parts.tmpl" -- DO NOT EDIT -- - package cdr import ( - "fmt" - "io/ioutil" + _ "embed" ) -func ReadAsset(file string, useLocal bool) ([]byte, error) { - if useLocal { - return ioutil.ReadFile(file) - } - if f, ok := files[file]; ok { - return []byte(f), nil - } - return nil, fmt.Errorf("file doesn't exist.") -} - -var files = map[string]string{ - "mixtape.tmpl": `{{define "mixtape"}} - - - Mixtape - - - - - - {{template "full-player" .Tracks}} - - -{{end}} - -{{define "track-info"}} - {{if eq .Num 1}} -

[Here I might introduce this mix.]

-

[Some notes about track 1.]

- {{else if eq .Num 2}} -

[Some notes about track 2.]

- {{else if eq .Num 5}} -

[Some notes about track 5.]

- {{end}} -{{end}}`, - "templates/parts.tmpl": `{{define "player"}} - {{with $x := index . 0}} - - {{end}} -{{end}} -{{define "playlist"}} -
    - {{range $i, $el := .}} - - {{$el.Artist}} - {{$el.Title}} - {{template "track-info" $el}} - - {{end}} -
-{{end}} - -{{define "track-info"}}{{end}} - -{{define "full-player"}} - {{template "player" .}} - {{template "playlist" .}} - {{template "playlist-js"}} -{{end}} - -{{define "playlist-js"}} - - -{{end}}`, -} \ No newline at end of file +//go:embed templates/parts.tmpl +var partsRawTmpl []byte