11import json
22import random
3+ import re
34
45from PySide6 .QtCore import QDateTime , QFile , QObject , QTextStream , QTime , Slot
56
@@ -8,6 +9,7 @@ class Notifications(QObject):
89 notificationsEnabled = False
910 notificationSentLock = False
1011 notificationTime = QDateTime ()
12+ currentVerse = ""
1113
1214 @Slot (result = bool )
1315 def getNotificationsEnabled (self ) -> bool :
@@ -72,17 +74,17 @@ def loadVerses(self) -> dict:
7274 }
7375
7476 contents = QTextStream (file )
75- verses_string = contents .readAll ()
77+ versesString = contents .readAll ()
7678
77- verses = json .loads (verses_string )
79+ verses = json .loads (versesString )
7880 # Choose a random verse
79- verse = verses ["all" ][random .randint (0 , len (verses ["all" ]))]
81+ self . currentVerse = verses ["all" ][random .randint (0 , len (verses ["all" ]))]
8082
8183 # TODO: Need to decide on the key names
8284 return {
83- "text" : verse ["verse" ],
84- "place" : verse ["place" ],
85- "location" : verse ["data" ]
85+ "text" : self . currentVerse ["verse" ],
86+ "place" : self . currentVerse ["place" ],
87+ "location" : self . currentVerse ["data" ]
8688 }
8789
8890 @Slot (str , result = "QVariant" )
@@ -92,11 +94,30 @@ def loadChapter(self, location: str) -> dict:
9294 return ["" , "" ]
9395
9496 contents = QTextStream (file )
95- contents_string = contents .readAll ()
97+ contentsString = contents .readAll ()
9698
97- contents_json = json .loads (contents_string )
99+ verse = self .currentVerse ["verse" ]
100+ contentsJson = json .loads (contentsString )
101+
102+ chapter = contentsJson
103+ chapterPlace = chapter ["read" ][0 ]["chapter" ]
104+
105+ highlightedChapter = self .highlightVerse (verse , chapter )
98106
99107 return {
100- "text" : contents_json [ "read" ][ 0 ][ "text" ] ,
101- "place" : contents_json [ "read" ][ 0 ][ "chapter" ]
108+ "text" : highlightedChapter ,
109+ "place" : chapterPlace
102110 }
111+
112+ def highlightVerse (self , verse : str , chapter : dict ) -> str :
113+ chapterText = chapter ["read" ][0 ]["text" ]
114+
115+ verseMatch = re .search (re .escape (verse ), chapterText )
116+ match = verseMatch .span ()
117+
118+ toHighlight = chapterText [match [0 ] : match [1 ]]
119+
120+ highlightedChapter = chapterText .replace (toHighlight , "<b>" + toHighlight + "</b>" )
121+
122+ return highlightedChapter
123+
0 commit comments