summaryrefslogtreecommitdiff
path: root/xslt/docbook/common/db-common.xsl
blob: b52ff83e688c4abfbe6afa79d65c1fb93b9e4540 (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
<?xml version='1.0' encoding='UTF-8'?><!-- -*- indent-tabs-mode: nil -*- -->
<!--
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option) any
later version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.

You should have received a copy of the GNU Lesser General Public License
along with this program; see the file COPYING.LGPL.  If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
-->

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:str="http://exslt.org/strings"
                exclude-result-prefixes="str"
                version="1.0">

<!--!!==========================================================================
DocBook Common

This stylesheet module provides utility templates for DocBook that are
independant of the target format.
-->

<xsl:key name="idkey" match="*" use="@id"/>


<!--**==========================================================================
db.linenumbering
Numbers each line in a verbatim environment
$node: The verbatim element to create the line numbering for
$number: The starting line number

This template outputs a string with line numbers for each line in a verbatim
elements.  Each line number is on its own line, allowing the output string to
be placed to the side of the verbatim output.
-->
<xsl:template name="db.linenumbering">
  <xsl:param name="node" select="."/>
  <xsl:param name="number" select="1"/>
  <xsl:param name="string">
    <xsl:choose>
      <xsl:when test="$node/node()[1]/self::text() and starts-with($node/node()[1], '&#x000A;')">
        <xsl:value-of select="substring-after(string($node), '&#x000A;')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="string($node)"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:param>
  <xsl:choose>
    <xsl:when test="contains($string, '&#x000A;')">
      <xsl:number value="$number"/>
      <xsl:text>&#x000A;</xsl:text>
      <xsl:call-template name="db.linenumbering">
        <xsl:with-param name="node" select="$node"/>
        <xsl:with-param name="number" select="$number + 1"/>
        <xsl:with-param name="string"
                        select="substring-after($string, '&#x000A;')"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:when test="string-length($string) != 0">
      <xsl:number value="$number"/>
      <xsl:text>&#x000A;</xsl:text>
    </xsl:when>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
db.linenumbering.start
Determines the starting line number for a verbatim element
$node: The verbatim element to determine the starting line number for

This template determines the starting line number for a verbatim element using
the #{continuation} attribute.  The template finds the first preceding element
of the same name, counts its lines, and handles any #{startinglinenumber} or
#{continuation} element it finds on that element.
-->
<xsl:template name="db.linenumbering.start">
  <xsl:param name="node" select="."/>
  <xsl:choose>
    <xsl:when test="$node/@startinglinenumber">
      <xsl:value-of select="$node/@startinglinenumber"/>
    </xsl:when>
    <xsl:when test="$node/@continuation">
      <xsl:variable name="prev" select="$node/preceding::*[name(.) = name($node)][1]"/>
      <xsl:choose>
        <xsl:when test="count($prev) = 0">1</xsl:when>
        <xsl:otherwise>
          <xsl:variable name="prevcount">
            <xsl:value-of select="count(str:split(string($prev), '&#x000A;'))"/>
          </xsl:variable>
          <xsl:variable name="prevstart">
            <xsl:call-template name="db.linenumbering.start">
              <xsl:with-param name="node" select="$prev"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="$prevstart + $prevcount"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:when>
    <xsl:otherwise>1</xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
db.orderedlist.start
Determines the number to use for the first #{listitem} in an #{orderedlist}
$node: The #{orderedlist} element to use

This template determines the starting number for an #{orderedlist} element using
the #{continuation} attribute.  Thi template finds the first preceding #{orderedlist}
element and counts its list items.  If that element also uses the #{continuation},
this template calls itself recursively to add that element's starting line number
to its list item count.
-->
<xsl:template name="db.orderedlist.start">
  <xsl:param name="node" select="."/>
  <xsl:choose>
    <xsl:when test="$node/@continutation != 'continues'">1</xsl:when>
    <xsl:otherwise>
      <xsl:variable name="prevlist"
                    select="$node/preceding::orderedlist[1]"/>
      <xsl:choose>
        <xsl:when test="count($prevlist) = 0">1</xsl:when>
        <xsl:otherwise>
          <xsl:variable name="prevlength" select="count($prevlist/listitem)"/>
          <xsl:variable name="prevstart">
            <xsl:call-template name="db.orderedlist.start">
              <xsl:with-param name="node" select="$prevlist"/>
            </xsl:call-template>
          </xsl:variable>
          <xsl:value-of select="$prevstart + $prevlength"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>


<!--**==========================================================================
db.personname
Outputs the name of a person
$node: The element containing tags such as #{firstname} and #{surname}
$lang: The language rules to use to construct the name

This template outputs the name of a person as modelled by the #{personname}
element.  The #{personname} element allows authors to mark up components of
a person's name, such as the person's first name and surname.  This template
assembled those into a string.
-->
<xsl:template name="db.personname">
  <xsl:param name="node" select="."/>
  <!-- FIXME: call i18n.locale -->
  <xsl:param name="lang" select="ancestor-or-self::*[@lang][1]/@lang"/>

  <!-- FIXME: Use xsl:choose for different language rules -->
  <xsl:if test="$node/honorific">
    <xsl:apply-templates select="$node/honorific[1]"/>
  </xsl:if>
  <xsl:choose>
    <xsl:when test="$node/@role = 'family-given'">
      <xsl:if test="$node/surname">
        <xsl:if test="$node/honorific">
          <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$node/surname[1]"/>
      </xsl:if>
      <xsl:if test="$node/othername">
        <xsl:if test="$node/honorific or $node/surname">
          <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$node/othername[1]"/>
      </xsl:if>
      <xsl:if test="$node/firstname">
        <xsl:if test="$node/honorific or $node/surname or $node/othername">
          <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$node/firstname[1]"/>
      </xsl:if>
    </xsl:when>
    <xsl:otherwise>
      <xsl:if test="$node/firstname">
        <xsl:if test="$node/honorific">
          <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$node/firstname[1]"/>
      </xsl:if>
      <xsl:if test="$node/othername">
        <xsl:if test="$node/honorific or $node/firstname">
          <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$node/othername[1]"/>
      </xsl:if>
      <xsl:if test="$node/surname">
        <xsl:if test="$node/honorific or $node/firstname or $node/othername">
          <xsl:text> </xsl:text>
        </xsl:if>
        <xsl:apply-templates select="$node/surname[1]"/>
      </xsl:if>
    </xsl:otherwise>
  </xsl:choose>
  <xsl:if test="$node/lineage">
    <xsl:text>, </xsl:text>
    <xsl:apply-templates select="$node/lineage[1]"/>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>