Partial refactor, discovered bug.
This commit is contained in:
parent
6ce1662497
commit
bf772a9d2f
1 changed files with 137 additions and 8 deletions
|
|
@ -5,6 +5,7 @@ import (
|
|||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
|
@ -14,6 +15,9 @@ import (
|
|||
)
|
||||
|
||||
var ErrNoMatches = errors.NewError("no passage or verses match")
|
||||
var ErrNoSuchTranslation = errors.NewError("no such translation")
|
||||
var ErrInvalidChapterNumber = errors.NewError("invalid chapter number")
|
||||
var ErrFileNotExists = errors.NewError("file does not exist")
|
||||
|
||||
// BibleRef reference and parser.
|
||||
type BibleRef struct {
|
||||
|
|
@ -160,9 +164,13 @@ type Translations struct {
|
|||
fs vfs.VFS
|
||||
}
|
||||
|
||||
func OpenTranslations(root vfs.VFS) (t *Translations, err error) {
|
||||
func LoadTranslations(root vfs.VFS) (t *Translations, err error) {
|
||||
var buf []byte
|
||||
var fp vfs.ReadOnlyFile
|
||||
t = &Translations{
|
||||
Bibles: make(map[string]*Bible),
|
||||
fs: root,
|
||||
}
|
||||
|
||||
fp, err = root.Open("translations.json")
|
||||
if err != nil {
|
||||
|
|
@ -177,11 +185,10 @@ func OpenTranslations(root vfs.VFS) (t *Translations, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
if err = json.Unmarshal(buf, &t); err != nil {
|
||||
if err = json.Unmarshal(buf, &t.Bibles); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
t.fs = root
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -190,13 +197,20 @@ func (t *Translations) OpenBible(translation string) (b *Bible, err error) {
|
|||
var ok bool
|
||||
var fp vfs.ReadOnlyFile
|
||||
|
||||
translation = strings.ToLower(translation)
|
||||
if translation == "" {
|
||||
translation = "bsb"
|
||||
}
|
||||
|
||||
if b, ok = t.Bibles[translation]; !ok {
|
||||
return
|
||||
e := ErrNoSuchTranslation.Copy()
|
||||
e.Attach("translation", translation)
|
||||
return nil, e
|
||||
}
|
||||
|
||||
if b.fs == nil {
|
||||
root := t.fs.Clone()
|
||||
root.Chdir(b.Path)
|
||||
root.Chdir(filepath.Join("translations", b.Path))
|
||||
b.fs = root
|
||||
}
|
||||
|
||||
|
|
@ -214,6 +228,7 @@ func (t *Translations) OpenBible(translation string) (b *Bible, err error) {
|
|||
if err = json.Unmarshal(buf, &b.Metadata); err != nil {
|
||||
return
|
||||
}
|
||||
b.Books = make(map[string]*Book)
|
||||
|
||||
return
|
||||
}
|
||||
|
|
@ -222,14 +237,83 @@ type Bible struct {
|
|||
Path string `json:"path"`
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Enabled string `json:"enabled"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Metadata *Metadata `json:"-"`
|
||||
Books map[string]*Book `json:"-"`
|
||||
fs vfs.VFS
|
||||
}
|
||||
|
||||
func (b *Bible) Reference(ref string) (*VerseSet, error) {
|
||||
pr, err := ParseRef(ref)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
book, err := b.OpenBook(pr.Book)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ch, err := book.OpenChapter(pr.Chapter)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println(pr.Verses)
|
||||
if pr.Verses.Stop == 0 {
|
||||
pr.Verses.Stop = pr.Verses.Start
|
||||
}
|
||||
|
||||
return ch.FilterVerses(pr.Verses.Start, pr.Verses.Stop), nil
|
||||
}
|
||||
|
||||
func (b *Bible) String() string {
|
||||
return fmt.Sprintf(`%s(<%v>, "%s", "/%s")`,
|
||||
strings.ToUpper(b.ID),
|
||||
b.Enabled,
|
||||
b.Title,
|
||||
b.Path,
|
||||
)
|
||||
}
|
||||
|
||||
func (b *Bible) OpenBook(book string) (*Book, error) {
|
||||
return nil, nil
|
||||
if b, ok := FindAlias(book); ok {
|
||||
book = b
|
||||
}
|
||||
if b, ok := b.Books[book]; ok {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
if _, err := b.fs.Stat(filepath.Join(strings.ToUpper(book), "meta.json")); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
root := b.fs.Clone()
|
||||
if err := root.Chdir(strings.ToUpper(book)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fp, err := root.Open("meta.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fp.Close()
|
||||
buf, err := io.ReadAll(fp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bk := &Book{
|
||||
Chapters: make([]*Chapter, 0),
|
||||
fs: root,
|
||||
}
|
||||
fmt.Println(string(buf))
|
||||
if err = json.Unmarshal(buf, bk); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
b.Books[book] = bk
|
||||
|
||||
return bk, nil
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
|
|
@ -251,10 +335,53 @@ type Book struct {
|
|||
NumChapters int `json:"numberOfChapters"`
|
||||
NumVerses int `json:"numberOfVerses"`
|
||||
Chapters []*Chapter `json:"-"`
|
||||
fs vfs.VFS
|
||||
}
|
||||
|
||||
func (b *Book) OpenChapter(ch int) (*Chapter, error) {
|
||||
return nil, nil
|
||||
var c *Chapter
|
||||
|
||||
if len(b.Chapters) == 0 {
|
||||
b.Chapters = make([]*Chapter, b.NumChapters)
|
||||
}
|
||||
|
||||
if ch < 0 {
|
||||
return nil, ErrInvalidChapterNumber
|
||||
}
|
||||
|
||||
if ch == 0 && HasSingleChapter(b.ID) {
|
||||
c = b.Chapters[0]
|
||||
ch = 1
|
||||
} else {
|
||||
if ch == 0 {
|
||||
return nil, ErrInvalidChapterNumber
|
||||
}
|
||||
c = b.Chapters[ch-1]
|
||||
}
|
||||
if c != nil {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
fp, err := b.fs.Open(filepath.Join(strconv.Itoa(ch), "chapter.json"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer fp.Close()
|
||||
|
||||
buf, err := io.ReadAll(fp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c = &Chapter{
|
||||
Book: b,
|
||||
ChapterDecl: &ChapterDecl{},
|
||||
}
|
||||
if err := json.Unmarshal(buf, c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type Chapter struct {
|
||||
|
|
@ -469,6 +596,7 @@ func (c *Content) UnmarshalJSON(d []byte) error {
|
|||
}
|
||||
}
|
||||
if v, ok := peek["nodeId"]; ok {
|
||||
fmt.Println(v)
|
||||
if err := json.Unmarshal(v, &c.NoteID); err != nil {
|
||||
return err
|
||||
} else {
|
||||
|
|
@ -493,6 +621,7 @@ func (c *Content) UnmarshalJSON(d []byte) error {
|
|||
}
|
||||
}
|
||||
case '"':
|
||||
fmt.Println(string(d))
|
||||
if err := json.Unmarshal(d, &c.Text); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue