Browse Source

Use Go's embed pkg instead of inline tool

This moves the minimum requirements to Go 1.16 to build this project.
main
Matt Baer 2 years ago
parent
commit
16e4510d9d
4 changed files with 14 additions and 156 deletions
  1. +5
    -1
      README.md
  2. +1
    -1
      go.mod
  3. +3
    -11
      render.go
  4. +5
    -143
      templates.go

+ 5
- 1
README.md View File

@@ -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)
```
```

+ 1
- 1
go.mod View File

@@ -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


+ 3
- 11
render.go View File

@@ -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")
}


+ 5
- 143
templates.go View File

@@ -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"}}
<html>
<head>
<title>Mixtape</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style type="text/css">
body {
font-size: 1.2em;
margin: 1em;
}
#playlist {
list-style: decimal-leading-zero;
margin: 1em 0;
}
#playlist li {
margin: 0.5em 0;
}
li p {
display: none;
font-style: italic;
}
li.active a {
font-weight: bold;
}
li.active p {
display: block;
}
</style>
</head>
<body>
{{template "full-player" .Tracks}}
</body>
</html>
{{end}}

{{define "track-info"}}
{{if eq .Num 1}}
<p>[Here I might introduce this mix.]</p>
<p>[Some notes about track 1.]</p>
{{else if eq .Num 2}}
<p>[Some notes about track 2.]</p>
{{else if eq .Num 5}}
<p>[Some notes about track 5.]</p>
{{end}}
{{end}}`,
"templates/parts.tmpl": `{{define "player"}}
{{with $x := index . 0}}
<audio id="player" preload="auto" tabindex="0" controls>
<source src="{{$x.Filename}}">
</audio>
{{end}}
{{end}}
{{define "playlist"}}
<ol id="playlist">
{{range $i, $el := .}}
<li{{if eq $i 0}} class="active"{{end}}>
<a class="track" href="{{$el.Filename}}">{{$el.Artist}} - {{$el.Title}}</a>
{{template "track-info" $el}}
</li>
{{end}}
</ol>
{{end}}

{{define "track-info"}}{{end}}

{{define "full-player"}}
{{template "player" .}}
{{template "playlist" .}}
{{template "playlist-js"}}
{{end}}

{{define "playlist-js"}}
<script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script>
<script type="text/javascript">
$(document).ready(function() {
var current = 0;
var $audio = $('#player');
var $playlist = $('#playlist');
var $tracks = $playlist.find('li a.track');
var len = $tracks.length;

$playlist.on('click', 'a.track', function (e) {
e.preventDefault();
link = $(this);
current = link.parent().index();
play(link, $audio[0]);
});
$audio[0].addEventListener('ended', function (e) {
playNext();
});
$('a#next').on('click', function (e) {
e.preventDefault();
playNext();
});
$('a#prev').on('click', function (e) {
e.preventDefault();
playPrev();
});

function playPrev() {
current--;
if (current <= 0) {
current = len - 1;
}
link = $playlist.find('a.track')[current];
play($(link), $audio[0]);
}

function playNext() {
current++;
if (current == len) {
current = 0;
}
link = $playlist.find('a.track')[current];
play($(link), $audio[0]);
}
//go:embed mixtape.tmpl
var defaultMixtapeTmpl []byte

function play($link, $player) {
$player.src = $link.attr('href');
par = $link.parent();
par.addClass('active').siblings().removeClass('active');
$player.load();
$player.play();
}
});
</script>
{{end}}`,
}
//go:embed templates/parts.tmpl
var partsRawTmpl []byte

Loading…
Cancel
Save