summaryrefslogtreecommitdiffstats
path: root/xslt/tellico-common.xsl
blob: 5a75efc57df8c22222b6279bfd958578536b6fdd (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:tc="http://periapsis.org/tellico/"
                exclude-result-prefixes="tc"
                version="1.0">

<!--
   ===================================================================
   Tellico XSLT file - some common templates.

   Copyright (C) 2004-2006 Robby Stephenson - robby@periapsis.org

   This XSLT stylesheet is designed to be used with the 'Tellico'
   application, which can be found at http://www.periapsis.org/tellico/
   ===================================================================
-->

<xsl:output method="html"
            indent="yes"
            doctype-public="-//W3C//DTD HTML 4.01//EN"
            doctype-system="http://www.w3.org/TR/html4/strict.dtd"
            encoding="utf-8"/>

<!-- Template for checking syntax version -->
<xsl:template name="syntax-version">
 <xsl:param name="this-version"/>
 <xsl:param name="data-version"/>
 <xsl:if test="$data-version &gt; $this-version">
  <xsl:message>
   <xsl:text>This stylesheet was designed for Tellico DTD version </xsl:text>
   <xsl:value-of select="$this-version"/>
   <xsl:text> or earlier, &#xa;but the input data file is version </xsl:text>
   <xsl:value-of select="$data-version"/>
   <xsl:text>. There might be some &#xa;problems with the output.</xsl:text>
  </xsl:message>
 </xsl:if>
</xsl:template>

<!-- template for creating attributes to scale an image to a max boundary size -->
<xsl:template name="image-size">
 <xsl:param name="limit-height" select="'0'"/>
 <xsl:param name="limit-width" select="'0'"/>
 <xsl:param name="image"/>

 <xsl:variable name="actual-width" select="$image/@width"/>
 <xsl:variable name="actual-height" select="$image/@height"/>

 <xsl:choose>
  <xsl:when test="$limit-width &gt; 0 and $limit-height &gt; 0 and 
                  ($actual-width &gt; $limit-width or $actual-height &gt; $limit-height)">

   <xsl:choose>

    <xsl:when test="$actual-width * $limit-height &lt; $actual-height * $limit-width">
     <xsl:attribute name="height">
      <xsl:value-of select="round($limit-height)"/>
     </xsl:attribute>
     <xsl:attribute name="width">
      <xsl:value-of select="round($actual-width * $limit-height div $actual-height)"/>
     </xsl:attribute>
    </xsl:when>

    <xsl:otherwise>
     <xsl:attribute name="width">
      <xsl:value-of select="round($limit-width)"/>
     </xsl:attribute>
     <xsl:attribute name="height">
      <xsl:value-of select="round($actual-height * $limit-width div $actual-width)"/>
     </xsl:attribute>
    </xsl:otherwise>

   </xsl:choose>

  </xsl:when>

  <!-- if both are smaller, no change -->
  <xsl:when test="$actual-width &gt; 0 and $actual-height &gt; 0">
   <xsl:attribute name="width">
    <xsl:value-of select="$actual-width"/>
   </xsl:attribute>
   <xsl:attribute name="height">
    <xsl:value-of select="$actual-height"/>
   </xsl:attribute>
  </xsl:when>

 </xsl:choose>
</xsl:template>

<!-- template for outputing most value types -->
<xsl:template name="simple-field-value">
 <xsl:param name="entry"/>
 <xsl:param name="field"/>

 <!-- if the field has multiple values, then there is
      no child of the entry with the field name -->
 <xsl:variable name="child" select="$entry/*[local-name(.)=$field]"/>
 <xsl:choose>
  <xsl:when test="$child">
   <xsl:variable name="f" select="$entry/../tc:fields/tc:field[@name = $field]"/>

   <!-- if the field is a bool type, just ouput an X, or use data image -->
   <xsl:choose>
    <!-- paragraphs need to have output escaping disabled so HTML works -->
    <xsl:when test="$f/@type=2">
     <xsl:value-of select="$child" disable-output-escaping="yes"/>
    </xsl:when>

    <xsl:when test="$f/@type=4">
     <img height="14" alt="&#x2713;">
      <xsl:attribute name="src">
       <xsl:value-of select="concat($datadir,'pics/checkmark.png')"/>
      </xsl:attribute>
     </img>
    </xsl:when>

    <!-- if it's a url, then add a hyperlink -->
    <xsl:when test="$f/@type=7">
     <a href="{$child}">
      <!-- The Amazon Web Services license requires the link -->
      <xsl:choose>
       <xsl:when test="$field = 'amazon'">
        <xsl:text>Buy from Amazon.com</xsl:text>
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="$child"/>
       </xsl:otherwise>
      </xsl:choose>
     </a>
    </xsl:when>

    <!-- if it's a date, format with hyphens -->
    <xsl:when test="$f/@type=12">
     <xsl:value-of select="$child/tc:year"/>
     <xsl:text>-</xsl:text>
     <xsl:value-of select="format-number($child/tc:month,'00')"/>
     <xsl:text>-</xsl:text>
     <xsl:value-of select="format-number($child/tc:day,'00')"/>
    </xsl:when>

    <!-- special case for rating -->
    <xsl:when test="$f/@type=14">
     <!-- get the number, could be 10, so can't just grab first digit -->
     <xsl:variable name="n">
      <xsl:choose>
       <xsl:when test="number(substring($child,1,1))">
        <xsl:choose>
         <xsl:when test="number(substring($child,1,2)) &lt; 11">
          <xsl:value-of select="number(substring($child,1,2))"/>
         </xsl:when>
         <xsl:otherwise>
          <xsl:value-of select="number(substring($child,1,1))"/>
         </xsl:otherwise>
        </xsl:choose>
       </xsl:when>
       <xsl:otherwise>
        <xsl:value-of select="false()"/>
       </xsl:otherwise>
      </xsl:choose>
     </xsl:variable>
     <xsl:if test="$n &gt; 0">
      <!-- the image is really 18 pixels high, but make it smaller to match default font -->
      <img height="14">
       <xsl:attribute name="src">
        <xsl:value-of select="concat($datadir,'pics/stars',$n,'.png')"/>
       </xsl:attribute>
       <xsl:attribute name="alt">
        <xsl:value-of select="concat($n,' stars')"/>
       </xsl:attribute>
      </img>
     </xsl:if>
     <xsl:if test="not($n)">
      <xsl:value-of select="$child"/>     
     </xsl:if>
    </xsl:when>
    
    <xsl:otherwise>
     <xsl:value-of select="$child"/>
     <!-- hack for running-time in videos -->
     <!--
     <xsl:if test="$field='running-time' and key('fieldsByName',$field)/@type=6">
      <xsl:text> minutes</xsl:text>
     </xsl:if>
     -->
    </xsl:otherwise>
   </xsl:choose>
  </xsl:when>

  <!-- now handle fields with multiple values -->
  <xsl:otherwise>
   <xsl:for-each select="$entry/*[local-name()=concat($field,'s')]/*">
    <xsl:value-of select="."/>
    <xsl:if test="position() != last()">
     <xsl:text>; </xsl:text>
    </xsl:if>
   </xsl:for-each>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

<xsl:template name="image-link">
 <xsl:param name="image"/>
 <xsl:param name="dir"/>
 <xsl:variable name="id" select="$image/@id"/>
 <xsl:choose>
  <xsl:when test="$image/@link = 'true'">
   <!-- the id _is_ the link -->
   <xsl:value-of select="$id"/>
  </xsl:when>
  <xsl:when test="string-length($dir) &gt; 0">
   <xsl:value-of select="concat($dir, $id)"/>
  </xsl:when>
  <!-- otherwise try $imgdir and $datadir -->
  <xsl:when test="string-length($datadir) &gt; 0">
   <xsl:value-of select="concat($datadir, $id)"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="concat($imgdir, $id)"/>
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

<xsl:template name="msqueeze">
 <xsl:param name="str"/>
 <xsl:param name="len"/>

 <xsl:variable name="slen" select="string-length($str)"/>
 <xsl:variable name="mid" select="floor($len div 2)"/>

 <xsl:choose>
  <xsl:when test="$slen &gt; $len">
   <xsl:value-of select="substring($str, 0, $mid - 2)"/>
   <xsl:text>[...]</xsl:text>
   <xsl:value-of select="substring($str, $slen - $mid + 3, $mid - 2)"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:value-of select="$str"/>   
  </xsl:otherwise>
 </xsl:choose>
</xsl:template>

<!-- sums all nodes, assuming they are in MM:SS format -->
<xsl:template name="sumTime">
 <xsl:param name="nodes" select="/.."/>
 <xsl:param name="totalMin" select="0"/>
 <xsl:param name="totalSec" select="0"/>

 <xsl:choose>

  <xsl:when test="not($nodes)">
   <xsl:value-of select="$totalMin + floor($totalSec div 60)"/>
   <xsl:text>:</xsl:text>
   <xsl:value-of select="format-number($totalSec mod 60, '00')"/>
  </xsl:when>

  <xsl:when test="string-length($nodes[1]) &gt; 0">
   <xsl:variable name="min">
    <xsl:value-of select="substring-before($nodes[1], ':')"/>
   </xsl:variable>
   <xsl:variable name="sec">
    <xsl:value-of select="substring-after($nodes[1], ':')"/>
   </xsl:variable>
   <xsl:call-template name="sumTime">
    <xsl:with-param name="nodes" select="$nodes[position() != 1]"/>
    <xsl:with-param name="totalMin" select="$totalMin + $min"/>
    <xsl:with-param name="totalSec" select="$totalSec + $sec"/>
   </xsl:call-template>
  </xsl:when>

  <xsl:otherwise>
   <xsl:call-template name="sumTime">
    <xsl:with-param name="nodes" select="$nodes[position() != 1]"/>
    <xsl:with-param name="totalMin" select="$totalMin"/>
    <xsl:with-param name="totalSec" select="$totalSec"/>
   </xsl:call-template>
  </xsl:otherwise>

 </xsl:choose>
</xsl:template>

<xsl:template name="columnTitle">
 <xsl:param name="index" select="1"/>
 <xsl:param name="max" select="10"/>
 <xsl:param name="elem" select="'th'"/>
 <xsl:param name="style"/>
 <xsl:param name="field" select="/.."/>

 <xsl:if test="not($index &gt; $max)">
  <xsl:element name="{$elem}">
   <xsl:if test="string-length($style)">
    <xsl:attribute name="style">
     <xsl:value-of select="$style"/>
    </xsl:attribute>
   </xsl:if>
   <xsl:value-of select="$field/tc:prop[@name = concat('column', $index)]"/>
  </xsl:element>

  <xsl:call-template name="columnTitle">
   <xsl:with-param name="index" select="$index + 1"/>
   <xsl:with-param name="max" select="$max"/>
   <xsl:with-param name="elem" select="$elem"/>
   <xsl:with-param name="style" select="$style"/>
   <xsl:with-param name="field" select="$field"/>
  </xsl:call-template>
 </xsl:if>
</xsl:template>

</xsl:stylesheet>