Statistics
| Revision:

root / tmp / org.txm.core / res / org / txm / xml / xsl / svn2cl.xsl @ 187

History | View | Annotate | Download (16.9 kB)

1
<?xml version="1.0" encoding="utf-8"?>
2

    
3
<!--
4
	USAGE:
5
	svn log --xml --verbose > foo.txt
6
	xsltproc etc/svn2cl.xsl foo.txt > ChangeLog
7

    
8

    
9
   svn2cl.xsl - xslt stylesheet for converting svn log to a normal
10
                changelog
11

    
12
   version 0.11
13

    
14
   Usage (replace ++ with two minus signs which aren't allowed
15
   inside xml comments):
16
     svn ++verbose ++xml log | \
17
       xsltproc ++stringparam strip-prefix `basename $(pwd)` \
18
                ++stringparam linelen 75 \
19
                ++stringparam groupbyday yes \
20
                ++stringparam separate-daylogs yes \
21
                ++stringparam include-rev yes \
22
                ++stringparam include-actions yes \
23
                ++stringparam breakbeforemsg yes/2 \
24
                ++stringparam reparagraph yes \
25
                ++stringparam authorsfile FILE \
26
                ++stringparam ignore-message-starting \
27
                svn2cl.xsl - > ChangeLog
28

    
29
   This file is based on several implementations of this conversion
30
   that I was not completely happy with and some other common
31
   xslt constructs found on the web.
32

    
33
   Copyright (C) 2004, 2005, 2006, 2007 Arthur de Jong.
34

    
35
   Redistribution and use in source and binary forms, with or without
36
   modification, are permitted provided that the following conditions
37
   are met:
38
   1. Redistributions of source code must retain the above copyright
39
      notice, this list of conditions and the following disclaimer.
40
   2. Redistributions in binary form must reproduce the above copyright
41
      notice, this list of conditions and the following disclaimer in
42
      the documentation and/or other materials provided with the
43
      distribution.
44
   3. The name of the author may not be used to endorse or promote
45
      products derived from this software without specific prior
46
      written permission.
47

    
48
   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51
   ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
52
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53
   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
54
   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55
   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
56
   IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
57
   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
58
   IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59

    
60
-->
61

    
62
<!DOCTYPE xsl:stylesheet [
63
 <!ENTITY tab "&#9;">
64
 <!ENTITY newl "&#38;#xA;">
65
 <!ENTITY space "&#32;">
66
]>
67

    
68
<xsl:stylesheet
69
  version="1.0"
70
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
71

    
72
 <xsl:output
73
   method="text"
74
   encoding="utf-8"
75
   media-type="text/plain"
76
   omit-xml-declaration="yes"
77
   standalone="yes"
78
   indent="no" />
79

    
80
 <xsl:strip-space elements="*" />
81

    
82
 <!-- the prefix of pathnames to strip -->
83
 <xsl:param name="strip-prefix" select="'/'" />
84

    
85
 <!-- the length of a line to wrap messages at -->
86
 <xsl:param name="linelen" select="75" />
87

    
88
 <!-- whether entries should be grouped by day -->
89
 <xsl:param name="groupbyday" select="'no'" />
90

    
91
 <!-- whether to seperate log messages by empty lines -->
92
 <xsl:param name="separate-daylogs" select="'no'" />
93

    
94
 <!-- whether a revision number should be included -->
95
 <xsl:param name="include-rev" select="'no'" />
96

    
97
 <!-- whether aaction labels should be added to files -->
98
 <xsl:param name="include-actions" select="'no'" />
99

    
100
 <!-- whether the log message should start on a new line -->
101
 <xsl:param name="breakbeforemsg" select="'no'" />
102

    
103
 <!-- whether the message should be rewrapped within one paragraph -->
104
 <xsl:param name="reparagraph" select="'no'" />
105

    
106
 <!-- whether certain messages should be ignored -->
107
 <xsl:param name="ignore-message-starting" select="''" />
108

    
109
 <!-- location of authors file if any -->
110
 <xsl:param name="authorsfile" select="''" />
111
 <xsl:key name="author-lookup" match="author" use="@uid" />
112
 <xsl:variable name="authors-top" select="document($authorsfile)/authors" />
113

    
114
 <!-- match the topmost log entry -->
115
 <xsl:template match="log">
116
  <xsl:choose>
117
   <xsl:when test="$ignore-message-starting != ''">
