<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="file:///usr/apps/docbook/xsl/1.67.2/manpages/docbook.xsl"/>

	<xsl:strip-space elements="refsection title row entry para tbody thead command option varlistentry constant"/>
	
<!-- Add a informal multicolumn table. 
	If the attribute 'frame' is set to 'all', the table will have a frame
	border. Otherwise no frame border will be setup.
-->
<xsl:template match="informaltable">
    <xsl:text>&#10;.TS&#10;</xsl:text>
	<xsl:if test="@frame = 'all'">
		<xsl:text>box;&#10;</xsl:text>
	</xsl:if>
	<xsl:apply-templates mode="itable"/>
    <xsl:text>.TE&#10;</xsl:text>
</xsl:template>

<!-- calculate the spec for a single column -->
<xsl:template name="colspec" mode="itable">
	<xsl:param name="outline" select="''"/>
	<xsl:param name="colsep" select="0"/>
	<xsl:param name="align" select="left"/>
	<xsl:param name="isnotlast" select="1"/>
	<xsl:param name="type" select="''"/>
	<xsl:param name="width" select="0"/>
	<xsl:param name="count" select="0"/>
	
	<xsl:if test="$type != ''">
		<xsl:variable name="sep">
			<xsl:choose>
				<xsl:when test="$colsep = '1'">
					<xsl:value-of select="'|'"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:value-of select="' '"/>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$align = 'center'">
				<xsl:text>c</xsl:text>
			</xsl:when>
			<xsl:when test="$align = 'rigth'">
				<xsl:text>r</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>l</xsl:text>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:value-of select="$outline"/>
		<xsl:if test="$width &gt; 0">
			<xsl:text>w(</xsl:text><xsl:value-of select="$width"/>
			<xsl:text>n)</xsl:text>		
		</xsl:if>
		<xsl:choose>
			<xsl:when test="$isnotlast = 0">
				<xsl:if test="$type = 'tbody'">
					<xsl:text>.</xsl:text>
				</xsl:if>
				<xsl:text>&#10;</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$sep"/>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="$count &gt; 0">
			<xsl:call-template name="colspec">
				<xsl:with-param name="type" select="$type"/>
				<xsl:with-param name="outline" select="$outline"/>
				<xsl:with-param name="colsep" select="$colsep"/>
				<xsl:with-param name="align" select="$align"/>
				<xsl:with-param name="isnotlast" 
					select="$count - 1"/>
				<xsl:with-param name="count" select="$count - 1"/>
			</xsl:call-template>			
		</xsl:if>
	</xsl:if>
</xsl:template>

<!--
NOTE: 

If colspecs are found, colnum attributes are ignored.
It is assumed, that colspecs occure in the same order as the columns
are printed.
	
