aboutsummaryrefslogtreecommitdiffstats
path: root/stylesheets/lfs-xsl/docbook-xsl-snapshot/common/utility.xsl
blob: 95cf7b37b31c2990e786b616a4e56e4467274d5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:doc="http://nwalsh.com/xsl/documentation/1.0"
                xmlns:dyn="http://exslt.org/dynamic"
                xmlns:saxon="http://icl.com/saxon"
                xmlns:xlink="http://www.w3.org/1999/xlink"
                exclude-result-prefixes="doc dyn saxon"
                version='1.0'>

<!-- ********************************************************************
     $Id$
     ********************************************************************

     This file is part of the XSL DocBook Stylesheet distribution.
     See ../README or http://docbook.sf.net/release/xsl/current/ for
     copyright and other information.

     ******************************************************************** -->
<doc:reference xmlns="" xml:id="utility">
  <info>
    <title>Common » Utility Template Reference</title>
    <releaseinfo role="meta">
      $Id$
    </releaseinfo>
  </info>
  <!-- * yes, partintro is a valid child of a reference... -->
  <partintro xml:id="partintro">
    <title>Introduction</title>
    <para>This is technical reference documentation for the
      miscellaneous utility templates in the DocBook XSL
      Stylesheets.</para>
    <note>
      <para>These templates are defined in a separate file from the set
        of “common” templates because some of the common templates
        reference DocBook XSL stylesheet parameters, requiring the
        entire set of parameters to be imported/included in any
        stylesheet that imports/includes the common templates.</para>
      <para>The utility templates don’t import or include any DocBook
        XSL stylesheet parameters, so the utility templates can be used
        without importing the whole set of parameters.</para>
    </note>
    <para>This is not intended to be user documentation. It is
      provided for developers writing customization layers for the
      stylesheets.</para>
  </partintro>
</doc:reference>

<!-- ====================================================================== -->

<doc:template name="log.message" xmlns="">
  <refpurpose>Logs/emits formatted notes and warnings</refpurpose>

  <refdescription id="log.message-desc">
    <para>The <function>log.message</function> template is a utility
    template for logging/emitting formatted messages&#xa0; that is,
    notes and warnings, along with a given log “level” and an
    identifier for the “source” that the message relates to.</para>
  </refdescription>

  <refparameter id="log.message-params">
    <variablelist>
      <varlistentry><term>level</term>
        <listitem>
          <para>Text to log/emit in the message-level field to
            indicate the message level
          (<literal>Note</literal> or
          <literal>Warning</literal>)</para>
        </listitem>
      </varlistentry>
      <varlistentry><term>source</term>
        <listitem>
          <para>Text to log/emit in the source field to identify the
            “source” to which the notification/warning relates.
            This can be any arbitrary string, but because the
            message lacks line and column numbers to identify the
            exact part of the source document to which it
            relates, the intention is that the value you pass
            into the <literal>source</literal> parameter should
            give the user some way to identify the portion of
            their source document on which to take potentially
            take action in response to the log message (for
            example, to edit, change, or add content).</para>
          <para>So the <literal>source</literal> value should be,
            for example, an ID, book/chapter/article title, title
            of some formal object, or even a string giving an
            XPath expression.</para>
        </listitem>
      </varlistentry>
      <varlistentry><term>context-desc</term>
        <listitem>
          <para>Text to log/emit in the context-description field to
            describe the context for the message.</para>
        </listitem>
      </varlistentry>
      <varlistentry><term>context-desc-field-length</term>
        <listitem>
          <para>Specifies length of the context-description field
            (in characters); default is 12</para>
          <para>If the text specified by the
            <literal>context-desc</literal> parameter is longer
            than the number of characters specified in
            <literal>context-desc-field-length</literal>, it is
            truncated to <literal>context-desc-field-length</literal>
            (12 characters by default).</para>
          <para>If the specified text is shorter than
            <literal>context-desc-field-length</literal>,
          it is right-padded out to
          <literal>context-desc-field-length</literal> (12 by
          default).</para>
        <para>If no value has been specified for the
          <literal>context-desc</literal> parameter, the field is
          left empty and the text of the log message begins with
          the value of the <literal>message</literal>
          parameter.</para>
        </listitem>
      </varlistentry>
      <varlistentry><term>message</term>
        <listitem>
          <para>Text to log/emit in the actual message field</para>
        </listitem>
      </varlistentry>
      <varlistentry><term>message-field-length</term>
        <listitem>
          <para>Specifies length of the message
            field (in characters); default is 45</para>
        </listitem>
      </varlistentry>
    </variablelist>
  </refparameter>
  <refreturn id="log.message-returns">
  <para>Outputs a message (generally, to standard error).</para></refreturn>
