nav.xsl 11 KB

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