From 519b1a362c1545be4d7b5c9a2e5b3fb6b1f67677 Mon Sep 17 00:00:00 2001 From: Manu Sporny Date: Tue, 14 Dec 2010 23:17:29 -0500 Subject: [PATCH] Finished alpha release of RDFa Live Loop tool. --- .htaccess | 8 +++ css/layout.css | 9 ++- git-update | 1 + index.html | 17 +++--- live-loop.js | 59 ++++++++++++++++++++ live-loop.py | 148 +++++++++++++++++++++++++++++++++++++++++++++++++ triples | 1 + 7 files changed, 232 insertions(+), 11 deletions(-) create mode 100644 .htaccess create mode 120000 git-update create mode 100644 live-loop.js create mode 100644 live-loop.py create mode 120000 triples diff --git a/.htaccess b/.htaccess new file mode 100644 index 0000000..9f6b906 --- /dev/null +++ b/.htaccess @@ -0,0 +1,8 @@ +AddHandler mod_python .py +PythonDebug On +PythonHandler live-loop + + + SetHandler mod_python + + diff --git a/css/layout.css b/css/layout.css index 67ad62c..8582627 100644 --- a/css/layout.css +++ b/css/layout.css @@ -74,8 +74,8 @@ padding-bottom: 50px; .onblack { background: #e9922a; color: #fff; -padding-top: 50px; -padding-bottom: 30px; +padding-top: 10px; +padding-bottom: 10px; } .onblack h2 { @@ -268,6 +268,11 @@ textarea { width: 99%; } +pre { +font-size: 1em; +line-height: 1em; +} + /* Provide higher res assets for iPhone 4 */ @media only screen and (-webkit-min-device-pixel-ratio: 2) { diff --git a/git-update b/git-update new file mode 120000 index 0000000..b91965d --- /dev/null +++ b/git-update @@ -0,0 +1 @@ +live-loop.py \ No newline at end of file diff --git a/index.html b/index.html index 7773961..fbcd65b 100644 --- a/index.html +++ b/index.html @@ -18,9 +18,10 @@ - + + - +

Live Loop

@@ -28,8 +29,8 @@

Live Loop

-

Play with RDFa. - Discover something new.

+

For Dave Lehn. + With love and hugs.

@@ -42,7 +43,7 @@

Play with RDFa.
<html>
- +
</html>
@@ -51,10 +52,8 @@

Play with RDFa.
- -

Your data:

-

-

+
+
diff --git a/live-loop.js b/live-loop.js new file mode 100644 index 0000000..4145948 --- /dev/null +++ b/live-loop.js @@ -0,0 +1,59 @@ +/** + * All of the live-loop javascript code necessary to make live loop work. + */ +(function($) +{ + // create the liveloop object + window.liveloop = window.liveloop || {}; + var liveloop = window.liveloop; + + liveloop.generateCodePrologue = function() + { + return "\n\n\n \n Test Snippet\n \n "; + } + + liveloop.setupCodePrologue = function() + { + var e = $("#code-prologue"); + var encodedPrologue = $('
').text(liveloop.generateCodePrologue()).html(); + e.html(encodedPrologue); + }; + + liveloop.generateCodeEpilogue = function() + { + return " \n"; + } + + liveloop.setupCodeEpilogue = function() + { + var e = $("#code-epilogue"); + var encodedEpilogue = $('
').text(liveloop.generateCodeEpilogue()).html(); + e.html(encodedEpilogue); + }; + + liveloop.setupUi = function() + { + liveloop.setupCodePrologue(); + liveloop.setupCodeEpilogue(); + $("#code-body").keyup(liveloop.updateTriples); + }; + + liveloop.updateTriples = function(event) + { + var rdfaDocument = liveloop.generateCodePrologue() + + $("#code-body").val() + liveloop.generateCodeEpilogue(); + $.ajax({ + url: "triples", + type: "POST", + success: function(data) + { + $("#triple-data").html(data); + }, + contentType: "application/xml+xhtml", + processData: false, + data: rdfaDocument + }); + }; + +})(jQuery); + diff --git a/live-loop.py b/live-loop.py new file mode 100644 index 0000000..b6e67c0 --- /dev/null +++ b/live-loop.py @@ -0,0 +1,148 @@ +## +# This is the web service for the Live Loop RDFa editor. +# License: LGPLv3 +# +# @author Manu Sporny +import os, os.path, sys +import subprocess +import rdfa + +try: + from mod_python import apache +except Exception, e: + pass + +## +# Formats a triple in a very special way. +# +# @param request the HTTP request to use when writing the output. +# @param subject the subject of the triple. +# @param predicate the predicate for the triple. +# @param obj the object of the triple. +# @param object_type the type for the object in the triple. +# @param datatype the datatype for the object in the triple. +# @param language the language for the object in the triple. +def writeTriple2( \ + request, subject, predicate, obj, object_type, datatype, language): + request.write("<%s> <%s> " % (subject, predicate)) + if(object_type == rdfa.RDF_TYPE_IRI): + request.write("<%s> . " % (obj,)) + else: + ostr = ""%s" ." % (obj,) + if(language != None): + ostr += "@%s" % (language,) + if(datatype != None): + ostr += "^^^<%s>" % (datatype,) + request.write(ostr) + +## +# Called whenever a triple is generated for the default graph by the +# underlying implementation. +# +# @param request the HTTP request to use when writing the output. +# @param subject the subject of the triple. +# @param predicate the predicate for the triple. +# @param obj the object of the triple. +# @param object_type the type for the object in the triple. +# @param datatype the datatype for the object in the triple. +# @param language the language for the object in the triple. +def defaultTriple2( \ + request, subject, predicate, obj, object_type, datatype, language): + + request.write("
") + writeTriple2( \ + request, subject, predicate, obj, object_type, datatype, language) + request.write("
") + +## +# Called whenever a triple is generated for the processor graph by the +# underlying implementation. +# +# @param request the HTTP request to use when writing the output. +# @param subject the subject of the triple. +# @param predicate the predicate for the triple. +# @param obj the object of the triple. +# @param object_type the type for the object in the triple. +# @param datatype the datatype for the object in the triple. +# @param language the language for the object in the triple. +def processorTriple2( \ + request, subject, predicate, obj, object_type, datatype, language): + + request.write("
") + if(object_type == rdfa.RDF_TYPE_NAMESPACE_PREFIX): + request.write("%s %s: %s ." % (subject, predicate, obj)) + else: + writeTriple( \ + request, subject, predicate, obj, object_type, datatype, language) + request.write("
") + +## +# Called whenever the processing buffer for the C-side needs to be re-filled. +# +# @param data the entire file blob +# @param bufferSize the size of the buffer to return. Returning anything less +# than bufferSize will halt execution after the returned +# buffer has been processed. +def handleBuffer2(data, bufferSize): + return data.read() + +## +# The handler function is what is called whenever an apache call is made. +# +# @param req the HTTP request. +# +# @return apache.OK if there wasn't an error, the appropriate error code if +# there was a failure. +def handler(req): + # File that runs an apache test. + status = apache.OK + + puri = req.parsed_uri + service = puri[-3] + argstr = puri[-2] + args = {} + + # Retrieve all of the unit tests from the W3C website + if(service.find("/live-loop/triples") != -1): + req.content_type = 'text/html' + if(req.method == "POST"): + document = req.read() + liveLoopDir = os.path.dirname(req.canonical_filename) + p = subprocess.Popen(["python", "live-loop.py"], + bufsize=4096, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.PIPE, close_fds=True, cwd=liveLoopDir) + p.stdin.write(document) + (so, se) = p.communicate() + req.write(so) + else: + req.content_type = 'text/plain' + req.write(""" +You can POST an XHTML+RDFa document to this service. The result will be an HTML snippet that can be placed inside of a
element in an HTML page. + +For example, try the following using CURL: + +curl -d "<\!DOCTYPE html PUBLIC \\"-//W3C//DTD XHTML+RDFa 1.1//EN\\" \\"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-2.dtd\\"> Test Snippet

This HTML snippet was written by Manu Sporny.

" %s +""" % (req.construct_url(req.unparsed_uri),)) + # Perform a git update in the current directory + elif(service.find("/live-loop/git-update") != -1): + testSuitePath = os.path.dirname(req.canonical_filename) + gitUpdatePath = os.path.join(testSuitePath, ".git") + p = subprocess.Popen(["git", "--git-dir", gitUpdatePath, "pull"], + bufsize=4096, stdout=subprocess.PIPE, stderr=subprocess.PIPE, + close_fds=True, cwd=testSuitePath) + (so, se) = p.communicate() + req.write("GIT status: %s%s" % (so, se)) + else: + req.content_type = 'text/html' + req.write("ERROR: Unknown Live Loop service: %s" % \ + (service,)) + + return status + +if __name__ == "__main__": + parser = rdfa.RdfaParser("http://example.com/sample.html") + parser.setDefaultGraphTripleHandler(defaultTriple2, sys.stdout) + parser.setProcessorGraphTripleHandler(processorTriple2, sys.stdout) + parser.setBufferHandler(handleBuffer2, sys.stdin) + parser.parse() + diff --git a/triples b/triples new file mode 120000 index 0000000..b91965d --- /dev/null +++ b/triples @@ -0,0 +1 @@ +live-loop.py \ No newline at end of file