118
    <!-- only handle logentries with don't contain the string -->
119
    <xsl:apply-templates select="logentry[not(starts-with(msg,$ignore-message-starting))]" />
120
   </xsl:when>
121
   <xsl:otherwise>
122
    <xsl:apply-templates select="logentry" />
123
   </xsl:otherwise>
124
  </xsl:choose>
125
  <!-- add newlines at the end of the changelog -->
126
  <xsl:text>&newl;</xsl:text>
127
 </xsl:template>
128

    
129
 <!-- format one entry from the log -->
130
 <xsl:template match="logentry">
131
  <xsl:choose>
132
   <!-- if we're grouping we should omit some headers -->
133
   <xsl:when test="$groupbyday='yes'">
134
    <!-- save log entry number -->
135
    <xsl:variable name="pos" select="position()" />
136
    <!-- fetch previous entry's date -->
137
    <xsl:variable name="prevdate">
138
     <xsl:apply-templates select="../logentry[position()=(($pos)-1)]/date" />
139
    </xsl:variable>
140
    <!-- fetch previous entry's author -->
141
    <xsl:variable name="prevauthor">
142
     <xsl:value-of select="normalize-space(../logentry[position()=(($pos)-1)]/author)" />
143
    </xsl:variable>
144
    <!-- fetch this entry's date -->
145
    <xsl:variable name="date">
146
     <xsl:apply-templates select="date" />
147
    </xsl:variable>
148
    <!-- fetch this entry's author -->
149
    <xsl:variable name="author">
150
     <xsl:value-of select="normalize-space(author)" />
151
    </xsl:variable>
152
    <!-- check if header is changed -->
153
    <xsl:if test="($prevdate!=$date) or ($prevauthor!=$author)">
154
     <!-- add newline -->
155
     <xsl:if test="not(position()=1)">
156
      <xsl:text>&newl;</xsl:text>
157
     </xsl:if>
158
     <!-- date -->
159
     <xsl:value-of select="$date" />
160
     <!-- two spaces -->
161
     <xsl:text>&space;&space;</xsl:text>
162
     <!-- author's name -->
163
     <xsl:apply-templates select="author" />
164
     <!-- two newlines -->
165
     <xsl:text>&newl;</xsl:text>
166
     <xsl:if test="$separate-daylogs!='yes'"><xsl:text>&newl;</xsl:text></xsl:if>
167
    </xsl:if>
168
   </xsl:when>
169
   <!-- write the log header -->
170
   <xsl:otherwise>
171
    <!-- add newline -->
172
    <xsl:if test="not(position()=1)">
173
     <xsl:text>&newl;</xsl:text>
174
    </xsl:if>
175
    <!-- date -->
176
    <xsl:apply-templates select="date" />
177
    <!-- two spaces -->
178
    <xsl:text>&space;&space;</xsl:text>
179
    <!-- author's name -->
180
    <xsl:apply-templates select="author" />
181
    <!-- two newlines -->
182
    <xsl:text>&newl;&newl;</xsl:text>
183
   </xsl:otherwise>
184
  </xsl:choose>
185
  <!-- get paths string -->
186
  <xsl:variable name="paths">
187
   <xsl:apply-templates select="paths" />
188
  </xsl:variable>
189
  <!-- get revision number -->
190
  <xsl:variable name="rev">
191
   <xsl:if test="$include-rev='yes'">
192
    <xsl:text>[r</xsl:text>
193
    <xsl:value-of select="@revision" />
194
    <xsl:text>]&space;</xsl:text>
195
   </xsl:if>
196
  </xsl:variable>
197
  <!-- trim trailing newlines -->
198
  <xsl:variable name="msg">
199
   <!-- add a line break before the log message -->
200
   <xsl:choose>
201
    <xsl:when test="$breakbeforemsg='yes'">
202
     <xsl:text>&newl;</xsl:text>
203
    </xsl:when>
204
    <xsl:when test="number($breakbeforemsg)&gt;0">
205
     <xsl:call-template name="newlines">
206
      <xsl:with-param name="count" select="number($breakbeforemsg)" />
207
     </xsl:call-template>
208
    </xsl:when>
209
   </xsl:choose>
210
   <xsl:call-template name="trim-newln">
211
    <xsl:with-param name="txt" select="msg" />
212
   </xsl:call-template>
213
  </xsl:variable>
