<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
		xmlns:w3c="http://www.w3.org/2001/02pd/rec54#"
		xmlns:contact="http://www.w3.org/2000/10/swap/pim/contact#"
		xmlns:dc="http://purl.org/dc/elements/1.1/"
		xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
		xmlns:doc="http://www.w3.org/2000/10/swap/pim/doc#"
		xmlns:html="http://www.w3.org/1999/xhtml"
		exclude-result-prefixes="doc rdf html dc w3c contact"
                version='1.0'>

<xsl:import href="/sourceforge/docbook/xsl/html/chunker.xsl"/>

<xsl:param name="chunker.output.method" select="'xml'"/>
<xsl:param name="chunker.output.encoding" select="'utf-8'"/>
<xsl:param name="chunker.output.omit-xml-declaration" select="'no'"/>
<xsl:param name="chunker.output.indent" select="'yes'"/>
<xsl:param name="chunker.output.doctype-public" select="''"/>
<xsl:param name="chunker.output.doctype-system" select="''"/>
<xsl:param name="saxon.character.representation" select="'decimal'"/>

<xsl:param name="specName"/>
<xsl:param name="category" select="'Unfiled'"/>
<xsl:param name="title"/>
<xsl:param name="depth" select="'0'"/>
<xsl:param name="subdirOnly" select="'yes'"/>

<xsl:variable name="ixlOptions">
  <LinkOptions>
    <MaximumDepth             value="{$depth}"/>
    <FollowOffsite            value="no"/>
    <SubDirOnly               value="{$subdirOnly}"/>
    <UnresolvedDetail         value="include"/>
    <Exclude>
      <!-- <Pattern>RE::table[1-9].jpg</Pattern> -->
    </Exclude>
    <Include>
      <!-- <Pattern>RE:C:Table8</Pattern> -->
    </Include>
  </LinkOptions>

  <ImageOptions>
    <AltText                  value="exclude"/>
    <Images                   value="include"/>
    <ResizeLargeImages        value="yes"/>
    <MaximumWidth             value="144"/>
    <MaximumHeight            value="144"/>
    <ImproveContrast          value="yes"/>
    <Dither                   value="yes"/>
    <Compress                 value="yes"/>
    <BitDepth1                value="exclude"/>
    <BitDepth2                value="exclude"/>
    <BitDepth4                value="include"/>
    <BitDepth8                value="include"/>
    <BitDepth16               value="include"/>
  </ImageOptions>

  <TableOptions>
    <IgnoreTables             value="no"/>
    <AddSeparators            value="no"/>
    <UseMinimumDepth          value="no"/>
    <MinimumDepth             value="1"/>
    <UseMaximumBottomReach    value="no"/>
    <MaximumBottomReach       value="1"/>
    <UnfoldFullPageTables     value="no"/>
    <IgnorePixelWidths        value="no"/>
  </TableOptions>

  <ColorOptions>
    <BackgroundColors         value="keep"/>
    <TextColors               value="keep"/>
  </ColorOptions>

  <MarginOptions>
    <LeftRightMargins         value="ignore"/>
    <LeftRightPadding         value="ignore"/>
  </MarginOptions>

  <SecurityOptions>
    <Convert                  value="allow"/>
    <CopyBeam                 value="allow"/>
    <CopyAndPaste             value="allow"/>
    <Modify                   value="allow"/>
    <Print                    value="allow"/>
    <Expiration>
      <Expires                value="no"/>
      <Date                   value="20030529"/>
    </Expiration>
    <Password>
      <Require                value="no"/>
      <Password>MyPassword</Password>
    </Password>
    <RegistrationCode>
      <Require                value="no"/>
      <DocumentID>UniqueID55PC</DocumentID>
    </RegistrationCode>
  </SecurityOptions>

  <TextOptions>
    <PreUseMonospaceFont      value="yes"/>
    <PreSingleLineBreaks      value="KeepAll"/>
    <ProcessLineBreaks        value="yes"/>
    <ConvertSingleLineBreaks  value="no"/>
    <Preformatted             value="no"/>
    <UseMonospaceFont         value="no"/>
    <MonospaceFontSize        value="10"/>
    <TabStopWidth             value="4"/>
  </TextOptions>

  <CookieOptions>
    <SendCookies              value="no"/>
    <ReceiveCookies           value="no"/>
    <Cookies>
      <Cookie>
	<ReadOnly             value="no"/>
	<DoNotSend            value="no"/>
	<Discard              value="no"/>
	<Secure               value="no"/>
	<Name>TOKEN</Name>
	<Value>48A897ZUIO98</Value>
	<Expires>2015/12/25 08:52:12</Expires>
	<Version>1</Version>
	<Path>/</Path>
	<Port>80,8000</Port>
	<Domain>.foo.com</Domain>
	<Comments>none</Comments>
	<CommentURL>http://www.foo.com/cookies.htm</CommentURL>
      </Cookie>
    </Cookies>
  </CookieOptions>
