Skip to content

Instantly share code, notes, and snippets.

@guillaumebort
Created October 8, 2010 08:43
Show Gist options
  • Save guillaumebort/616525 to your computer and use it in GitHub Desktop.
Save guillaumebort/616525 to your computer and use it in GitHub Desktop.
package controllers
import play._
import play.mvc._
import play.libs._
object Application extends Controller {
def index = Template
def check(text: String) = {
val xml = WS.url("https://www.google.com/tbproxy/spell?lang=en&hl=en").body(
<spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1">
<text>{text}</text>
</spellrequest>
).post().getXml()
val mistakes = xml \\ "c" map { elem =>
(elem \ "@o", elem \ "@l", elem.text)
}
Template(mistakes)
}
}
#{extends 'main.html' /}
#{set title:'Result' /}
<p id="result">
${text}
</p>
<script type="text/javascript" charset="utf-8">
var text = $('#result').text().trim(), mistakes = ${mistakes.json()}.reverse, html = '', o = 0, token
while(token = mistakes.pop()) {
html += text.substring(o, token._1)
o = token._1 + token._2
html += '<em>' + text.substring(token._1, o) + '</em>'
}
html += text.substring(o, text.length)
$('#result').html(html)
</script>
<hr>
<a href="@{index()}">Back</a>
<style type="text/css" media="screen">
em {
color: red;
}
</style>
#{extends 'main.html' /}
#{set title:'Home' /}
#{form @check()}
<textarea name="text"></textarea>
<br>
<input type="submit" value="Check!">
#{/form}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment