CoCalc Public Fileswww / cgi-bin / mfd / mfe_glossary.pyOpen with one click!
Author: William A. Stein
Compute Environment: Ubuntu 18.04 (Deprecated)
1
import constants, mfd
2
3
class Glossary:
4
def __init__(self):
5
return
6
7
def Link(self, phrase):
8
#s = '<a href="%s/mfe_glossary_html.py?phrase=%s">%s</a> '%\
9
# (constants.CGIROOT, phrase, phrase)
10
url = "%s/mfe_glossary_html.py?phrase=%s"%(constants.CGIROOT, phrase)
11
js = "onClick=\"window.open('%s','Definition of %s','height=100,width=400');return false\""%(url,phrase)
12
s = '<a href="%s/mfe_glossary_html.py?phrase=%s" %s>%s</a>'%\
13
(constants.CGIROOT, phrase, js, phrase)
14
return s
15
16
def Define(self, phrase):
17
s="<h1>"
18
#for x in phrase.split():
19
# s = s + (x.lower()).capitalize() + " "
20
s=s+phrase
21
s=s+"</h1>"
22
phrase = phrase.lower()
23
def_text = mfd.Get_Definition_From_Database(phrase)
24
def_text = Glossarize(def_text)
25
s=s+ def_text
26
return s
27
28
29
# replace each phrase in string that is defined in the database glossary
30
# with an html link to that definition.
31
glossary = Glossary()
32
def Glossarize(string):
33
wordlist = mfd.List_Of_Words_Defined_In_Glossary()
34
wordlist.sort() # assumes that no phrase is a subphrase of another phrase, unless the subphrase begins in same way
35
for word in wordlist:
36
i=0
37
while 1:
38
pos = string[i:].upper().find(word.upper())
39
if pos == -1:
40
break
41
i = pos + i
42
link = glossary.Link(string[i:i+len(word)])
43
string = string[:i] + link + string[i+len(word):]
44
i = i + len(link)
45
return string
46