Completed JSON-LD -> TURTLE serialization. Added TURTLE syntax highlighting.
authorManu Sporny <msporny@digitalbazaar.com>
Fri, 08 Jul 2011 23:57:43 -0400
changeset 55 ca55facec680
parent 54 7dbe2a8f87fe
child 56 610e29eef20f
Completed JSON-LD -> TURTLE serialization. Added TURTLE syntax highlighting.
playground/index.html
playground/jsonld-turtle.js
playground/lang-turtle.js
playground/playground-examples.js
playground/playground.css
playground/playground.js
playground/prettify.css
--- a/playground/index.html	Fri Jul 08 16:52:23 2011 -0400
+++ b/playground/index.html	Fri Jul 08 23:57:43 2011 -0400
@@ -21,6 +21,7 @@
       <script type="text/javascript" src="js_beautify.js"></script>
       <script type="text/javascript" src="prettify.js"></script>
       <script type="text/javascript" src="lang-jsonld.js"></script>
+      <script type="text/javascript" src="lang-turtle.js"></script>
       <script type="text/javascript" src="jsonld.js"></script>
       <script type="text/javascript" src="jsonld-turtle.js"></script>
       <script type="text/javascript" src="playground.js"></script>
@@ -76,9 +77,9 @@
                   onkeyup="playground.process()"></textarea>
             </div>
 
-            <div id="markup-errors"></div>
+            <div id="markup-errors" class="errors"></div>
 
-            <div id="frame-errors"></div>
+            <div id="frame-errors" class="errors"></div>
 
             <div id="tabs">
                 <ul>
@@ -97,7 +98,7 @@
                 <pre id="expanded" class="prettyprint language-jsonld"></pre>
                 <pre id="normalized" class="prettyprint language-jsonld"></pre>
                 <pre id="framed" class="prettyprint language-jsonld"></pre>
-                <pre id="turtle" class="prettyprint language-ttl"></pre>
+                <pre id="turtle" class="prettyprint language-turtle"></pre>
             </div>
 
          </div>
--- a/playground/jsonld-turtle.js	Fri Jul 08 16:52:23 2011 -0400
+++ b/playground/jsonld-turtle.js	Fri Jul 08 23:57:43 2011 -0400
@@ -50,9 +50,24 @@
 {
    var rval = undefined;
 
-   // FIXME: Implement printing out arrays of objects
-
-   if(typeof(obj) == "object")
+   if(obj instanceof Array)
+   {
+      var firstItem = true;
+      for(i in obj)
+      {
+         if(firstItem == true)
+         {
+            firstItem = false;
+            rval = "\n      ";
+         }
+         else
+         {
+            rval += ",\n      ";
+         }
+         rval += printObject(obj[i]);
+      }
+   }
+   else if(obj instanceof Object)
    {
       if("@literal" in obj && "@datatype" in obj)
       {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/playground/lang-turtle.js	Fri Jul 08 23:57:43 2011 -0400
@@ -0,0 +1,90 @@
+/**
+ * @fileoverview
+ * Registers a language handler for TURTLE.
+ *
+ * @author Manu Sporny <msporny@digitalbazaar.com>
+ * @author David I. Lehn <dlehn@digitalbazaar.com>
+ */
+
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_KEYWORD, /^.*/]
+    ]), ['turtle-keyword']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_ATTRIB_NAME + " turtle-curie", /^.*/]
+    ]), ['turtle-property']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_TAG + " turtle-uri", /^.*/]
+    ]), ['turtle-uri']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_TAG + " turtle-curie", /^.*/]
+    ]), ['turtle-curie']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_STRING, /^.*/]
+    ]), ['turtle-string']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_LITERAL, /^.*/]
+    ]), ['turtle-typed-literal-value']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_TYPE + " turtle-curie", /^.*/]
+    ]), ['turtle-typed-literal-type']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      // FIXME: use RE that excludes ^^ from the value
+      ["lang-turtle-typed-literal-value", /^(.*\^\^)/],
+      ["lang-turtle-typed-literal-type", /^(.*)/],
+    ]), ['turtle-typed-literal']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [
+      //[PR.PR_PUNCTUATION, /^[:|>?]+/, null, ':|>?'],
+      //[PR.PR_PLAIN, /^\s+/, null, ' \t\r\n']
+    ],
+    [
+      // common "keywords"
+      ["lang-turtle-keyword", /^"(@prefix)"\s*:/],
+      // empty string
+      // other properties
+      ["lang-turtle-property", /^"(.*)"\^\^/],
+      //[PR.PR_LITERAL, /^""/],
+      ["lang-turtle-string", /^"([^"]*)"/],
+      // "<...>"
+      ["lang-turtle-uri", /^<(http:.*)> /],
+      ["lang-turtle-uri", /^<(http:.*)>\./],
+      ["lang-turtle-uri", /^<(http:.*)>\;/],
+      ["lang-turtle-uri", /^<(http:.*)>/],
+      // typed literals
+      ["lang-turtle-typed-literal", /^"([^^"]+\^\^[^"]+)"/],
+      // "foo:bar"
+      ["lang-turtle-curie", /^(_:[^ ]*)/],
+      // literal strings
+      ["lang-turtle-string", /^""/],
+      // constants and native types
+      [PR.PR_LITERAL, /^(?:true|false|null|undefined)/],
+      [PR.PR_LITERAL,
+       new RegExp(
+           '^(?:'
+           // A hex number
+           + '0x[a-f0-9]+'
+           // or an octal or decimal number,
+           + '|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)'
+           // possibly in scientific notation
+           + '(?:e[+\\-]?\\d+)?'
+           + ')', 'i'),
+       null, '0123456789'],
+      [PR.PR_PLAIN, /^\w+/]
+    ]), ['turtle']);
--- a/playground/playground-examples.js	Fri Jul 08 16:52:23 2011 -0400
+++ b/playground/playground-examples.js	Fri Jul 08 23:57:43 2011 -0400
@@ -93,6 +93,7 @@
       "@context":
       {
          "gr": "http://purl.org/goodrelations/v1#",
+         "pto": "http://www.productontology.org/id/",
          "foaf": "http://xmlns.com/foaf/0.1/",
          "xsd": "http://www.w3.org/2001/XMLSchema#",
          "@coerce":
--- a/playground/playground.css	Fri Jul 08 16:52:23 2011 -0400
+++ b/playground/playground.css	Fri Jul 08 23:57:43 2011 -0400
@@ -23,6 +23,11 @@
     width: 100%;
 }
 
