chunk-common.xsl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:xweb="xalan://com.nwalsh.xalan.Website"
  4. xmlns:sweb="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.Website"
  5. exclude-result-prefixes="sweb xweb"
  6. version="1.0">
  7. <xsl:output method="html"/>
  8. <xsl:param name="output-root" select="'.'"/>
  9. <xsl:param name="dry-run" select="'0'"/>
  10. <xsl:param name="rebuild-all" select="'0'"/>
  11. <xsl:template match="autolayout">
  12. <!-- Regenerate olink database? -->
  13. <xsl:if test="$collect.xref.targets = 'yes' or
  14. $collect.xref.targets = 'only'">
  15. <xsl:apply-templates select="." mode="collect.targets"/>
  16. </xsl:if>
  17. <xsl:if test="$collect.xref.targets != 'only'" >
  18. <xsl:apply-templates select="toc|notoc" mode="make"/>
  19. </xsl:if>
  20. </xsl:template>
  21. <xsl:template match="toc|tocentry|notoc" mode="make">
  22. <xsl:call-template name="make.tocentry"/>
  23. <xsl:apply-templates select="tocentry" mode="make"/>
  24. </xsl:template>
  25. <xsl:template name="make.tocentry">
  26. <xsl:variable name="srcFile" select="@page"/>
  27. <xsl:if test="@page and @href">
  28. <xsl:message terminate="yes">
  29. <xsl:text>Fail: tocentry has both page and href attributes.</xsl:text>
  30. </xsl:message>
  31. </xsl:if>
  32. <xsl:variable name="filename">
  33. <xsl:choose>
  34. <xsl:when test="@filename">
  35. <xsl:value-of select="@filename"/>
  36. </xsl:when>
  37. <xsl:otherwise>index.html</xsl:otherwise>
  38. </xsl:choose>
  39. </xsl:variable>
  40. <xsl:variable name="dir">
  41. <xsl:apply-templates select="." mode="calculate-dir"/>
  42. </xsl:variable>
  43. <!--
  44. <xsl:message>
  45. <xsl:text>!!</xsl:text>
  46. <xsl:value-of select="$dir"/>
  47. <xsl:text>!!</xsl:text>
  48. <xsl:value-of select="$filename-prefix"/>
  49. <xsl:text>!!</xsl:text>
  50. <xsl:value-of select="$filename"/>
  51. </xsl:message>
  52. -->
  53. <xsl:variable name="targetFile">
  54. <xsl:value-of select="$dir"/>
  55. <xsl:value-of select="$filename-prefix"/>
  56. <xsl:value-of select="$filename"/>
  57. </xsl:variable>
  58. <xsl:choose>
  59. <xsl:when test="function-available('sweb:exists')">
  60. <xsl:if test="not(@href) and not(sweb:exists($srcFile))">
  61. <xsl:message terminate="yes">
  62. <xsl:value-of select="$srcFile"/>
  63. <xsl:text> does not exist.</xsl:text>
  64. </xsl:message>
  65. </xsl:if>
  66. </xsl:when>
  67. <xsl:when test="function-available('xweb:exists')">
  68. <xsl:if test="not(@href) and not(xweb:exists($srcFile))">
  69. <xsl:message terminate="yes">
  70. <xsl:value-of select="$srcFile"/>
  71. <xsl:text> does not exist.</xsl:text>
  72. </xsl:message>
  73. </xsl:if>
  74. </xsl:when>
  75. <xsl:otherwise>
  76. <xsl:message terminate="no">
  77. <xsl:value-of select="$srcFile"/>
  78. </xsl:message>
  79. </xsl:otherwise>
  80. </xsl:choose>
  81. <xsl:variable name="output-file">
  82. <xsl:choose>
  83. <xsl:when test="@href">
  84. <xsl:value-of select="@href"/>
  85. </xsl:when>
  86. <xsl:otherwise>
  87. <xsl:value-of select="$output-root"/>
  88. <xsl:text>/</xsl:text>
  89. <xsl:value-of select="$targetFile"/>
  90. </xsl:otherwise>
  91. </xsl:choose>
  92. </xsl:variable>
  93. <xsl:variable name="needsUpdate">
  94. <xsl:choose>
  95. <xsl:when test="@href">0</xsl:when>
  96. <xsl:when test="function-available('sweb:needsUpdate')">
  97. <xsl:choose>
  98. <xsl:when test="$rebuild-all != 0
  99. or sweb:needsUpdate($autolayout-file, $output-file)
  100. or sweb:needsUpdate($srcFile, $output-file)">
  101. <xsl:text>1</xsl:text>
  102. </xsl:when>
  103. <xsl:otherwise>0</xsl:otherwise>
  104. </xsl:choose>
  105. </xsl:when>
  106. <xsl:when test="function-available('xweb:needsUpdate')">
  107. <xsl:choose>
  108. <xsl:when test="$rebuild-all != 0
  109. or xweb:needsUpdate($autolayout-file, $output-file)
  110. or xweb:needsUpdate($srcFile, $output-file)">
  111. <xsl:text>1</xsl:text>
  112. </xsl:when>
  113. <xsl:otherwise>0</xsl:otherwise>
  114. </xsl:choose>
  115. </xsl:when>
  116. <xsl:otherwise>1</xsl:otherwise>
  117. </xsl:choose>
  118. </xsl:variable>
  119. <xsl:choose>
  120. <xsl:when test="$needsUpdate != 0">
  121. <xsl:message>
  122. <xsl:text>Update: </xsl:text>
  123. <xsl:value-of select="$output-file"/>
  124. <xsl:text>: </xsl:text>
  125. <xsl:value-of select="$srcFile"/>
  126. </xsl:message>
  127. <xsl:variable name="webpage" select="document($srcFile,.)"/>
  128. <xsl:variable name="content">
  129. <xsl:apply-templates select="$webpage/webpage"/>
  130. </xsl:variable>
  131. <xsl:if test="$dry-run = 0">
  132. <xsl:call-template name="write.chunk">
  133. <xsl:with-param name="filename" select="$output-file"/>
  134. <xsl:with-param name="content" select="$content"/>
  135. </xsl:call-template>
  136. </xsl:if>
  137. </xsl:when>
  138. <xsl:otherwise>
  139. <xsl:message>
  140. <xsl:text>Up-to-date: </xsl:text>
  141. <xsl:value-of select="$output-file"/>
  142. </xsl:message>
  143. </xsl:otherwise>
  144. </xsl:choose>
  145. </xsl:template>
  146. <xsl:template match="*" mode="calculate-dir">
  147. <xsl:choose>
  148. <xsl:when test="@dir">
  149. <!-- if there's a directory, use it -->
  150. <xsl:choose>
  151. <xsl:when test="starts-with(@dir, '/')">
  152. <!-- if the directory on this begins with a "/", we're done... -->
  153. <xsl:value-of select="substring-after(@dir, '/')"/>
  154. </xsl:when>
  155. <xsl:otherwise>
  156. <xsl:value-of select="@dir"/>
  157. </xsl:otherwise>
  158. </xsl:choose>
  159. </xsl:when>
  160. <xsl:when test="parent::*">
  161. <!-- if there's a parent, try it -->
  162. <xsl:apply-templates select="parent::*" mode="calculate-dir"/>
  163. </xsl:when>
  164. <xsl:otherwise>
  165. <!-- nop -->
  166. </xsl:otherwise>
  167. </xsl:choose>
  168. </xsl:template>
  169. <xsl:template match="autolayout" mode="collect.targets">
  170. <xsl:choose>
  171. <xsl:when test="$website.database.document = ''">
  172. <xsl:message>
  173. Must specify a $website.database.document parameter when
  174. $collect.xref.targets is set to 'yes' or 'only'.
  175. The xref targets were not collected.
  176. </xsl:message>
  177. </xsl:when>
  178. <xsl:otherwise>
  179. <xsl:choose>
  180. <xsl:when test="$website.database.document">
  181. <xsl:call-template name="write.chunk">
  182. <xsl:with-param name="filename"
  183. select="$website.database.document"/>
  184. <xsl:with-param name="method" select="'xml'"/>
  185. <xsl:with-param name="encoding" select="'utf-8'"/>
  186. <xsl:with-param name="omit-xml-declaration" select="'no'"/>
  187. <xsl:with-param name="indent" select="'yes'"/>
  188. <xsl:with-param name="quiet" select="0"/>
  189. <xsl:with-param name="content">
  190. <targetset>
  191. <xsl:apply-templates select="." mode="olink.mode"/>
  192. </targetset>
  193. </xsl:with-param>
  194. </xsl:call-template>
  195. </xsl:when>
  196. <xsl:otherwise>
  197. <!-- Else write to standard output -->
  198. <xsl:apply-templates select="." mode="olink.mode"/>
  199. </xsl:otherwise>
  200. </xsl:choose>
  201. </xsl:otherwise>
  202. </xsl:choose>
  203. </xsl:template>
  204. </xsl:stylesheet>