dita-utilities.xsl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?xml version="1.0" encoding="utf-8"?>
  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. <!-- Common utilities that can be used by DITA transforms -->
  8. <xsl:stylesheet version="2.0"
  9. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  10. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  11. xmlns:dita-ot="http://dita-ot.sourceforge.net/ns/201007/dita-ot"
  12. exclude-result-prefixes="xs dita-ot">
  13. <xsl:include href="plugin:org.dita.base:xsl/common/functions.xsl"/>
  14. <xsl:param name="defaultLanguage" select="'en'" as="xs:string"/>
  15. <xsl:param name="DEFAULTLANG" select="if (/*/@xml:lang) then /*/@xml:lang else $defaultLanguage" as="xs:string"/>
  16. <xsl:param name="variableFiles.url" select="'plugin:org.dita.base:xsl/common/strings.xml'"/>
  17. <xsl:variable name="pixels-per-inch" select="number(96)"/>
  18. <xsl:key name="id" match="*[@id]" use="@id"/>
  19. <!-- Function to determine the current language, and return it in lower case -->
  20. <xsl:template name="getLowerCaseLang">
  21. <xsl:value-of select="dita-ot:get-current-language(.)"/>
  22. </xsl:template>
  23. <xsl:function name="dita-ot:capitalize">
  24. <xsl:param name="text" as="xs:string"/>
  25. <xsl:value-of select="concat(upper-case(substring($text, 1, 1)),
  26. lower-case(substring($text, 2)))"/>
  27. </xsl:function>
  28. <xsl:template match="*" mode="get-first-topic-lang">
  29. <xsl:sequence select="dita-ot:get-first-topic-language(.)"/>
  30. </xsl:template>
  31. <xsl:template match="*" mode="get-render-direction">
  32. <xsl:param name="lang">
  33. <xsl:apply-templates select="/*" mode="get-first-topic-lang"/>
  34. </xsl:param>
  35. <xsl:variable name="l" select="tokenize($lang, '-')[1]" as="xs:string"/>
  36. <xsl:choose>
  37. <xsl:when test="$l = 'ar'">rtl</xsl:when>
  38. <xsl:when test="$l = 'arc'">rtl</xsl:when>
  39. <xsl:when test="$l = 'bcc'">rtl</xsl:when>
  40. <xsl:when test="$l = 'bqi'">rtl</xsl:when>
  41. <xsl:when test="$l = 'ckb'">rtl</xsl:when>
  42. <xsl:when test="$l = 'dv'">rtl</xsl:when>
  43. <xsl:when test="$l = 'fa'">rtl</xsl:when>
  44. <xsl:when test="$l = 'glk'">rtl</xsl:when>
  45. <xsl:when test="$l = 'he'">rtl</xsl:when>
  46. <xsl:when test="$l = 'lrc'">rtl</xsl:when>
  47. <xsl:when test="$l = 'mzn'">rtl</xsl:when>
  48. <xsl:when test="$l = 'pnb'">rtl</xsl:when>
  49. <xsl:when test="$l = 'ps'">rtl</xsl:when>
  50. <xsl:when test="$l = 'sd'">rtl</xsl:when>
  51. <xsl:when test="$l = 'ug'">rtl</xsl:when>
  52. <xsl:when test="$l = 'ur'">rtl</xsl:when>
  53. <xsl:when test="$l = 'yi'">rtl</xsl:when>
  54. <xsl:otherwise>ltr</xsl:otherwise>
  55. </xsl:choose>
  56. </xsl:template>
  57. <xsl:variable name="variableFiles" select="document($variableFiles.url)/langlist/lang" as="element(lang)*"/>
  58. <!-- Deprecated. Use getVariable template instead. -->
  59. <xsl:template name="getString">
  60. <xsl:param name="stringName"/>
  61. <xsl:call-template name="output-message">
  62. <xsl:with-param name="id" select="'DOTX066W'"/>
  63. <xsl:with-param name="msgparams">%1=getString</xsl:with-param>
  64. </xsl:call-template>
  65. <xsl:call-template name="getVariable">
  66. <xsl:with-param name="id" select="string($stringName)"/>
  67. </xsl:call-template>
  68. </xsl:template>
  69. <xsl:template name="getVariable">
  70. <xsl:param name="id" as="xs:string"/>
  71. <xsl:param name="params" as="node()*"/>
  72. <xsl:param name="ctx" as="node()" select="."/>
  73. <xsl:sequence select="dita-ot:get-variable($ctx, $id, $params)"/>
  74. </xsl:template>
  75. <xsl:template name="findString">
  76. <xsl:param name="id" as="xs:string"/>
  77. <xsl:param name="params" as="node()*"/>
  78. <xsl:param name="ancestorlang" as="xs:string*"/>
  79. <xsl:param name="defaultlang" as="xs:string*"/>
  80. <xsl:param name="originallang" as="xs:string*" select="$ancestorlang[1]"/>
  81. <xsl:variable name="l" select="($ancestorlang, $defaultlang)[1]" as="xs:string?"/>
  82. <xsl:choose>
  83. <xsl:when test="exists($l)">
  84. <xsl:variable name="variablefile" select="$variableFiles[lower-case(@xml:lang) = lower-case($l)]/@filename" as="xs:string*"/>
  85. <xsl:variable name="variable" as="element()*">
  86. <xsl:for-each select="$variablefile">
  87. <xsl:sequence select="document(., $variableFiles[1])/*/*[@name = $id or @id = $id]"/><!-- strings/str/@name opentopic-vars:vars/opentopic-vars:variable/@id -->
  88. </xsl:for-each>
  89. </xsl:variable>
  90. <xsl:choose>
  91. <xsl:when test="exists($variable)">
  92. <xsl:apply-templates select="$variable[last()]" mode="processVariableBody">
  93. <xsl:with-param name="params" select="$params"/>
  94. </xsl:apply-templates>
  95. <xsl:if test="empty($ancestorlang)">
  96. <xsl:call-template name="output-message">
  97. <xsl:with-param name="id" select="'DOTX001W'"/>
  98. <xsl:with-param name="msgparams">%1=<xsl:value-of select="$id"/>;%2=<xsl:value-of select="$originallang"/>;%3=<xsl:value-of select="$DEFAULTLANG"/></xsl:with-param>
  99. </xsl:call-template>
  100. </xsl:if>
  101. </xsl:when>
  102. <xsl:otherwise>
  103. <xsl:call-template name="findString">
  104. <xsl:with-param name="id" select="$id"/>
  105. <xsl:with-param name="params" select="$params"/>
  106. <xsl:with-param name="ancestorlang" select="$ancestorlang[position() gt 1]"/>
  107. <xsl:with-param name="defaultlang" select="if (exists($ancestorlang)) then $defaultlang else $defaultlang[position() gt 1]"/>
  108. <xsl:with-param name="originallang" select="$originallang"/>
  109. </xsl:call-template>
  110. </xsl:otherwise>
  111. </xsl:choose>
  112. </xsl:when>
  113. <xsl:otherwise>
  114. <xsl:variable name="variablefile" select="$variableFiles[@xml:lang='']/@filename" as="xs:string*"/>
  115. <xsl:variable name="variable" as="element()*">
  116. <xsl:for-each select="$variablefile">
  117. <xsl:sequence select="document(., $variableFiles[1])/*/*[@name = $id or @id = $id]"/>
  118. </xsl:for-each>
  119. </xsl:variable>
  120. <xsl:choose>
  121. <xsl:when test="exists($variable)">
  122. <xsl:apply-templates select="$variable[last()]" mode="processVariableBody">
  123. <xsl:with-param name="params" select="$params"/>
  124. </xsl:apply-templates>
  125. </xsl:when>
  126. <xsl:otherwise>
  127. <xsl:value-of select="$id"/>
  128. <xsl:call-template name="output-message">
  129. <xsl:with-param name="id" select="'DOTX052W'"/>
  130. <xsl:with-param name="msgparams">%1=<xsl:value-of select="$id"/></xsl:with-param>
  131. </xsl:call-template>
  132. </xsl:otherwise>
  133. </xsl:choose>
  134. </xsl:otherwise>
  135. </xsl:choose>
  136. </xsl:template>
  137. <!-- Support legacy variable syntax -->
  138. <xsl:template match="str" mode="processVariableBody">
  139. <xsl:param name="params"/>
  140. <xsl:copy-of select="node()"/>
  141. </xsl:template>
  142. <xsl:template match="variable" mode="processVariableBody">
  143. <xsl:param name="params" as="node()*"/>
  144. <xsl:for-each select="node()">
  145. <xsl:choose>
  146. <xsl:when test="self::param">
  147. <xsl:variable name="param-name" select="@ref-name" as="xs:string"/>
  148. <xsl:copy-of select="$params/descendant-or-self::*[name() = $param-name]/node()"/>
  149. </xsl:when>
  150. <xsl:when test="self::variableref">
  151. <xsl:call-template name="getVariable">
  152. <xsl:with-param name="id" select="@refid"/>
  153. <xsl:with-param name="params" select="$params"/>
  154. </xsl:call-template>
  155. </xsl:when>
  156. <xsl:otherwise>
  157. <xsl:copy-of select="."/>
  158. </xsl:otherwise>
  159. </xsl:choose>
  160. </xsl:for-each>
  161. </xsl:template>
  162. <xsl:template name="length-to-pixels">
  163. <xsl:param name="dimen"/>
  164. <!-- We handle units of cm, mm, in, pt, pc, px. We also accept em,
  165. but just treat 1em=1pc. An omitted unit is taken as px. -->
  166. <xsl:variable name="units" select="substring($dimen, string-length($dimen) - 1)"/>
  167. <xsl:variable name="numeric-value" select="number(substring($dimen, 1, string-length($dimen) - 2))"/>
  168. <xsl:choose>
  169. <xsl:when test="string(number($dimen)) != 'NaN'">
  170. <!-- Since $units is a number, the input was unitless, so we default
  171. the unit to pixels and just return the input value -->
  172. <xsl:value-of select="round(number($dimen))"/>
  173. </xsl:when>
  174. <xsl:when test="string($numeric-value) = 'NaN'">
  175. <!-- If the input isn't valid, just return 100% -->
  176. <xsl:value-of select="'100%'"/>
  177. </xsl:when>
  178. <xsl:when test="$units='cm'">
  179. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 2.54)"/>
  180. </xsl:when>
  181. <xsl:when test="$units='mm'">
  182. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 25.4)"/>
  183. </xsl:when>
  184. <xsl:when test="$units='in'">
  185. <xsl:value-of select="round($numeric-value * $pixels-per-inch)"/>
  186. </xsl:when>
  187. <xsl:when test="$units='pt'">
  188. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 72)"/>
  189. </xsl:when>
  190. <xsl:when test="$units='pc'">
  191. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 6)"/>
  192. </xsl:when>
  193. <xsl:when test="$units='px'">
  194. <xsl:value-of select="round($numeric-value)"/>
  195. </xsl:when>
  196. <xsl:when test="$units='em'">
  197. <xsl:value-of select="round($numeric-value * $pixels-per-inch div 6)"/>
  198. </xsl:when>
  199. <xsl:otherwise>
  200. <!-- If the input isn't valid, just return 100% -->
  201. <xsl:value-of select="'100%'"/>
  202. </xsl:otherwise>
  203. </xsl:choose>
  204. </xsl:template>
  205. <xsl:template name="replace">
  206. <xsl:param name="text" as="xs:string?"/>
  207. <xsl:param name="from" as="xs:string"/>
  208. <xsl:param name="to"/>
  209. <xsl:choose>
  210. <xsl:when test="contains($text, $from)">
  211. <xsl:sequence select="substring-before($text, $from)[string-length(.) gt 0]"/>
  212. <xsl:copy-of select="$to"/>
  213. <xsl:call-template name="replace">
  214. <xsl:with-param name="text" select="substring-after($text, $from)[string-length(.) gt 0]"/>
  215. <xsl:with-param name="from" select="$from"/>
  216. <xsl:with-param name="to" select="$to"/>
  217. </xsl:call-template>
  218. </xsl:when>
  219. <xsl:otherwise>
  220. <xsl:sequence select="$text"/>
  221. </xsl:otherwise>
  222. </xsl:choose>
  223. </xsl:template>
  224. <!-- replace all the blank in file name or directory with %20 -->
  225. <xsl:template name="replace-blank">
  226. <xsl:param name="file-origin"></xsl:param>
  227. <xsl:choose>
  228. <xsl:when test="contains($file-origin,' ')">
  229. <xsl:call-template name="replace-blank">
  230. <xsl:with-param name="file-origin">
  231. <xsl:value-of select="substring-before($file-origin,' ')"/>%20<xsl:value-of select="substring-after($file-origin,' ')"/>
  232. </xsl:with-param>
  233. </xsl:call-template>
  234. </xsl:when>
  235. <xsl:otherwise>
  236. <xsl:value-of select="$file-origin"/>
  237. </xsl:otherwise>
  238. </xsl:choose>
  239. </xsl:template>
  240. <!-- Return the portion of an HREF value up to the file's extension. This assumes
  241. that the file has an extension, and that the topic and/or element ID does not
  242. contain a period. Written to allow references such as com.example.dita.files/file.dita#topic -->
  243. <!-- Deprecated: use replace-extension instead -->
  244. <xsl:template match="*" mode="parseHrefUptoExtension">
  245. <xsl:param name="href" select="@href"/>
  246. <xsl:call-template name="output-message">
  247. <xsl:with-param name="id" select="'DOTX069W'"/>
  248. <xsl:with-param name="msgparams">%1=parseHrefUptoExtension</xsl:with-param>
  249. </xsl:call-template>
  250. <xsl:variable name="uptoDot" select="substring-before($href,'.')" as="xs:string"/>
  251. <xsl:variable name="afterDot" select="substring-after($href,'.')" as="xs:string"/>
  252. <xsl:value-of select="$uptoDot"/>
  253. <xsl:choose>
  254. <!-- No more periods, so this is at the extension -->
  255. <xsl:when test="not(contains($afterDot,'.'))"/>
  256. <!-- Multiple slashes; at least one must be a directory, so it's before the extension -->
  257. <xsl:when test="contains(substring-after($afterDot,'/'),'/')">
  258. <xsl:text>.</xsl:text>
  259. <xsl:value-of select="substring-before($afterDot,'/')"/>
  260. <xsl:text>/</xsl:text>
  261. <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="substring-after($afterDot,'/')"/></xsl:apply-templates>
  262. </xsl:when>
  263. <!-- Multiple periods, no slashes, no topic or element ID, so the file name contains more periods -->
  264. <xsl:when test="not(contains($afterDot,'#'))">
  265. <xsl:text>.</xsl:text>
  266. <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
  267. </xsl:when>
  268. <!-- Multiple periods, no slashes, with #. Move to next period. Needs additional work to support
  269. IDs containing periods. -->
  270. <xsl:otherwise>
  271. <xsl:text>.</xsl:text>
  272. <xsl:apply-templates select="." mode="parseHrefUptoExtension"><xsl:with-param name="href" select="$afterDot"/></xsl:apply-templates>
  273. </xsl:otherwise>
  274. </xsl:choose>
  275. </xsl:template>
  276. <!-- Get filename base -->
  277. <xsl:template name="getFileName">
  278. <xsl:param name="filename"/>
  279. <xsl:param name="extension"/>
  280. <xsl:choose>
  281. <xsl:when test="contains($filename, $extension)">
  282. <xsl:call-template name="substring-before-last">
  283. <xsl:with-param name="text" select="$filename"/>
  284. <xsl:with-param name="delim" select="$extension"/>
  285. </xsl:call-template>
  286. </xsl:when>
  287. <xsl:otherwise>
  288. <xsl:value-of select="$filename"/>
  289. </xsl:otherwise>
  290. </xsl:choose>
  291. </xsl:template>
  292. <!-- Replace file extension in a URI -->
  293. <xsl:template name="replace-extension" as="xs:string">
  294. <xsl:param name="filename" as="xs:string"/>
  295. <xsl:param name="extension" as="xs:string"/>
  296. <xsl:param name="ignore-fragment" select="false()"/>
  297. <xsl:variable name="f" as="xs:string">
  298. <xsl:value-of>
  299. <xsl:call-template name="substring-before-last">
  300. <xsl:with-param name="text">
  301. <xsl:choose>
  302. <xsl:when test="contains($filename, '#')">
  303. <xsl:value-of select="substring-before($filename, '#')"/>
  304. </xsl:when>
  305. <xsl:otherwise>
  306. <xsl:value-of select="$filename"/>
  307. </xsl:otherwise>
  308. </xsl:choose>
  309. </xsl:with-param>
  310. <xsl:with-param name="delim" select="'.'"/>
  311. </xsl:call-template>
  312. </xsl:value-of>
  313. </xsl:variable>
  314. <xsl:value-of>
  315. <xsl:if test="string($f)">
  316. <xsl:value-of select="concat($f, $extension)"/>
  317. </xsl:if>
  318. <xsl:if test="not($ignore-fragment) and contains($filename, '#')">
  319. <xsl:value-of select="concat('#', substring-after($filename, '#'))"/>
  320. </xsl:if>
  321. </xsl:value-of>
  322. </xsl:template>
  323. <xsl:function name="dita-ot:substring-before-last" as="xs:string?">
  324. <xsl:param name="text" as="xs:string"/>
  325. <xsl:param name="delim" as="xs:string"/>
  326. <xsl:call-template name="substring-before-last">
  327. <xsl:with-param name="text" select="$text"/>
  328. <xsl:with-param name="delim" select="$delim" />
  329. </xsl:call-template>
  330. </xsl:function>
  331. <xsl:template name="substring-before-last" as="xs:string?">
  332. <xsl:param name="text" as="xs:string"/>
  333. <xsl:param name="delim" as="xs:string"/>
  334. <xsl:param name="acc" as="xs:string?"/>
  335. <xsl:choose>
  336. <xsl:when test="string($text) and string($delim)">
  337. <xsl:variable name="tail" select="substring-after($text, $delim)" />
  338. <xsl:choose>
  339. <xsl:when test="contains($tail, $delim)">
  340. <xsl:call-template name="substring-before-last">
  341. <xsl:with-param name="text" select="$tail" />
  342. <xsl:with-param name="delim" select="$delim" />
  343. <xsl:with-param name="acc" select="concat($acc, substring-before($text, $delim), $delim)"/>
  344. </xsl:call-template>
  345. </xsl:when>
  346. <xsl:otherwise>
  347. <xsl:value-of select="concat($acc, substring-before($text, $delim))"/>
  348. </xsl:otherwise>
  349. </xsl:choose>
  350. </xsl:when>
  351. <xsl:otherwise>
  352. <xsl:sequence select="$acc"/>
  353. </xsl:otherwise>
  354. </xsl:choose>
  355. </xsl:template>
  356. <xsl:template match="processing-instruction('workdir-uri')" mode="get-work-dir">
  357. <xsl:value-of select="."/>
  358. </xsl:template>
  359. <xsl:template match="processing-instruction('path2project-uri')" mode="get-path2project">
  360. <xsl:choose>
  361. <!-- Backwards compatibility with path2project that is empty when current directory is the root directory -->
  362. <xsl:when test=". = './'"/>
  363. <xsl:otherwise>
  364. <xsl:value-of select="."/>
  365. </xsl:otherwise>
  366. </xsl:choose>
  367. </xsl:template>
  368. <xsl:template match="processing-instruction('path2project')" mode="get-path2project">
  369. <xsl:call-template name="get-path2project">
  370. <xsl:with-param name="s" select="."/>
  371. </xsl:call-template>
  372. </xsl:template>
  373. <xsl:template name="get-path2project">
  374. <!-- Deal with being handed a Windows backslashed path by accident. -->
  375. <!-- This code only changes \ to / and doesn't handle the many other situations
  376. where a URI differs from a file path. Hopefully they don't occur in path2proj anyway. -->
  377. <xsl:param name="s"/>
  378. <xsl:choose>
  379. <xsl:when test="contains($s, '\')">
  380. <xsl:value-of select="substring-before($s, '\')"/>
  381. <xsl:text>/</xsl:text>
  382. <xsl:call-template name="get-path2project">
  383. <xsl:with-param name="s" select="substring-after($s, '\')"/>
  384. </xsl:call-template>
  385. </xsl:when>
  386. <xsl:otherwise>
  387. <xsl:value-of select="$s"/>
  388. </xsl:otherwise>
  389. </xsl:choose>
  390. </xsl:template>
  391. <xsl:function name="dita-ot:get-closest-topic" as="element()">
  392. <xsl:param name="n" as="node()"/>
  393. <xsl:sequence
  394. select="$n/ancestor-or-self::*[contains(@class, ' topic/topic ')][1]"/>
  395. </xsl:function>
  396. <xsl:include href="plugin:org.dita.base:xsl/common/uri-utils.xsl"/>
  397. </xsl:stylesheet>