<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version='1.0'>
  <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/html/docbook.xsl"/>

  <!-- ==================================================================== -->

  <!-- * for Lineannotation in Funcprototype output in synopsis part of  -->
  <!-- * Funcsynopses in K&R-style output, put a non-breaking space for -->
  <!-- * the Lineannotation, then wrap it in a Code section in output. -->
  <xsl:template match="funcprototype/lineannotation" mode="kr-funcsynopsis-mode">
    <xsl:text>&#160;</xsl:text>
    <code class="lineannotation">
      <xsl:apply-templates/>
    </code>
  </xsl:template>
  <!-- * ignore all other instances of Lineannotation in Funcprototype -->
  <xsl:template match="funcprototype/lineannotation" mode="kr-tabular"/> <!-- default -->
  <xsl:template match="funcprototype/lineannotation" mode="kr-nontabular"/>
  <xsl:template match="funcprototype/lineannotation" mode="ansi-tabular"/>
  <xsl:template match="funcprototype/lineannotation" mode="ansi-nontabular"/>

  <!-- ==================================================================== -->

  <!-- * process Lineannotation in synopsis part of K&R-style -->
  <!-- * Funcsynopsis (both tabular and nontabuler -->

  <xsl:template match="paramdef" mode="kr-tabular-funcsynopsis-mode"> <!-- default -->
    <tr>
      <xsl:choose>
        <xsl:when test="type and funcparams">
          <td>
            <xsl:apply-templates select="type" mode="kr-tabular-funcsynopsis-mode"/>
            <xsl:text>&#160;</xsl:text>
          </td>
          <td>
            <xsl:apply-templates select="type/following-sibling::node()"
                                 mode="kr-tabular-funcsynopsis-mode"/>
          </td>
        </xsl:when>
        <xsl:when test="funcparams">
          <td colspan="2">
            <xsl:apply-templates mode="kr-tabular-funcsynopsis-mode"/>
          </td>
        </xsl:when>
        <xsl:otherwise>
          <td>
            <xsl:apply-templates select="parameter/preceding-sibling::node()[not(self::parameter)]"
                                 mode="kr-tabular-funcsynopsis-mode"/>
            <xsl:text>&#160;</xsl:text>
          </td>
          <td>
            <xsl:apply-templates select="parameter"
                                 mode="kr-tabular"/>
            <xsl:apply-templates select="parameter/following-sibling::node()[not(self::parameter)]"
                                 mode="kr-tabular-funcsynopsis-mode"/>
            <xsl:text>;</xsl:text>
            <xsl:apply-templates select="following-sibling::*[1][self::lineannotation]"
                                 mode="kr-funcsynopsis-mode"/>
          </td>
        </xsl:otherwise>
      </xsl:choose>
    </tr>
  </xsl:template>

  <xsl:template match="paramdef" mode="kr-funcsynopsis-mode">
    <xsl:if test="preceding-sibling::paramdef"><br/></xsl:if>
    <xsl:apply-templates mode="kr-funcsynopsis-mode"/>
    <xsl:text>;</xsl:text>
    <xsl:apply-templates select="following-sibling::*[1][self::lineannotation]"
                         mode="kr-funcsynopsis-mode"/>
  </xsl:template>

  <!-- ==================================================================== -->

  <!-- * prevent Lineannotation from being considered as a sibling when -->
  <!-- * trying to determine if this Paramdef is the final child of a -->
  <!-- * Funcprotype -->
  <xsl:template match="paramdef" mode="kr-nontabular">
    <xsl:apply-templates select="parameter" mode="kr-nontabular"/>
    <xsl:choose>
      <xsl:when test="following-sibling::*[not(self::lineannotation)]">
        <xsl:text>, </xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <code>)</code>
        <xsl:text>;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="paramdef" mode="kr-tabular">
    <td>
      <xsl:apply-templates select="parameter" mode="kr-tabular"/>
      <xsl:choose>
        <xsl:when test="following-sibling::*[not(self::lineannotation)]">
          <xsl:text>, </xsl:text>
        </xsl:when>
        <xsl:otherwise>
          <code>)</code>
          <xsl:text>;</xsl:text>
        </xsl:otherwise>
      </xsl:choose>
    </td>
    <td>&#160;</td>
  </xsl:template>

  <xsl:template match="paramdef" mode="ansi-nontabular">
    <xsl:apply-templates mode="ansi-nontabular"/>
    <xsl:choose>
      <xsl:when test="following-sibling::*[not(self::lineannotation)]">
        <xsl:text>, </xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <code>)</code>
        <xsl:text>;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="paramdef" mode="ansi-tabular">
    <xsl:choose>
      <xsl:when test="type and funcparams">
        <td>
          <xsl:apply-templates select="type" mode="kr-tabular-funcsynopsis-mode"/>
          <xsl:text>&#160;</xsl:text>
        </td>
        <td>
          <xsl:apply-templates select="type/following-sibling::node()"
                               mode="kr-tabular-funcsynopsis-mode"/>
        </td>
      </xsl:when>
      <xsl:otherwise>
        <td>
          <xsl:apply-templates select="parameter/preceding-sibling::node()[not(self::parameter)]"
                               mode="ansi-tabular"/>
          <xsl:text>&#160;</xsl:text>
        </td>
        <td>
          <xsl:apply-templates select="parameter"
                               mode="ansi-tabular"/>
          <xsl:apply-templates select="parameter/following-sibling::node()[not(self::parameter)]"
                               mode="ansi-tabular"/>
          <xsl:choose>
            <xsl:when test="following-sibling::*[not(self::lineannotation)]">
              <xsl:text>, </xsl:text>
            </xsl:when>
            <xsl:otherwise>
              <code>)</code>
              <xsl:text>;</xsl:text>
            </xsl:otherwise>
          </xsl:choose>
        </td>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
