function(head, req) {
  provides('json',
    function() {
      var row;
      var urls = [];
      while(row = getRow()) {
        urls.push({
          "body": {
            "url": row.id,
            "nav_label": row.value
          },
          // TODO: properly build children; rather than flat list
          "children": false
        });
      }
      return JSON.stringify({urls: urls});
    }
  );
}

@@ -97,32 +97,355 @@
     
-while(row = getRow()
+var url = %7B%7D;%0A      var previous_locations = %5B%5D;%0A%0A      /**%0A       * @param object url%0A       * @parem string parent_url%0A       *%0A       * @return void Adds to the global urls variable%0A       **/%0A      function addToTree(url, parent_url) %7B%0A        var total;%0A        // we have a top-level element%0A        if (previous_locations.length === 0
 ) %7B%0A
+  
     
@@ -458,17 +458,21 @@
 ls.push(
-%7B
+url);
 %0A       
@@ -478,196 +478,863 @@
     
-%22body%22: %7B%0A            %22url%22: row.id,%0A            %22nav_label%22: row.value%0A          %7D,%0A          // TODO: properly build children; rather than flat list%0A          %22children%22: false%0A        %7D
+previous_locations.push(urls%5Burls.length-1%5D);%0A        %7D else if (parent_url !== previous_locations%5Bprevious_locations.length-1%5D.body.url) %7B%0A          // work our way back to the top of the tree%0A          previous_locations.pop();%0A          // tray a branch higher%0A          addToTree(url, parent_url);%0A        %7D else %7B%0A          // once we have a match, add it to the tree%0A          total = previous_locations%5Bprevious_locations.length-1%5D.children.push(url);%0A          previous_locations.push(previous_locations%5Bprevious_locations.length-1%5D.children%5Btotal-1%5D);%0A        %7D%0A      %7D%0A%0A      while(row = getRow()) %7B%0A        url = %7B%0A          %22body%22: %7B%0A            %22url%22: row.id,%0A            %22nav_label%22: row.value%0A          %7D,%0A          %22children%22: %5B%5D%0A        %7D;%0A%0A        parent_url = row.id.substr(0, row.id.lastIndexOf('/'));%0A        addToTree(url, parent_url
 );%0A 

Make sitemap hierarchical