utility.xsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?xml version='1.0'?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
  4. xmlns:dyn="http://exslt.org/dynamic"
  5. xmlns:saxon="http://icl.com/saxon"
  6. exclude-result-prefixes="doc dyn saxon"
  7. version='1.0'>
  8. <!-- ********************************************************************
  9. $Id$
  10. ********************************************************************
  11. This file is part of the XSL DocBook Stylesheet distribution.
  12. See ../README or http://docbook.sf.net/release/xsl/current/ for
  13. copyright and other information.
  14. ******************************************************************** -->
  15. <doc:reference xmlns="">
  16. <referenceinfo>
  17. <releaseinfo role="meta">
  18. $Id$
  19. </releaseinfo>
  20. <corpauthor>The DocBook Project Development Team</corpauthor>
  21. <copyright><year>2007</year>
  22. <holder>The DocBook Project</holder>
  23. </copyright>
  24. </referenceinfo>
  25. <title>Utility Template Reference</title>
  26. <partintro id="partintro">
  27. <title>Introduction</title>
  28. <para>This is technical reference documentation for the
  29. miscellaneous utility templates in the DocBook XSL
  30. Stylesheets.</para>
  31. <note>
  32. <para>These templates are defined in a separate file from the set
  33. of “common” templates because some of the comment templates
  34. reference DocBook XSL stylesheet parameters, requiring the
  35. entire set of parameters to be imported/included in any
  36. stylesheet that imports/includes the common templates.</para>
  37. <para>The utility templates don’t import or include any DocBook
  38. XSL stylesheet parameters, so the utility templates can be used
  39. without importing the whole set of parameters.</para>
  40. </note>
  41. <para>The following documentation is not intended to be
  42. <quote>user</quote> documentation. It is provided for developers
  43. writing customization layers for the stylesheets, and for anyone
  44. who's interested in <quote>how it works</quote>.</para>
  45. </partintro>
  46. </doc:reference>
  47. <!-- ====================================================================== -->
  48. <doc:template name="log.message" xmlns="">
  49. <refpurpose>Logs/emits formatted notes and warnings</refpurpose>
  50. <refdescription id="log.message-desc">
  51. <para>The <function>log.message</function> template is a utility
  52. template for logging/emitting formatted messages&#xa0;– that is,
  53. notes and warnings, along with a given log “level” and an
  54. identifier for the “source” that the message relates to.</para>
  55. </refdescription>
  56. <refparameter id="log.message-params">
  57. <variablelist>
  58. <varlistentry>
  59. <term>level</term>
  60. <listitem>
  61. <para>Text to log/emit in the message-level field to
  62. indicate the message level
  63. (<literal>Note</literal> or
  64. <literal>Warning</literal>)</para>
  65. </listitem>
  66. </varlistentry>
  67. <varlistentry>
  68. <term>source</term>
  69. <listitem>
  70. <para>Text to log/emit in the source field to identify the
  71. “source” to which the notification/warning relates.
  72. This can be any arbitrary string, but because the
  73. message lacks line and column numbers to identify the
  74. exact part of the source document to which it
  75. relates, the intention is that the value you pass
  76. into the <literal>source</literal> parameter should
  77. give the user some way to identify the portion of
  78. their source document on which to take potentially
  79. take action in response to the log message (for
  80. example, to edit, change, or add content).</para>
  81. <para>So the <literal>source</literal> value should be,
  82. for example, an ID, book/chapter/article title, title
  83. of some formal object, or even a string giving an
  84. XPath expression.</para>
  85. </listitem>
  86. </varlistentry>
  87. <varlistentry>
  88. <term>context-desc</term>
  89. <listitem>
  90. <para>Text to log/emit in the context-description field to
  91. describe the context for the message.</para>
  92. </listitem>
  93. </varlistentry>
  94. <varlistentry>
  95. <term>context-desc-field-length</term>
  96. <listitem>
  97. <para>Specifies length of the context-description field
  98. (in characters); default is 12</para>
  99. <para>If the text specified by the
  100. <literal>context-desc</literal> parameter is longer
  101. than the number of characters specified in
  102. <literal>context-desc-field-length</literal>, it is
  103. truncated to <literal>context-desc-field-length</literal>
  104. (12 characters by default).</para>
  105. <para>If the specified text is shorter than
  106. <literal>context-desc-field-length</literal>,
  107. it is right-padded out to
  108. <literal>context-desc-field-length</literal> (12 by
  109. default).</para>
  110. <para>If no value has been specified for the
  111. <literal>context-desc</literal> parameter, the field is
  112. left empty and the text of the log message begins with
  113. the value of the <literal>message</literal>
  114. parameter.</para>
  115. </listitem>
  116. </varlistentry>
  117. <varlistentry>
  118. <term>message</term>
  119. <listitem>
  120. <para>Text to log/emit in the actual message field</para>
  121. </listitem>
  122. </varlistentry>
  123. <varlistentry>
  124. <term>message-field-length</term>
  125. <listitem>
  126. <para>Specifies length of the message
  127. field (in characters); default is 45</para>
  128. </listitem>
  129. </varlistentry>
  130. </variablelist>
  131. </refparameter>
  132. <refreturn id="log.message-returns">
  133. <para>Outputs a message (generally, to standard error).</para></refreturn>
  134. </doc:template>
  135. <xsl:template name="log.message">
  136. <xsl:param name="level"/>
  137. <xsl:param name="source"/>
  138. <xsl:param name="context-desc"/>
  139. <xsl:param name="context-desc-field-length">12</xsl:param>
  140. <xsl:param name="context-desc-padded">
  141. <xsl:if test="not($context-desc = '')">
  142. <xsl:call-template name="pad-string">
  143. <xsl:with-param name="leftRight">right</xsl:with-param>
  144. <xsl:with-param name="padVar"
  145. select="substring($context-desc, 1, $context-desc-field-length)"/>
  146. <xsl:with-param name="length" select="$context-desc-field-length"/>
  147. </xsl:call-template>
  148. </xsl:if>
  149. </xsl:param>
  150. <xsl:param name="message"/>
  151. <xsl:param name="message-field-length" select="45"/>
  152. <xsl:param name="message-padded">
  153. <xsl:variable name="spaces-for-blank-level">
  154. <!-- * if the level field is blank, we'll need to pad out -->
  155. <!-- * the message field with spaces to compensate -->
  156. <xsl:choose>
  157. <xsl:when test="$level = ''">
  158. <xsl:value-of select="4 + 2"/>
  159. <!-- * 4 = hard-coded length of comment text ("Note" or "Warn") -->
  160. <!-- * + 2 = length of colon-plus-space separator ": " -->
  161. </xsl:when>
  162. <xsl:otherwise>
  163. <xsl:value-of select="0"/>
  164. </xsl:otherwise>
  165. </xsl:choose>
  166. </xsl:variable>
  167. <xsl:variable name="spaces-for-blank-context-desc">
  168. <!-- * if the context-description field is blank, we'll need -->
  169. <!-- * to pad out the message field with spaces to compensate -->
  170. <xsl:choose>
  171. <xsl:when test="$context-desc = ''">
  172. <xsl:value-of select="$context-desc-field-length + 2"/>
  173. <!-- * + 2 = length of colon-plus-space separator ": " -->
  174. </xsl:when>
  175. <xsl:otherwise>
  176. <xsl:value-of select="0"/>
  177. </xsl:otherwise>
  178. </xsl:choose>
  179. </xsl:variable>
  180. <xsl:variable name="extra-spaces"
  181. select="$spaces-for-blank-level + $spaces-for-blank-context-desc"/>
  182. <xsl:call-template name="pad-string">
  183. <xsl:with-param name="leftRight">right</xsl:with-param>
  184. <xsl:with-param name="padVar"
  185. select="substring($message, 1, ($message-field-length + $extra-spaces))"/>
  186. <xsl:with-param name="length"
  187. select="$message-field-length + $extra-spaces"/>
  188. </xsl:call-template>
  189. </xsl:param>
  190. <!-- * emit the actual log message -->
  191. <xsl:message>
  192. <xsl:if test="not($level = '')">
  193. <xsl:value-of select="$level"/>
  194. <xsl:text>: </xsl:text>
  195. </xsl:if>
  196. <xsl:if test="not($context-desc = '')">
  197. <xsl:value-of select="$context-desc-padded"/>
  198. <xsl:text>: </xsl:text>
  199. </xsl:if>
  200. <xsl:value-of select="$message-padded"/>
  201. <xsl:text> </xsl:text>
  202. <xsl:value-of select="$source"/>
  203. </xsl:message>
  204. </xsl:template>
  205. <!-- ===================================== -->
  206. <doc:template name="get.doc.title" xmlns="">
  207. <refpurpose>Gets a title from the current document</refpurpose>
  208. <refdescription id="get.doc.title-desc">
  209. <para>The <function>get.doc.title</function> template is a
  210. utility template for returning the first title found in the
  211. current document.</para>
  212. </refdescription>
  213. <refreturn id="get.doc.title-returns">
  214. <para>Returns a string containing some identifying title for the
  215. current document .</para></refreturn>
  216. </doc:template>
  217. <xsl:template name="get.doc.title">
  218. <xsl:choose>
  219. <xsl:when test="//*[local-name() = 'title'
  220. or local-name() = 'refname']">
  221. <xsl:value-of select="//*[local-name() = 'title'
  222. or local-name() = 'refname'][1]"/>
  223. </xsl:when>
  224. <xsl:when test="substring(local-name(*[1]),
  225. string-length(local-name(*[1])-3) = 'info')
  226. and *[1]/*[local-name() = 'title']">
  227. <xsl:value-of select="*[1]/*[local-name() = 'title'][1]"/>
  228. </xsl:when>
  229. </xsl:choose>
  230. </xsl:template>
  231. <!-- ===================================== -->
  232. <doc:template name="pad-string" xmlns="">
  233. <refpurpose>Right-pads or left-pads a string out to a certain length</refpurpose>
  234. <refdescription id="pad-string-desc">
  235. <para>This function takes string <parameter>padVar</parameter> and
  236. pads it out in the direction <parameter>rightLeft</parameter> to
  237. the string-length <parameter>length</parameter>, using string
  238. <parameter>padChar</parameter> (a space character by default) as
  239. the padding string (note that <parameter>padChar</parameter> can
  240. be a string; it is not limited to just being a single
  241. character).</para>
  242. <note>
  243. <para>This function began as a copy of Nate Austin's
  244. <function>prepend-pad</function> function in the <ulink
  245. url="http://www.dpawson.co.uk/xsl/sect2/padding.html" >Padding
  246. Content</ulink> section of Dave Pawson's <ulink
  247. url="http://www.dpawson.co.uk/xsl/index.html" >XSLT
  248. FAQ</ulink>.</para>
  249. </note>
  250. </refdescription>
  251. <refreturn id="pad-string-returns">
  252. <para>Returns a (padded) string.</para></refreturn>
  253. </doc:template>
  254. <xsl:template name="pad-string">
  255. <!-- * recursive template to right/left pad the value with -->
  256. <!-- * whatever padChar is passed in -->
  257. <xsl:param name="padChar" select="' '"/>
  258. <xsl:param name="leftRight">left</xsl:param>
  259. <xsl:param name="padVar"/>
  260. <xsl:param name="length"/>
  261. <xsl:choose>
  262. <xsl:when test="string-length($padVar) &lt; $length">
  263. <xsl:call-template name="pad-string">
  264. <xsl:with-param name="padChar" select="$padChar"/>
  265. <xsl:with-param name="leftRight" select="$leftRight"/>
  266. <xsl:with-param name="padVar">
  267. <xsl:choose>
  268. <!-- * determine whether string should be -->
  269. <!-- * right- or left-padded -->
  270. <xsl:when test="$leftRight = 'left'">
  271. <!-- * pad it to left -->
  272. <xsl:value-of select="concat($padChar,$padVar)"/>
  273. </xsl:when>
  274. <xsl:otherwise>
  275. <!-- * otherwise, right-pad the string -->
  276. <xsl:value-of select="concat($padVar,$padChar)"/>
  277. </xsl:otherwise>
  278. </xsl:choose>
  279. </xsl:with-param>
  280. <xsl:with-param name="length" select="$length"/>
  281. </xsl:call-template>
  282. </xsl:when>
  283. <xsl:otherwise>
  284. <xsl:value-of
  285. select="substring($padVar,string-length($padVar) - $length + 1)"/>
  286. </xsl:otherwise>
  287. </xsl:choose>
  288. </xsl:template>
  289. </xsl:stylesheet>