Bug fixes for lcs.diff2html; xml.writer
[factor/jcg.git] / basis / lcs / diff2html / diff2html.factor
blobee303cc5a5868d067dc9d77dba202fe4b03acfd2
1 ! Copyright (C) 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: lcs xml.interpolate xml.writer kernel strings ;
4 FROM: accessors => item>> ;
5 FROM: io => write ;
6 FROM: sequences => each if-empty when-empty map ;
7 IN: lcs.diff2html
9 GENERIC: diff-line ( obj -- xml )
11 : item-string ( item -- string )
12     item>> [ CHAR: no-break-space 1string ] when-empty ;
14 M: retain diff-line
15     item-string
16     [XML <td class="retain"><-></td> XML]
17     dup [XML <tr><-><-></tr> XML] ;
19 M: insert diff-line
20     item-string [XML
21         <tr>
22             <td> </td>
23             <td class="insert"><-></td>
24         </tr>
25     XML] ;
27 M: delete diff-line
28     item-string [XML
29         <tr>
30             <td class="delete"><-></td>
31             <td> </td>
32         </tr>
33     XML] ;
35 : htmlize-diff ( diff -- xml )
36     [ diff-line ] map
37     [XML
38         <table width="100%" class="comparison">
39             <tr><th>Old</th><th>New</th></tr>
40             <->
41         </table>
42     XML] ;