Added Bible passage reference parser.
This commit is contained in:
parent
6c61618bbe
commit
7fc76d5416
1 changed files with 107 additions and 4 deletions
|
|
@ -7,10 +7,15 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"go.destrealm.net/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrNoMatches = errors.NewError("no passage or verses match")
|
||||||
|
|
||||||
type Translations struct {
|
type Translations struct {
|
||||||
Bibles map[string]*Bible
|
Bibles map[string]*Bible
|
||||||
}
|
}
|
||||||
|
|
@ -79,6 +84,99 @@ type BibleRef struct {
|
||||||
} `json:"verses"`
|
} `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 {
|
func (r *BibleRef) Code() string {
|
||||||
code, ok := FindAlias(r.Book)
|
code, ok := FindAlias(r.Book)
|
||||||
if ok {
|
if ok {
|
||||||
|
|
@ -94,19 +192,24 @@ func (r *BibleRef) String() string {
|
||||||
} else {
|
} else {
|
||||||
sb.WriteString("???")
|
sb.WriteString("???")
|
||||||
}
|
}
|
||||||
sb.WriteString(" ")
|
sb.WriteByte(' ')
|
||||||
if r.Chapter > 0 {
|
if r.Chapter > 0 {
|
||||||
sb.WriteString(strconv.Itoa(r.Chapter))
|
sb.WriteString(strconv.Itoa(r.Chapter))
|
||||||
}
|
}
|
||||||
if r.Verse > 0 {
|
if r.Verse > 0 {
|
||||||
|
if r.Chapter > 0 {
|
||||||
|
sb.WriteByte(':')
|
||||||
|
}
|
||||||
sb.WriteString(strconv.Itoa(r.Verse))
|
sb.WriteString(strconv.Itoa(r.Verse))
|
||||||
} else {
|
} else {
|
||||||
if r.Verses.Start > 0 {
|
if r.Verses.Start > 0 {
|
||||||
|
if r.Chapter > 0 {
|
||||||
|
sb.WriteByte(':')
|
||||||
|
}
|
||||||
sb.WriteString(strconv.Itoa(r.Verses.Start))
|
sb.WriteString(strconv.Itoa(r.Verses.Start))
|
||||||
if r.Verses.Stop > 0 {
|
if r.Verses.Stop > 0 {
|
||||||
|
sb.WriteByte('-')
|
||||||
} else {
|
sb.WriteString(strconv.Itoa(r.Verses.Stop))
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue