refactoring with internal

This commit is contained in:
github_username_here
2026-03-04 14:21:24 +01:00
parent 8fdf7c190a
commit 7355158ff9
6 changed files with 88 additions and 70 deletions

21
internal/config/config.go Normal file
View File

@@ -0,0 +1,21 @@
package config
type Chapter struct{
Verses []string
Number int
}
type Book struct{
Chapters []Chapter
Version string
Name string
}
type Cfg struct{
Book Book
ConfigPath string
YamlPath string
HtmlPath string
BookList map[string]int
ProperName map[string]struct{}
}

View File

@@ -0,0 +1,72 @@
Aaron
Abraham
Adam
Amalek
Ammon
Amoréens
Babylone
Baal
Benjamin
Basan
Cham
David
Dathan
Dieu
Dor
Ephraïm
Édom
Égypte
Égyptiens
Éphrata
Galaad
Guébal
Hagaréniens
Hermon
Horeb
Isaac
Ismaëlites
Israël
Jacob
Jabin
Jerusalem
Joseph
Juda
Liban
Lévi
Manassé
Madian
Massa
Melchisedek
Mitsear
Mériba
Moab
Moïse
Néphtali
Oreb
Og
Pharaon
Philistins
Peor
Phinéas
Rouge
Saba
Salem
Seba
Sichem
Sirion
Sisera
Sihon
Silo
Sinaï
Seigneur
Sion
Succoth
Tarsis
Thabor
Tsalmon
Tsalmunna
Tyr
YAHWEH
Zabulon
Zébach
Zeeb

View File

@@ -1,43 +1,44 @@
package main
package fetcher
import(
"project.hechon.fr/internal/config"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
"strconv"
"strings"
)
type chapter struct{
verses []string
}
type book struct{
chapters []chapter
}
func FetchChapter(cfg *config.Cfg , searchedChapter string) ( error){
func fetchChapter(book, searchedChapter, version string) (chapter, error){
url := "https://www.biblegateway.com/passage/?search=" + book + "%20" + searchedChapter +"&version=" + version
url := "https://www.biblegateway.com/passage/?search=" + cfg.Book.Name + "%20" + searchedChapter +"&version=" + cfg.Book.Version
res, err := http.Get(url)
if err != nil {
log.Printf("cant get %s : %v\n", url, err)
return chapter{}, err
return err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
log.Printf("error code %v: %v\n", res.StatusCode, res.Status)
return chapter{}, err
log.Printf("error %v\n", res.Status)
return err
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Printf("error reading the http response: %v\n", err)
return chapter{}, err
return err
}
chapterText := chapter{}
chapterNum, err := strconv.Atoi(searchedChapter)
if err != nil{
log.Printf("searched chapter %v is not a valid number%v", searchedChapter, err)
}
chapterText := config.Chapter{
Number: chapterNum,
}
replacer := strings.NewReplacer(
"L'Éternel", "YAHWEH",
"l'Éternel", "YAHWEH",
@@ -59,10 +60,12 @@ func fetchChapter(book, searchedChapter, version string) (chapter, error){
verse, _ = strings.CutPrefix(rawText, strconv.Itoa(i+1))
}
verse = replacer.Replace(verse)
chapterText.verses = append(chapterText.verses, strings.TrimSpace(verse))
chapterText.Verses = append(chapterText.Verses, strings.TrimSpace(verse))
})
return chapterText, nil
chapterText = StripAlternativeVersification(chapterText)
cfg.Book.Chapters = append(cfg.Book.Chapters, chapterText)
return nil
}
/*

View File

@@ -1,6 +1,7 @@
package main
package fetcher
import(
"project.hechon.fr/internal/config"
"log"
"regexp"
"strings"
@@ -8,21 +9,28 @@ import(
"unicode/utf8"
)
func stripAlternativeVersification(rawChapter chapter) chapter{
func StripAlternativeVersification(rawChapter config.Chapter) config.Chapter{
/* a handful of verses have 2 traditions of numbering, the alternative
* rendering appears then between brackets. This is a simple use of
* regexp to get rid of it
*/
regExp:=regexp.MustCompile(`\(\d+:\d+\)`)
var treatedChapter chapter
for _, verse := range rawChapter.verses{
var treatedChapter config.Chapter
for _, verse := range rawChapter.Verses{
modifiedVerse := string(regExp.ReplaceAll([]byte(verse), []byte("")))
log.Printf("%v\n", modifiedVerse)
treatedChapter.verses = append(treatedChapter.verses, modifiedVerse)
treatedChapter.Verses = append(treatedChapter.Verses, modifiedVerse)
}
return treatedChapter
}
func splitInPart(verse string, properName map[string]struct{})(){
func SplitInPart(verse string, properName map[string]struct{})([]string){
var parts []string
currentPart := ""
words := strings.Split(strings.TrimSpace(verse), " ")
wordsCount := len(words)
@@ -34,7 +42,7 @@ func splitInPart(verse string, properName map[string]struct{})(){
trimedWord := strings.TrimRight(word, ".,")
_, isProperName := properName[trimedWord]
if i != 0 && unicode.IsUpper(r) && !isProperName{
log.Printf("%s", currentPart)
parts = append(parts, currentPart)
currentPart = word
isTreated = true
}else{
@@ -44,7 +52,8 @@ func splitInPart(verse string, properName map[string]struct{})(){
}
if !isTreated {
log.Printf("%s", currentPart)
parts = append(parts, currentPart)
}
return parts
}