map2hhcImpl.xsl 18 KB

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