From 7fc76d541645d49f79b6e949e6d769e8a914945d Mon Sep 17 00:00:00 2001 From: "Benjamin A. Shelton" Date: Fri, 9 Oct 2026 21:39:18 -0600 Subject: [PATCH] Added Bible passage reference parser. --- internal/bible/types.go | 111 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 107 insertions(+), 4 deletions(-) diff --git a/internal/bible/types.go b/internal/bible/types.go index c843cb6..b135a47 100644 --- a/internal/bible/types.go +++ b/internal/bible/types.go @@ -7,10 +7,15 @@ import ( "io" "os" "path/filepath" + "regexp" "strconv" "strings" + + "go.destrealm.net/errors" ) +var ErrNoMatches = errors.NewError("no passage or verses match") + type Translations struct { Bibles map[string]*Bible } @@ -79,6 +84,99 @@ type BibleRef struct { } `json:"verses"` } +func NewBibleRef() *BibleRef { + return &BibleRef{ + Verses: struct { + Start int "json:\"start,omitempty\"" + Stop int "json:\"stop,omitempty\"" + }{}, + } +} + +func ParseRef(ref string) (*BibleRef, error) { + regs, err := regexp.Compile(`((\d+(\+|\s))?[a-zA-Z]+)(\+|\s)?([0-9]+-?[0-9]*)?\:?([0-9]+(-?([0-9]+))?)?`) + if err != nil { + return nil, err + } + + s := regs.FindAllStringSubmatch(ref, 1) + if len(s) != 1 || len(s[0]) != 9 { + return nil, ErrNoMatches + } + + for i, t := range s[0] { + fmt.Println(i, ":", t) + } + + var ok bool + var book string + var chapter int + var verseStart int + var verseStop int + bref := NewBibleRef() + + if book, ok = FindAlias(s[0][1]); !ok { + return nil, ErrNoMatches + } + bref.Book = book + + fmt.Println(book) + // Handle single-chapter books (e.g. Philemon). + if _, ok := singleChapters[book]; ok { + if strings.Contains(s[0][5], "-") { + vv := strings.Split(s[0][5], "-") + verseStart, err = strconv.Atoi(vv[0]) + if err != nil { + return nil, ErrNoMatches.Wrap(err) + } + verseStop, err = strconv.Atoi(vv[1]) + if err != nil { + return nil, ErrNoMatches.Wrap(err) + } + } else { + verseStart, err = strconv.Atoi(s[0][5]) + if err != nil { + return nil, ErrNoMatches.Wrap(err) + } + } + bref.Verses.Start = verseStart + bref.Verses.Stop = verseStop + return bref, nil + } + + // The parser deliberately rejects chapter ranges (e.g. Matthew 12-13) and + // returns an error. + chapter, err = strconv.Atoi(s[0][5]) + if err != nil { + return nil, ErrNoMatches.Wrap(err) + } + if strings.Contains(s[0][6], "-") { + vv := strings.Split(s[0][6], "-") + verseStart, err = strconv.Atoi(vv[0]) + if err != nil { + return nil, ErrNoMatches.Wrap(err) + } + verseStop, err = strconv.Atoi(vv[1]) + if err != nil { + return nil, ErrNoMatches.Wrap(err) + } + } else { + // If the chapter is specified and no verses are provided, this will + // return an error here since the empty string cannot be converted to an + // integer. This *mmight* be a bug, as the intent is to only provide a + // limited scope of verses, an entire chapter is out of the question for a reasonable quote. + verseStart, err = strconv.Atoi(s[0][6]) + if err != nil { + return nil, ErrNoMatches.Wrap(err) + } + } + bref.Chapter = chapter + bref.Verses.Start = verseStart + bref.Verses.Stop = verseStop + + return bref, nil +} + func (r *BibleRef) Code() string { code, ok := FindAlias(r.Book) if ok { @@ -94,19 +192,24 @@ func (r *BibleRef) String() string { } else { sb.WriteString("???") } - sb.WriteString(" ") + sb.WriteByte(' ') if r.Chapter > 0 { sb.WriteString(strconv.Itoa(r.Chapter)) } if r.Verse > 0 { + if r.Chapter > 0 { + sb.WriteByte(':') + } sb.WriteString(strconv.Itoa(r.Verse)) } else { if r.Verses.Start > 0 { + if r.Chapter > 0 { + sb.WriteByte(':') + } sb.WriteString(strconv.Itoa(r.Verses.Start)) if r.Verses.Stop > 0 { - - } else { - + sb.WriteByte('-') + sb.WriteString(strconv.Itoa(r.Verses.Stop)) } }