Skip to content
This repository was archived by the owner on May 15, 2021. It is now read-only.

Commit bc77d1e

Browse files
author
Seraphim R.P
committed
Prioritize version in reference vs. preference.
1 parent 359029b commit bc77d1e

File tree

2 files changed

+20
-19
lines changed

2 files changed

+20
-19
lines changed

routes/verses/parsing.go

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strconv"
88
"strings"
99

10+
"gorm.io/gorm"
1011
"internal.kerygma.digital/kerygma-digital/biblebot/backend/models"
1112
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/bookmap"
1213
"internal.kerygma.digital/kerygma-digital/biblebot/backend/utils/namefetcher"
@@ -48,21 +49,21 @@ func FindBooksInString(str string) (string, []models.BookSearchResult) {
4849
}
4950

5051
// GenerateReference creates a reference object based on a BookSearchResult and the surrounding values in a string.
51-
func GenerateReference(str string, bookSearchResult models.BookSearchResult, version models.Version) *models.Reference {
52+
func GenerateReference(db gorm.DB, str string, bookSearchResult models.BookSearchResult, version models.Version) *models.Reference {
5253
book := bookSearchResult.Name
5354
startingChapter := 0
5455
startingVerse := 0
5556
endingChapter := 0
5657
endingVerse := 0
57-
//tokenIdxAfterSpan := 0
58+
tokenIdxAfterSpan := 0
5859

5960
tokens := strings.Split(str, " ")
6061

6162
if bookSearchResult.Index+2 <= len(tokens) {
6263
relevantToken := tokens[bookSearchResult.Index+1:][0]
6364

6465
if strings.Contains(relevantToken, ":") {
65-
//tokenIdxAfterSpan = bookSearchResult.Index + 2
66+
tokenIdxAfterSpan = bookSearchResult.Index + 2
6667

6768
colonRegex, _ := regexp.Compile(":")
6869
colonQuantity := len(colonRegex.FindAllStringIndex(relevantToken, -1))
@@ -93,8 +94,6 @@ func GenerateReference(str string, bookSearchResult models.BookSearchResult, ver
9394
endingVerse = secondNum
9495
}
9596
}
96-
97-
break
9897
case 1:
9998
pair := strings.Split(relevantToken, ":")
10099

@@ -115,6 +114,8 @@ func GenerateReference(str string, bookSearchResult models.BookSearchResult, ver
115114

116115
num, err := strconv.Atoi(pairValue)
117116
if err != nil {
117+
// We know that BibleGateway will extend to the end of a chapter with this syntax,
118+
// but for other sources this is likely not available.
118119
if version.Source == "bg" {
119120
// Instead of returning nil, we'll break out of the loop
120121
// in the event that the span exists to extend to the end of a chapter.
@@ -125,10 +126,8 @@ func GenerateReference(str string, bookSearchResult models.BookSearchResult, ver
125126
switch idx {
126127
case 0:
127128
startingVerse = num
128-
break
129129
case 1:
130130
endingVerse = num
131-
break
132131
default:
133132
return nil
134133
}
@@ -137,15 +136,19 @@ func GenerateReference(str string, bookSearchResult models.BookSearchResult, ver
137136
if endingVerse == 0 && spanQuantity == 0 {
138137
endingVerse = startingVerse
139138
}
140-
141-
break
142139
}
143140

144-
// TODO: This after DBs implemented.
145-
/*if len(tokens) > tokenIdxAfterSpan {
146-
lastToken = strings.ToUpper(tokens[tokenIdxAfterSpan])
147-
// if version exists corresponding to lastToken, use that instead
148-
}*/
141+
// If the last token of the reference is a version abbreviation, utilize that version.
142+
if len(tokens) > tokenIdxAfterSpan {
143+
lastToken := strings.ToUpper(tokens[tokenIdxAfterSpan])
144+
145+
var idealVersion models.Version
146+
db.Where(&models.Version{Abbreviation: lastToken}).First(&idealVersion)
147+
148+
if idealVersion.Abbreviation == lastToken {
149+
version = idealVersion
150+
}
151+
}
149152
}
150153
} else {
151154
return nil
@@ -192,8 +195,8 @@ func isValueInString(value string, str string) bool {
192195
}
193196

194197
func removePunctuation(str string) string {
195-
noPunctuationRegex, _ := regexp.Compile("[^\\w\\s]|_")
196-
minimizeWhitespaceRegex, _ := regexp.Compile("\\s+")
198+
noPunctuationRegex, _ := regexp.Compile(`[^\w\s]|_`)
199+
minimizeWhitespaceRegex, _ := regexp.Compile(`\s+`)
197200

198201
return minimizeWhitespaceRegex.ReplaceAllString(noPunctuationRegex.ReplaceAllString(str, ""), " ")
199202
}

routes/verses/verses.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func fetchVerse(c *fiber.Ctx) error {
5959

6060
config.DB.Where(&models.Version{Abbreviation: ctx.Prefs.Version}).First(&ver)
6161

62-
ref := GenerateReference(str, bsr, ver)
62+
ref := GenerateReference(config.DB, str, bsr, ver)
6363
if ref == nil {
6464
continue
6565
}
@@ -87,10 +87,8 @@ func ProcessVerse(ref *models.Reference, titles bool, verseNumbers bool) (*model
8787
switch ref.Version.Source {
8888
case "bg":
8989
provider = bgProvider
90-
break
9190
case "ab":
9291
provider = abProvider
93-
break
9492
default:
9593
return nil, logger.LogWithError("processVerse", "invalid provider found in reference", nil)
9694
}

0 commit comments

Comments
 (0)