Trac

root/branches/0.10/svn2cl.xsl

Revision 4054, 15.8 kB (checked in by romanb, 8 months ago)

added xslt stylesheet to generate changelogs with svn log

Line 
1 <?xml version="1.0" encoding="utf-8"?>
2
3 <!--
4
5    svn2cl.xsl - xslt stylesheet for converting svn log to a normal
6                 changelog
7
8    version 0.9
9
10    Usage (replace ++ with two minus signs which aren't allowed
11    inside xml comments):
12      svn ++verbose ++xml log | \
13        xsltproc ++stringparam strip-prefix `basename $(pwd)` \
14                 ++stringparam linelen 75 \
15                 ++stringparam groupbyday yes \
16                 ++stringparam separate-daylogs yes \
17                 ++stringparam include-rev yes \
18                 ++stringparam breakbeforemsg yes/2 \
19                 ++stringparam reparagraph yes \
20                 ++stringparam authorsfile FILE \
21                 ++stringparam ignore-message-starting \
22                 svn2cl.xsl - > ChangeLog
23
24    This file is based on several implementations of this conversion
25    that I was not completely happy with and some other common
26    xslt constructs found on the web.
27
28    Copyright (C) 2004, 2005, 2006, 2007 Arthur de Jong.
29
30    Redistribution and use in source and binary forms, with or without
31    modification, are permitted provided that the following conditions
32    are met:
33    1. Redistributions of source code must retain the above copyright
34       notice, this list of conditions and the following disclaimer.
35    2. Redistributions in binary form must reproduce the above copyright
36       notice, this list of conditions and the following disclaimer in
37       the documentation and/or other materials provided with the
38       distribution.
39    3. The name of the author may not be used to endorse or promote
40       products derived from this software without specific prior
41       written permission.
42
43    THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
44    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
45    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46    ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
47    DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
49    GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
51    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
52    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
53    IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54
55 -->
56
57 <!DOCTYPE xsl:stylesheet [
58  <!ENTITY tab "&#9;">
59  <!ENTITY newl "&#10;">
60  <!ENTITY space "&#32;">
61 ]>
62
63 <xsl:stylesheet
64   version="1.0"
65   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
66
67  <xsl:output
68    method="text"
69    encoding="utf-8"
70    media-type="text/plain"
71    omit-xml-declaration="yes"
72    standalone="yes"
73    indent="no" />
74
75  <xsl:strip-space elements="*" />
76
77  <!-- the prefix of pathnames to strip -->
78  <xsl:param name="strip-prefix" select="'/'" />
79
80  <!-- the length of a line to wrap messages at -->
81  <xsl:param name="linelen" select="75" />
82  
83  <!-- whether entries should be grouped by day -->
84  <xsl:param name="groupbyday" select="'no'" />
85
86  <!-- whether to seperate log messages by empty lines -->
87  <xsl:param name="separate-daylogs" select="'no'" />
88
89  <!-- whether a revision number should be included -->
90  <xsl:param name="include-rev" select="'no'" />
91
92  <!-- whether the log message should start on a new line -->
93  <xsl:param name="breakbeforemsg" select="'no'" />
94
95  <!-- whether the message should be rewrapped within one paragraph -->
96  <xsl:param name="reparagraph" select="'no'" />
97
98  <!-- whether certain messages should be ignored -->
99  <xsl:param name="ignore-message-starting" select="''" />
100
101  <!-- location of authors file if any -->
102  <xsl:param name="authorsfile" select="''" />
103  <xsl:key name="author-lookup" match="author" use="@uid" />
104  <xsl:variable name="authors-top" select="document($authorsfile)/authors" />
105
106  <!-- match the topmost log entry -->
107  <xsl:template match="log">
108   <xsl:choose>
109    <xsl:when test="$ignore-message-starting != ''">
110     <!-- only handle logentries with don't contain the string -->
111     <xsl:apply-templates select="logentry[not(starts-with(msg,$ignore-message-starting))]" />
112    </xsl:when>
113    <xsl:otherwise>
114     <xsl:apply-templates select="logentry" />
115    </xsl:otherwise>
116   </xsl:choose>
117   <!-- add newlines at the end of the changelog -->
118   <xsl:text>&newl;</xsl:text>
119  </xsl:template>
120
121  <!-- format one entry from the log -->
122  <xsl:template match="logentry">
123   <xsl:choose>
124    <!-- if we're grouping we should omit some headers -->
125    <xsl:when test="$groupbyday='yes'">
126     <!-- save log entry number -->
127     <xsl:variable name="pos" select="position()" />
128     <!-- fetch previous entry's date -->
129     <xsl:variable name="prevdate">
130      <xsl:apply-templates select="../logentry[position()=(($pos)-1)]/date" />
131     </xsl:variable>
132     <!-- fetch previous entry's author -->
133     <xsl:variable name="prevauthor">
134      <xsl:value-of select="normalize-space(../logentry[position()=(($pos)-1)]/author)" />
135     </xsl:variable>
136     <!-- fetch this entry's date -->
137     <xsl:variable name="date">
138      <xsl:apply-templates select="date" />
139     </xsl:variable>
140     <!-- fetch this entry's author -->
141     <xsl:variable name="author">
142      <xsl:value-of select="normalize-space(author)" />
143     </xsl:variable>
144     <!-- check if header is changed -->
145     <xsl:if test="($prevdate!=$date) or ($prevauthor!=$author)">
146      <!-- add newline -->
147      <xsl:if test="not(position()=1)">
148       <xsl:text>&newl;</xsl:text>
149      </xsl:if>
150      <!-- date -->
151      <xsl:value-of select="$date" />
152      <!-- two spaces -->
153      <xsl:text>&space;&space;</xsl:text>
154      <!-- author's name -->
155      <xsl:apply-templates select="author" />
156      <!-- two newlines -->
157      <xsl:text>&newl;</xsl:text>
158      <xsl:if test="$separate-daylogs!='yes'"><xsl:text>&newl;</xsl:text></xsl:if>
159     </xsl:if>
160    </xsl:when>
161    <!-- write the log header -->
162    <xsl:otherwise>
163     <!-- add newline -->
164     <xsl:if test="not(position()=1)">
165      <xsl:text>&newl;</xsl:text>
166     </xsl:if>
167     <!-- date -->
168     <xsl:apply-templates select="date" />
169     <!-- two spaces -->
170     <xsl:text>&space;&space;</xsl:text>
171     <!-- author's name -->
172     <xsl:apply-templates select="author" />
173     <!-- two newlines -->
174     <xsl:text>&newl;&newl;</xsl:text>
175    </xsl:otherwise>
176   </xsl:choose>
177   <!-- get paths string -->
178   <xsl:variable name="paths">
179    <xsl:apply-templates select="paths" />
180    <xsl:text>:&space;</xsl:text>
181   </xsl:variable>
182   <!-- get revision number -->
183   <xsl:variable name="rev">
184    <xsl:if test="$include-rev='yes'">
185     <xsl:text>[r</xsl:text>
186     <xsl:value-of select="@revision" />
187     <xsl:text>]&space;</xsl:text>
188    </xsl:if>
189   </xsl:variable>
190   <!-- trim trailing newlines -->
191   <xsl:variable name="msg">
192    <!-- add a line break before the log message -->
193    <xsl:choose>
194     <xsl:when test="$breakbeforemsg='yes'">
195      <xsl:text>&newl;</xsl:text>
196     </xsl:when>
197     <xsl:when test="number($breakbeforemsg)&gt;0">
198      <xsl:call-template name="newlines">
199       <xsl:with-param name="count" select="number($breakbeforemsg)" />
200      </xsl:call-template>
201     </xsl:when>
202    </xsl:choose>
203    <xsl:call-template name="trim-newln">
204     <xsl:with-param name="txt" select="msg" />
205    </xsl:call-template>
206   </xsl:variable>
207   <!-- add newline here if separate-daylogs is in effect -->
208   <xsl:if test="$groupbyday='yes' and $separate-daylogs='yes'"><xsl:text>&newl;</xsl:text></xsl:if>
209   <!-- first line is indented (other indents are done in wrap template) -->
210   <xsl:text>&tab;*&space;</xsl:text>
211   <!-- print the paths and message nicely wrapped -->
212   <xsl:call-template name="wrap">
213    <xsl:with-param name="txt" select="concat($rev,$msg)" />
214   </xsl:call-template>
215  </xsl:template>
216
217  <!-- format date -->
218  <xsl:template match="date">
219   <xsl:variable name="date" select="normalize-space(.)" />
220   <!-- output date part -->
221   <xsl:value-of select="substring($date,1,10)" />
222   <!-- output time part -->
223   <xsl:if test="$groupbyday!='yes'">
224    <xsl:text>&space;</xsl:text>
225    <xsl:value-of select="substring($date,12,5)" />
226   </xsl:if>
227  </xsl:template>
228
229  <!-- format author -->
230  <xsl:template match="author">
231   <xsl:variable name="uid" select="normalize-space(.)" />
232   <!-- try to lookup author in authorsfile -->
233   <xsl:choose>
234    <xsl:when test="$authorsfile!=''">
235     <xsl:for-each select="$authors-top">
236      <xsl:variable name="author" select="key('author-lookup',$uid)" />
237      <!-- present result -->
238      <xsl:choose>
239       <xsl:when test="string($author/.)">
240        <xsl:apply-templates select="$author/node()" mode="copy" />
241       </xsl:when>
242       <xsl:otherwise>
243        <xsl:value-of select="$uid" />
244       </xsl:otherwise>
245      </xsl:choose>
246     </xsl:for-each>
247    </xsl:when>
248    <xsl:otherwise>
249     <xsl:value-of select="$uid" />
250    </xsl:otherwise>
251   </xsl:choose>
252  </xsl:template>
253
254  <!-- copy but normalize text -->
255  <xsl:template match="text()" mode="copy">
256   <xsl:value-of select="normalize-space(.)" />
257  </xsl:template>
258
259  <!-- simple copy template -->
260  <xsl:template match="@*|node()" mode="copy">
261   <xsl:copy>
262    <xsl:apply-templates select="@*|node()" mode="copy" />
263   </xsl:copy>
264  </xsl:template>
265
266  <!-- present a list of paths names -->
267  <xsl:template match="paths">
268   <xsl:choose>
269    <!-- only handle paths that begin with the path and strip the path -->
270    <xsl:when test="$strip-prefix != ''">
271     <!-- if strip-prefix does not start with a slash, prepend it -->
272     <xsl:variable name="tmpstrip1">
273      <xsl:choose>
274       <xsl:when test="starts-with($strip-prefix,'/')">
275        <xsl:value-of select="$strip-prefix" />
276       </xsl:when>
277       <xsl:otherwise>
278        <xsl:value-of select="concat('/',$strip-prefix)" />
279       </xsl:otherwise>
280      </xsl:choose>
281     </xsl:variable>
282     <!-- strip trailing slash from strip-prefix -->
283     <xsl:variable name="tmpstrip2">
284      <xsl:choose>
285       <xsl:when test="substring($tmpstrip1,string-length($tmpstrip1),1)='/'">
286        <xsl:value-of select="substring($tmpstrip1,1,string-length($tmpstrip1)-1)" />
287       </xsl:when>
288       <xsl:otherwise>
289        <xsl:value-of select="$tmpstrip1" />
290       </xsl:otherwise>
291      </xsl:choose>
292     </xsl:variable>
293     <!-- filter on all entries within directory -->
294     <xsl:for-each select="path[starts-with(concat(normalize-space(.),'/'),concat($tmpstrip2,'/'))]">
295      <xsl:sort select="normalize-space(.)" data-type="text" />
296      <!-- unless we are the first entry, add a comma -->
297      <xsl:if test="not(position()=1)">
298       <xsl:text>,&space;</xsl:text>
299      </xsl:if>
300      <!-- print the path name -->
301      <xsl:call-template name="printpath">
302       <xsl:with-param name="path" select="substring(normalize-space(.),string-length($strip-prefix)+3)" />
303      </xsl:call-template>
304     </xsl:for-each>
305    </xsl:when>
306    <!-- print a simple list of all paths -->
307    <xsl:otherwise>
308     <xsl:for-each select="path">
309      <xsl:sort select="normalize-space(.)" data-type="text" />
310      <!-- unless we are the first entry, add a comma -->
311      <xsl:if test="not(position()=1)">
312       <xsl:text>,&space;</xsl:text>
313      </xsl:if>
314      <!-- print the path name -->
315      <xsl:value-of select="normalize-space(.)" />
316     </xsl:for-each>
317    </xsl:otherwise>
318   </xsl:choose>
319  </xsl:template>
320
321  <!-- transform path to something printable -->
322  <xsl:template name="printpath">
323   <!-- fetch the pathname -->
324   <xsl:param name="path" />
325   <!-- strip leading slash -->
326   <xsl:variable name="tmp1">
327    <xsl:choose>
328     <xsl:when test="starts-with($path,'/')">
329      <xsl:value-of select="substring($path,2)" />
330     </xsl:when>
331     <xsl:otherwise>
332      <xsl:value-of select="$path" />
333     </xsl:otherwise>
334    </xsl:choose>
335   </xsl:variable>
336   <!-- translate empty string to dot -->
337   <xsl:choose>
338    <xsl:when test="$tmp1 = ''">
339     <xsl:text>.</xsl:text>
340    </xsl:when>
341    <xsl:otherwise>
342     <xsl:value-of select="$tmp1" />
343    </xsl:otherwise>
344   </xsl:choose>
345  </xsl:template>
346
347  <!-- string-wrapping template -->
348  <xsl:template name="wrap">
349   <xsl:param name="txt" />
350   <xsl:variable name="normtxt" select="normalize-space($txt)" />
351   <xsl:choose>
352    <xsl:when test="contains($txt,'&newl;')">
353      <!-- text contains newlines, do the first line -->
354      <xsl:call-template name="wrap">
355       <xsl:with-param name="txt" select="substring-before($txt,'&newl;')" />
356      </xsl:call-template>
357      <!-- print tab -->
358      <xsl:text>&tab;&space;&space;</xsl:text>
359      <!-- wrap the rest of the text -->
360      <xsl:call-template name="wrap">
361       <xsl:with-param name="txt" select="substring-after($txt,'&newl;')" />
362      </xsl:call-template>
363    </xsl:when>
364    <xsl:when test="(string-length($normtxt) &lt; (($linelen)-9)) or not(contains($normtxt,' '))">
365     <!-- this is easy, nothing to do -->
366     <xsl:value-of select="$normtxt" />
367     <!-- add newline -->
368     <xsl:text>&newl;</xsl:text>
369    </xsl:when>
370    <xsl:otherwise>
371     <!-- find the first line -->
372     <xsl:variable name="tmp" select="substring($normtxt,1,(($linelen)-9))" />
373     <xsl:variable name="line">
374      <xsl:choose>
375       <!-- if our attempt contains spaces wrap on that -->
376       <xsl:when test="contains($tmp,' ')">
377        <xsl:call-template name="find-line">
378         <xsl:with-param name="txt" select="$tmp" />
379        </xsl:call-template>
380       </xsl:when>
381       <!-- otherwise use the first non-space characters from the text -->
382       <xsl:otherwise>
383        <xsl:value-of select="substring-before($normtxt,' ')" />
384       </xsl:otherwise>
385      </xsl:choose>
386     </xsl:variable>
387     <!-- print line -->
388     <xsl:value-of select="$line" />
389     <!-- print newline and tab -->
390     <xsl:text>&newl;&tab;&space;&space;</xsl:text>
391     <!-- wrap the rest of the text -->
392     <xsl:call-template name="wrap">
393      <xsl:with-param name="txt" select="normalize-space(substring($normtxt,string-length($line)+1))" />
394     </xsl:call-template>
395    </xsl:otherwise>
396   </xsl:choose>
397  </xsl:template>
398
399  <!-- template to trim line to contain space as last char -->
400  <xsl:template name="find-line">
401   <xsl:param name="txt" />
402   <xsl:choose>
403    <xsl:when test="substring($txt,string-length($txt),1)=' '">
404     <xsl:value-of select="substring($txt,1,string-length($txt)-1)" />
405    </xsl:when>
406    <xsl:otherwise>
407     <xsl:call-template name="find-line">
408      <xsl:with-param name="txt" select="substring($txt,1,string-length($txt)-1)" />
409     </xsl:call-template>
410    </xsl:otherwise>
411   </xsl:choose>
412  </xsl:template>
413
414  <!-- template to trim trailing and starting newlines -->
415  <xsl:template name="trim-newln">
416   <xsl:param name="txt" />
417   <xsl:choose>
418    <!-- find starting newlines -->
419    <xsl:when test="substring($txt,1,1) = '&newl;'">
420     <xsl:call-template name="trim-newln">
421      <xsl:with-param name="txt" select="substring($txt,2)" />
422     </xsl:call-template>
423    </xsl:when>
424    <!-- find trailing newlines -->
425    <xsl:when test="substring($txt,string-length($txt),1) = '&newl;'">
426     <xsl:call-template name="trim-newln">
427      <xsl:with-param name="txt" select="substring($txt,1,string-length($txt)-1)" />
428     </xsl:call-template>
429    </xsl:when>
430    <!-- if the message has paragrapgs, find the first one -->
431    <xsl:when test="$reparagraph='yes' and contains($txt,'&newl;&newl;')">
432      <!-- remove newlines from first paragraph -->
433      <xsl:value-of select="normalize-space(substring-before($txt,'&newl;&newl;'))" />
434      <!-- paragraph separator -->
435      <xsl:text>&newl;&newl;</xsl:text>
436      <!-- do the rest of the text -->
437      <xsl:call-template name="trim-newln">
438       <xsl:with-param name="txt" select="substring-after($txt,'&newl;&newl;')" />
439      </xsl:call-template>
440    </xsl:when>
441    <!-- remove more single newlines -->
442    <xsl:when test="$reparagraph='yes'">
443     <xsl:value-of select="normalize-space($txt)" />
444    </xsl:when>
445    <!-- no newlines found, we're done -->
446    <xsl:otherwise>
447     <xsl:value-of select="$txt" />
448    </xsl:otherwise>
449   </xsl:choose>
450  </xsl:template>
451
452  <!-- insert a number of newlines -->
453  <xsl:template name="newlines">
454   <xsl:param name="count" />
455   <xsl:text>&newl;</xsl:text>
456   <xsl:if test="$count&gt;1">
457    <xsl:call-template name="newlines">
458     <xsl:with-param name="count" select="($count)-1" />
459    </xsl:call-template>
460   </xsl:if>
461  </xsl:template>
462
463 </xsl:stylesheet>
Note: See TracBrowser for help on using the browser.