map2html5Impl.xsl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!--
  3. This file is part of the DITA Open Toolkit project.
  4. Copyright 2012 Jarno Elovirta
  5. See the accompanying LICENSE file for applicable license.
  6. -->
  7. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  8. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  9. xmlns:ditamsg="http://dita-ot.sourceforge.net/ns/200704/ditamsg" version="2.0"
  10. exclude-result-prefixes="dita-ot ditamsg">
  11. <xsl:param name="OUTEXT" select="'.html'"/>
  12. <xsl:param name="WORKDIR">
  13. <xsl:apply-templates select="/processing-instruction('workdir-uri')[1]" mode="get-work-dir"/>
  14. </xsl:param>
  15. <xsl:template match="*[contains(@class, ' map/map ')]" mode="toc">
  16. <xsl:param name="pathFromMaplist"/>
  17. <xsl:if test="descendant::*[contains(@class, ' map/topicref ')]
  18. [not(@toc = 'no')]
  19. [not(@processing-role = 'resource-only')]">
  20. <nav>
  21. <ul>
  22. <xsl:call-template name="commonattributes"/>
  23. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  24. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  25. </xsl:apply-templates>
  26. </ul>
  27. </nav>
  28. </xsl:if>
  29. </xsl:template>
  30. <xsl:template match="*[contains(@class, ' map/topicref ')]
  31. [not(@toc = 'no')]
  32. [not(@processing-role = 'resource-only')]"
  33. mode="toc">
  34. <xsl:param name="pathFromMaplist"/>
  35. <xsl:variable name="title">
  36. <xsl:apply-templates select="." mode="get-navtitle"/>
  37. </xsl:variable>
  38. <xsl:choose>
  39. <xsl:when test="normalize-space($title)">
  40. <li>
  41. <xsl:call-template name="commonattributes"/>
  42. <xsl:choose>
  43. <!-- If there is a reference to a DITA or HTML file, and it is not external: -->
  44. <xsl:when test="normalize-space(@href)">
  45. <a>
  46. <xsl:attribute name="href">
  47. <xsl:choose>
  48. <xsl:when test="@copy-to and not(contains(@chunk, 'to-content')) and
  49. (not(@format) or @format = 'dita' or @format = 'ditamap') ">
  50. <xsl:if test="not(@scope = 'external')">
  51. <xsl:value-of select="$pathFromMaplist"/>
  52. </xsl:if>
  53. <xsl:call-template name="replace-extension">
  54. <xsl:with-param name="filename" select="@copy-to"/>
  55. <xsl:with-param name="extension" select="$OUTEXT"/>
  56. </xsl:call-template>
  57. <xsl:if test="not(contains(@copy-to, '#')) and contains(@href, '#')">
  58. <xsl:value-of select="concat('#', substring-after(@href, '#'))"/>
  59. </xsl:if>
  60. </xsl:when>
  61. <xsl:when test="not(@scope = 'external') and (not(@format) or @format = 'dita' or @format = 'ditamap')">
  62. <xsl:if test="not(@scope = 'external')">
  63. <xsl:value-of select="$pathFromMaplist"/>
  64. </xsl:if>
  65. <xsl:call-template name="replace-extension">
  66. <xsl:with-param name="filename" select="@href"/>
  67. <xsl:with-param name="extension" select="$OUTEXT"/>
  68. </xsl:call-template>
  69. </xsl:when>
  70. <xsl:otherwise><!-- If non-DITA, keep the href as-is -->
  71. <xsl:if test="not(@scope = 'external')">
  72. <xsl:value-of select="$pathFromMaplist"/>
  73. </xsl:if>
  74. <xsl:value-of select="@href"/>
  75. </xsl:otherwise>
  76. </xsl:choose>
  77. </xsl:attribute>
  78. <xsl:if test="@scope = 'external' or not(not(@format) or @format = 'dita' or @format = 'ditamap')">
  79. <xsl:attribute name="target">_blank</xsl:attribute>
  80. </xsl:if>
  81. <xsl:value-of select="$title"/>
  82. </a>
  83. </xsl:when>
  84. <xsl:otherwise>
  85. <xsl:value-of select="$title"/>
  86. </xsl:otherwise>
  87. </xsl:choose>
  88. <!-- If there are any children that should be in the TOC, process them -->
  89. <xsl:if test="descendant::*[contains(@class, ' map/topicref ')]
  90. [not(@toc = 'no')]
  91. [not(@processing-role = 'resource-only')]">
  92. <ul>
  93. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  94. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  95. </xsl:apply-templates>
  96. </ul>
  97. </xsl:if>
  98. </li>
  99. </xsl:when>
  100. <xsl:otherwise><!-- if it is an empty topicref -->
  101. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  102. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  103. </xsl:apply-templates>
  104. </xsl:otherwise>
  105. </xsl:choose>
  106. </xsl:template>
  107. <!-- If toc=no, but a child has toc=yes, that child should bubble up to the top -->
  108. <xsl:template match="*[contains(@class, ' map/topicref ')]
  109. [@toc = 'no']
  110. [not(@processing-role = 'resource-only')]"
  111. mode="toc">
  112. <xsl:param name="pathFromMaplist"/>
  113. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  114. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  115. </xsl:apply-templates>
  116. </xsl:template>
  117. <xsl:template match="*" mode="toc" priority="-1"/>
  118. <xsl:template match="*" mode="get-navtitle">
  119. <xsl:choose>
  120. <!-- If navtitle is specified -->
  121. <xsl:when test="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' topic/navtitle ')]">
  122. <xsl:apply-templates select="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' topic/navtitle ')]"
  123. mode="dita-ot:text-only"/>
  124. </xsl:when>
  125. <xsl:when test="@navtitle">
  126. <xsl:value-of select="@navtitle"/>
  127. </xsl:when>
  128. <!-- If there is no title and none can be retrieved, check for <linktext> -->
  129. <xsl:when test="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]">
  130. <xsl:apply-templates select="*[contains(@class, ' map/topicmeta ')]/*[contains(@class, ' map/linktext ')]"
  131. mode="dita-ot:text-only"/>
  132. </xsl:when>
  133. <!-- No local title, and not targeting a DITA file. Could be just a container setting
  134. metadata, or a file reference with no title. Issue message for the second case. -->
  135. <xsl:otherwise>
  136. <xsl:if test="normalize-space(@href)">
  137. <xsl:apply-templates select="." mode="ditamsg:could-not-retrieve-navtitle-using-fallback">
  138. <xsl:with-param name="target" select="@href"/>
  139. <xsl:with-param name="fallback" select="@href"/>
  140. </xsl:apply-templates>
  141. <xsl:value-of select="@href"/>
  142. </xsl:if>
  143. </xsl:otherwise>
  144. </xsl:choose>
  145. </xsl:template>
  146. <xsl:template match="*" mode="ditamsg:could-not-retrieve-navtitle-using-fallback">
  147. <xsl:param name="target"/>
  148. <xsl:param name="fallback"/>
  149. <xsl:call-template name="output-message">
  150. <xsl:with-param name="id" select="'DOTX009W'"/>
  151. <xsl:with-param name="msgparams">%1=<xsl:value-of select="$target"/>;%2=<xsl:value-of select="$fallback"/></xsl:with-param>
  152. </xsl:call-template>
  153. </xsl:template>
  154. <!-- Normalize map -->
  155. <xsl:template match="/ | @* | node()" mode="normalize-map">
  156. <xsl:copy>
  157. <xsl:apply-templates select="@* | node()" mode="normalize-map"/>
  158. </xsl:copy>
  159. </xsl:template>
  160. <xsl:template match="*[contains(@class, ' mapgroup-d/topicgroup ')]" mode="normalize-map">
  161. <xsl:apply-templates select="* except *[contains(@class, ' map/topicmeta ')]" mode="normalize-map"/>
  162. </xsl:template>
  163. </xsl:stylesheet>