<!DOCTYPE html> <html>

<head>
  <meta charset="UTF-8"/>
  <meta http-equiv="X-UA-Compatible" content="IE=EDGE"/>

  <title>Documentation</title>

  <style>
    body {
      counter-reset: h1counter;
    }
    h1 { font-size: 1.5em; counter-reset: h2counter; }
    h2 { font-size: 1.2em; }
    h3 { font-size: 1.0em }
    h1:before {
        content: counter(h1counter) ".\0000a0\0000a0";
        counter-increment: h1counter;
    }
    h2:before {
      content: counter(h1counter) "." counter(h2counter) ".\0000a0\0000a0";
      counter-increment: h2counter;
    }
    .code {
      border: 1px dashed #425d73; background-color: #DEE6EE; margin:0px; padding: 0.5em 1em; font-family: monospace; font-size:0.9em;
    }
  </style>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.5.0/lodash.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.2/marked.min.js"></script>
  <script src="https://cdn.rawgit.com/vkiryukhin/vkBeautify/master/vkbeautify.js"></script>
  <script>//<![CDATA[
    $.fn.serializeXML = function(){
      var out = '';
      if (typeof XMLSerializer == 'function') {
        var xs = new XMLSerializer();
        this.each(function() {
          out += xs.serializeToString(this);
        });
      } else if (this[0] && this[0].xml != 'undefined') {
        this.each(function() {
          out += this.xml;
        });
      }
      return out;
    };

    function docChildren(path,name,data){
      var ret= [];
      var subject = path ? $(path,data) : $(data);
      subject.children().each(function(k,ele){
        if (ele.localName == name && ele.namespaceURI == "http://riddl.org/ns/documentation/1.0") {
          ret.push(ele);
        }
      });
      return $(ret);
    }

    function codeFormat(temp) {
      temp = temp.replace(/\&/g,"&amp;").replace(/\"/g,"&quot;").replace(/>/g,"&gt;").replace(/</g,"&lt;").replace(/'/g,"&apos;");
      temp = temp.replace(/\t/g,'  ');
      temp = temp.replace(/\r/g,'');
      temp = temp.replace(/^$/g,' ');
      temp = temp.replace(/^ */gm,function mf(m){
        var tm = (m.length + 2) * 0.6 + 2 * 0.6;
        return "<div style='text-indent:-" + tm + "em;margin-left:" + tm + "em'>" + (Array(m.length + 1).join("&#160;"));
      });
      temp = temp.replace(/  /g," &#160;");
      temp = temp.replace(/\n/g,"</div>\n");
      temp += "</div>\n";
      temp = temp.replace(/href=&quot;([^&]+)&quot;/g,'href=&quot;<a href="/__doc_includes/$1">$1</a>&quot;')
      return temp;
    }

    $(document).ready(function(){
      var rpath = "<%= @h['RIDDL_DECLARATION_PATH'] %>";
      var pathele = _.compact(rpath.split('/'));

      var t = $('#breadcrumblink')[0];
      var link = "";
      _.each(pathele,function(e){
        link = link + "/" + e;
        var clone = document.importNode(t.content, true);
        $('a',clone).attr('href',link + "/?doc=true");
        $('a',clone).text(e);
        $('#breadcrumb').append(clone);
      });

      $.ajax({
        url: rpath + "?riddl-resource-description",
        success: function(data){
          $('#main').append(marked(docChildren('description > resource','documentation',data).text()));

          var subr = $('description > resource > resource',data);
          if (subr.length == 0) {
            $('#subresources').remove();
          } else {
            $('description > resource > resource',data).each(function(k,ele){
              var sum = docChildren(null,'documentation',ele).attr('summary');
              var exa = docChildren(null,'example',ele);
                  exa = exa.length == 0 ? null : exa.text();
              if (exa) {
                var rell = exa;
                if ($(ele).attr('relative')) {
                  var relt = exa + ' (' + $(ele).attr('relative') + ')';
                } else {
                  var relt = '{} (any string possible)';
                }
              } else {
                var rell = $(ele).attr('relative');
                var relt = _.trim(rell);
              }
              var t = $('#subresourceitem')[0];
              var clone = document.importNode(t.content, true);
              $('a',clone).attr('href','./' + rell + '/?doc=true');
              if (relt != "") {
                $('a',clone).text(relt);
              }
              if (sum) { $('.content',clone).text(sum); }
              $('#subresources .anchor').append(clone);
            });
          }

          var oprs = $('description > resource > get, description > resource > post, description > resource > put, description > resource > patch, description > resource > delete, description > resource > request',data);
          if (oprs.length == 0) {
            $('#operations').remove();
          } else {
            oprs.each(function(k,ele){
              var sum = docChildren(null,'documentation',ele).attr('summary');
              var details = docChildren(null,'documentation',ele).text();
              var example =  docChildren(null,'example',ele);
              var ex = null;

              var tin = $(ele).attr('in');
              var tout = $(ele).attr('out');
              var whatl = $(ele).prop('tagName');
              var whatd = tin == "*" ? '' : '( ' + tin + ' )';
                  whatd += tout ? ' : ' +  tout : '';

              var tinmess = "";
              if (tin && tin != "*") {
                $('description > message[name="' + tin + '"]',data).each(function(k,mess){
                  tinmess = mess;
                  _.each(mess.attributes,function(attr){
                    if (attr.localName == 'example' && attr.namespaceURI == "http://riddl.org/ns/documentation/1.0") {
                      ex = attr.nodeValue;
                    }
                  });
                });
              }
              if (example.length > 0) { ex = example.text(); }

              var toutmess = "";
              if (tout && tout != "*") {
                $('description > message[name="' + tout + '"]',data).each(function(k,mess){
                  toutmess = mess;
                });
              }

              // Overview
              var t = $('#operationitem')[0];
              var clone = document.importNode(t.content, true);
              $('.what .link',clone).text(whatl);
              $('.what .link',clone).attr('href','#' + (whatl + '_' + whatd).replace(/ /g,'_'));
              $('.what .details',clone).text(whatd);
              if (ex != null) {
                if (ex == "")
                  $('.what .try a',clone).attr('href',window.location.pathname);
                else
                  $('.what .try a',clone).attr('href','?' + ex);
              } else {
                $('.what .try',clone).remove();
              }
              if (sum) { $('.content',clone).text(sum); }
              $('#operations .anchor').append(clone);

              //Details
              var t = $('#operationsec')[0];
              var clone = document.importNode(t.content, true);
              $('.what',clone).text(whatl + ' ' + whatd);
              $('.what',clone).attr('id',(whatl + '_' + whatd).replace(/ /g,'_'));
              if (_.trim(details) == "") {
                $('.text',clone).text(sum);
              } else {
                $('.text',clone).append(marked(details));
              }
              if (ex != null) {
                if (ex == "") {
                  $('.try a',clone).attr('href',window.location.pathname);
                } else {
                  $('.try a',clone).attr('href','?' + ex);
                  $('.try a',clone).text('?' + ex);
                }
              } else {
                $('.try',clone).remove();
              }

              if (tin && tin != "*") {
                $('.input .message',clone).html(codeFormat(vkbeautify.xml($(tinmess).serializeXML(),'  ')));
              } else {
                $('.input',clone).remove();
              }
              if (tout) {
                $('.output .message',clone).html(codeFormat(vkbeautify.xml($(toutmess).serializeXML(),'  ')));
              } else {
                $('.output',clone).remove();
              }
              $('#operations').append(clone);
            });
          }
        }
      });
    });
  //]]></script>
</head>
<body>
  <template id="breadcrumblink"><span> / </span><a href=""></a></template>
  <template id="subresourceitem"><li><div class='what'><a class='details' href=''>Example missing</a></div><div class="content"></div></li></template>
  <template id="operationitem"><li><div class='what'><a class='link' href=''></a> <span class='details'></span> <span class='try'>(<a href=''>Try it!</a>)</span></div><div class="content"></div></li></template>
  <template id="operationsec">
    <section>
      <h2 class="what" id=""></h2>
      <p class="try">Example: <a href=""><i>no parameters</i></a></p>
      <p class="text"></p>
      <div class="input">
        <h3>Input</h3>
        <div class='message code'></div>
      </div>
      <div class="output">
        <h3>Output</h3>
        <div class='message code'></div>
      </div>
    </section>
  </template>

  <nav id="breadcrumb">[<a href="/">TOP</a>]</nav>
  <section id="main"></section>
  <section id="subresources">
    <h1>Sub-Resources</h1>
    <p>From here on you can go to the following sub resources:</p>
    <ul class="anchor"></ul>
  </section>
  <section id="operations">
    <h1>Operations</h1>
    <section>
      <p>For this resource the following operations are possible:</p>
      <ul class="anchor"></ul>
    </section>
  </section>
</body>

</html>