<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <html>
      <xsl:apply-templates select="langs/head"/>
      <xsl:apply-templates select="langs/body"/>
    </html>
  </xsl:template>

  <xsl:template match="head">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="author" content="Gan Uesli Starling" />
    <meta name="copyright" content="2003, Gan Uesli Starling" />
    <meta name="description" content="Languages: Python, Perl." />
    <meta name="keywords" content="starling, python, perl" />
    <link rel="stylesheet" type="text/css" href="../starling.css" />
    <base target="_blank" />
    <style type="text/css">
    span.op {color:crimson;font-family:monospace;font-weight:bold;}
    span.in {color:blue;font-family:monospace;font-weight:normal;}
    span.mod {color:gray;font-family:monospace;font-weight:normal;}
    span.out {color:purple;font-family:monospace;font-weight:normal;}
    span.arg {color:brown;font-family:monospace;font-weight:normal;}
    td.code {color:gray;font-family:monospace;font-weight:normal;}
    </style>
    </head>
  </xsl:template>

  <xsl:template match="body">
    <body>
      <xsl:apply-templates select="lang" />
    </body>
  </xsl:template>

  <xsl:template match="lang">
    <body>
      <hr/>
      <xsl:for-each select="type">
        <xsl:sort select="name" data-type="text"/>
        <h1 class="left">
          <xsl:value-of select="../name"/> :
          <xsl:value-of select="name"/>
        </h1>
        <table cellpadding="5" width="100%" border="1">
          <xsl:call-template name="clues"/>
        </table>
        <br />
      </xsl:for-each>
    </body>
  </xsl:template>

  <xsl:template name="type">
  </xsl:template>

  <xsl:template name="clues">
    <tr>
      <th align="left">Syntax</th> 
      <th align="left">Does</th>
      <th align="left">Related</th>
    </tr>
    <xsl:for-each select="clue">
      <xsl:sort select="syn/op" data-type="text"/>
      <tr>
        <xsl:apply-templates/> 
      </tr>
    </xsl:for-each>
  </xsl:template>

  <xsl:template match="syn">
    <td align="left" valign="middle" width="40%" class="code">
      <xsl:apply-templates/>
    </td>
  </xsl:template>

  <xsl:template match="does">
    <td align="left" valign="middle" width="40%">
      <xsl:apply-templates/>
    </td>
  </xsl:template>

  <xsl:template match="see">
    <td align="left" valign="middle" width="20%">
      <xsl:apply-templates/>
    </td>
  </xsl:template>

  <xsl:template match="op">
    <xsl:variable  name="lh_text">
      <xsl:value-of select="text()[1]"/>
    </xsl:variable>
    <xsl:choose>
      <xsl:when test="contains($lh_text,'.')">
        <span class="mod">
          <xsl:value-of select="substring-before ($lh_text, '.')"/>
          <xsl:text>.</xsl:text>
        </span>
        <span class="op">
          <xsl:value-of select="substring-after ($lh_text, '.')"/>
          <xsl:apply-templates select="*[not(self::op)]"/>
		      <xsl:value-of select="text()[2]"/>
        </span>
      </xsl:when>
      <xsl:otherwise>
        <span class="op">
          <xsl:apply-templates/>
        </span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="in">
    <span class="in">
       <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="out">
    <span class="out">
       <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="arg">
    <span class="arg">
       <xsl:apply-templates/>
    </span>
  </xsl:template>

  <xsl:template match="option">
    <span class="mod">
       <xsl:text>[</xsl:text>
    </span>
    <xsl:apply-templates/>
    <span class="mod">
       <xsl:text>]</xsl:text>
    </span>
  </xsl:template>

  <xsl:template match="i">
    <i>
       <xsl:apply-templates/>
    </i>
  </xsl:template>

  <xsl:template match="br">
    <br />
  </xsl:template>


</xsl:stylesheet>