</doc:template>
<xsl:template name="log.message">
  <xsl:param name="level"/>
  <xsl:param name="source"/>
  <xsl:param name="context-desc"/>
  <xsl:param name="context-desc-field-length">12</xsl:param>
  <xsl:param name="context-desc-padded">
    <xsl:if test="not($context-desc = '')">
      <xsl:call-template name="pad-string">
        <xsl:with-param name="leftRight">right</xsl:with-param>
        <xsl:with-param name="padVar"
          select="substring($context-desc, 1, $context-desc-field-length)"/>
        <xsl:with-param name="length" select="$context-desc-field-length"/>
      </xsl:call-template>
    </xsl:if>
  </xsl:param>
  <xsl:param name="message"/>
  <xsl:param name="message-field-length" select="45"/>
  <xsl:param name="message-padded">
    <xsl:variable name="spaces-for-blank-level">
      <!-- * if the level field is blank, we'll need to pad out -->
      <!-- * the message field with spaces to compensate -->
      <xsl:choose>
        <xsl:when test="$level = ''">
          <xsl:value-of select="4 + 2"/>
          <!-- * 4 = hard-coded length of comment text ("Note" or "Warn") -->
          <!-- * + 2 = length of colon-plus-space separator ": " -->
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="0"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="spaces-for-blank-context-desc">
      <!-- * if the context-description field is blank, we'll need -->
      <!-- * to pad out the message field with spaces to compensate -->
      <xsl:choose>
        <xsl:when test="$context-desc = ''">
          <xsl:value-of select="$context-desc-field-length + 2"/>
          <!-- * + 2 = length of colon-plus-space separator ": " -->
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="0"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>
    <xsl:variable name="extra-spaces"
      select="$spaces-for-blank-level + $spaces-for-blank-context-desc"/>
    <xsl:call-template name="pad-string">
      <xsl:with-param name="leftRight">right</xsl:with-param>
      <xsl:with-param name="padVar"
        select="substring($message, 1, ($message-field-length + $extra-spaces))"/>
      <xsl:with-param name="length"
        select="$message-field-length + $extra-spaces"/>
    </xsl:call-template>
  </xsl:param>
  <!-- * emit the actual log message -->
  <xsl:message>
    <xsl:if test="not($level = '')">
      <xsl:value-of select="$level"/>
      <xsl:text>: </xsl:text>
    </xsl:if>
    <xsl:if test="not($context-desc = '')">
      <xsl:value-of select="$context-desc-padded"/>
      <xsl:text>: </xsl:text>
    </xsl:if>
    <xsl:value-of select="$message-padded"/>
    <xsl:text>  </xsl:text>
    <xsl:value-of select="$source"/>
  </xsl:message>
</xsl:template>

<!-- ===================================== -->
<doc:template name="get.doc.title" xmlns="">
  <refpurpose>Gets a title from the current document</refpurpose>
  <refdescription id="get.doc.title-desc">
    <para>The <function>get.doc.title</function> template is a
      utility template for returning the first title found in the
      current document.</para>
  </refdescription>
  <refreturn id="get.doc.title-returns">
  <para>Returns a string containing some identifying title for the
    current document .</para></refreturn>
</doc:template>
<xsl:template name="get.doc.title">
  <xsl:choose>
    <xsl:when test="//*[local-name() = 'title'
      or local-name() = 'refname']">
      <xsl:value-of select="//*[local-name() = 'title'
        or local-name() = 'refname'][1]"/>
    </xsl:when>
    <xsl:when test="substring(local-name(*[1]),
      string-length(local-name(*[1])-3) = 'info')
      and *[1]/*[local-name() = 'title']">
      <xsl:value-of select="*[1]/*[local-name() = 'title'][1]"/>
    </xsl:when>
  </xsl:choose>
</xsl:template>

<!-- ===================================== -->
<doc:template name="pad-string" xmlns="">
  <refpurpose>Right-pads or left-pads a string out to a certain length</refpurpose>
  <refdescription id="pad-string-desc">
    <para>This function takes string <parameter>padVar</parameter> and
      pads it out in the direction <parameter>rightLeft</parameter> to
      the string-length <parameter>length</parameter>, using string
      <parameter>padChar</parameter> (a space character by default) as
      the padding string (note that <parameter>padChar</parameter> can
      be a string; it is not limited to just being a single
      character).</para>
    <note>
      <para>This function began as a copy of Nate Austin's
        <function>prepend-pad</function> function in the <link
          xlink:href="http://www.dpawson.co.uk/xsl/sect2/padding.html" >Padding
          Content</link> section of Dave Pawson's <link
          xlink:href="http://www.dpawson.co.uk/xsl/index.html" >XSLT
          FAQ</link>.</para>
    </note>
  </refdescription>
  <refreturn id="pad-string-returns">
  <para>Returns a (padded) string.</para></refreturn>
</doc:template>
<xsl:template name="pad-string">
  <!-- * recursive template to right/left pad the value with -->
  <!-- * whatever padChar is passed in -->
  <xsl:param name="padChar" select="' '"/>
  <xsl:param name="leftRight">left</xsl:param>
  <xsl:param name="padVar"/>
  <xsl:param name="length"/>
  <xsl:choose>
    <xsl:when test="string-length($padVar) &lt; $length">
      <xsl:call-template name="pad-string">
        <xsl:with-param name="padChar" select="$padChar"/>
        <xsl:with-param name="leftRight" select="$leftRight"/>
        <xsl:with-param name="padVar">
          <xsl:choose>
            <!-- * determine whether string should be -->
            <!-- * right- or left-padded -->
            <xsl:when test="$leftRight = 'left'">
              <!-- * pad it to left -->
              <xsl:value-of select="concat($padChar,$padVar)"/>
            </xsl:when>
            <xsl:otherwise>
              <!-- * otherwise, right-pad the string -->
              <xsl:value-of select="concat($padVar,$padChar)"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:with-param>
        <xsl:with-param name="length" select="$length"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of 
        select="substring($padVar,string-length($padVar) - $length + 1)"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

</xsl:stylesheet>