<?xml version="1.0"?>

<!-- This stylesheet was adapted by Joe Orr from Default.xsl distributed with Apache Xalan -->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
                xmlns="http://www.w3.org/TR/REC-html40">
                
<xsl:output method="html" indent="yes" encoding="iso-8859-1" />
<xsl:strip-space elements="*"/>

<!-- Main Template -->
<xsl:template match="/">
  <HTML>
    <HEAD>
      <STYLE type="text/css">
        BODY {font:x-small 'Verdana'; margin-right:1.5em}
        PRE {margin:0px; display:inline}
        .code {font-weight:bold; font-family:'Courier New';}
        .keyword {color:blue}
        .codeComment {color:red}
      <!-- container for expanding/collapsing content -->
        .c  {cursor:hand; margin-top:0em; margin-bottom:0em}
      <!-- button - contains +/-/nbsp -->
        .b  {color:red; font-family:'Courier New'; font-weight:bold; text-decoration:none}
      <!-- element container -->
        .e  {margin-left:1em; text-indent:-1em; margin-right:1em; margin-top:0em; margin-bottom:0em}
      <!-- comment or cdata -->
        .k  {margin-left:1em; text-indent:-1em; margin-right:1em}
      <!-- tag -->
        .t  {color:#990000}
      <!-- tag in xsl namespace -->
        .xt {color:#990099}
      <!-- attribute in xml or xmlns namespace -->
        .ns {color:red}
      <!-- markup characters -->
        .m  {color:blue}
      <!-- text node -->
        .tx {font-weight:bold}
      <!-- multi-line (block) cdata -->
        .db {text-indent:0px; margin-left:1em; margin-top:0px; margin-bottom:0px;
             padding-left:.3em; border-left:1px solid #CCCCCC; font:small Courier}
      <!-- single-line (inline) cdata -->
        .di {font:small Courier}
      <!-- DOCTYPE declaration -->
        .d  {color:blue}
      <!-- pi -->
        .pi {color:blue}
      <!-- multi-line (block) comment -->
        .cb {text-indent:0px; margin-left:1em; margin-top:0px; margin-bottom:0px;
             padding-left:.3em; font:small Courier; color:#888888}
      <!-- single-line (inline) comment -->
        .ci {font:small Courier; color:#888888}
      </STYLE>

      <SCRIPT type="text/javascript"><xsl:comment><![CDATA[
        // Detect and switch the display of CDATA and comments from an inline view
        //  to a block view if the comment or CDATA is multi-line.
        function f(e) {
          // if this element is an inline comment, and contains more than a single
          //  line, turn it into a block comment.
          if (e.className == "ci") {
            if (e.children(0).innerText.indexOf("\n") > 0)
              fix(e, "cb");
          }
          
          // if this element is an inline cdata, and contains more than a single
          //  line, turn it into a block cdata.
          if (e.className == "di") {
            if (e.children(0).innerText.indexOf("\n") > 0)
              fix(e, "db");
          }
          
          // remove the id since we only used it for cleanup
          e.id = "";
        }
        
        // Fix up the element as a "block" display and enable expand/collapse on it
        function fix(e, cl) {
          // change the class name and display value
          e.className = cl;
          e.style.display = "block";
          
          // mark the comment or cdata display as a expandable container
          j = e.parentElement.children(0);
          j.className = "c";

          // find the +/- symbol and make it visible - the dummy link enables tabbing
          k = j.children(0);
          k.style.visibility = "visible";
          k.href = "#";
        }

        // Change the +/- symbol and hide the children.  This function works on "element"
        //  displays
        function ch(e) {
          // find the +/- symbol
          mark = e.children(0).children(0);
          
          // if it is already collapsed, expand it by showing the children
          if (mark.innerText == "+")
          {
            mark.innerText = "-";
            for (var i = 1; i < e.children.length; i++)
              e.children(i).style.display = "block";
          }
          
          // if it is expanded, collapse it by hiding the children
          else if (mark.innerText == "-")
          {
            mark.innerText = "+";
            for (var i = 1; i < e.children.length; i++)
              e.children(i).style.display="none";
          }
        }

        function close(mark) {
        
          if (mark.parentElement == null) return;
        
          mark.innerText = "+";
          for (var i = 1; i < mark.parentElement.parentElement.children.length; i++) {
            mark.parentElement.parentElement.children(i).style.display="none";
          }
        }
        function open(mark) {
        
          if (mark.parentElement == null) return;
        
          mark.innerText = "-";
          for (var i = 1; i < mark.parentElement.parentElement.children.length; i++) {
            mark.parentElement.parentElement.children(i).style.display="block";
          }
        }

        
        // Change the +/- symbol and hide the children.  This function works on "comment"
        //  and "cdata" displays
        function ch2(e) {
          // find the +/- symbol, and the "PRE" element that contains the content
          mark = e.children(0).children(0);
          contents = e.children(1);
          
          // if it is already collapsed, expand it by showing the children
          if (mark.innerText == "+")
          {
            mark.innerText = "-";
            // restore the correct "block"/"inline" display type to the PRE
            if (contents.className == "db" || contents.className == "cb")
              contents.style.display = "block";
            else contents.style.display = "inline";
          }
          
          // if it is expanded, collapse it by hiding the children
          else if (mark.innerText == "-") {
            mark.innerText = "+";
            contents.style.display = "none";
          }
        }
        
        // Handle a mouse click
        function cl() {
          e = window.event.srcElement;
          
          // make sure we are handling clicks upon expandable container elements
          if (e.className != "c") {
            e = e.parentElement;
            if (e.className != "c"){
              return;
            }
          }
          e = e.parentElement;
          
          // call the correct funtion to change the collapse/expand state and display
          if (e.className == "e")
            ch(e);
          if (e.className == "k")
            ch2(e);
        }
        
        // Erase bogus link info from the status window
        function h()
        {
          window.status=" ";
        }

        // Set the onclick handler
        document.onclick = cl;
        
        function closeAll() {

          var sections = document.all.tags("A");
          for(i=0; i < sections.length; i++) {
            if (sections(i).className == "b") {
              close(sections(i)); 
            }
          }

        }
        
        function openAll() {

          var sections = document.all.tags("A");
          for(i=0; i < sections.length; i++) {
            if (sections(i).className == "b") {
              open(sections(i)); 
            }
          }
        }
      
        function formatText(item) {
          
          var txt = item.innerHTML;

          // line breaks (XSL has weaker string manipulation routines than
          // Javascript, so the XSL code below just substitutes \x95 for \n.
          // The next two lines substitute "<br>" for \x95).
          
          // following line doesn't work (why?) so had to use '~~' instead of \x95 -->
          //re = /\x95/gm;

          // kludge to get rid of extra blank lines that seem to keep showing up 
          re = /~~~/gm;
          txt = txt.replace(re, "");

          re = /~*@code~*/gm;
          txt = txt.replace(re, '~@code~');

          // OK, now let's get rid of those '~' we put in place of LFs 
          re = /~/gm;
          txt = txt.replace(re, "<br>");

          // fix spaces (same situation as above, XSL code below substitutes 
          // "&nbsp;" for all spaces. Next two lines replace these with \x20
          // where appropriate)
          re = /&nbsp;(?!&nbsp;)/gm;
          txt = txt.replace(re, " ");

          var docsFlag = false;
          if (txt.indexOf('@docs', 0) != -1) docsFlag = true;

          // get rid of the "@docs" directive
          re = /@docs.*?<br>/g;
          txt = txt.replace(re, "");

          //re = /<br>.*?@code/;
          //txt = txt.replace(re, "");

          // format the code line by line
          var i = txt.indexOf('@code', 0);

          if (i != -1) {
            var txt1 = txt.substring(0, i - 1);
            var txt2 = txt.substring(i + 5);

            var b = txt2.indexOf('<br>');
            var b2;
            var line = '';
            var allLines = '';
            var lineLeft = '';
            var lineRight = '';
            while (b != -1) {
            
              line = txt2.substring(0, b + 4);
              txt2 = txt2.substring(b + 4);

              b = txt2.indexOf('<br>');

              if (line.indexOf('&gt;&gt;') != -1) {
                re = new RegExp("&lt;&lt;(.*)&gt;&gt;", "");
                var arr = re.exec(line);
                var s = RegExp.$1;
                re = / /g;
                s = s.replace(re, "_");
                re = /^_+/g;
                s = s.replace(re, "");
                re = /_+$/g;
                s = s.replace(re, "");
                s = "<a href='#AA_" + s + "_AA'>";
                allLines = allLines + s + line + "</a><br>";
                continue;
              }

              //syntax hiliting
              re = /(close\W|for\W|foreach\W|if\W|local\W|my\W|no\W|next\W|open\W|package\W|return\W|strict\W|sub\W|unless\W|use\W|while\W)/g;
              line = line.replace(re, "<span class='keyword'>$1</span>");


              // colorize the comments
              b2 = line.indexOf("#");
              if (b2 != -1) {
                lineLeft = line.substring(0, b2 - 1);
                lineRight = line.substring(b2);
                re = /<span.*?>/g;
                lineRight = lineRight.replace(re, "");
                re = /<\/span.*?>/g;
                lineRight = lineRight.replace(re, "");              
                line = lineLeft + "<span class='codeComment'>" + lineRight + "</span>";

              }

              // colorize string literals
              allLines = allLines + "<span class='code'>" + line + "</span>";

            }
          
            //re = /(close\W|for\W|foreach\W|if\W|local\W|my\W|no\W|next\W|open\W|package\W|return\W|strict\W|sub\W|unless\W|use\W|while\W)/g;
            //txt2 = txt2.replace(re, "<span class='keyword'>$1</span>");

            //re = /(#.*?)<span.*?>(.*?)<\/span>(.*?)<br>/g;
            //txt2 = txt2.replace(re, "$1$2$3<br>");

            //re = /(@code.*)$/;
            //txt = txt.replace(re, "<span class='code'>$1</span>");

            if (docsFlag) {
              return txt1 + "<br><br>" + allLines + "<br>";
            } else {
              return txt1 + "" + allLines + "<br>";
            }
          } else {
            return txt;
          }
        }
      
        function format() {

          var sections = document.all.tags("SPAN");
          for(i=0; i < sections.length; i++) {
            if (sections(i).className == "leoText") {
               sections(i).innerHTML = formatText(sections(i));
            }
          }
          //closeAll();
        }
        
      ]]>//</xsl:comment></SCRIPT>
    </HEAD>

    <BODY class="st" onload="format()">
    <p>
    <a href="javascript:closeAll()" class="c">- All</a> &#160;
    <a href="javascript:openAll()" class="c">+ All</a>
    </p>

    <xsl:apply-templates select="/leo_file/vnodes/v/vh" />
    
    </BODY>

  </HTML>
</xsl:template>

<xsl:template match="vh">
  <DIV class="e">
  <DIV class="c" STYLE="margin-left:1em;text-indent:-2em">
    <A href="#" onclick="return false" onfocus="h()" class="b">-</A>
    <A name="{translate(., ' &lt;&gt;','_AA')}"></A> 
    <xsl:text> </xsl:text>
    <SPAN class="t"><xsl:value-of select="."/></SPAN>
  </DIV>
  <xsl:variable name="textID" select="../@t">
  </xsl:variable>
  <xsl:apply-templates select="//t[@tx=$textID]"/>
  <xsl:apply-templates select="../v/vh"/>
  </DIV>
</xsl:template>

<xsl:template match="t">
<br />
<span class='leoText'>
<!-- replace LF with '~' and replace spaces with #A0 (which will be output as &nbsp;, bc this is html output)-->
<xsl:value-of select="translate(., '&#10;&#32;', '~&#xA0;')"/>
<br />
<br />
</span>
</xsl:template>

</xsl:stylesheet>