Skip to content

Commit

Permalink
fix templates
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Oct 9, 2024
1 parent 31c442f commit fede02d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 36 deletions.
5 changes: 0 additions & 5 deletions internal/cast/chapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import (
"strings"
)

type Chapter struct {
Segments []*ChapterSegment
Body string
}

type ChapterSegment struct {
Title string `json:"title"`
Start uint64 `json:"start"`
Expand Down
42 changes: 19 additions & 23 deletions internal/cast/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ type Episode struct {
Slug string
RawBody, Body string
URL *url.URL
Chapter *Chapter
ChaptersBody string

rootDir string
audioBaseURL *url.URL
Expand Down Expand Up @@ -401,14 +401,11 @@ func (ep *Episode) init(loc *time.Location) error {
}

if len(ep.audio.Chapters) > 0 {
chapter := &Chapter{
Segments: ep.audio.Chapters,
}
if err := chapter.init(); err != nil {
ep.ChaptersBody, err = buildChaptersBody(ep.audio.Chapters)
if err != nil {
return err
}
ep.Chapter = chapter
ep.EpisodeFrontMatter.Chapters = chapter.Segments
ep.EpisodeFrontMatter.Chapters = ep.audio.Chapters
}
md := NewMarkdown()
var buf bytes.Buffer
Expand All @@ -419,16 +416,21 @@ func (ep *Episode) init(loc *time.Location) error {
return nil
}

func (chap *Chapter) init() error {
tmpl, err := template.New("chapters").Parse(chaperTmpl)
if err != nil {
return err
}
const chaperTmplStr = `<ul class="chapters">
{{- range . -}}
<li><time>{{ .Start }}</time> {{ .Title }}</li>
{{- end -}}</ul>
`

var chaptertmpl = template.Must(template.New("chapters").Parse(chaperTmplStr))

func buildChaptersBody(chapters []*ChapterSegment) (string, error) {
data := []struct {
Title string
Start string
}{}
for _, ch := range chap.Segments {

for _, ch := range chapters {
data = append(data, struct {
Title string
Start string
Expand All @@ -437,20 +439,14 @@ func (chap *Chapter) init() error {
Start: convertStartToString(ch.Start),
})
}

var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return err
if err := chaptertmpl.Execute(&buf, chapters); err != nil {
return "", err
}
chap.Body = buf.String()
return nil
return buf.String(), nil
}

const chaperTmpl = `<ul class="chapters">
{{- range . -}}
<li><time>{{ .Start }}</time> {{ .Title }}</li>
{{- end -}}</ul>
`

func (epm *EpisodeFrontMatter) PubDate() time.Time {
return epm.pubDate
}
Expand Down
5 changes: 1 addition & 4 deletions internal/cast/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@ func (f *Feed) AddEpisode(ep *Episode, audioBaseURL *url.URL) (int, error) {
if err != nil {
return 0, err
}
chapterBody := ""
if ep.Chapter != nil {
chapterBody = ep.Chapter.Body
}
chapterBody := ep.ChaptersBody
description := chapterBody + ep.Body
if description == "" {
description = ep.Subtitle
Expand Down
4 changes: 2 additions & 2 deletions internal/cast/testdata/init/template/episode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<audio controls src="{{.Episode.AudioURL}}"></audio>
</figure>

{{if .Episode.Chapter -}}
{{if ne .Episode.ChaptersBody "" -}}
<aside class="chapters">
<h2>Chapters</h2>
{{.Episode.Chapter.Body | html}}
{{.Episode.ChaptersBody | html}}
</aside>
{{end}}

Expand Down
4 changes: 2 additions & 2 deletions testdata/dev/template/episode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
<audio controls src="{{.Episode.AudioURL}}"></audio>
</figure>

{{if .Episode.Chapter -}}
{{if ne .Episode.ChaptersBody "" -}}
<aside class="chapters">
<h2>Chapters</h2>
{{.Episode.Chapter.Body | html}}
{{.Episode.ChaptersBody | html}}
</aside>
{{end}}

Expand Down

0 comments on commit fede02d

Please sign in to comment.