19
19
from uniquebible .util .TextCommandParser import TextCommandParser
20
20
from uniquebible .util .RemoteCliMainWindow import RemoteCliMainWindow
21
21
from uniquebible .util .TextUtil import TextUtil
22
- from uniquebible .util .RegexSearch import RegexSearch
23
22
from urllib .parse import urlparse , parse_qs
24
23
from uniquebible .util .FileUtil import FileUtil
25
24
from uniquebible .util .LanguageUtil import LanguageUtil
@@ -245,11 +244,11 @@ def runStartupPlugins(self):
245
244
def execPythonFile (self , script ):
246
245
self .textCommandParser .parent .execPythonFile (script )
247
246
248
- def updateData (self , plainOutput = False , allowPrivateData = False ):
249
- if plainOutput :
247
+ def updateData (self , rawOutput = False , allowPrivateData = False ):
248
+ if rawOutput :
250
249
config .displayLanguage = "en_GB"
251
250
config .standardAbbreviation = "ENG"
252
- self .path = re .sub ("^/plain" , "/index.html" , self .path )
251
+ self .path = re .sub ("^/(html|json| plain) " , "/index.html" , self .path )
253
252
config .webHomePage = "{0}.html" .format (config .webPrivateHomePage ) if allowPrivateData else "index.html"
254
253
# Check language
255
254
# Traditional Chinese
@@ -321,21 +320,6 @@ def handleBadRequests(self):
321
320
else :
322
321
self .blankPage ()
323
322
324
- def getCommentaryContent (self , chapterCommentary , fullVerseList ):
325
- pattern = '(<vid id="v[0-9]+?.[0-9]+?.[0-9]+?"></vid>)<hr>'
326
- searchReplaceItems = ((pattern , r"<hr>\1" ),)
327
- chapterCommentary = RegexSearch .deepReplace (chapterCommentary , pattern , searchReplaceItems )
328
- verseCommentaries = chapterCommentary .split ("<hr>" )
329
-
330
- fullVerseList = [f'<vid id="v{ b } .{ c } .{ v } "' for b , c , v , * _ in fullVerseList ]
331
-
332
- loaded = []
333
- for i in verseCommentaries :
334
- for ii in fullVerseList :
335
- if i .strip () and not i in loaded and ii in i :
336
- loaded .append (i )
337
- return "<hr>" .join (loaded )
338
-
339
323
def do_GET (self ):
340
324
try :
341
325
self .clientIP = self .client_address [0 ]
@@ -356,11 +340,12 @@ def do_GET(self):
356
340
query_components = parse_qs (urlparse (self .path ).query )
357
341
private = query_components .get ("private" , [])
358
342
allowPrivateData = True if private and private [0 ].strip () == config .webPrivateHomePage else False
359
- plainOutput = True if self .path .startswith ("/plain?" ) else False
343
+ config .rawOutput = True if re .search (r"^/(html|json|plain)\?" , self .path ) else False
344
+ api = re .sub (r"^/(html|json|plain)\?.*?$" , r"\1" , self .path ) if config .rawOutput else ""
360
345
# update resource path
361
- self .updateData (plainOutput = plainOutput , allowPrivateData = allowPrivateData )
346
+ self .updateData (rawOutput = config . rawOutput , allowPrivateData = allowPrivateData )
362
347
363
- if plainOutput :
348
+ if config . rawOutput :
364
349
cmd = query_components .get ("cmd" , [])
365
350
if cmd :
366
351
self .command = query_components ["cmd" ][0 ].strip ()
@@ -373,15 +358,20 @@ def do_GET(self):
373
358
# output
374
359
self .commonHeader ()
375
360
_ , content , _ = self .textCommandParser .parser (self .command , "http" )
376
- # refine commentary output
377
- if content and self .command .strip ().lower ().startswith ("commentary:::" ):
378
- verseList = self .parser .extractAllReferences (self .command )
379
- if verseList :
380
- fullVerseList = Bible (text = "KJV" ).getEverySingleVerseList (verseList [:1 ])
381
- content = self .getCommentaryContent (content , fullVerseList )
382
361
content = content .replace ("<u><b>" , "<u><b># " )
383
- plainText = TextUtil .htmlToPlainText (content ).strip ()
384
- self .wfile .write (bytes (plainText , "utf8" ))
362
+ # convert to plain text for plain endpoint
363
+ if api in ("json" , "plain" ):
364
+ content = TextUtil .htmlToPlainText (content ).strip ()
365
+ if api == "json" :
366
+ output = {}
367
+ for index , item in enumerate (self .command .split (":::" )):
368
+ if index == 0 :
369
+ output ["keyword" ] = item .strip ()
370
+ else :
371
+ output [f"parameter_{ index } " ] = item .strip ()
372
+ output ["content" ] = content
373
+ content = json .dumps (output )
374
+ self .wfile .write (bytes (content , "utf8" ))
385
375
# restore user config
386
376
config .addFavouriteToMultiRef = addFavouriteToMultiRef
387
377
elif self .ignoreCommand (self .path ):
0 commit comments