+.errors {
+    font-style: bold;
+    color: red;
+}
+
 li.button-list
 {
     display: block;
--- a/playground/playground.js	Fri Jul 08 16:52:23 2011 -0400
+++ b/playground/playground.js	Fri Jul 08 23:57:43 2011 -0400
@@ -43,46 +43,51 @@
    {
       var input = null;
       var frame = null;
+      var errors = false;
 
       try
       {
-         var input = JSON.parse($("#markup").val());
+         $("#markup-errors").text("");
+         input = JSON.parse($("#markup").val());
       }
       catch(e)
       {
-         console.log(e);
-         $("#markup-errors").html(e);
+         $("#markup-errors").text("JSON markup - " + e);
+         errors = true;
       }
 
-
       try
       {
+         $("#frame-errors").text("");
          var frame = JSON.parse($("#frame").val());
       }
       catch(e)
       {
-         console.log(e);
-         $("#frame-errors").html(e);
+         $("#frame-errors").text("JSON-LD frame - " + e);
+         errors = true;
       }
 
-      var normalized = forge.jsonld.normalize(input);
-      var expanded = forge.jsonld.removeContext(input);
-      var compacted = forge.jsonld.changeContext(
-         input["@context"] || {}, input);
-      var framed = forge.jsonld.frame(input, frame);
-      var turtle = forge.jsonld.turtle(input);
+      if(!errors)
+      {
+         var normalized = forge.jsonld.normalize(input);
+         var expanded = forge.jsonld.removeContext(input);
+         var compacted = forge.jsonld.changeContext(
+            input["@context"] || {}, input);
+         var framed = forge.jsonld.frame(input, frame);
+         var turtle = forge.jsonld.turtle(input);
 
-      $("#normalized").html(js_beautify(JSON.stringify(normalized)),
-         { "indent_size": 3, "brace_style": "expand" });
-      $("#compacted").html(js_beautify(JSON.stringify(compacted)),
-         { "indent_size": 3, "brace_style": "expand" });
-      $("#expanded").html(js_beautify(JSON.stringify(expanded)),
-         { "indent_size": 3, "brace_style": "expand" });
-      $("#framed").html(js_beautify(JSON.stringify(framed)),
-         { "indent_size": 3, "brace_style": "expand" });
-      $("#turtle").html(playground.htmlEscape(turtle));
+         $("#normalized").html(js_beautify(JSON.stringify(normalized)),
+            { "indent_size": 3, "brace_style": "expand" });
+         $("#compacted").html(js_beautify(JSON.stringify(compacted)),
+            { "indent_size": 3, "brace_style": "expand" });
+         $("#expanded").html(js_beautify(JSON.stringify(expanded)),
+            { "indent_size": 3, "brace_style": "expand" });
+         $("#framed").html(js_beautify(JSON.stringify(framed)),
+            { "indent_size": 3, "brace_style": "expand" });
+         $("#turtle").html(playground.htmlEscape(turtle));
 
-      prettyPrint();
+         prettyPrint();
+      }
    }
    
    playground.populate = function(type)
--- a/playground/prettify.css	Fri Jul 08 16:52:23 2011 -0400
+++ b/playground/prettify.css	Fri Jul 08 23:57:43 2011 -0400
@@ -41,6 +41,14 @@
 {
     color: #8A93CD;
 }
+.tag.turtle-uri
+{
+    color: #8A93CD;
+}
+.tag.turtle-curie
+{
+    color: #AC84CB;
+}
 .atn
 {
     color: #E0E2E4;
@@ -49,6 +57,10 @@
 {
     color: #AC84CB;
 }
+.atn.turtle-curie
+{
+    color: #FACD22;
+}
 .atv
 {
     color: #EC7600;