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
194197func  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}
0 commit comments