File: diff2html.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-5
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 35,880 kB
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (71 lines) | stat: -rw-r--r-- 3,109 bytes parent folder | download | duplicates (6)
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
using System;
using System.IO;

class diff2html{
    public static void Main( string[] args ){
        if( args.Length <= 0 ){
            return;
        }
        if( !File.Exists( args[0] ) ){
            return;
        }
        string infile = args[0];
        string outfile = Path.GetFileNameWithoutExtension( infile ) + ".html";
        using( StreamWriter sw = new StreamWriter( outfile ) )
        using( StreamReader sr = new StreamReader( infile ) ){
            sw.WriteLine( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">" );
            sw.WriteLine( "<html lang=\"ja-JP\">" );
            sw.WriteLine( "<head>" );
            sw.WriteLine( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">" );
            sw.WriteLine( "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">" );
            sw.WriteLine( "<title>" + Path.GetFileNameWithoutExtension( infile ) + "</title>" );
            sw.WriteLine( "<style type=\"text/css\">" );
            sw.WriteLine( "<!--" );
            sw.WriteLine( ".header{" );
            sw.WriteLine( "    color: #800000;" );
            sw.WriteLine( "    background-color: #ffff80;" );
            sw.WriteLine( "}" );
            sw.WriteLine( ".removed{" );
            sw.WriteLine( "    background-color: #ff8080;" );
            sw.WriteLine( "}" );
            sw.WriteLine( ".added{" );
            sw.WriteLine( "    background-color: #80ff80;" );
            sw.WriteLine( "}" );
            sw.WriteLine( ".location{" );
            sw.WriteLine( "    color: #ff0000;" );
            sw.WriteLine( "}" );
            sw.WriteLine( "-->" );
            sw.WriteLine( "</style>" );
            sw.WriteLine( "</head>" );
            sw.WriteLine( "<body>" );
            sw.WriteLine( "<pre>" );
            string line = "";
            while( (line = sr.ReadLine()) != null ){
                if( line.Trim() == "" ){
                    sw.WriteLine( "<br>" );
                    continue;
                }
                string style_class = "";
                if( line.StartsWith( "===" ) || line.StartsWith( "---" ) || line.StartsWith( "+++" ) ){
                    style_class = "header";
                }else if( line.StartsWith( "-" ) ){
                    style_class = "removed";
                }else if( line.StartsWith( "+" ) ){
                    style_class = "added";
                }else if( line.StartsWith( "@" ) ){
                    style_class = "location";
                }
                if( style_class == "" ){
                    sw.Write( "<code>" );
                }else{
                    sw.Write( "<code class=" + style_class + ">" );
                }
                sw.Write( line.Replace( "<", "&lt;" ).Replace( ">", "&gt;" ).Replace( "&", "&amp;" ) );
                sw.WriteLine( "</code>" );
            }
            sw.WriteLine( "</pre>" );
            sw.WriteLine( "</body>" );
            sw.WriteLine( "</html>" );
        }
    }
}