2004-06-03 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / web / web / mono-rss.cs
1 //
2 // Generates the Mono RSS feed
3 //
4 // Miguel de Icaza
5 //
6 using System;
7 using System.IO;
8 using System.Xml;
9 using System.Text;
10 using RSS;
11
12 class X {
13         static RSS.RSS rss;
14         static Channel c;
15         static int item_count;
16         static int line;
17
18         static int GetMonth (string s)
19         {
20                 switch (s){
21                 case "Jan": return 1;
22                 case "Feb": return 2; 
23                 case "Mar": return 3;
24                 case "Apr": return 4; 
25                 case "May": return 5;
26                 case "Jun": return 6; 
27                 case "Jul": return 7;
28                 case "Aug": return 8; 
29                 case "Sep": return 9;
30                 case "Oct": return 10; 
31                 case "Nov": return 11; 
32                 case "Dec": return 12; 
33                 }
34                 throw new Exception ("Can not parse month name: " + s);
35         }
36
37         static int GetDay (string s)
38         {
39                 int d = s [0] - '0';
40
41                 if (Char.IsDigit (s [1])){
42                         d = d * 10 + (s [1] - '0');
43                 }
44                 return d;
45         }
46         
47         static void PopulateRSS (StreamReader input)
48         {
49                 string s;
50                 
51                 while ((s = input.ReadLine ()) != null){
52                         line++;
53                         if (s.StartsWith ("@item "))
54                                 break;
55                 }
56
57                 if (s == null || !s.StartsWith ("@item ")){
58                         Console.WriteLine ("Could not find beginning of text to RSS");
59                         return;
60                 }
61
62                 Item i = null;
63                 string description = "";
64                 do {
65                         if (s.StartsWith ("@item ")){
66                                 if (item_count++ > 25)
67                                         break;
68
69                                 if (i != null){
70                                         i.Description = description;
71                                         description = "";
72                                 }
73                                 
74                                 string title = s.Substring (6);
75                                 string link = "http://www.go-mono.com/index.html#";
76                                 foreach (char ch in title){
77                                         if (ch != ' ')
78                                                 link += ch;
79                                 }
80                                 
81                                 i = c.NewItem ();
82                                 i.Title = title;
83                                 i.Link = link;
84                                 DateTime dt = new DateTime (2004, GetMonth (s.Substring (6, 3)), GetDay (s.Substring (10, 2)));
85                                 i.PubDate = dt.ToString ("R");
86                         } else {
87                                 description += "\n" + (s == "\n" ? "<p>" : s);
88                         }
89                         line++;
90                 } while ((s = input.ReadLine ()) != null);
91
92                 if (i != null){
93                         i.Description = description;
94                 }
95         }
96         
97         static void MakeRSS (string input, string output)
98         {
99                 rss = new RSS.RSS ();
100                 c = rss.NewChannel ("Mono Project News", "http://www.go-mono.com");
101                 
102                 c.Title = "Mono Project News";
103                 c.Link = "http://www.go-mono.com";
104                 c.Description =
105                 "News from the Mono project: a portable implementation of the .NET Framework";
106                 c.WebMaster = "webmaster@go-mono.com";
107                 c.ManagingEditor = "miguel@ximian.com";
108                 string t = File.GetLastWriteTime (input).ToString ("r");
109                 c.PubDate = t;
110                 c.LastBuildDate = t;
111
112                 using (FileStream fs = new FileStream (input, FileMode.Open)){
113                         using (StreamReader input_stream = new StreamReader (fs)){
114                                 try {
115                                         PopulateRSS (input_stream);
116                                 } catch {
117                                         Console.WriteLine ("{0} failure while loading: {1}", line, input);
118                                         throw;
119                                 }
120                         }
121                 }
122                 
123                 rss.XmlDocument.Save (output);
124         }
125         
126         static int Main (string [] args)
127         {
128                 switch (args.Length){
129                 case 0:
130                         MakeRSS ("index", "index.rss");
131                         break;
132                 case 2:
133                         MakeRSS (args [0], args [1]);
134                         break;
135                         
136                 default:
137                         Console.WriteLine ("Usage is: mono-rss [input output.rss]");
138                         return 1;
139                 }
140
141                 return 0;
142         }
143 }