nav.xsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. This file is part of the DITA Open Toolkit project.
  4. Copyright 2015 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:xs="http://www.w3.org/2001/XMLSchema"
  9. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  10. xmlns:ditamsg="http://dita-ot.sourceforge.net/ns/200704/ditamsg"
  11. version="2.0"
  12. exclude-result-prefixes="xs dita-ot ditamsg">
  13. <xsl:import href="plugin:org.dita.html5:xsl/map2html5Impl.xsl"/>
  14. <xsl:param name="nav-toc" as="xs:string?"/>
  15. <xsl:param name="FILEDIR" as="xs:string?"/>
  16. <xsl:param name="FILENAME" as="xs:string?"/>
  17. <xsl:param name="input.map.url" as="xs:string?"/>
  18. <xsl:variable name="input.map" as="document-node()?">
  19. <xsl:apply-templates select="document($input.map.url)" mode="normalize-map"/>
  20. </xsl:variable>
  21. <xsl:attribute-set name="toc">
  22. <xsl:attribute name="role">toc</xsl:attribute>
  23. </xsl:attribute-set>
  24. <xsl:template match="*" mode="gen-user-sidetoc">
  25. <xsl:if test="$nav-toc = ('partial', 'full')">
  26. <nav xsl:use-attribute-sets="toc">
  27. <ul>
  28. <xsl:choose>
  29. <xsl:when test="$nav-toc = 'partial'">
  30. <xsl:apply-templates select="$current-topicref" mode="toc-pull">
  31. <xsl:with-param name="pathFromMaplist" select="$PATH2PROJ" as="xs:string"/>
  32. <xsl:with-param name="children" as="element()*">
  33. <xsl:apply-templates select="$current-topicref/*[contains(@class, ' map/topicref ')]" mode="toc">
  34. <xsl:with-param name="pathFromMaplist" select="$PATH2PROJ" as="xs:string"/>
  35. </xsl:apply-templates>
  36. </xsl:with-param>
  37. </xsl:apply-templates>
  38. </xsl:when>
  39. <xsl:when test="$nav-toc = 'full'">
  40. <xsl:apply-templates select="$input.map" mode="toc">
  41. <xsl:with-param name="pathFromMaplist" select="$PATH2PROJ" as="xs:string"/>
  42. </xsl:apply-templates>
  43. </xsl:when>
  44. </xsl:choose>
  45. </ul>
  46. </nav>
  47. </xsl:if>
  48. </xsl:template>
  49. <xsl:variable name="current-file" select="dita-ot:normalize-href(if ($FILEDIR = '.') then $FILENAME else concat($FILEDIR, '/', $FILENAME))" as="xs:string?"/>
  50. <xsl:variable name="current-topicrefs" select="$input.map//*[contains(@class, ' map/topicref ')][dita-ot:get-path($PATH2PROJ, .) = $current-file]" as="element()*"/>
  51. <xsl:variable name="current-topicref" select="$current-topicrefs[1]" as="element()?"/>
  52. <xsl:template match="*[contains(@class, ' map/map ')]" mode="toc-pull">
  53. <xsl:param name="pathFromMaplist" select="$PATH2PROJ" as="xs:string"/>
  54. <xsl:param name="children" select="()" as="element()*"/>
  55. <xsl:param name="parent" select="parent::*" as="element()?"/>
  56. <xsl:copy-of select="$children"/>
  57. </xsl:template>
  58. <xsl:template match="*" mode="toc-pull" priority="-10">
  59. <xsl:param name="pathFromMaplist" as="xs:string"/>
  60. <xsl:param name="children" select="()" as="element()*"/>
  61. <xsl:param name="parent" select="parent::*" as="element()?"/>
  62. <xsl:apply-templates select="$parent" mode="toc-pull">
  63. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  64. <xsl:with-param name="children" select="$children"/>
  65. </xsl:apply-templates>
  66. </xsl:template>
  67. <xsl:template match="*[contains(@class, ' map/topicref ')]
  68. [not(@toc = 'no')]
  69. [not(@processing-role = 'resource-only')]"
  70. mode="toc-pull" priority="10">
  71. <xsl:param name="pathFromMaplist" as="xs:string"/>
  72. <xsl:param name="children" select="()" as="element()*"/>
  73. <xsl:param name="parent" select="parent::*" as="element()?"/>
  74. <xsl:variable name="title">
  75. <xsl:apply-templates select="." mode="get-navtitle"/>
  76. </xsl:variable>
  77. <xsl:apply-templates select="$parent" mode="toc-pull">
  78. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  79. <xsl:with-param name="children" as="element()*">
  80. <xsl:apply-templates select="preceding-sibling::*[contains(@class, ' map/topicref ')]" mode="toc">
  81. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  82. </xsl:apply-templates>
  83. <xsl:choose>
  84. <xsl:when test="normalize-space($title)">
  85. <li>
  86. <xsl:if test=". is $current-topicref">
  87. <xsl:attribute name="class">active</xsl:attribute>
  88. </xsl:if>
  89. <xsl:choose>
  90. <xsl:when test="normalize-space(@href)">
  91. <a>
  92. <xsl:attribute name="href">
  93. <xsl:if test="not(@scope = 'external')">
  94. <xsl:value-of select="$pathFromMaplist"/>
  95. </xsl:if>
  96. <xsl:choose>
  97. <xsl:when test="@copy-to and not(contains(@chunk, 'to-content')) and
  98. (not(@format) or @format = 'dita' or @format = 'ditamap') ">
  99. <xsl:call-template name="replace-extension">
  100. <xsl:with-param name="filename" select="@copy-to"/>
  101. <xsl:with-param name="extension" select="$OUTEXT"/>
  102. </xsl:call-template>
  103. <xsl:if test="not(contains(@copy-to, '#')) and contains(@href, '#')">
  104. <xsl:value-of select="concat('#', substring-after(@href, '#'))"/>
  105. </xsl:if>
  106. </xsl:when>
  107. <xsl:when test="not(@scope = 'external') and (not(@format) or @format = 'dita' or @format = 'ditamap')">
  108. <xsl:call-template name="replace-extension">
  109. <xsl:with-param name="filename" select="@href"/>
  110. <xsl:with-param name="extension" select="$OUTEXT"/>
  111. </xsl:call-template>
  112. </xsl:when>
  113. <xsl:otherwise>
  114. <xsl:value-of select="@href"/>
  115. </xsl:otherwise>
  116. </xsl:choose>
  117. </xsl:attribute>
  118. <xsl:value-of select="$title"/>
  119. </a>
  120. </xsl:when>
  121. <xsl:otherwise>
  122. <xsl:value-of select="$title"/>
  123. </xsl:otherwise>
  124. </xsl:choose>
  125. <xsl:if test="exists($children)">
  126. <ul xsl:use-attribute-sets="nav.ul">
  127. <xsl:copy-of select="$children"/>
  128. </ul>
  129. </xsl:if>
  130. </li>
  131. </xsl:when>
  132. <xsl:otherwise>
  133. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  134. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  135. </xsl:apply-templates>
  136. </xsl:otherwise>
  137. </xsl:choose>
  138. <xsl:apply-templates select="following-sibling::*[contains(@class, ' map/topicref ')]" mode="toc">
  139. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  140. </xsl:apply-templates>
  141. </xsl:with-param>
  142. </xsl:apply-templates>
  143. </xsl:template>
  144. <xsl:attribute-set name="nav.ul">
  145. </xsl:attribute-set>
  146. <xsl:template match="*" mode="toc" priority="-10">
  147. <xsl:param name="pathFromMaplist" as="xs:string"/>
  148. <xsl:apply-templates select="*[contains(@class, ' map/topicref ')]" mode="toc">
  149. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  150. </xsl:apply-templates>
  151. </xsl:template>
  152. <xsl:template match="*[contains(@class, ' map/topicref ')]
  153. [not(@toc = 'no')]
  154. [not(@processing-role = 'resource-only')]"
  155. mode="toc" priority="10">
  156. <xsl:param name="pathFromMaplist" as="xs:string"/>
  157. <xsl:param name="children" select="if ($nav-toc = 'full') then *[contains(@class, ' map/topicref ')] else ()" as="element()*"/>
  158. <xsl:variable name="title">
  159. <xsl:apply-templates select="." mode="get-navtitle"/>
  160. </xsl:variable>
  161. <xsl:choose>
  162. <xsl:when test="normalize-space($title)">
  163. <li>
  164. <xsl:if test=". is $current-topicref">
  165. <xsl:attribute name="class">active</xsl:attribute>
  166. </xsl:if>
  167. <xsl:choose>
  168. <xsl:when test="normalize-space(@href)">
  169. <a>
  170. <xsl:attribute name="href">
  171. <xsl:if test="not(@scope = 'external')">
  172. <xsl:value-of select="$pathFromMaplist"/>
  173. </xsl:if>
  174. <xsl:choose>
  175. <xsl:when test="@copy-to and not(contains(@chunk, 'to-content')) and
  176. (not(@format) or @format = 'dita' or @format = 'ditamap') ">
  177. <xsl:call-template name="replace-extension">
  178. <xsl:with-param name="filename" select="@copy-to"/>
  179. <xsl:with-param name="extension" select="$OUTEXT"/>
  180. </xsl:call-template>
  181. <xsl:if test="not(contains(@copy-to, '#')) and contains(@href, '#')">
  182. <xsl:value-of select="concat('#', substring-after(@href, '#'))"/>
  183. </xsl:if>
  184. </xsl:when>
  185. <xsl:when test="not(@scope = 'external') and (not(@format) or @format = 'dita' or @format = 'ditamap')">
  186. <xsl:call-template name="replace-extension">
  187. <xsl:with-param name="filename" select="@href"/>
  188. <xsl:with-param name="extension" select="$OUTEXT"/>
  189. </xsl:call-template>
  190. </xsl:when>
  191. <xsl:otherwise>
  192. <xsl:value-of select="@href"/>
  193. </xsl:otherwise>
  194. </xsl:choose>
  195. </xsl:attribute>
  196. <xsl:value-of select="$title"/>
  197. </a>
  198. </xsl:when>
  199. <xsl:otherwise>
  200. <span>
  201. <xsl:value-of select="$title"/>
  202. </span>
  203. </xsl:otherwise>
  204. </xsl:choose>
  205. <xsl:if test="exists($children)">
  206. <ul>
  207. <xsl:apply-templates select="$children" mode="#current">
  208. <xsl:with-param name="pathFromMaplist" select="$pathFromMaplist"/>
  209. </xsl:apply-templates>
  210. </ul>
  211. </xsl:if>
  212. </li>
  213. </xsl:when>
  214. </xsl:choose>
  215. </xsl:template>
  216. <xsl:function name="dita-ot:get-path" as="xs:string?">
  217. <xsl:param name="pathFromMaplist" as="xs:string"/>
  218. <xsl:param name="node" as="element()"/>
  219. <xsl:for-each select="$node">
  220. <xsl:value-of>
  221. <xsl:if test="not(@scope = 'external')">
  222. <xsl:call-template name="strip-leading-parent">
  223. <xsl:with-param name="path" select="$pathFromMaplist"/>
  224. </xsl:call-template>
  225. </xsl:if>
  226. <xsl:choose>
  227. <xsl:when test="@copy-to and not(contains(@chunk, 'to-content')) and
  228. (not(@format) or @format = 'dita' or @format = 'ditamap') ">
  229. <xsl:value-of select="@copy-to"/>
  230. </xsl:when>
  231. <xsl:when test="contains(@chunk, 'to-content')">
  232. <xsl:value-of select="substring-before(@href,'#')"/>
  233. </xsl:when>
  234. <xsl:otherwise>
  235. <xsl:value-of select="@href"/>
  236. </xsl:otherwise>
  237. </xsl:choose>
  238. </xsl:value-of>
  239. </xsl:for-each>
  240. </xsl:function>
  241. <xsl:template name="strip-leading-parent">
  242. <xsl:param name="path" as="xs:string"/>
  243. <xsl:choose>
  244. <xsl:when test="starts-with($path, '../')">
  245. <xsl:call-template name="strip-leading-parent">
  246. <xsl:with-param name="path" select="substring($path, 4)"/>
  247. </xsl:call-template>
  248. </xsl:when>
  249. <xsl:otherwise>
  250. <xsl:value-of select="$path"/>
  251. </xsl:otherwise>
  252. </xsl:choose>
  253. </xsl:template>
  254. </xsl:stylesheet>