HUGE refactor. working towards web UI
This commit is contained in:
parent
dd54e8cc49
commit
1f94eecca9
29 changed files with 1669 additions and 162 deletions
108
templates/text.go
Normal file
108
templates/text.go
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
package templates
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"arimelody.space/melody-vod-manager/scanner"
|
||||
)
|
||||
|
||||
|
||||
|
||||
type (
|
||||
TextTemplates struct {
|
||||
Title *template.Template
|
||||
Description *template.Template
|
||||
DefaultTags []string
|
||||
}
|
||||
|
||||
TextTemplateData struct {
|
||||
Title string
|
||||
Part uint
|
||||
Date time.Time
|
||||
Category *scanner.Category
|
||||
}
|
||||
)
|
||||
|
||||
const DefaultTitleTemplate =
|
||||
"{{.Title}} - {{FormatTime .Date \"02 Jan 2006\"}}"
|
||||
const DefaultDescriptionTemplate =
|
||||
"Streamed on {{FormatTime .Date \"02 January 2006\"}}"
|
||||
|
||||
func FetchTemplates(ctx context.Context, templateDir string) (*TextTemplates, error) {
|
||||
tmpl := TextTemplates{}
|
||||
logger := slog.Default().WithGroup("TEMPLATE")
|
||||
|
||||
var tagsPath = path.Join(templateDir, "tags.txt")
|
||||
var titlePath = path.Join(templateDir, "title.txt")
|
||||
var descriptionPath = path.Join(templateDir, "description.txt")
|
||||
|
||||
// tags
|
||||
if tagsFile, err := os.ReadFile(tagsPath); err == nil {
|
||||
tmpl.DefaultTags = strings.Split(string(tagsFile), "\n")
|
||||
} else {
|
||||
if !os.IsNotExist(err) { return nil, err }
|
||||
|
||||
logger.Info(fmt.Sprintf(
|
||||
"%s not found. No default tags will be used.",
|
||||
tagsPath,
|
||||
))
|
||||
tmpl.DefaultTags = []string{}
|
||||
}
|
||||
|
||||
// title
|
||||
titleTemplate := template.New("title").Funcs(templateFuncs)
|
||||
if titleFile, err := os.ReadFile(titlePath); err == nil {
|
||||
tmpl.Title, err = titleTemplate.Parse(string(titleFile))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse title template: %v", err)
|
||||
}
|
||||
} else {
|
||||
if !os.IsNotExist(err) { return nil, err }
|
||||
|
||||
logger.Info(
|
||||
fmt.Sprintf(
|
||||
"%s not found. Falling back to default template:",
|
||||
titlePath,
|
||||
),
|
||||
"template",
|
||||
DefaultTitleTemplate,
|
||||
)
|
||||
tmpl.Title, err = titleTemplate.Parse(DefaultTitleTemplate)
|
||||
if err != nil { panic(err) }
|
||||
|
||||
os.WriteFile(titlePath, []byte(DefaultTitleTemplate), 0644)
|
||||
}
|
||||
|
||||
// description
|
||||
descriptionTemplate := template.New("description").Funcs(templateFuncs)
|
||||
if descriptionFile, err := os.ReadFile(descriptionPath); err == nil {
|
||||
tmpl.Description, err = descriptionTemplate.Parse(string(descriptionFile))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse description template: %v", err)
|
||||
}
|
||||
} else {
|
||||
if !os.IsNotExist(err) { return nil, err }
|
||||
|
||||
logger.Info(
|
||||
fmt.Sprintf(
|
||||
"%s not found. Falling back to default template:",
|
||||
descriptionPath,
|
||||
),
|
||||
"template",
|
||||
DefaultDescriptionTemplate,
|
||||
)
|
||||
tmpl.Description, err = descriptionTemplate.Parse(DefaultDescriptionTemplate)
|
||||
if err != nil { panic(err) }
|
||||
|
||||
os.WriteFile(descriptionPath, []byte(DefaultDescriptionTemplate), 0644)
|
||||
}
|
||||
|
||||
return &tmpl, nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue