Selective emboldeing of text in a webpage
- by Eknath Iyer
while printing out utf-8 characters onto a webpage, if encapsulate them with they get emboldened, but anything else, the page turns blank. Why?
def main():
print "Content-type: text/html\r\n\r\n";
print '<html>'
print '<head>'
print '<style type="text/css">'
print '.highlight { background-color: yellow }'
print '.color1 { color: green; }'
print '.color2 { color: blue; }'
print '.color3 { color: purple; }'
print '.color4 { color: red; }'
print '.color5 { color: teal; }'
print '.color6 { color: yellow; }'
print '.color7 { color: orange; }'
print '.color8 { color: violet; }'
print '</style></head>'
print '<body>'
form = cgi.FieldStorage()
ch = form.getvalue('choice')
if ch == 'English':
in_sent = form.getvalue('f1')
in_sent = in_sent.lower()
cho=0
elif ch == 'Hindi':
in_sent = trans_he(form.getvalue('transl1').decode("utf-8")).strip()
cho=1
#cho = 0 for english
#cho = 1 for hindi
adict=[]
print '<center><u> User Input Sentence ==> <b>', in_sent,'</b></u></center><br>'
in_sent=in_sent.strip().split(' ')
colordict={}
counter=1
for word in in_sent:
colordict[word]=counter
counter = counter + 1
f = open('bidirectional.alignment.txt','rb').read()
records=f.strip().split('\n\n\n')
for record in records:
el=[]
el2 = []
#basic file processing is done here.
record = record.strip().split('\n')
source = record[cho]
target = record[(cho+1)%2]
source_sent = source.split(' # ')[1]
target_sent = target.split(' # ')[1]
source_words = source_sent.strip().split(' ')
target_words = target_sent.strip().split(' ')
trans_index = source.split(' # ')[2].strip().split(' ')
for word in in_sent:
if word in source_words:
if int(trans_index[source_words.index(word)]) > 0:
tword=target_words[(int(trans_index[source_words.index(word)])-1)]
target_sent = target_sent.replace(tword+' ','<b>'+tword+' </b>')
# When the <b> tag is used here(for the 'target_sent = ...' statement). it is fine. But when <b> is replaced by something like in the next line or even <i> or <u>, it doesn't show an output at all
source_sent = source_sent.replace(word+' ','<span class="color1">'+word+' </span>')
el2.append(source_sent)
el2.append(target_sent)
el.append(target_sent.count('<b>'))
el.append(el2)
if target_sent.count('<b>') > 0:
adict.append(el)
print '<table><tr><td><center><h1>SOURCE LANGUAGE</h1></center></td><td><center> <h1>TARGET LANGUAGE</h1></center></td></tr>'
for entry in adict:
print '<tr><td>',entry[1][0],'</td><td>',trans_eh(entry[1][1]).encode("utf-8"),'</td> </tr>'
print '</table></body>'
print '</html>'
main()