</xsl:variable>

<xsl:template match="/">
  <xsl:if test="$specName = ''">
    <xsl:message terminate="yes">You must specify a specName</xsl:message>
  </xsl:if>

  <xsl:variable name="uri" select="concat('http://www.w3.org/TR/', $specName)"/>
  <xsl:variable name="uriSlash" select="concat($uri, '/')"/>

  <xsl:variable name="desc"
		select="/rdf:RDF/w3c:WD[doc:versionOf/@rdf:resource = $uri]
		        | /rdf:RDF/w3c:LastCall[doc:versionOf/@rdf:resource = $uri]
		        | /rdf:RDF/w3c:Retired[doc:versionOf/@rdf:resource = $uri]
		        | /rdf:RDF/w3c:NOTE[doc:versionOf/@rdf:resource = $uri]
		        | /rdf:RDF/w3c:REC[doc:versionOf/@rdf:resource = $uri]
		        | /rdf:RDF/w3c:ActivityStatement[doc:versionOf/@rdf:resource
                                                         = $uri]
		        | /rdf:RDF/w3c:WD[doc:versionOf/@rdf:resource = $uriSlash]
		        | /rdf:RDF/w3c:LastCall[doc:versionOf/@rdf:resource = $uriSlash]
		        | /rdf:RDF/w3c:Retired[doc:versionOf/@rdf:resource = $uriSlash]
		        | /rdf:RDF/w3c:NOTE[doc:versionOf/@rdf:resource = $uriSlash]
		        | /rdf:RDF/w3c:REC[doc:versionOf/@rdf:resource = $uriSlash]
		        | /rdf:RDF/w3c:ActivityStatement[doc:versionOf/@rdf:resource
                                                         = $uriSlash]"/>

  <xsl:if test="count($desc) != 1">
    <xsl:message terminate="yes">
      <xsl:value-of select="$uri"/>
      <xsl:text> matches </xsl:text>
      <xsl:value-of select="count($desc)"/>
      <xsl:text> items.</xsl:text>
    </xsl:message>
  </xsl:if>

  <xsl:variable name="spec" select="document($desc/@rdf:about, .)"/>
  <xsl:variable name="specTitle">
    <xsl:choose>
      <xsl:when test="$title != ''">
	<xsl:value-of select="$title"/>
      </xsl:when>
      <xsl:otherwise>
	<xsl:value-of select="$desc/dc:title"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:variable>

  <xsl:call-template name="write.chunk">
    <xsl:with-param name="filename" select="concat($specName, '.ixl')"/>
    <xsl:with-param name="content">
      <iSiloXDocumentList>
	<iSiloXDocument>
	  <Source>
	    <Sources>
	      <Path><xsl:value-of select="$uri"/></Path>
	    </Sources>
	  </Source>

	  <Destination>
	    <Title><xsl:value-of select="$specTitle"/></Title>
	    <Files>
	      <Path><xsl:value-of select="$specName"/>.pdb</Path>
	    </Files>
	  </Destination>

	  <xsl:copy-of select="$ixlOptions"/>
	  
	  <Messages>
	    <Title><xsl:value-of select="$desc/dc:title"/></Title>
	    <Version>
	      <xsl:choose>
		<xsl:when test="local-name($desc) = 'WD'">
		  <xsl:text>W3C Working Draft</xsl:text>
		</xsl:when>
		<xsl:when test="local-name($desc) = 'LastCall'">
		  <xsl:text>W3C LC Working Draft</xsl:text>
		</xsl:when>
		<xsl:when test="local-name($desc) = 'Retired'">
		  <xsl:text>W3C Retired</xsl:text>
		</xsl:when>
		<xsl:when test="local-name($desc) = 'NOTE'">
		  <xsl:text>W3C Note</xsl:text>
		</xsl:when>
		<xsl:when test="local-name($desc) = 'REC'">
		  <xsl:text>W3C Recommendation</xsl:text>
		</xsl:when>
		<xsl:when test="local-name($desc) = 'ActivityStatement'">
		  <xsl:text>W3C Activity Statement</xsl:text>
		</xsl:when>
		<xsl:otherwise>W3C</xsl:otherwise>
	      </xsl:choose>
	      <xsl:text> </xsl:text>
	      <xsl:value-of select="$desc/dc:date"/>
	    </Version>

	    <Copyright>
	      <xsl:value-of select="$spec//html:p[@class='copyright'][1]"/>
	    </Copyright>

	    <AuthorName>
	      <xsl:for-each select="$desc/w3c:editor">
		<xsl:value-of select="contact:fullName"/>
		<xsl:if test="position() &lt; last()">, </xsl:if>
	      </xsl:for-each>
	    </AuthorName>
	    <PublisherName>World Wide Web Consortium.</PublisherName>
	  </Messages>

	  <LastConversion>
	    <Date>2002/08/01 08:12:02</Date>
	    <Size value="123456789"/>
	  </LastConversion>

	  <Bookmarks>
	    <Document>
	      <UseFile                value="yes"/>
	      <Path><xsl:value-of select="$specName"/>.marks</Path>
	      <UseNamedAnchors        value="no"/>
	      <NamedAnchorType        value="WordWebPage"/>
	      <UseIncludeFilter       value="no"/>
	      <IncludePrefix>bm</IncludePrefix>
	      <UseExcludeFilter       value="no"/>
	      <ExcludePrefix>bm_</ExcludePrefix>
	    </Document>
	  </Bookmarks>

	  <DocumentOptions>
	    <PageBounds               value="hard"/>
	    <UseDefaultCategory>
	      <xsl:attribute name="value">
		<xsl:choose>
		  <xsl:when test="$category = 'Unfiled'">no</xsl:when>
		  <xsl:otherwise>yes</xsl:otherwise>
		</xsl:choose>
	      </xsl:attribute>
	    </UseDefaultCategory>
	    <DefaultCategory><xsl:value-of select="$category"/></DefaultCategory>
	    <HomePageNumber           value="1"/>
	    <OpenHomePageOnDateChange value="yes"/>
	  </DocumentOptions>
	</iSiloXDocument>
      </iSiloXDocumentList>
    </xsl:with-param>
  </xsl:call-template>

  <xsl:variable name="tocH2"
		select="$spec//html:h2[contains(.,'Contents')][1]
		        |$spec//html:h2[contains(.,' contents')][1]
		        |$spec//html:h1[contains(.,'Contents')][1]"/>"/>

  <xsl:call-template name="write.chunk">
    <xsl:with-param name="filename" select="concat($specName, '.marks')"/>
    <xsl:with-param name="content">
      <xsl:choose>
	<xsl:when test="$spec//html:div[@class='toc']">
	  <xsl:apply-templates select="$spec//html:div[@class='toc'][last()]" mode="toc">
	    <xsl:with-param name="uri" select="$uri"/>
	  </xsl:apply-templates>
	</xsl:when>
	<xsl:when test="$tocH2/following-sibling::html:ul
		        |$tocH2/following-sibling::html:dl">
	  <xsl:apply-templates select="$tocH2//following-sibling::*[1]" mode="toc">
	    <xsl:with-param name="uri" select="$uri"/>
	  </xsl:apply-templates>
	</xsl:when>
	<xsl:when test="$tocH2/following-sibling::html:p[@class='toc']">
	  <xsl:apply-templates select="$tocH2//following-sibling::html:p[@class='toc']"
			       mode="toc">
	    <xsl:with-param name="uri" select="$uri"/>
	  </xsl:apply-templates>
	  <xsl:apply-templates select="$tocH2//following-sibling::html:p[@class='appendix']"
			       mode="toc">
	    <xsl:with-param name="uri" select="$uri"/>
	  </xsl:apply-templates>
	</xsl:when>
	<xsl:otherwise>
	  <xsl:message>Can't find ToC.</xsl:message>
	</xsl:otherwise>
      </xsl:choose>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

<xsl:template match="*" mode="toc">
  <xsl:param name="uri"/>

  <div>
    <xsl:for-each select=".//html:a[@href]">
      <!-- the section number preceds the anchor... -->
      <xsl:variable name="snum">
	<xsl:call-template name="trimLeadingNbsp">
	  <xsl:with-param name="string"
			  select="normalize-space(preceding-sibling::text()[1])"/>
	</xsl:call-template>
      </xsl:variable>

      <a href="{$uri}{@href}">
	<xsl:value-of select="$snum"/>
	<xsl:if test="$snum != ''">
	  <xsl:text> </xsl:text>
	</xsl:if>
	<xsl:value-of select="normalize-space(.)"/>
      </a>
    </xsl:for-each>
  </div>
</xsl:template>

<xsl:template name="trimLeadingNbsp">
  <xsl:param name="string"/>

  <xsl:choose>
    <xsl:when test="starts-with($string, '&#160;')">
      <xsl:call-template name="trimLeadingNbsp">
	<xsl:with-param name="string"
			select="substring-after($string, '&#160;')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$string"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>
