source: clfs-embedded/BOOK/stylesheets/lfs-xsl/docbook-xsl-snapshot/common/utility.xsl @ 9882b55

Last change on this file since 9882b55 was 9882b55, checked in by Manuel Canales Esparcia <manuel@…>, 15 years ago

Added the new lfs-xsl stylesheets.

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