214
  <!-- add newline here if separate-daylogs is in effect -->
215
  <xsl:if test="$groupbyday='yes' and $separate-daylogs='yes'"><xsl:text>&newl;</xsl:text></xsl:if>
216
  <!-- first line is indented (other indents are done in wrap template) -->
217
  <xsl:text>&tab;*&space;</xsl:text>
218
  <!-- set up the text to wrap -->
219
  <xsl:variable name="txt">
220
   <xsl:value-of select="$rev" />
221
   <xsl:if test="$paths!=''">
222
    <xsl:value-of select="concat($paths,':&space;')" />
223
   </xsl:if>
224
   <xsl:value-of select="$msg" />
225
  </xsl:variable>
226
  <!-- print the paths and message nicely wrapped -->
227
  <xsl:call-template name="wrap">
228
   <xsl:with-param name="txt" select="$txt" />
229
  </xsl:call-template>
230
 </xsl:template>
231

    
232
 <!-- format date -->
233
 <xsl:template match="date">
234
  <xsl:variable name="date" select="normalize-space(.)" />
235
  <!-- output date part -->
236
  <xsl:value-of select="substring($date,1,10)" />
237
  <!-- output time part -->
238
  <xsl:if test="$groupbyday!='yes'">
239
   <xsl:text>&space;</xsl:text>
240
   <xsl:value-of select="substring($date,12,5)" />
241
  </xsl:if>
242
 </xsl:template>
243

    
244
 <!-- format author -->
245
 <xsl:template match="author">
246
  <xsl:variable name="uid" select="normalize-space(.)" />
247
  <!-- try to lookup author in authorsfile -->
248
  <xsl:choose>
249
   <xsl:when test="$authorsfile!=''">
250
    <xsl:for-each select="$authors-top">
251
     <xsl:variable name="author" select="key('author-lookup',$uid)" />
252
     <!-- present result -->
253
     <xsl:choose>
254
      <xsl:when test="string($author/.)">
255
       <xsl:apply-templates select="$author/node()" mode="copy" />
256
      </xsl:when>
257
      <xsl:otherwise>
258
       <xsl:value-of select="$uid" />
259
      </xsl:otherwise>
260
     </xsl:choose>
261
    </xsl:for-each>
262
   </xsl:when>
263
   <xsl:otherwise>
264
    <xsl:value-of select="$uid" />
265
   </xsl:otherwise>
266
  </xsl:choose>
267
 </xsl:template>
268

    
269
 <!-- copy but normalize text -->
270
 <xsl:template match="text()" mode="copy">
271
  <xsl:value-of select="normalize-space(.)" />
272
 </xsl:template>
273

    
274
 <!-- simple copy template -->
275
 <xsl:template match="@*|node()" mode="copy">
276
  <xsl:copy>
277
   <xsl:apply-templates select="@*|node()" mode="copy" />
278
  </xsl:copy>
279
 </xsl:template>
280

    
281
 <!-- present a list of paths names -->
282
 <xsl:template match="paths">
283
  <xsl:choose>
284
   <!-- only handle paths that begin with the path and strip the path -->
285
   <xsl:when test="$strip-prefix != ''">
286
    <!-- if strip-prefix does not start with a slash, prepend it -->
287
    <xsl:variable name="tmpstrip1">
288
     <xsl:choose>
289
      <xsl:when test="starts-with($strip-prefix,'/')">
290
       <xsl:value-of select="$strip-prefix" />
291
      </xsl:when>
292
      <xsl:otherwise>
293
       <xsl:value-of select="concat('/',$strip-prefix)" />
294
      </xsl:otherwise>
295
     </xsl:choose>
296
    </xsl:variable>
297
    <!-- strip trailing slash from strip-prefix -->
298
    <xsl:variable name="tmpstrip2">
299
     <xsl:choose>
300
      <xsl:when test="substring($tmpstrip1,string-length($tmpstrip1),1)='/'">
301
       <xsl:value-of select="substring($tmpstrip1,1,string-length($tmpstrip1)-1)" />
302
      </xsl:when>
303
      <xsl:otherwise>
304
       <xsl:value-of select="$tmpstrip1" />
305
      </xsl:otherwise>
306
     </xsl:choose>
307
    </xsl:variable>
308
    <!-- filter on all entries within directory -->
309
    <xsl:for-each select="path[starts-with(concat(normalize-space(.),'/'),concat($tmpstrip2,'/'))]">
