map2hhcImpl.xsl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?xml version="1.0"?>
  2. <!-- This file is part of the DITA Open Toolkit project hosted on
  3. Sourceforge.net. See the accompanying license.txt file for
  4. applicable licenses.-->
  5. <!-- (c) Copyright IBM Corp. 2004, 2005 All Rights Reserved. -->
  6. <!--
  7. Conversion from DITA map or maplist to HTML Help contents file.
  8. Input = one DITA map file, or a maplist pointing to multiple maps
  9. Output = one HHC contents file for use with the HTML Help compiler.
  10. Options:
  11. /OUTEXT = XHTML output extension (default is '.html')
  12. /WORKDIR = The working directory that contains the document being transformed.
  13. Needed as a directory prefix for the @href "document()" function calls.
  14. Default is './'
  15. -->
  16. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  17. version="2.0"
  18. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  19. exclude-result-prefixes="dita-ot"
  20. >
  21. <!-- Include error message template -->
  22. <xsl:import href="plugin:org.dita.base:xsl/common/output-message.xsl"/>
  23. <xsl:import href="plugin:org.dita.base:xsl/common/dita-utilities.xsl"/>
  24. <xsl:import href="plugin:org.dita.base:xsl/common/dita-textonly.xsl"/>
  25. <!-- Set the prefix for error message numbers -->
  26. <!-- Deprecated since 2.3 -->
  27. <xsl:variable name="msgprefix">DOTX</xsl:variable>
  28. <xsl:variable name="newline"><xsl:text>
  29. </xsl:text></xsl:variable>
  30. <!-- *************************** Command line parameters *********************** -->
  31. <xsl:param name="OUTEXT" select="'.html'"/><!-- "htm" and "html" are valid values -->
  32. <xsl:param name="WORKDIR" select="'./'"/>
  33. <!-- *********************************************************************************
  34. Setup the HTML wrapper for the table of contents
  35. ********************************************************************************* -->
  36. <xsl:template match="/">
  37. <xsl:value-of select="$newline"/>
  38. <html>
  39. <xsl:value-of select="$newline"/>
  40. <head>
  41. <xsl:value-of select="$newline"/>
  42. <xsl:comment> Sitemap 1.0 </xsl:comment>
  43. <xsl:value-of select="$newline"/>
  44. </head>
  45. <xsl:value-of select="$newline"/>
  46. <body>
  47. <xsl:apply-templates/>
  48. <xsl:value-of select="$newline"/>
  49. </body>
  50. <xsl:value-of select="$newline"/>
  51. </html>
  52. </xsl:template>
  53. <!-- *********************************************************************************
  54. If processing only a single map, setup the HTML wrapper and output the contents.
  55. Otherwise, just process the contents.
  56. ********************************************************************************* -->
  57. <xsl:template match="/*[contains(@class, ' map/map ')]">
  58. <xsl:param name="pathFromMaplist"/>
  59. <xsl:if test=".//*[contains(@class, ' map/topicref ')][not(@toc='no')][not(@processing-role='resource-only')]">
  60. <xsl:value-of select="$newline"/>
  61. <UL>
  62. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]">
  63. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  64. </xsl:apply-templates>
  65. </UL>
  66. </xsl:if>
  67. </xsl:template>
  68. <!-- *********************************************************************************
  69. Output each topic as an <OBJECT> in an <LI>. Each object takes 2 parameters:
  70. - A name must be given. If it is specified on <topicref>, use that, otherwise try
  71. to open the file and retrieve the title. First look for a navigation title,
  72. followed by the main topic title. Last, try to use <linktext> specified in the map.
  73. Failing that, use *** and issue a message.
  74. - An HREF is optional. If none is specified, this will generate a wrapper (which
  75. shows up as a book instead of a page in the HTML Help viewer). The HREF is only
  76. valid for inclusion in the HHC if it is not external, and if it points to a DITA
  77. or HTML file.
  78. If this topicref has any child topicref's that will be part of the navigation,
  79. output a <UL> around them and process the contents.
  80. Move the real work to the named template, to easily check @format for
  81. files that cannot be included.
  82. ********************************************************************************* -->
  83. <xsl:template match="*[contains(@class, ' map/topicref ')]">
  84. <xsl:param name="pathFromMaplist"/>
  85. <xsl:choose>
  86. <!-- If TOC is no, go ahead and process children; if TOC is turned back on,
  87. those topics will rise to this level -->
  88. <xsl:when test="@toc='no' or @processing-role='resource-only'">
  89. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]">
  90. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  91. </xsl:apply-templates>
  92. </xsl:when>
  93. <!-- If this this a container (no href or href='', no title), just process children -->
  94. <xsl:when test="(not(@href) or @href='') and not(@navtitle) and not(*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' topic/navtitle ')]) and
  95. not(*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')])">
  96. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')][not(contains(@toc,'no'))][not(@processing-role='resource-only')]">
  97. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  98. </xsl:apply-templates>
  99. </xsl:when>
  100. <!-- If this this a container (no href, with title), output a TOC node with
  101. the title, and process children -->
  102. <xsl:when test="not(@href)">
  103. <xsl:call-template name="output-toc-entry">
  104. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  105. </xsl:call-template>
  106. </xsl:when>
  107. <xsl:when test="not(@format)">
  108. <xsl:call-template name="output-toc-entry">
  109. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  110. </xsl:call-template>
  111. </xsl:when>
  112. <xsl:when test="@format='dita'">
  113. <xsl:call-template name="output-toc-entry">
  114. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  115. </xsl:call-template>
  116. </xsl:when>
  117. <xsl:when test="(contains(@format,'htm') or contains(@format,'HTM')) and (@scope='external' or @scope='peer')">
  118. <!-- The html file is not available, so of course it cannot be included -->
  119. <xsl:if test="not(contains(@href,'://'))">
  120. <!-- Seems to be a local file, but marked external or peer, so we can't tell if it's available -->
  121. <xsl:call-template name="output-message">
  122. <xsl:with-param name="id" select="'DOTX048I'"/>
  123. <xsl:with-param name="msgparams">%1=<xsl:value-of select="@href"/></xsl:with-param>
  124. </xsl:call-template>
  125. </xsl:if>
  126. <xsl:call-template name="output-toc-entry">
  127. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  128. </xsl:call-template>
  129. </xsl:when>
  130. <xsl:when test="contains(@format,'htm') or contains(@format,'HTM')">
  131. <xsl:call-template name="output-toc-entry">
  132. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  133. </xsl:call-template>
  134. </xsl:when>
  135. <xsl:otherwise>
  136. <!-- @format is not DITA and not HTML, so the target will be ignored for HTML Help -->
  137. <xsl:call-template name="output-message">
  138. <xsl:with-param name="id" select="'DOTX007I'"/>
  139. <xsl:with-param name="msgparams">%1=<xsl:value-of select="@href"/></xsl:with-param>
  140. </xsl:call-template>
  141. </xsl:otherwise>
  142. </xsl:choose>
  143. </xsl:template>
  144. <xsl:template name="output-toc-entry">
  145. <xsl:param name="pathFromMaplist"/>
  146. <xsl:variable name="WORKDIR">
  147. <xsl:apply-templates select="/processing-instruction('workdir-uri')[1]" mode="get-work-dir"/>
  148. </xsl:variable>
  149. <xsl:value-of select="$newline"/>
  150. <!-- if current node is not topicgroup and not empty or current node
  151. is empty but there is a valid child, we will generate
  152. the entry in toc -->
  153. <xsl:if test="not(contains(@class,' mapgroup-d/topicgroup '))and (@href and not(@href='') or .//*[@href and not(@href='')])">
  154. <LI> <OBJECT type="text/sitemap">
  155. <xsl:element name="param">
  156. <xsl:attribute name="name">Name</xsl:attribute>
  157. <xsl:attribute name="value">
  158. <xsl:choose>
  159. <!-- If navtitle is specified, use it -->
  160. <xsl:when test="*[contains(@class,'- map/topicmeta ')]/*[contains(@class, '- topic/navtitle ')]">
  161. <xsl:apply-templates select="*[contains(@class,'- map/topicmeta ')]/*[contains(@class, '- topic/navtitle ')]" mode="dita-ot:text-only"/>
  162. </xsl:when>
  163. <xsl:when test="not(*[contains(@class,'- map/topicmeta ')]/*[contains(@class, '- topic/navtitle ')]) and @navtitle"><xsl:value-of select="@navtitle"/></xsl:when>
  164. <!-- If this references a DITA file (has @href, not "local" or "external"),
  165. try to open the file and get the title -->
  166. <xsl:when test="@href and not(@href='') and
  167. not ((ancestor-or-self::*/@scope)[last()]='external') and
  168. not ((ancestor-or-self::*/@scope)[last()]='peer') and
  169. not ((ancestor-or-self::*/@type)[last()]='external') and
  170. not ((ancestor-or-self::*/@type)[last()]='local')">
  171. <!-- Need to worry about targeting a nested topic? Not for now. -->
  172. <!--<xsl:variable name="FileWithPath"><xsl:value-of select="$WORKDIR"/><xsl:choose>-->
  173. <xsl:variable name="FileWithPath"><xsl:choose>
  174. <xsl:when test="@copy-to"><xsl:value-of select="$WORKDIR"/><xsl:value-of select="@copy-to"/></xsl:when>
  175. <xsl:when test="contains(@href,'#')"><xsl:value-of select="$WORKDIR"/><xsl:value-of select="substring-before(@href,'#')"/></xsl:when>
  176. <xsl:otherwise><xsl:value-of select="$WORKDIR"/><xsl:value-of select="@href"/></xsl:otherwise></xsl:choose></xsl:variable>
  177. <xsl:variable name="TargetFile" select="document($FileWithPath,/)"/>
  178. <xsl:choose>
  179. <xsl:when test="not($TargetFile)"> <!-- DITA file does not exist -->
  180. <xsl:choose>
  181. <xsl:when test="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]"> <!-- attempt to recover by using linktext -->
  182. <xsl:apply-templates select="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]" mode="dita-ot:text-only"/>
  183. </xsl:when>
  184. <xsl:otherwise>
  185. <xsl:call-template name="output-message">
  186. <xsl:with-param name="id" select="'DOTX008W'"/>
  187. <xsl:with-param name="msgparams">%1=<xsl:value-of select="@href"/></xsl:with-param>
  188. </xsl:call-template>
  189. </xsl:otherwise>
  190. </xsl:choose>
  191. </xsl:when>
  192. <!-- First choice for navtitle: topic/titlealts/navtitle -->
  193. <xsl:when test="$TargetFile/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/titlealts ')]/*[contains(@class,' topic/navtitle ')]">
  194. <xsl:apply-templates select="$TargetFile/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/titlealts ')]/*[contains(@class,' topic/navtitle ')]" mode="dita-ot:text-only"/>
  195. </xsl:when>
  196. <!-- Second choice for navtitle: topic/title -->
  197. <xsl:when test="$TargetFile/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/title ')]">
  198. <xsl:apply-templates select="$TargetFile/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/title ')]" mode="dita-ot:text-only"/>
  199. </xsl:when>
  200. <!-- This might be a combo article; modify the same queries: dita/topic/titlealts/navtitle -->
  201. <xsl:when test="$TargetFile/dita/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/titlealts ')]/*[contains(@class,' topic/navtitle ')]">
  202. <xsl:apply-templates select="$TargetFile/dita/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/titlealts ')]/*[contains(@class,' topic/navtitle ')]" mode="dita-ot:text-only"/>
  203. </xsl:when>
  204. <!-- Second choice: dita/topic/title -->
  205. <xsl:when test="$TargetFile/dita/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/title ')]">
  206. <xsl:apply-templates select="$TargetFile/dita/*[contains(@class,' topic/topic ')]/*[contains(@class,' topic/title ')]" mode="dita-ot:text-only"/>
  207. </xsl:when>
  208. <!-- Last choice: use the linktext specified within the topicref -->
  209. <xsl:when test="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]">
  210. <xsl:apply-templates select="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]" mode="dita-ot:text-only"/>
  211. </xsl:when>
  212. <xsl:otherwise>
  213. <xsl:call-template name="output-message">
  214. <xsl:with-param name="id" select="'DOTX009W'"/>
  215. <xsl:with-param name="msgparams">%1=<xsl:value-of select="@TargetFile"/>;%2=***</xsl:with-param>
  216. </xsl:call-template>
  217. <xsl:text>***</xsl:text>
  218. </xsl:otherwise>
  219. </xsl:choose>
  220. </xsl:when>
  221. <!-- If there is no title and none can be retrieved, check for <linktext> -->
  222. <xsl:when test="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]">
  223. <xsl:apply-templates select="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]" mode="dita-ot:text-only"/>
  224. </xsl:when>
  225. <!-- No local title, and not targeting a DITA file. Could be just a container setting
  226. metadata, or a file reference with no title. Issue message for the second case. -->
  227. <xsl:otherwise>
  228. <xsl:if test="@href and not(@href='')">
  229. <xsl:call-template name="output-message">
  230. <xsl:with-param name="id" select="'DOTX009W'"/>
  231. <xsl:with-param name="msgparams">%1=<xsl:value-of select="@href"/>;%2=<xsl:value-of select="@href"/></xsl:with-param>
  232. </xsl:call-template>
  233. <xsl:value-of select="@href"/>
  234. </xsl:if>
  235. </xsl:otherwise>
  236. </xsl:choose>
  237. </xsl:attribute> <!-- End of @value (the title) -->
  238. </xsl:element> <!-- End of <param> element -->
  239. <!-- If there is a reference to a DITA or HTML file, and it is not external:
  240. allow non-dita, external values in navigation. -->
  241. <xsl:if test="@href">
  242. <xsl:variable name="topicID">
  243. <xsl:choose>
  244. <xsl:when test="contains(@href, '#')">
  245. <xsl:value-of select="concat('#', substring-after(@href, '#'))"/>
  246. </xsl:when>
  247. <xsl:otherwise>
  248. <xsl:value-of select="''"/>
  249. </xsl:otherwise>
  250. </xsl:choose>
  251. </xsl:variable>
  252. <xsl:element name="param">
  253. <xsl:attribute name="name">Local</xsl:attribute>
  254. <xsl:choose> <!-- What if targeting a nested topic? Need to keep the ID? -->
  255. <xsl:when test="@copy-to and (not(@format) or @format = 'dita')">
  256. <xsl:attribute name="value">
  257. <xsl:value-of select="$pathFromMaplist"/>
  258. <xsl:call-template name="replace-extension">
  259. <xsl:with-param name="filename" select="@copy-to"/>
  260. <xsl:with-param name="extension" select="$OUTEXT"/>
  261. <xsl:with-param name="ignore-fragment" select="true()"/>
  262. </xsl:call-template>
  263. <xsl:value-of select="$topicID"/>
  264. </xsl:attribute>
  265. </xsl:when>
  266. <xsl:when test="@href and (not(@format) or @format = 'dita')">
  267. <xsl:attribute name="value">
  268. <xsl:value-of select="$pathFromMaplist"/>
  269. <xsl:call-template name="replace-extension">
  270. <xsl:with-param name="filename" select="@href"/>
  271. <xsl:with-param name="extension" select="$OUTEXT"/>
  272. </xsl:call-template>
  273. </xsl:attribute>
  274. </xsl:when>
  275. <xsl:when test="contains(@href,'.htm') and @scope!='external'">
  276. <xsl:attribute name="value"><xsl:value-of select="$pathFromMaplist"/><xsl:value-of select="@href"/></xsl:attribute>
  277. </xsl:when>
  278. <xsl:otherwise> <!-- If non-DITA, keep the href as-is -->
  279. <xsl:attribute name="value"><xsl:value-of select="@href"/></xsl:attribute>
  280. </xsl:otherwise>
  281. </xsl:choose>
  282. </xsl:element>
  283. </xsl:if>
  284. </OBJECT>
  285. <!-- If there are any children that should be in the TOC, process them -->
  286. <xsl:if test="descendant::*[contains(@class, ' map/topicref ')][not(contains(@toc,'no'))][not(@processing-role='resource-only')]">
  287. <UL>
  288. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]">
  289. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  290. </xsl:apply-templates>
  291. </UL>
  292. </xsl:if>
  293. <xsl:value-of select="$newline"/>
  294. </LI>
  295. </xsl:if>
  296. </xsl:template>
  297. <!-- These are here just to prevent accidental fallthrough -->
  298. <xsl:template match="*[contains(@class, ' map/navref ')]"/>
  299. <xsl:template match="*[contains(@class, ' map/anchor ')]"/>
  300. <xsl:template match="*[contains(@class, ' map/reltable ')]"/>
  301. <xsl:template match="*[contains(@class, ' map/topicmeta ')]"/>
  302. <xsl:template match="*">
  303. <xsl:apply-templates/>
  304. </xsl:template>
  305. <!-- Template to get the relative path to a map -->
  306. <xsl:template name="getRelativePath">
  307. <xsl:param name="remainingPath" select="@file"/>
  308. <xsl:choose>
  309. <xsl:when test="contains($remainingPath,'/')">
  310. <xsl:value-of select="substring-before($remainingPath,'/')"/><xsl:text>/</xsl:text>
  311. <xsl:call-template name="getRelativePath">
  312. <xsl:with-param name="remainingPath" select="substring-after($remainingPath,'/')"/>
  313. </xsl:call-template>
  314. </xsl:when>
  315. <xsl:when test="contains($remainingPath,'\')">
  316. <xsl:value-of select="substring-before($remainingPath,'\')"/><xsl:text>/</xsl:text>
  317. <xsl:call-template name="getRelativePath">
  318. <xsl:with-param name="remainingPath" select="substring-after($remainingPath,'\')"/>
  319. </xsl:call-template>
  320. </xsl:when>
  321. </xsl:choose>
  322. </xsl:template>
  323. </xsl:stylesheet>