Permalink
Browse files

allow url validation

  • Loading branch information...
1 parent 90151b5 commit 2b211815c91c2f54d9c47628b6e13bfe7e7a037d @ptarjan committed May 28, 2013
Showing with 62 additions and 38 deletions.
  1. +53 −37 index.html
  2. +9 −1 main.py
View
@@ -6,7 +6,7 @@
body {
text-align : center;
}
-form {
+#left {
float : left;
width : 500px;
}
@@ -70,6 +70,12 @@
#pay_my_bills {
margin-top: 10px;
}
+#url_form input[name=url] {
+ width: 75%;
+}
+.error {
+ color: red;
+}
</style>
<script type="text/javascript">
@@ -91,37 +97,43 @@
<div class="header">
<h1>Online <a href="http://yaml.org" target="_new">YAML</a> Parser</h1>
</div>
- <form method="POST" action="/">
- <textarea id="yaml" class="yaml" name="yaml">{{ yaml }}</textarea>
- <div>
- Output:
- <input type="radio" name="type" id="type_json" value="json"
- {% ifequal type "json" %}checked="checked"{% endifequal %}
- /><label for="type_json">json</label>
- <input type="radio" name="type" id="type_python" value="python"
- {% ifequal type "python" %}checked="checked"{% endifequal %}
- /><label for="type_python">python</label>
- <input type="radio" name="type" id="type_canonical_yaml" value="canonical_yaml"
- {% ifequal type "canonical_yaml" %}checked="checked"{% endifequal %}
- /><label for="type_canonical_yaml">canonical yaml</label>
- <a id="link" href="?yaml={{ yaml|urlencode }}&type={{ type|urlencode }}">
- Link to this page
- </a>
- </div>
- <div id="pay_my_bills">
- <script type="text/javascript"><!--
- google_ad_client = "ca-pub-7342863608664054";
- /* online yaml parser banner */
- google_ad_slot = "1929248609";
- google_ad_width = 468;
- google_ad_height = 60;
- //-->
- </script>
- <script type="text/javascript"
- src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
- </script>
- </div>
- </form>
+ <div id="left">
+ <form id="yaml_form" method="POST" action="/">
+ <textarea id="yaml" class="yaml" name="yaml">{{ yaml }}</textarea>
+ <div>
+ Output:
+ <input type="radio" name="type" id="type_json" value="json"
+ {% ifequal type "json" %}checked="checked"{% endifequal %}
+ /><label for="type_json">json</label>
+ <input type="radio" name="type" id="type_python" value="python"
+ {% ifequal type "python" %}checked="checked"{% endifequal %}
+ /><label for="type_python">python</label>
+ <input type="radio" name="type" id="type_canonical_yaml" value="canonical_yaml"
+ {% ifequal type "canonical_yaml" %}checked="checked"{% endifequal %}
+ /><label for="type_canonical_yaml">canonical yaml</label>
+ <a id="link" href="?yaml={{ yaml|urlencode }}&type={{ type|urlencode }}">
+ Link to this page
+ </a>
+ </div>
+ <div id="pay_my_bills">
+ <script type="text/javascript"><!--
+ google_ad_client = "ca-pub-7342863608664054";
+ /* online yaml parser banner */
+ google_ad_slot = "1929248609";
+ google_ad_width = 468;
+ google_ad_height = 60;
+ //-->
+ </script>
+ <script type="text/javascript"
+ src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
+ </script>
+ </div>
+ </form>
+ <form id="url_form" action="/">
+ <input name="url" value="{{ url|default:"http://" }}" />
+ <input type="submit" value="Validate URL" />
+ </form>
+ </div>
<div id="right">
<div class="code">
<h2>Output</h2>
@@ -149,24 +161,28 @@
$(function() {
var submit = function() {
- $("form").submit();
+ $("#yaml_form").submit();
return false;
}
$('#yaml').keyup(function() {
- $("form").submit();
+ $("#yaml_form").submit();
});
$('input:radio[name=type]').click(function() {
- $("form").submit();
+ $("#yaml_form").submit();
});
-$("form").submit(function(ev) {
+$("#yaml_form").submit(function(ev) {
var inputData = {yaml: $("#yaml").val(), type: $('input:radio[name=type]:checked').val() };
$.ajax({
url: "/ajax?callback=?",
data: inputData,
success:
function(data) {
$("#output").text(data);
+ console.log(data);
+ if (data.match(/^ERROR/)) {
+ $("#output").wrapInner('<span class="error"/>');
+ }
$("#link").attr("href", "?" + $.param(inputData));
},
error:
@@ -183,7 +199,7 @@
$("#yaml").val(
urldecode($(this).attr("href").replace("?yaml=", ""))
);
- $("form").submit();
+ $("#yaml_form").submit();
return false;
});
View
@@ -20,6 +20,7 @@
import yaml
import simplejson
import pprint
+import urllib2
import wsgiref.handlers
from django_jsonencoder import DjangoJSONEncoder
@@ -54,14 +55,21 @@ def getOutput(y, type) :
class MainHandler(webapp.RequestHandler):
def get(self):
y = self.request.get("yaml", default_yaml)
+ url = self.request.get("url")
+ if url:
+ try:
+ y = urllib2.urlopen(url).read()
+ except Exception:
+ pass
type = self.request.get("type", "json")
+
output = getOutput(y, type)
-
template_values = {}
template_values['output'] = output
template_values['yaml'] = y
template_values['examples'] = examples
template_values['type'] = type
+ template_values['url'] = url
path = os.path.join(os.path.dirname(__file__), 'index.html')
self.response.out.write(template.render(path, template_values))

0 comments on commit 2b21181

Please sign in to comment.