310
     <xsl:sort select="normalize-space(.)" data-type="text" />
311
     <!-- unless we are the first entry, add a comma -->
312
     <xsl:if test="not(position()=1)">
313
      <xsl:text>,&space;</xsl:text>
314
     </xsl:if>
315
     <!-- print the path name -->
316
     <xsl:call-template name="printpath">
317
      <xsl:with-param name="path" select="substring(normalize-space(.),string-length($strip-prefix)+3)" />
318
     </xsl:call-template>
319
     <!-- add the action flag -->
320
     <xsl:if test="$include-actions='yes'">
321
      <xsl:apply-templates select="." mode="action"/>
322
     </xsl:if>
323
    </xsl:for-each>
324
   </xsl:when>
325
   <!-- print a simple list of all paths -->
326
   <xsl:otherwise>
327
    <xsl:for-each select="path">
328
     <xsl:sort select="normalize-space(.)" data-type="text" />
329
     <!-- unless we are the first entry, add a comma -->
330
     <xsl:if test="not(position()=1)">
331
      <xsl:text>,&space;</xsl:text>
332
     </xsl:if>
333
     <!-- print the path name -->
334
     <xsl:value-of select="normalize-space(.)" />
335
     <!-- add the action flag -->
336
     <xsl:if test="$include-actions='yes'">
337
      <xsl:apply-templates select="." mode="action"/>
338
     </xsl:if>
339
    </xsl:for-each>
340
   </xsl:otherwise>
341
  </xsl:choose>
342
 </xsl:template>
343

    
344
 <xsl:template match="path" mode="action">
345
  <xsl:choose>
346
   <xsl:when test="@action='D'">
347
    <xsl:text>[DEL]</xsl:text>
348
   </xsl:when>
349
   <xsl:when test="@copyfrom-path">
350
    <xsl:text>[CPY]</xsl:text>
351
   </xsl:when>
352
   <xsl:when test="@action='D'">
353
    <xsl:text>[ADD]</xsl:text>
354
   </xsl:when>
355
  </xsl:choose>
356
 </xsl:template>
357

    
358
 <!-- transform path to something printable -->
359
 <xsl:template name="printpath">
360
  <!-- fetch the pathname -->
361
  <xsl:param name="path" />
362
  <!-- strip leading slash -->
363
  <xsl:variable name="tmp1">
364
   <xsl:choose>
365
    <xsl:when test="starts-with($path,'/')">
366
     <xsl:value-of select="substring($path,2)" />
367
    </xsl:when>
368
    <xsl:otherwise>
369
     <xsl:value-of select="$path" />
370
    </xsl:otherwise>
371
   </xsl:choose>
372
  </xsl:variable>
373
  <!-- translate empty string to dot -->
374
  <xsl:choose>
375
   <xsl:when test="$tmp1 = ''">
376
    <xsl:text>.</xsl:text>
377
   </xsl:when>
378
   <xsl:otherwise>
379
    <xsl:value-of select="$tmp1" />
380
   </xsl:otherwise>
381
  </xsl:choose>
382
 </xsl:template>
383

    
384
 <!-- string-wrapping template -->
385
 <xsl:template name="wrap">
386
  <xsl:param name="txt" />
387
  <xsl:variable name="normtxt" select="normalize-space($txt)" />
388
  <xsl:choose>
389
   <xsl:when test="contains($txt,'&newl;')">
390
     <!-- text contains newlines, do the first line -->
391
     <xsl:call-template name="wrap">
392
      <xsl:with-param name="txt" select="substring-before($txt,'&newl;')" />
393
     </xsl:call-template>
394
     <!-- print tab -->
395
     <xsl:text>&tab;&space;&space;</xsl:text>
396
     <!-- wrap the rest of the text -->
397
     <xsl:call-template name="wrap">
398
      <xsl:with-param name="txt" select="substring-after($txt,'&newl;')" />
399
     </xsl:call-template>
400
   </xsl:when>
401
   <xsl:when test="(string-length($normtxt) &lt; (($linelen)-9)) or not(contains($normtxt,' '))">
402
    <!-- this is easy, nothing to do -->
403
    <xsl:value-of select="$normtxt" />
404
    <!-- add newline -->
405
    <xsl:text>&newl;</xsl:text>
406
   </xsl:when>
407
   <xsl:otherwise>
