Commit 20b3ab707e105206de150a5e4baec5a4715ecb6e

  • avatar
  • arvind
  • Sat Jun 16 11:16:31 IST 2012
Respose in JSON.  use jquery for ajax.
  
3434 page = a.read()
3535 a.close()
3636 except ValueError:
37 return "The link is malformed, click <a href='http://dev.a11y.in/web?foruri={0}&lang={1}&interactive=1'>here</a> to be redirected.".format(quote_plus(unquote_plus(d['foruri'])),request.args['lang'])
37 return "The link is malformed, click <a href='http://dev.a11y.in/web?foruri={0}&lang={1}&interactive=1'>here</a> to be redirected.".format(quote_plus(unquote_plus(d['foruri'].encode('utf-8'))),request.args['lang'])
3838 except urllib2.URLError:
3939 return render_template('error.html')
4040 try:
4141 page = unicode(page,'utf-8') #Hack to fix improperly displayed chars on wikipedia.
4242 except UnicodeDecodeError:
4343 pass #Some pages may not need be utf-8'ed
44 g.root = lxml.html.parse(StringIO.StringIO(page)).getroot()
44 try:
45 g.root = lxml.html.parse(StringIO.StringIO(page)).getroot()
46 except ValueError:
47 g.root = lxml.html.parse(d['foruri']).getroot() #Sometimes creators of the page lie about the encoding, thus leading to this execption. http://lxml.de/parsing.html#python-unicode-strings
4548 if request.args.has_key('lang') == False and request.args.has_key('blog') == False:
4649 g.root.make_links_absolute(d['foruri'], resolve_base_href = True)
4750 for i in g.root.iterlinks():
4851 if i[1] == 'href' and i[0].tag != 'link':
49 i[0].attrib['href'] = 'http://{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href']))
52 try:
53 i[0].attrib['href'] = 'http://{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href']))
54 except KeyError:
55 i[0].attrib['href'] = '{0}?foruri={1}'.format(conf.DEPLOYURL[0],quote_plus(i[0].attrib['href'].encode('utf-8')))
5056 setScripts()
5157 g.root.body.set("onload","a11ypi.loadOverlay();")
5258 return lxml.html.tostring(g.root)
6161 setScripts()
6262 setSocialScript()
6363 g.root.body.set("onload","a11ypi.ren();a11ypi.tweet(); a11ypi.facebook(); a11ypi.loadOverlay();")
64 g.root.make_links_absolute(d['foruri'], resolve_base_href = True)
6465 return lxml.html.tostring(g.root)
6566
6667 elif request.args.has_key('lang') == True and request.args.has_key('blog') == False:
67 script_jq_mini = root.makeelement('script')
68 root.body.append(script_jq_mini)
68 script_jq_mini = g.root.makeelement('script')
69 g.root.body.append(script_jq_mini)
6970 script_jq_mini.set("src", conf.JQUERYURL[0] + "/jquery-1.7.min.js")
7071 script_jq_mini.set("type", "text/javascript")
7172 d['lang'] = request.args['lang']
72 script_test = root.makeelement('script')
73 root.body.append(script_test)
73 script_test = g.root.makeelement('script')
74 g.root.body.append(script_test)
7475 script_test.set("src", conf.APPURL[0] + "/server/ui.js")
7576 script_test.set("type", "text/javascript")
76 root.body.set("onload","a11ypi.ren()");
77 root.make_links_absolute(d['foruri'], resolve_base_href = True)
78 return lxml.html.tostring(root)
77 g.root.body.set("onload","a11ypi.ren()");
78 return lxml.html.tostring(g.root)
7979
8080 elif request.args.has_key('interactive') == True and request.args.has_key('blog') == True and request.args.has_key('lang') == True:
8181 setScripts()
237237 response = jsonify(d)
238238 response.headers['Access-Control-Allow-Origin'] = '*'
239239 return response
240@app.route("/replace", methods=['GET'])
241def replace():
242 collection = g.db['post']
243 lang = request.args['lang']
244 url = request.args['url']
245 query = collection.group(
246 key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'),
247 condition={"about" : url, "lang" : lang,"elementtype":"text"},
248 initial={'narration': []},
249 reduce=Code('function(doc,out){out.narration.push(doc);}')
250 )
251
252 audio_query =collection.group(
253 key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'),
254 condition={"about" : url, "lang" : lang, 'elementtype':"audio/ogg"},
255 initial={'narration': []},
256 reduce=Code('function(doc,out){out.narration.push(doc);}')
257 )
258
259 image_query =collection.group(
260 key = Code('function(doc){return {"xpath" : doc.xpath, "about": doc.url}}'),
261 condition={"about" : url, "lang" : lang, 'elementtype':"image"},
262 initial={'narration': []},
263 reduce=Code('function(doc,out){out.narration.push(doc);}')
264 )
265 try:
266 for i in audio_query:
267 query.append(i)
268 except IndexError:
269 pass
270 try:
271 for i in image_query:
272 query.append(i)
273 except IndexError:
274 pass
275
276 for i in query:
277 for y in i['narration']:
278 del(y['_id'])
279 d = {}
280 d['r'] = query
281 response = jsonify(d)
282 response.headers['Access-Control-Allow-Origin'] = '*'
283 return response
284
285 # string=''
286 # if len(query)==0:
287 # print >> environ['wsgi.errors'], 'empty'
288 # return 'empty'
289 # else:
290 # for key in query:
291 # # print >> environ['wsgi.errors'], query
292 # post = key['narration'][len(key['narration'])-1] #Fetching the last done re-narration
293
294 # try:
295 # string+="###"
296
297 # for key in post:
298 # if type(post[key]) is not float:
299 # if key != '_id':
300 # try:
301 # if type(post[key]) is unicode:
302 # string+="&"+str(key)+"::"+ post[key].encode('utf-8')
303 # else:
304 # string+="&"+str(key)+"::"+ post[key]
305 # except TypeError:
306 # print >> environ['wsgi.errors'], key
307 # else:
308 # try:
309 # string+="&"+str(key)+"::"+ str(post[key])
310 # except TypeError:
311 # print >> environ['wsgi.errors'], key
312 # except UnicodeEncodeError:
313 # print >> environ['wsgi.errors'], key
314 # print >> environ['wsgi.errors'], 'Error Encoding request string'
315 # return 'empty'
316
317 # return string
318
240319import logging,os
241320from logging import FileHandler
242321
server/ui.js
(26 / 47)
  
