64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package renderer
|
||
import (
|
||
"log"
|
||
"project.hechon.fr/internal/config"
|
||
"project.hechon.fr/internal/models"
|
||
"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("can’t 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
|
||
currentStrophe := models.Strophe{
|
||
Who: yamlData.PoeticLines[0].Who,
|
||
To: yamlData.PoeticLines[0].To,
|
||
Of: yamlData.PoeticLines[0].Of,
|
||
}
|
||
|
||
for _, poeticLine := range yamlData.PoeticLines{
|
||
log.Printf("first line: %v - %v - %v", poeticLine.Who, poeticLine.To, poeticLine.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.Who = poeticLine.Who
|
||
currentStrophe.Of = poeticLine.Of
|
||
currentStrophe.To = poeticLine.To
|
||
}
|
||
|
||
}
|
||
|
||
psalm.Strophes = append(psalm.Strophes, currentStrophe)
|
||
|
||
log.Printf("%v", psalm)
|
||
return nil
|
||
|
||
|
||
|
||
}
|