2010-04-08 Zoltan Varga <vargaz@gmail.com>
[mono/afaerber.git] / web / web / mono-rss.cs
blobab39f6e91c5c7364fe4e55b3576dc674fd1d9a0e
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;
12 class X {
13 static RSS.RSS rss;
14 static Channel c;
15 static int item_count;
16 static int line;
18 static int GetMonth (string s)
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;
34 throw new Exception ("Can not parse month name: " + s);
37 static int GetDay (string s)
39 int d = s [0] - '0';
41 if (Char.IsDigit (s [1])){
42 d = d * 10 + (s [1] - '0');
44 return d;
47 static void PopulateRSS (StreamReader input)
49 string s;
51 while ((s = input.ReadLine ()) != null){
52 line++;
53 if (s.StartsWith ("@item "))
54 break;
57 if (s == null || !s.StartsWith ("@item ")){
58 Console.WriteLine ("Could not find beginning of text to RSS");
59 return;
62 Item i = null;
63 string description = "";
64 do {
65 if (s.StartsWith ("@item ")){
66 if (item_count++ > 25)
67 break;
69 if (i != null){
70 i.Description = description;
71 description = "";
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;
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);
89 line++;
90 } while ((s = input.ReadLine ()) != null);
92 if (i != null){
93 i.Description = description;
97 static void MakeRSS (string input, string output)
99 rss = new RSS.RSS ();
100 c = rss.NewChannel ("Mono Project News", "http://www.go-mono.com");
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;
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;
123 rss.XmlDocument.Save (output);
126 static int Main (string [] args)
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;
136 default:
137 Console.WriteLine ("Usage is: mono-rss [input output.rss]");
138 return 1;
141 return 0;