Files
psalms/internal/renderer/fetchingyaml.go
github_username_here 7a7129ec29 correction bug empty html
2026-03-30 16:01:48 +02:00

83 lines
2.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package renderer
import (
"log"
"project.hechon.fr/internal/config"
"project.hechon.fr/internal/models"
"html/template"
"gopkg.in/yaml.v3"
"os"
"path"
"strconv"
"fmt"
)
func ExtractChapter(cfg *config.Cfg, chapterNum int) (error){
fileName := path.Join(cfg.YamlPath, cfg.Book.Name + strconv.Itoa(chapterNum) + ".yaml")
file, err := os.Open(fileName)
if err != nil {
log.Printf("cant open %v: %v", fileName, err)
return err
}
var yamlData models.Yaml
if err = yaml.NewDecoder(file).Decode(&yamlData) ; err != nil{
return err
}
log.Printf("%v", yamlData)
if len(yamlData.PoeticLines) == 0 || len(yamlData.PoeticLines[0].Cola) == 0 {
return fmt.Errorf("no data in %v", fileName)
}
var psalm models.Psalm
var currentStrophe models.Strophe
for i, poeticLine := range yamlData.PoeticLines{
log.Printf("first line: %v - %v - %v", poeticLine.Who, poeticLine.To, poeticLine.Of)
if i == 0 {
currentStrophe = models.Strophe{
Who: yamlData.PoeticLines[0].Who,
To: yamlData.PoeticLines[0].To,
Of: yamlData.PoeticLines[0].Of,
}
}
if poeticLine.Who == currentStrophe.Who &&
poeticLine.To == currentStrophe.To &&
poeticLine.Of == currentStrophe.Of {
currentStrophe.Colas = append(currentStrophe.Colas, poeticLine.Cola)
}else{
psalm.Strophes = append(psalm.Strophes, currentStrophe)
currentStrophe = models.Strophe{
Who: poeticLine.Who,
To: poeticLine.To,
Of: poeticLine.Of,
Colas: [][]models.Colon{poeticLine.Cola},
}
}
}
psalm.Strophes = append(psalm.Strophes, currentStrophe)
log.Printf("====== %v %v========", psalm.Strophes, chapterNum)
tmpl := template.Must(template.ParseFiles( "internal/models/index.html", "internal/models/psalm.html"))
outputFileName := path.Join(cfg.HtmlPath, cfg.Book.Name + strconv.Itoa(chapterNum) + ".html")
outputFile, err := os.Create(outputFileName)
if err!= nil {
return fmt.Errorf("cant create %v", outputFileName)
}
defer outputFile.Close()
tmpl.Execute(outputFile, psalm.Strophes)
log.Printf("%v", psalm)
return nil
}