408
    <!-- find the first line -->
409
    <xsl:variable name="tmp" select="substring($normtxt,1,(($linelen)-9))" />
410
    <xsl:variable name="line">
411
     <xsl:choose>
412
      <!-- if our attempt contains spaces wrap on that -->
413
      <xsl:when test="contains($tmp,' ')">
414
       <xsl:call-template name="find-line">
415
        <xsl:with-param name="txt" select="$tmp" />
416
       </xsl:call-template>
417
      </xsl:when>
418
      <!-- otherwise use the first non-space characters from the text -->
419
      <xsl:otherwise>
420
       <xsl:value-of select="substring-before($normtxt,' ')" />
421
      </xsl:otherwise>
422
     </xsl:choose>
423
    </xsl:variable>
424
    <!-- print line -->
425
    <xsl:value-of select="$line" />
426
    <!-- print newline and tab -->
427
    <xsl:text>&newl;&tab;&space;&space;</xsl:text>
428
    <!-- wrap the rest of the text -->
429
    <xsl:call-template name="wrap">
430
     <xsl:with-param name="txt" select="normalize-space(substring($normtxt,string-length($line)+1))" />
431
    </xsl:call-template>
432
   </xsl:otherwise>
433
  </xsl:choose>
434
 </xsl:template>
435

    
436
 <!-- template to trim line to contain space as last char -->
437
 <xsl:template name="find-line">
438
  <xsl:param name="txt" />
439
  <xsl:choose>
440
   <xsl:when test="substring($txt,string-length($txt),1)=' '">
441
    <xsl:value-of select="substring($txt,1,string-length($txt)-1)" />
442
   </xsl:when>
443
   <xsl:otherwise>
444
    <xsl:call-template name="find-line">
445
     <xsl:with-param name="txt" select="substring($txt,1,string-length($txt)-1)" />
446
    </xsl:call-template>
447
   </xsl:otherwise>
448
  </xsl:choose>
449
 </xsl:template>
450

    
451
 <!-- template to trim trailing and starting newlines -->
452
 <xsl:template name="trim-newln">
453
  <xsl:param name="txt" />
454
  <xsl:choose>
455
   <!-- find starting newlines -->
456
   <xsl:when test="substring($txt,1,1) = '&newl;'">
457
    <xsl:call-template name="trim-newln">
458
     <xsl:with-param name="txt" select="substring($txt,2)" />
459
    </xsl:call-template>
460
   </xsl:when>
461
   <!-- find trailing newlines -->
462
   <xsl:when test="substring($txt,string-length($txt),1) = '&newl;'">
463
    <xsl:call-template name="trim-newln">
464
     <xsl:with-param name="txt" select="substring($txt,1,string-length($txt)-1)" />
465
    </xsl:call-template>
466
   </xsl:when>
467
   <!-- if the message has paragrapgs, find the first one -->
468
   <xsl:when test="$reparagraph='yes' and contains($txt,'&newl;&newl;')">
469
     <!-- remove newlines from first paragraph -->
470
     <xsl:value-of select="normalize-space(substring-before($txt,'&newl;&newl;'))" />
471
     <!-- paragraph separator -->
472
     <xsl:text>&newl;&newl;</xsl:text>
473
     <!-- do the rest of the text -->
474
     <xsl:call-template name="trim-newln">
475
      <xsl:with-param name="txt" select="substring-after($txt,'&newl;&newl;')" />
476
     </xsl:call-template>
477
   </xsl:when>
478
   <!-- remove more single newlines -->
479
   <xsl:when test="$reparagraph='yes'">
480
    <xsl:value-of select="normalize-space($txt)" />
481
   </xsl:when>
482
   <!-- no newlines found, we're done -->
483
   <xsl:otherwise>
484
    <xsl:value-of select="$txt" />
485
   </xsl:otherwise>
486
  </xsl:choose>
487
 </xsl:template>
488

    
489
 <!-- insert a number of newlines -->
490
 <xsl:template name="newlines">
491
  <xsl:param name="count" />
492
  <xsl:text>&newl;</xsl:text>
493
  <xsl:if test="$count&gt;1">
494
   <xsl:call-template name="newlines">
495
    <xsl:with-param name="count" select="($count)-1" />
496
   </xsl:call-template>
497
  </xsl:if>
498
 </xsl:template>
499

    
500
</xsl:stylesheet>