utility.xsl 12 KB

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