At the momemt, absolute colwidth attributes are honored and interpreted
as width of 'n's. I would prefer to change this to % of the available 
line width of the given medium, however, AFAIK there is no facility
to determine this value:(

Furthermore, to reduce complexity of the stylesheet, all rows must always
have the same number of entryS!


-->
<xsl:template match="tgroup" mode="itable">
	<xsl:variable name="tgroup.cols" select="count(colspec)"/>
	<xsl:variable name="thead.cols" select="count(thead/colspec)"/>
	<xsl:variable name="thead.entries" select="count(thead/row[1]/entry)"/>
	<xsl:variable name="tbody.entries" select="count(tbody/row[1]/entry)"/>
	
	<xsl:variable name="head.cols">
		<xsl:choose>
			<xsl:when test="count(thead) = 0">0</xsl:when>
			<xsl:when test="$thead.cols &gt; 0">
				<xsl:value-of select="$thead.cols"/>
			</xsl:when>
			<xsl:when test="$tgroup.cols &gt; 0">
				<xsl:value-of select="$tgroup.cols"/>
			</xsl:when>
			<xsl:when test="$thead.entries &gt; 0">
				<xsl:value-of select="$thead.cols"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$tbody.entries"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:variable name="body.cols">
		<xsl:choose>
			<xsl:when test="$tgroup.cols = 0">
				<xsl:value-of select="$tbody.entries"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="$tgroup.cols"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:variable>

	<!-- need to add header column specs ? -->
	<xsl:if test="$head.cols &gt; 0">
		<xsl:choose>
			<xsl:when test="$thead.cols &gt; 0">
				<xsl:for-each select="thead/colspec">
					<xsl:call-template name="colspec">
						<xsl:with-param name="type" select="'head'"/>
						<xsl:with-param name="outline" select="'b'"/>
						<xsl:with-param name="colsep" select="@colsep"/>
						<xsl:with-param name="align" select="@align"/>
						<xsl:with-param name="width" select="@colwidth"/>
						<xsl:with-param name="isnotlast" 
							select="$thead.cols - position()"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:when test="$tgroup.cols &gt; 0">
				<xsl:for-each select="colspec">
					<xsl:call-template name="colspec">
						<xsl:with-param name="type" select="'head'"/>
						<xsl:with-param name="outline" select="'b'"/>
						<xsl:with-param name="colsep" select="@colsep"/>
						<xsl:with-param name="align" select="@align"/>
						<xsl:with-param name="width" select="@colwidth"/>
						<xsl:with-param name="isnotlast" 
							select="$tgroup.cols - position()"/>
					</xsl:call-template>
				</xsl:for-each>
			</xsl:when>
			<xsl:otherwise>
				<xsl:call-template name="colspec">
					<xsl:with-param name="type" select="'head'"/>
					<xsl:with-param name="outline" select="'b'"/>
					<xsl:with-param name="align" select="@align"/>
					<xsl:with-param name="isnotlast" 
						select="$head.cols - 1"/>
					<xsl:with-param name="count" select="$head.cols - 1"/>
				</xsl:call-template>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:if>
	<!-- now the body columns specs -->
	<xsl:choose>
		<xsl:when test="$tgroup.cols &gt; 0">
			<xsl:for-each select="colspec">
				<xsl:call-template name="colspec">
					<xsl:with-param name="type" select="'tbody'"/>
					<xsl:with-param name="colsep" select="@colsep"/>
					<xsl:with-param name="align" select="@align"/>
					<xsl:with-param name="width" select="@colwidth"/>
					<xsl:with-param name="isnotlast" 
						select="$tgroup.cols - position()"/>
				</xsl:call-template>
			</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
			<xsl:call-template name="colspec">
				<xsl:with-param name="type" select="'tbody'"/>
				<xsl:with-param name="align" select="@align"/>
				<xsl:with-param name="isnotlast" 
					select="$body.cols - 1"/>
				<xsl:with-param name="count" select="$body.cols - 1"/>
			</xsl:call-template>
		</xsl:otherwise>
	</xsl:choose>
	<xsl:apply-templates mode="itable"/>
</xsl:template>

<xsl:template match="thead" mode="itable">
    <xsl:apply-templates mode="itable"/>
</xsl:template>
	
<xsl:template match="tbody" mode="itable">
    <xsl:apply-templates mode="itable"/>
</xsl:template>

<!-- ignored since already processed in the tgroup template -->
<xsl:template match="colspec" mode="itable">
</xsl:template>

<!-- on multi column tables we enclose entries with paraS in T{ ... T} to tie
	the content to one block -->
<xsl:template match="row" mode="itable">
    <xsl:for-each select="entry">
		<xsl:if test="count(para) &gt; 0 or count(link) &gt; 0">
			<xsl:text>T{&#10;</xsl:text>
		</xsl:if>
    	<xsl:for-each select="node()">
	    	<xsl:choose>
	    		<xsl:when test="self::para|self::link">
	    			<xsl:apply-templates mode="itable.para" select=".">
	    				<xsl:with-param name="pos" select="position()"/>
	    			</xsl:apply-templates>
	    		</xsl:when>
	    		<xsl:otherwise>
	    			<xsl:apply-templates select="."/>
	    		</xsl:otherwise>
	    	</xsl:choose>
    	</xsl:for-each>
		<xsl:if test="count(para) &gt; 0 or count(link) &gt; 0">
			<xsl:text>&#10;T}&#10;</xsl:text>
	    </xsl:if>
    	<xsl:choose>
    		<xsl:when test="position() = last()">
    			<xsl:text>&#10;</xsl:text>
    		</xsl:when>
    		<xsl:otherwise>
    			<xsl:text>&#9;</xsl:text>
    		</xsl:otherwise>
    	</xsl:choose>
    </xsl:for-each>
</xsl:template>

<!-- in tables we do not wanna have .PP etc. but usual linebreaks -->
<xsl:template match="para" mode="itable.para">
	<xsl:param name="pos" select="0"/>
	<xsl:if test="$pos &gt; 1">
		<xsl:text>&#10;.br&#10;</xsl:text>
	</xsl:if>
	<xsl:apply-templates/>
</xsl:template>
<!-- in tables we do not wanna have .PP etc. but usual linebreaks -->
<xsl:template match="link" mode="itable.para">
	<xsl:param name="pos" select="0"/>
	<xsl:if test="$pos &gt; 1">
		<xsl:text>&#10;.br&#10;</xsl:text>
	</xsl:if>
	<xsl:apply-templates select="."/>
</xsl:template>

<!--
<xsl:template match="text()">
    <xsl:copy-of select="translate(.,'&#9;&#10;','')"/>
</xsl:template>
-->
	
</xsl:stylesheet>