131131 },
132132 ren: function()
133133 {
134 var xhr = new XMLHttpRequest();
135 xhr.onreadystatechange = function()
136 {
137 if(xhr.readyState == 4)
138 {
139 if(xhr.responseText =='empty')
140 {
141 a11ypi.clearMenu();
142 alert("An internal server error occured, please try later.");
143 }
144 else
145 {
146 var info_template = '<div id="infoDiv"></div>';
147 $('#renarrated_overlay').append(info_template);
148 d ={};
149 //a11ypi.response = xhr.responseText;
150 var response=xhr.responseText.substring(3).split('###');
151 for (var j= 0; j< response.length ; j++){
152 chunk = response[j].substring(1).split('&');
153
154 for (var i= 0; i< chunk.length ; i++){
155 pair =chunk[i].split("::");
156 key = pair[0];
157 value = pair[1];
158 d[key] = value;
159 }
160 path = d['xpath'];
161 newContent = d['data'];
162 elementType = d['elementtype'];
163 a11ypi.evaluate(path,newContent,elementType);
164 }
165 }
166 }
167 }
168134
169135 d = window.location.search.split('?')[1];
170136 var a =[];
171137 for (var i = 0;i<d.split('&').length;i++){
172138 a[d.split('&')[i].split('=')[0]] = d.split('&')[i].split('=')[1];
173139 }
174 var url = a['foruri'];
140 var url = decodeURIComponent(a['foruri']);
175141 var lang = a['lang'];
176 var data="url="+url+"&lang="+encodeURIComponent(lang);
177 xhr.open("POST","http://dev.a11y.in/replace",true);
178 xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
179 xhr.send(data);
142 $.getJSON("http://dev.a11y.in/web/replace?",{"url":url,"lang":lang},function(data)
143 {
144 for(var i=0;i<data['r'].length;i++)
145 {
146 for(var x in data['r'][i]['narration'])
147 {
148 path = data['r'][i]['narration'][x]['xpath'];
149 newContent = data['r'][i]['narration'][x]['data'];
150 elementType = data['r'][i]['narration'][x]['elementtype'];
151 a11ypi.evaluate(path,newContent,elementType);
152 }
153 }
154 });
180155 },
181156 evaluate: function()
182157 {
263263 var para = document.createElement("p");
264264 var newel = document.createElement("a");
265265 newel.textContent = menu_list[i];
266 $(newel).attr("href","http://127.0.0.1:5000/?foruri="+page+"&blog="+blog+"&lang="+menu_list[i]+"&interactive=1");
266 $(newel).attr("href","http://dev.a11y.in/web/?foruri="+page+"&blog="+blog+"&lang="+menu_list[i]+"&interactive=1");
267267 para.appendChild(newel);
268268 xyz.appendChild(para);
269269 }
282282 a[d.split('&')[i].split('=')[0]] = d.split('&')[i].split('=')[1];
283283 }
284284
285 window.location = "http://127.0.0.1:5000/?foruri="+a['foruri']+"&blog="+a['blog'] + "&lang=" + e.value+"&interactive=1";
285 window.location = "http://dev.a11y.in/web/?foruri="+a['foruri']+"&blog="+a['blog'] + "&lang=" + e.value+"&interactive=1";
286286 window.reload();
287287 },
288288 showOriginal: function(){
487487 source: function(req, add){
488488
489489 //pass request to server
490 $.getJSON("http://127.0.0.1:5000//getLoc?", req, function(data) {
490 $.getJSON("http://dev.a11y.in/web//getLoc?", req, function(data) {
491491 $('#loc-img').hide();
492492
493493 //create array for response objects
510510 source: function(req, add){
511511
512512 //pass request to server
513 $.getJSON("http://127.0.0.1:5000//getLang?", req, function(data) {
513 $.getJSON("http://dev.a11y.in/web//getLang?", req, function(data) {
514514 $('#lang-img').hide();
515515
516516 //create array for response objects
666666 var para = document.createElement("p");
667667 var newel = document.createElement("a");
668668 newel.textContent = menu_list[i];
669 newel.setAttribute("href", "http://127.0.0.1:5000/?foruri="+encodeURIComponent(menu_list[i]));
669 newel.setAttribute("href", "http://dev.a11y.in/web/?foruri="+encodeURIComponent(menu_list[i]));
670670 newel.setAttribute("class","alipiShowLink");
671671 para.appendChild(newel);
672672 xyz.append(para);
746746 if ($("#blog-filter").val() == null)
747747 { }
748748 else {
749 window.open("http://127.0.0.1:5000/?foruri=" + a['foruri'] + "&blog=" + $("#blog-filter").val());
749 window.open("http://dev.a11y.in/web/?foruri=" + a['foruri'] + "&blog=" + $("#blog-filter").val());
750750 }
751751 },
752752 share: function() {
766766 $('#icon-down').show();
767767// $('#element_edit_overlay').addClass('barOnTop');
768768 $('#renarrated_overlay').hide();
769
769 // $(window).click(function(event){
770 // console.log(event);
771 // event.stopPropagation();
772 // event.preventDefault();
773 // });
770774 $('body *').contents().filter(function(){
771775 {
772776 try{
796796 }
797797 }
798798 }).click(pageEditor.noEdit);
799
799
800800 $(document).mouseover(a11ypi.highlightOnHover);
801801 $(document).mouseout(a11ypi.unhighlightOnMouseOut);
802802 },
  
6666 temp['about'] = element.attrib['about']
6767 temp['xpath'] = element.attrib['xpath']
6868 data =''
69 # for i in element.iterdescendants():
70 # data += etree.tostring(i)
7169 ret = pat.search(lxml.html.tostring(element))
7270 data = lxml.html.tostring(element).partition(ret.group())[2]
7371 data = data.rpartition('</p>')[0]
74 temp['data'] = element.text_content()
72 temp['data'] = data
7573 temp['blog'] = url
7674 temp['bxpath'] = makePath(element)
7775 temp['ren_id'] = ren_id