<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  version="1.0">
  <xsl:output 
    method="text"     
    media-type="text/plain"
    indent="yes"
    encoding="UTF-8"/>
  <!-- 
     Copyright (c) 2003 by Gan Uesli Starling 
     Free for non-commerical use provided that this notice be retained. 
     No commercial use whatever except by written arrangement with the author. 
   -->
  
  <!-- BEGIN USER-DEFINED GLOBAL VARIABLES -->
  <!-- END ALL-PURPOSE GENERIC TEMPLATES -->
  
  <xsl:template match="/">
    <xsl:apply-templates select="howto/head"/>
    <xsl:apply-templates select="howto/body"/>
  </xsl:template>
  
  <xsl:template match="head">
  </xsl:template>
  
  <xsl:template match="body">
    <xsl:choose>
      <xsl:when test="//head/pdfmarks/@body='no'">
      </xsl:when>
      <xsl:otherwise>
				<xsl:text>&#10;&#10;</xsl:text>
				<!-- Def to excape pdfmarks on old PostScript engines -->
				<xsl:text>&#10;/pdfmark where</xsl:text>
        <xsl:text>&#10;{pop} {userdict /pdfmark /cleartomark load put} ifelse</xsl:text>
				<xsl:call-template name="pdfmark"/>
				<xsl:text>&#10;&#10;</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="pdfmark">
		<!-- PostScript comment -->
		<xsl:text>&#10;%% BEGIN pdfmarks generated by XSLT</xsl:text>
		<xsl:text>&#10;%% Swap '?' in page # and Y pdfmarks via gus_pdfmark.pl script.</xsl:text>
		<xsl:text>&#10;[/Count 0 /Page ? /View [/XYZ 0 ? 1.0] /Title (</xsl:text>
    <xsl:apply-templates select="title"/>
		<xsl:text>) /OUT pdfmark</xsl:text>
    <xsl:call-template name="pdfmark_section"/>
		<!-- PostScript comment -->
		<xsl:text>&#10;%% END pdfmarks generated by XSLT</xsl:text>
  </xsl:template>
   
  <xsl:template name="pdfmark_section">
    <xsl:if test="section">
      <xsl:for-each select="section">
				<xsl:text>&#10;[/Count </xsl:text>
				<xsl:choose>
				  <xsl:when test="//head/pdfmarks/@topic='no'">
					  <xsl:text>0</xsl:text>
					</xsl:when>
					<xsl:otherwise>
			    	<xsl:value-of select="count(topic)"/>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> /Page ? /View [/XYZ 0 ? 1.0] /Title (</xsl:text>
        <xsl:apply-templates select="title"/>
				<xsl:text>) /OUT pdfmark</xsl:text>
        <xsl:choose>
          <xsl:when test="//head/pdfmarks/@topic='no'">
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="pdfmark_topic"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:if> 
  </xsl:template>
   
  <xsl:template name="pdfmark_topic">
    <xsl:if test="topic"> 
      <xsl:for-each select="topic">
				<xsl:text>&#10;  [/Count </xsl:text>
				<xsl:choose>
				  <xsl:when test="//head/pdfmarks/@subtopic='no'">
					  <xsl:text>0</xsl:text>
					</xsl:when>
					<xsl:otherwise>
			    	<xsl:value-of select="count(subtopic)"/>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> /Page ? /View [/XYZ 0 ? 1.0] /Title (</xsl:text>
        <xsl:apply-templates select="title"/>
				<xsl:text>) /OUT pdfmark</xsl:text>
        <xsl:choose>
          <xsl:when test="//head/pdfmarks/@subtopic='no'">
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="pdfmark_subtopic"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>
   
  <xsl:template name="pdfmark_subtopic">
    <xsl:if test="subtopic">
      <xsl:for-each select="subtopic">
				<xsl:text>&#10;    [/Count </xsl:text>
				<xsl:choose>
				  <xsl:when test="//head/pdfmarks/@subsubtopic='no'">
					  <xsl:text>0</xsl:text>
					</xsl:when>
					<xsl:otherwise>
			    	<xsl:value-of select="count(subsubtopic)"/>
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text> /Page ? /View [/XYZ 0 ? 1.0] /Title (</xsl:text>
        <xsl:apply-templates select="title"/>
				<xsl:text>) /OUT pdfmark</xsl:text>
        <xsl:choose>
          <xsl:when test="//head/pdfmarks/@subsubtopic='no'">
          </xsl:when>
          <xsl:otherwise>
            <xsl:call-template name="pdfmark_subsubtopic"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </xsl:if>
  </xsl:template>
   
  <xsl:template name="pdfmark_subsubtopic">
    <xsl:if test="subsubtopic">
    	<xsl:for-each select="subsubtopic">
				<xsl:text>&#10;      [/Page ? /View [/XYZ 0 ? 1.0] /Title (</xsl:text>
        <xsl:apply-templates select="title"/>
				<xsl:text>) /OUT pdfmark</xsl:text>
    	</xsl:for-each>
    </xsl:if>
  </xsl:template>
  
  <xsl:template match="title">
    <xsl:value-of select="."/>
  </xsl:template>

</xsl:stylesheet>
