Replace TURTLE tab with N-Quads. Syntax highlighting needs improvement.
authorDave Longley <dlongley@digitalbazaar.com>
Mon, 30 Apr 2012 14:36:19 -0400
changeset 599 682fd66c2118
parent 598 d0b88ceef5ea
child 600 dd52f1782d63
Replace TURTLE tab with N-Quads. Syntax highlighting needs improvement.
common/lang-nquads.js
playground/index.html
playground/jsonld.js
playground/playground.js
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/lang-nquads.js	Mon Apr 30 14:36:19 2012 -0400
@@ -0,0 +1,82 @@
+/**
+ * @fileoverview
+ * Registers a language handler for prettify.js for N-Quads.
+ *
+ * @author David I. Lehn <[email protected]>
+ * @author Manu Sporny <[email protected]>
+ * @author Dave Longley <[email protected]>
+ */
+// FIXME: copied from turtle, needs simplification and clean up
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_ATTRIB_NAME + " nquads-curie", /^.*/]
+    ]), ['nquads-property']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_TAG + " nquads-uri", /^.*/]
+    ]), ['nquads-uri']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_TAG + " nquads-curie", /^.*/]
+    ]), ['nquads-curie']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_STRING, /^.*/]
+    ]), ['nquads-string']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_LITERAL, /^.*/]
+    ]), ['nquads-typed-literal-value']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      [PR.PR_TYPE + " nquads-curie", /^.*/]
+    ]), ['nquads-typed-literal-type']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [], [
+      // FIXME: use RE that excludes ^^ from the value
+      ["lang-nquads-typed-literal-value", /^(.*\^\^)/],
+      ["lang-nquads-typed-literal-type", /^(.*)/],
+    ]), ['nquads-typed-literal']);
+PR.registerLangHandler(
+  PR.createSimpleLexer(
+    [
+      //[PR.PR_PUNCTUATION, /^[:|>?]+/, null, ':|>?'],
+      //[PR.PR_PLAIN, /^\s+/, null, ' \t\r\n']
+    ],
+    [
+      // empty string
+      // other properties
+      ["lang-nquads-property", /^"(.*)"\^\^/],
+      //[PR.PR_LITERAL, /^""/],
+      ["lang-nquads-string", /^"([^"]*)"/],
+      // "<...>"
+      ["lang-nquads-uri", /^<([^:]+:[^>]*)>[ \t]*/],
+      ["lang-nquads-uri", /^<([^:]+:[^>]*)>\./],
+      // typed literals
+      ["lang-nquads-typed-literal", /^"([^^"]+\^\^[^"]+)"/],
+      // "foo:bar"
+      ["lang-nquads-curie", /^(_:[^ ]*)/],
+      // literal strings
+      ["lang-nquads-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+/]
+    ]), ['nquads']);
--- a/playground/index.html	Mon Apr 30 14:10:38 2012 -0400
+++ b/playground/index.html	Mon Apr 30 14:36:19 2012 -0400
@@ -16,9 +16,8 @@
       <script type="text/javascript" src="js_beautify.js"></script>
       <script type="text/javascript" src="../common/prettify.js"></script>
       <script type="text/javascript" src="../common/lang-jsonld.js"></script>
-      <script type="text/javascript" src="../common/lang-turtle.js"></script>
+      <script type="text/javascript" src="../common/lang-nquads.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>
       <script type="text/javascript" src="playground-examples.js"></script>
    </head>
@@ -86,14 +85,14 @@
                        href="#normalized"><span>Normalized</span></a></li>
                     <li><a id="tab-framed" 
                        href="#framed"><span>Framed</span></a></li>
-                    <li><a id="tab-turtle" 
-                       href="#turtle"><span>TURTLE</span></a></li>
+                    <li><a id="tab-nquads" 
+                       href="#nquads"><span>N-Quads</span></a></li>
                 </ul>
                 <pre id="compacted" class="prettyprint lang-jsonld"></pre>
                 <pre id="expanded" class="prettyprint lang-jsonld"></pre>
                 <pre id="normalized" class="prettyprint lang-jsonld"></pre>
                 <pre id="framed" class="prettyprint lang-jsonld"></pre>
-                <pre id="turtle" class="prettyprint lang-turtle"></pre>
+                <pre id="nquads" class="prettyprint lang-nquads"></pre>
             </div>
 
          </div>
--- a/playground/jsonld.js	Mon Apr 30 14:10:38 2012 -0400
+++ b/playground/jsonld.js	Mon Apr 30 14:36:19 2012 -0400
@@ -379,7 +379,7 @@
  *          to convert.
  * @param [options] the options to use:
  *          [format] the format if input is a string:
- *            'text/x-nquads' for N-Quads (default).
+ *            'application/nquads' for N-Quads (default).
  *          [notType] true to use rdf:type, false to use @type (default).
  * @param callback(err, output) called once the operation completes.
  */
