forked from slaporte/Disambiguity
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
151 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Author: Samat Jain <http://samat.org/> | ||
# License: MIT (same as Bottle) | ||
|
||
import bottle | ||
|
||
from bottle import request, response, route | ||
from bottle import install, uninstall | ||
|
||
from json import dumps as json_dumps | ||
|
||
|
||
class JSONAPIPlugin(object): | ||
name = 'jsonapi' | ||
api = 1 | ||
|
||
def __init__(self, json_dumps=json_dumps): | ||
uninstall('json') | ||
self.json_dumps = json_dumps | ||
|
||
def apply(self, callback, context): | ||
dumps = self.json_dumps | ||
if not dumps: return callback | ||
def wrapper(*a, **ka): | ||
r = callback(*a, **ka) | ||
|
||
# Attempt to serialize, raises exception on failure | ||
json_response = dumps(r) | ||
|
||
# Set content type only if serialization succesful | ||
response.content_type = 'application/json' | ||
|
||
# Wrap in callback function for JSONP | ||
callback_function = request.query.get('callback') | ||
if callback_function: | ||
json_response = ''.join([callback_function, '(', json_response, ')']) | ||
|
||
return json_response | ||
return wrapper | ||
|
||
|
||
install(JSONAPIPlugin()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,76 @@ | ||
<html> | ||
<head> | ||
<script type="text/javascript" src="js/jquery.js"></script> | ||
<script type="text/javascript" src='js/dab.js'></script> | ||
<script type="text/javascript"> | ||
|
||
var global_timeout = 5000; | ||
|
||
function random_int (min, max) { | ||
return Math.floor(Math.random() * (max - min + 1)) + min; | ||
} | ||
|
||
function do_query(url, complete_callback, kwargs) { | ||
var all_kwargs = { | ||
url: url, | ||
type: 'get', | ||
dataType: 'jsonp', | ||
timeout: global_timeout, | ||
success: function(data) { | ||
console.log('successful ajax query to ' + url); | ||
complete_callback(null, data); | ||
}, | ||
error: function(jqXHR, textStatus, errorThrown) { | ||
console.log('failed query to ' + url); | ||
complete_callback(errorThrown, null); | ||
}, | ||
complete: function(jqXHR, textStatus) { | ||
// TODO: error handling (jsonp doesn't get error() calls for a lot of errors) | ||
}, | ||
}; | ||
|
||
for (var key in kwargs) { | ||
if(kwargs.hasOwnProperty(key)) { | ||
all_kwargs[key] = kwargs[key]; | ||
} | ||
} | ||
$.ajax(all_kwargs); | ||
} | ||
|
||
do_query('http://localhost:8080/fake/', page_setup); | ||
|
||
|
||
|
||
function page_setup(err, data){ | ||
var rand_choice = random_int(0, data.dabs.length - 1); | ||
console.log(rand_choice); | ||
var dab = data.dabs[rand_choice]; | ||
var choices = dab.options, | ||
page_title = dab.page_title, | ||
dab_title = dab.title, | ||
dab_context = dab.context; | ||
|
||
$('#page_title').html(page_title); | ||
$('#phrase').html(dab_title); | ||
$('#context').html(dab_context); | ||
$('span:contains("disambiguation")"').parents('sup').hide(); | ||
for(var i = 0; i < choices.length; i++) { | ||
$('#choices').append('<li>' + choices[i].text + '</li>'); | ||
} | ||
} | ||
</script> | ||
<style> | ||
.dab-link { | ||
background-color: yellow; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>DAB game</h1> | ||
<p>Page: <span id='page_title'></span></p> | ||
<p>Dabblet: <span id='phrase'></span></p> | ||
<h2>context</h2> | ||
<div id='phrase'></div> | ||
<div id='inst'></div> | ||
<ul id='as'></ul> | ||
<div id='context'></div> | ||
<ul id='choices'></ul> | ||
</body> | ||
</html> |