@@ -396,7 +396,7 @@
 
   // set default options
   if(!('format' in options)) {
-    options.format = 'text/x-nquads';
+    options.format = 'application/nquads';
   }
   if(!('notType' in options)) {
     options.notType = false;
@@ -404,7 +404,7 @@
 
   if(_isString(statements)) {
     // supported formats
-    if(options.format === 'text/x-nquads') {
+    if(options.format === 'application/nquads') {
       statements = _parseNQuads(statements);
     }
     else {
@@ -424,7 +424,7 @@
  * @param input the JSON-LD input.
  * @param [options] the options to use:
  *          [format] the format to use to output a string:
- *            'text/x-nquads' for N-Quads (default).
+ *            'application/nquads' for N-Quads (default).
  *          [resolver(url, callback(err, jsonCtx))] the URL resolver to use.
  * @param callback(err, statement) called when a statement is output, with the
  *          last statement as null.
@@ -450,7 +450,7 @@
 
   if('format' in options) {
     // supported formats
-    if(options.format === 'text/x-nquads') {
+    if(options.format === 'application/nquads') {
       cb = function(err, statement) {
         if(err) {
           return callback(err);
--- a/playground/playground.js	Mon Apr 30 14:10:38 2012 -0400
+++ b/playground/playground.js	Mon Apr 30 14:36:19 2012 -0400
@@ -122,7 +122,7 @@
       getParameterByName('param'), 'param', 'param');
 
     var startTab = getParameterByName('startTab');
-    if (startTab) {
+    if(startTab) {
         // strip 'tab-' to get the tab's panel's I D
         $('#tabs').tabs("select", "#"+startTab.substr(4));
     }
@@ -132,8 +132,9 @@
     $.when(jsonLdDeferred, frameDeferred, contextDeferred, paramDeferred)
       .done(function() {
         // Maintain backwards permalink compatability
-        if (queryData['param'] && !(queryData['frame'] || queryData['context'])) {
-            queryData['frame'] = queryData['context'] = queryData['param'];
+        if(queryData['param'] &&
+          !(queryData['frame'] || queryData['context'])) {
+          queryData['frame'] = queryData['context'] = queryData['param'];
         }
         // populate UI with data
         playground.populateWithJSON(queryData);
@@ -159,7 +160,7 @@
     playground.activeTab = ui.tab.id;
     if(ui.tab.id === 'tab-compacted' || ui.tab.id === 'tab-framed') {
       // if the 'compact' or 'frame' tab is selected, display the appropriate
-      // input textarea 
+      // input textarea
       $('#markup').addClass('compressed');
 
       if(ui.tab.id === 'tab-compacted') {
@@ -239,13 +240,18 @@
         callback();
       });
     }
-    else if(playground.activeTab === 'tab-turtle') {
-      jsonld.turtle(input, options, function(err, turtle) {
+    else if(playground.activeTab === 'tab-nquads') {
+      options.format = 'application/nquads';
+      var nquads = '';
+      jsonld.toRDF(input, options, function(err, quad) {
         if(err) {
           return callback(err);
         }
-        $('#turtle').html(playground.htmlEscape(turtle));
-        callback();
+        if(quad === null) {
+          $('#nquads').html(playground.htmlEscape(nquads));
+          return callback();
+        }
+        nquads += quad;
       });
     }
   };
@@ -286,7 +292,7 @@
         toValidate = $('#frame').val();
         needParam = true;
     }
-    
+
     if (needParam) {
         try {
           var param = JSON.parse(toValidate);
@@ -317,7 +323,7 @@
       // generate a link for current data
       var link = '?json-ld=' + encodeURIComponent(JSON.stringify(input));
       if($('#frame').val().length > 0) {
-        link += '&frame=' + 
+        link += '&frame=' +
             encodeURIComponent($("#frame").val());
       }
       if($('#context').val().length > 0) {
@@ -325,7 +331,7 @@
             encodeURIComponent($("#context").val());
       }
 
-      // Start at the currently active tab 
+      // Start at the currently active tab
       link += '&startTab=' + encodeURIComponent(playground.activeTab);
 
       var permalink = '<a href="' + link + '">permalink</a>';
@@ -402,7 +408,7 @@
 
     if('context' in data && data.context !== null) {
       hasData = true;
-      // fill the context input box with the given context 
+      // fill the context input box with the given context
       $('#context').val(js_beautify(
         data.context, {'indent_size': 2, 'brace_style': 'expand'}));
     }
@@ -436,12 +442,12 @@
       data.markup = JSON.stringify(playground.examples[name]);
 
       if(name in playground.frames) {
-        // fill the frame with the example 
+        // fill the frame with the example
         data.frame = JSON.stringify(playground.frames[name]);
       }
 
       if(name in playground.contexts) {
-        // fill the context with the example 
+        // fill the context with the example
         data.contexts = JSON.stringify(playground.contexts[name]);
       }
       else if('@context' in playground.examples[name]) {