Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 962 lines (827 sloc) 28.756 kb
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
1 //
2 // Lame Blog 1.0
3 //
4 // Features:
5 // Per-day entries
6 // HTML and .txt files supported (pulls header from the file).
7 // Include text support
8 //
9 //
10 // Template macros:
11 //
2fc1420 * lb.cs: Some changes to get things going with the new template.
Jackson Harper authored
12 // @BLOG_ENTRIES@
13 // The blob entries rendered
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
14 //
0d54b2b @migueldeicaza Flush my changes
migueldeicaza authored
15 // TODO:
16 // Add images, so I can do:
17 // @image file
18 // @caption Caption
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
19 //
20
21 using System;
22 using System.IO;
23 using System.Text;
24 using System.Text.RegularExpressions;
25 using System.Collections;
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
26 using System.Collections.Generic;
27 using System.Diagnostics;
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
28 using System.Globalization;
29 using System.Web;
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
30 using System.Xml;
31 using System.Xml.Serialization;
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
32 using System.Linq;
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
33 using Rss;
6882b82 @migueldeicaza Update
migueldeicaza authored
34
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
35 class DayEntry : IComparable {
36 public DateTime Date;
37 public string Body;
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
38 public string Caption = "";
39 public string DateCaption;
a41ba97 * lb.cs:
Jonathan Pryor authored
40 public string Category = "";
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
41 public bool Comments = true; // DateTime.Now > new DateTime (2007, 1, 1);
590eca4 @migueldeicaza Some updates: always comment after jan 2007, small css tuneups
migueldeicaza authored
42 public string RenderedComment;
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
43 public List<string> Images;
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
44
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
45 Blog blog;
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
46 public string extra = "";
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
47
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
48 public string blog_base;
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
49
50 //
51 // Text to inline CSS for the blog for classes code, code-csharp and shell
52 //
a788bc1 @migueldeicaza Update LameBlog
migueldeicaza authored
53 const string code_style = "class=\"code\" style=\"border-style: solid; background: #ddddff; border-width: 1px; padding: 2pt;\"";
54 const string code_csharp_style = "class=\"code-csharp\" style=\"border-style: solid; background: #ddddff; border-width: 1px; padding: 2pt;\"";
66795b8 @migueldeicaza Further
migueldeicaza authored
55 const string shell_style = "style=\"border-style: solid; background: #000000; color: #bbbbbb; border-width: 1px; padding: 2pt;\"";
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
56
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
57 //
58 // The date when we started using filestamps instead of the hardcoded 4pm
59 //
60 public DateTime SwitchDate = new DateTime (2005, 05, 25, 0, 0, 8);
bd39a67 @migueldeicaza Fix the date computation, so it does not roll
migueldeicaza authored
61 public DateTime SecondFix = new DateTime (2005, 09, 30, 0, 0, 8);
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
62
6882b82 @migueldeicaza Update
migueldeicaza authored
63 DayEntry (Blog blog, string file)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
64 {
65 this.blog = blog;
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
66 blog_base = blog.config.BlogWebDirectory;
6882b82 @migueldeicaza Update
migueldeicaza authored
67 ParseDate (file);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
68
69 using (FileStream i = File.OpenRead (file)){
7577226 * lb.cs: Read input & output file encodings from the config file
Jonathan Pryor authored
70 using (StreamReader s = new StreamReader (i, Encoding.GetEncoding (blog.config.InputEncoding))){
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
71 if (file.EndsWith (".html"))
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
72 Load (s, true, file);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
73 else if (file.EndsWith (".txt"))
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
74 Load (s, false, file);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
75 }
76 }
77 }
78
6882b82 @migueldeicaza Update
migueldeicaza authored
79 public static DayEntry Load (Blog blog, string file)
80 {
81 DayEntry de = null;
82
83 try {
84 de = new DayEntry (blog, file);
1b62266 @gonzalop 2006-02-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
gonzalop authored
85 } catch (Exception e) {
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
86 if (blog.Verbose)
87 Console.WriteLine ("Failed to load file: {0}. Reason: {1}", file, e.Message);
6882b82 @migueldeicaza Update
migueldeicaza authored
88 }
89 return de;
90 }
bd39a67 @migueldeicaza Fix the date computation, so it does not roll
migueldeicaza authored
91
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
92 void ParseDate (string file)
93 {
94 int month;
95
a41ba97 * lb.cs:
Jonathan Pryor authored
96 int idx = file.IndexOf (blog.config.BlogDirectory);
97 string entry = file;
98 if (idx >= 0)
99 entry = file.Substring (blog.config.BlogDirectory.Length);
f56b535 @migueldeicaza Update
migueldeicaza authored
100 Match match = Regex.Match (entry, "^(.*/)?(20[0-9][0-9])/([a-z]+)-0*([0-9]+)(-[0-9])?");
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
101
a41ba97 * lb.cs:
Jonathan Pryor authored
102 Category = match.Groups [1].Value;
103 int year = Int32.Parse (match.Groups [2].Value);
104 int day = Int32.Parse (match.Groups [4].Value);
105 string month_name = match.Groups [3].Value;
106 extra = match.Groups [5].Value;
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
107
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
108 switch (month_name){
109 case "jan":
110 month = 1; break;
111 case "feb":
112 month = 2; break;
113 case "mar":
114 month = 3; break;
115 case "apr":
116 month = 4; break;
117 case "may":
118 month = 5; break;
119 case "jun":
120 month = 6; break;
121 case "jul":
122 month = 7; break;
123 case "aug":
124 month = 8; break;
125 case "sep":
126 month = 9; break;
127 case "oct":
128 month = 10; break;
129 case "nov":
130 month = 11; break;
131 case "dec":
132 month = 12; break;
133 default:
134 throw new Exception ("Unknown month: " + month_name + " from: " + file);
135 }
136
66795b8 @migueldeicaza Further
migueldeicaza authored
137 Date = new DateTime (year, month, day, 13, 55, 0);
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
138
139 //
140 // Start using the file time stamp as the publishing date, this is
141 // better than hardcoding a value. To avoid people sending us long
142 // rants, only do this after today
143 //
144 if (Date > SwitchDate){
145 FileInfo fi = new FileInfo (file);
bd39a67 @migueldeicaza Fix the date computation, so it does not roll
migueldeicaza authored
146 DateTime access_date;
147
148 if (Date >= SecondFix)
149 access_date = fi.LastWriteTime;
150 else
151 access_date = fi.LastWriteTimeUtc;
51463b0 @migueldeicaza Fix the date
migueldeicaza authored
152
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
153 Date = new DateTime (year, month, day, access_date.Hour, access_date.Minute, 0);
154 }
155
156 DateCaption = String.Format ("{0:dd} {0:MMM} {0:yyyy}", Date);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
157 }
158
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
159 void Load (StreamReader i, bool is_html, string file)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
160 {
161 bool caption_found = false;
2fc1420 * lb.cs: Some changes to get things going with the new template.
Jackson Harper authored
162 bool in_pre = false;
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
163 StringBuilder sb = new StringBuilder ();
164 string s;
165
166 while ((s = i.ReadLine ()) != null){
167 if (!caption_found){
168 if (is_html){
169 if (s.StartsWith ("<h1>")){
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
170 Caption = s.Replace ("<h1>", "").Replace ("</h1>", "");
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
171 caption_found = true;
172 continue;
173 } else if (s.StartsWith ("#include")){
174 sb.Append (Include (s.Substring (9), out Caption));
175 caption_found = true;
176 continue;
177 }
178 } else {
179 if (s.StartsWith ("@") && !caption_found){
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
180 Caption = s.Substring (1);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
181 caption_found = true;
182 continue;
183 }
184 }
185 }
1b62266 @gonzalop 2006-02-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
gonzalop authored
186
187 // '#date' followed by the output of:
188 // LC_TIME=C date -u +"%a, %d %b %Y %T GMT"
189 // will set the publication date.
190 // Example:
191 // #date Thu, 09 Feb 2006 18:42:56 GMT
192 if (s.StartsWith ("#date ")) {
193 try {
194 Date = DateTime.ParseExact (s.Substring (6), "r", null);
195 } catch (Exception e) {
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
196 Console.WriteLine ("Error parsing: '{0}'\n{1} on {2}", s.Substring (5), e, file);
1b62266 @gonzalop 2006-02-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
gonzalop authored
197 Environment.Exit (1);
198 }
199 continue;
200 }
201
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
202 if (!is_html){
2fc1420 * lb.cs: Some changes to get things going with the new template.
Jackson Harper authored
203 if (s == "" && !in_pre)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
204 sb.Append ("<p>");
205 else if (s.StartsWith ("@"))
206 sb.Append (String.Format ("<h1>{0}</h1>", s.Substring (1)));
2fc1420 * lb.cs: Some changes to get things going with the new template.
Jackson Harper authored
207 else if (s.StartsWith ("#pre"))
208 in_pre = true;
209 else if (s.StartsWith ("#endpre"))
210 in_pre = false;
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
211 else
212 sb.Append (s);
213 } else {
214 if (s.StartsWith ("#include")){
215 string c;
216 sb.Append (Include (s.Substring (9), out c));
217 continue;
80b63e0 @migueldeicaza Add pic support
migueldeicaza authored
218 } else if (s.StartsWith ("#pic")){
219 int idx = s.IndexOf (",");
220 if (idx == -1){
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
221 Console.WriteLine ("Wrong #pic command on {0}", file);
80b63e0 @migueldeicaza Add pic support
migueldeicaza authored
222 continue;
223 }
224
225 string filename = s.Substring (5, idx-5);
226 string caption = s.Substring (idx + 1);
5ccb9a2 @migueldeicaza Flush
migueldeicaza authored
227 sb.Append (String.Format ("<p><center><a href=\"{0}pic.php?name={1}&caption={2}\"><img border=0 src=\"{3}/pictures/small-{1}\"></a><p>{2}</center></p>", blog_base, filename, caption, blog.config.BlogImageBasedir));
0bd0ed3 @migueldeicaza Small touch ups
migueldeicaza authored
228 continue;
80b63e0 @migueldeicaza Add pic support
migueldeicaza authored
229
5e56455 @migueldeicaza Add support for comments to lame blog
migueldeicaza authored
230 } else if (s.StartsWith ("#comment")){
231 Comments = true;
232 continue;
ec5bd90 @migueldeicaza Updates
migueldeicaza authored
233 } else if (s.StartsWith ("#nocomment")){
234 Comments = false;
235 continue;
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
236 } else if (s.StartsWith ("#thumbnail")) {
237 Match m = Regex.Match (s, @"^#thumbnail\s+(?<filename>[^\s]+)\s+(?<desc>.*)$");
238 if (m.Groups.Count > 0) {
239 string filename = Path.GetFileName (m.Groups ["filename"].Value);
240 string thumbnail = LB.GetThumbnailName (filename);
241 sb.AppendFormat ("<blockquote><p><a href=\"@ENTRY_PATH@{0}\"><img " +
242 "src=\"@ENTRY_PATH@{1}\" title=\"{2}\" " +
243 "alt=\"{2}\" /></a></p></blockquote>\n",
244 filename, thumbnail,
245 m.Groups.Count == 1 ? "" : m.Groups ["desc"].Value);
246 Images = Images ?? new List<string> ();
247 Images.Add (m.Groups ["filename"].Value);
248 continue;
249 }
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
250 }
251 sb.Append (s);
252 }
253 sb.Append ("\n");
254 }
255 Body = sb.ToString ();
256 }
257
258 public int CompareTo (object o)
259 {
260 return Date.CompareTo (((DayEntry) o).Date);
261 }
262
263 string Include (string file, out string caption)
264 {
265 if (file.StartsWith ("~/")){
266 file = Environment.GetEnvironmentVariable ("HOME") + "/" + file.Substring (2);
267 }
268
269 string article_file = "./texts/" + Path.GetFileName (file);
270 File.Copy (file, article_file, true);
271 article_file = article_file.Substring (1);
272
273 //
274 // Remove header stuff, and include inline, stick a copy
275 //
276 StringBuilder r = new StringBuilder ();
277
278 caption = "";
36141f6 @migueldeicaza Add all.html back, add email address back, add removed styles back
migueldeicaza authored
279
280 Console.WriteLine ("Reading: " + file);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
281 using (FileStream i = File.OpenRead (file)){
7577226 * lb.cs: Read input & output file encodings from the config file
Jonathan Pryor authored
282 StreamReader s = new StreamReader (i, Encoding.GetEncoding (blog.config.InputEncoding));
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
283 string line;
284 bool output = false;
285
286 while ((line = s.ReadLine ()) != null){
287 Match m = Regex.Match (line, "<title>(.*)</title>");
288 if (m.Groups.Count > 1){
289 caption = line.Substring (m.Groups [1].Index, m.Groups [1].Length);
290 blog.AddArticle (blog_base + article_file, caption);
291 continue;
292 }
293 if (!output){
0d54b2b @migueldeicaza Flush my changes
migueldeicaza authored
294 if (line == "<!--start-->"){
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
295 output = true;
296 r.Append (String.Format ("<h3>{0}: (<a href=\"{2}{1}\">Article Permalink</a>)</h3>", caption, article_file, blog_base));
297 }
298 continue;
299 }
36141f6 @migueldeicaza Add all.html back, add email address back, add removed styles back
migueldeicaza authored
300 line = Regex.Replace (line, "class=\"code\"", code_style);
a788bc1 @migueldeicaza Update LameBlog
migueldeicaza authored
301 line = Regex.Replace (line, "class=\"code-csharp\"", code_csharp_style);
36141f6 @migueldeicaza Add all.html back, add email address back, add removed styles back
migueldeicaza authored
302 line = Regex.Replace (line, "class=\"shell\"", shell_style);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
303 r.Append (line);
304 r.Append ("\n");
305 }
306 }
307 return r.ToString ();
308 }
309
310 public string PermaLink {
311 get {
a41ba97 * lb.cs:
Jonathan Pryor authored
312 return String.Format ("archive{2}{0:yyyy}/{0:MMM}-{0:dd}{1}.html", Date, extra, Category);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
313 }
314 }
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
315
316 public string Id {
317 get {
318 return string.Format ("entry{0}{1}{2}",
319 Category.Replace ('/', '-'), Date.ToString ("yyyy-MM-ddThh:mm:sstt"), extra);
320 }
321 }
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
322 }
323
324 class Blog {
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
325 public Config config;
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
326 public bool Verbose;
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
327 string template;
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
328 public DateTime pubDate = new DateTime (1, 1, 1);
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
329 string entry_template;
a353072 @migueldeicaza Add analytics support
migueldeicaza authored
330 string analytics = "";
5e56455 @migueldeicaza Add support for comments to lame blog
migueldeicaza authored
331 string comments = "";
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
332 string archive_navigator;
a41ba97 * lb.cs:
Jonathan Pryor authored
333 Hashtable category_entries = new Hashtable ();
334
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
335 List<DayEntry> entries = new List<DayEntry> ();
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
336
337 public int Entries {
338 get {
339 return entries.Count;
340 }
341 }
342
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
343 public Blog (Config config, string template)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
344 {
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
345 this.template = template;
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
346 this.config = config;
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
347 this.entry_template = File.OpenText (config.EntryTemplate).ReadToEnd ();
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
348 Verbose = config.Verbose;
6882b82 @migueldeicaza Update
migueldeicaza authored
349
a41ba97 * lb.cs:
Jonathan Pryor authored
350 LoadDirectory (new DirectoryInfo (config.BlogDirectory));
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
351
352 Console.WriteLine ("Loaded: {0} days", entries.Count);
353
354 entries.Sort ();
a41ba97 * lb.cs:
Jonathan Pryor authored
355 foreach (DayEntry de in entries)
356 AddCategory (category_entries, de);
a353072 @migueldeicaza Add analytics support
migueldeicaza authored
357
c6b9e6e @migueldeicaza Fix
migueldeicaza authored
358 if (config.AnalyticsStub != null && config.AnalyticsStub.Length > 0)
a353072 @migueldeicaza Add analytics support
migueldeicaza authored
359 analytics = File.OpenText (config.AnalyticsStub).ReadToEnd ();
5e56455 @migueldeicaza Add support for comments to lame blog
migueldeicaza authored
360 if (config.CommentsStub != null && config.CommentsStub.Length > 0){
361 comments = File.OpenText (config.CommentsStub).ReadToEnd ();
362 }
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
363
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
364 GenerateArchiveNavigation ();
a41ba97 * lb.cs:
Jonathan Pryor authored
365 }
366
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
367 void GenerateArchiveNavigation ()
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
368 {
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
369 StringBuilder ab = new StringBuilder ();
370
371 ab.Append ("<div class=\"archive\">");
372
373 var grouping = from be in entries
374 group be by be.Date.Year into years
375 orderby years.Key descending
376 select new {
377 Year = years.Key,
378 Months = from ybe in years
379 group ybe by ybe.Date.Month into monthly
380 orderby monthly.Key
381 select new { Month = monthly.Key, Entries = monthly } };
382
383 foreach (var year_group in grouping){
384 ab.Append (String.Format ("\n<br/><b>{0}</b>\n", year_group.Year));
385 int count = 0;
386 foreach (var month in year_group.Months){
387 if ((count++ % 6) == 0){
388 ab.Append ("<br/>\n");
389 }
390 DateTime year_month = new DateTime (year_group.Year, month.Month, 1);
391 string month_archive_path = String.Format ("archive/{0:yyyy}/{0:MMM}.html", year_month);
392
393 ab.Append (String.Format ("<a href=\"{1}/{2}\">{0:MMM}</a> ", year_month, config.BlogWebDirectory, month_archive_path));
394 }
395 }
396 ab.Append ("</div>");
397 archive_navigator = ab.ToString ();
398
399 foreach (var year_group in grouping){
400 foreach (var month in year_group.Months){
401 DateTime year_month = new DateTime (year_group.Year, month.Month, 1);
402 string month_archive_path = String.Format ("archive/{0:yyyy}/{0:MMM}.html", year_month);
403
404 IList month_entries = month.Entries.ToList<DayEntry> ();
405 RenderHtml (Path.Combine (LB.config.Prefix, month_archive_path), "../../", month_entries, 0, month_entries.Count, false);
406 }
407 }
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
408 }
409
a41ba97 * lb.cs:
Jonathan Pryor authored
410 void LoadDirectory (DirectoryInfo dir)
411 {
3d18eb2 @migueldeicaza Update
migueldeicaza authored
412 if (dir.Name.EndsWith ("drafts"))
413 return;
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
414 if (Verbose)
415 Console.WriteLine ("dir:" + dir);
a41ba97 * lb.cs:
Jonathan Pryor authored
416 foreach (DirectoryInfo subdir in dir.GetDirectories ()) {
417 LoadDirectory (subdir);
418 }
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
419
a41ba97 * lb.cs:
Jonathan Pryor authored
420 foreach (FileInfo file in dir.GetFiles ()) {
421 if (!(file.Name.EndsWith (".html") || file.Name.EndsWith (".txt")))
422 continue;
423 DayEntry de = DayEntry.Load (this, file.FullName);
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
424 if (de != null) {
a41ba97 * lb.cs:
Jonathan Pryor authored
425 entries.Add (de);
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
426 if (de.Date > pubDate)
427 pubDate = de.Date;
428 }
a41ba97 * lb.cs:
Jonathan Pryor authored
429 }
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
430 }
431
a41ba97 * lb.cs:
Jonathan Pryor authored
432 // A category is always of the form "/((.+)/)*", e.g. /, /foo/, /foo/bar/
433 // Add the DayEntry to its category and all parent categories
434 void AddCategory (Hashtable hash, DayEntry day)
435 {
436 string category = day.Category;
437 do {
438 IList entries = (IList) hash [category];
439 if (entries == null) {
440 entries = new ArrayList ();
441 hash [category] = entries;
442 }
443 entries.Add (day);
444 int n = category.Length > 2
445 ? category.LastIndexOf ('/', category.Length-2)
446 : -1;
447 category = (n == -1) ? null : category.Substring (0, n+1);
448 } while (category != null && category.Length > 0);
449 }
450
66795b8 @migueldeicaza Further
migueldeicaza authored
451 static DateTime LastDate = new DateTime (2004, 5, 19, 0, 0, 0);
452
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
453 void Render (TextWriter o, IList entries, int idx, string blog_base, bool include_daily_anchor, bool single_entry)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
454 {
455 DayEntry d = (DayEntry) entries [idx];
456
6e61492 * template: Fixed it so it validates against validator.w3.org
Martin Willemoes Hansen authored
457 string anchor = HttpUtility.UrlEncode (d.Date.ToString ()).Replace ('%','-').Replace ('+', '-');
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
458 string entry_anchor = "";
66795b8 @migueldeicaza Further
migueldeicaza authored
459 if (include_daily_anchor || d.Date < LastDate)
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
460 entry_anchor = String.Format ("<a name=\"{0}\"></a>", anchor);
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
461
b6d92bf @migueldeicaza Add comments
migueldeicaza authored
462 string entry_specific = "";
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
463 string navigation = "";
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
464
465 if (single_entry){
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
466 navigation = GetEntryNavigation (entries, idx, blog_base);
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
467 if (config.EntrySpecific != null && config.EntrySpecific != String.Empty)
468 entry_specific = File.OpenText (config.EntrySpecific).ReadToEnd ();
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
469 }
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
470
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
471 Hashtable substitutions = new Hashtable ();
472 substitutions.Add ("@ENTRY_ANCHOR@", entry_anchor);
473 substitutions.Add ("@ENTRY_NAVIGATION@", navigation);
474 substitutions.Add ("@ENTRY_SPECIFIC@", entry_specific);
475
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
476 FillEntrySubstitutions (substitutions, d, blog_base, single_entry);
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
477
478 StringWriter body = new StringWriter (new StringBuilder (d.Body.Length));
479 Translate (d.Body, body, substitutions);
480
481 substitutions.Add ("@ENTRY_BODY@", body.ToString ());
482 Translate (entry_template, o, substitutions);
483 }
484
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
485 void FillEntrySubstitutions (Hashtable substitutions, DayEntry d, string blog_base, bool single_entry)
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
486 {
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
487 string category_paths = GetCategoryPaths (d, blog_base);
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
488 string entry_path = LB.GetEntryPath (blog_base, d);
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
489
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
490 substitutions.Add ("@ENTRY_ID@", d.Id);
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
491 substitutions.Add ("@ENTRY_PATH@", entry_path);
492 substitutions.Add ("@ENTRY_PERMALINK@", d.PermaLink);
493 substitutions.Add ("@ENTRY_CAPTION@", d.Caption);
c1aafa4 @migueldeicaza Improve
migueldeicaza authored
494 substitutions.Add ("@ENTRY_CAPTION_ENC@", HttpUtility.UrlEncode (d.Caption));
3d18eb2 @migueldeicaza Update
migueldeicaza authored
495 substitutions.Add ("@BASEDIR@", config.BlogWebDirectory);
1c55267 @gonzalop 2006-02-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
gonzalop authored
496 substitutions.Add ("@BASEIMAGES@", config.BlogImageBasedir);
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
497 substitutions.Add ("@COPYRIGHT@", config.Copyright);
498 substitutions.Add ("@ENTRY_CATEGORY@", d.Category);
499 substitutions.Add ("@ENTRY_DATECAPTION@", d.DateCaption);
500 substitutions.Add ("@ENTRY_CATEGORY_PATHS@", category_paths);
3d18eb2 @migueldeicaza Update
migueldeicaza authored
501 substitutions.Add ("@BLOGWEBDIR@", config.BlogWebDirectory);
502 substitutions.Add ("@ENTRY_URL_PERMALINK@", Path.Combine (config.BlogWebDirectory, d.PermaLink));
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
503
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
504 if (d.Comments && single_entry){
5e56455 @migueldeicaza Add support for comments to lame blog
migueldeicaza authored
505 StringWriter rendered_comment = new StringWriter (new StringBuilder (comments.Length));
506 Translate (comments, rendered_comment, substitutions);
507 substitutions.Add ("@COMMENTS@", rendered_comment.ToString ());
590eca4 @migueldeicaza Some updates: always comment after jan 2007, small css tuneups
migueldeicaza authored
508 d.RenderedComment = rendered_comment.ToString ();
509 } else
5e56455 @migueldeicaza Add support for comments to lame blog
migueldeicaza authored
510 substitutions.Add ("@COMMENTS@", "");
a41ba97 * lb.cs:
Jonathan Pryor authored
511 }
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
512
a41ba97 * lb.cs:
Jonathan Pryor authored
513 string GetEntryNavigation (IList entries, int idx, string blog_base)
514 {
515 DayEntry prev = (DayEntry) (idx > 0 ? entries [idx-1] : null);
516 DayEntry next = (DayEntry) (idx+1 < entries.Count ? entries [idx+1] : null);
517
518 StringBuilder nav = new StringBuilder ();
519
520 if (prev != null)
521 nav.Append (string.Format ("<a href=\"{0}{1}\">&laquo; {2}</a> | \n",
522 blog_base, prev.PermaLink, prev.Caption));
523 nav.Append (string.Format ("<a href=\"{0}{1}\">Main</a>\n",
524 blog_base, config.BlogFileName));
525 if (next != null)
526 nav.Append (string.Format (" | <a href=\"{0}{1}\">{2} &raquo;</a> \n",
527 blog_base, next.PermaLink, next.Caption));
d418a12 @migueldeicaza Flush my changes
migueldeicaza authored
528 #if false
529 nav.Append (string.Format ("<h3><a href=\"{2}{0}\" class=\"entryTitle\">{3}</a></h3>",
530 d.PermaLink, d.DateCaption, blog_base, d.Caption));
531 nav.Append ("<div class='blogentry'>" + d.Body + "</div>");
532 nav.Append (string.Format ("<div class='footer'>Posted by {2} on <a href=\"{0}{1}\">{3}</a></div><p>",
533 blog_base, d.PermaLink, config.Copyright, d.DateCaption));
534 #endif
a41ba97 * lb.cs:
Jonathan Pryor authored
535 return nav.ToString ();
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
536 }
a41ba97 * lb.cs:
Jonathan Pryor authored
537
358b9c2 * lb.cs: Add entry template support (so each entry can be templated).
Jonathan Pryor authored
538 string GetCategoryPaths (DayEntry d, string blog_base)
539 {
540 string[] paths = d.Category.Split ('/');
541 // It'll be more common for no category to be used -- the "/" category --
542 // which requires 24 characters. Optimize for the common case.
543 StringBuilder cat_paths = new StringBuilder (32);
544
545 // Skip the last paths entry, as it's the "" string
546 for (int i = 0; i < paths.Length-1; ++i) {
547 string parent = string.Join ("/", paths, 0, i+1) + "/";
548 cat_paths.AppendFormat ("<a href=\"{0}archive{1}\">{2}/</a>",
549 blog_base, parent, paths [i]);
550 }
551
552 return cat_paths.ToString ();
553 }
554
555 // Single-pass s/@...@/.../ replacement engine.
556 // `substitutions' contains search text (including @) and replacement text.
557 void Translate (string input, TextWriter o, Hashtable substitutions)
558 {
559 int token = -1;
560 bool escape = false;
561 for (int i = 0; i < input.Length; ++i) {
562 char c = input [i];
563 string subst = null;
564 if (token >= 0)
565 subst = input.Substring (token, i-token+1);
566 switch (c) {
567 case '\\':
568 escape = true;
569 break;
570 case '@':
571 if (escape) {
572 escape = false;
573 // Only write a new @ if not inside @...@.
574 if (token == -1) {
575 escape = false;
576 o.Write ('@');
577 }
578 break;
579 }
580 if (token == -1) {
581 token = i;
582 }
583 else {
584 string rep = (string) substitutions [subst];
585 if (rep == null) {
586 // No match; look for a new match from this point
587 o.Write (subst.Substring (0, subst.Length-1));
588 token = i;
589 }
590 else {
591 o.Write (rep);
592 token = -1;
593 }
594 }
595 break;
596 case '\r': case '\n':
597 if (token != -1) {
598 o.Write (subst);
599 token = -1;
600 escape = false;
601 break;
602 }
603 goto default;
604 default:
605 if (escape && token == -1) {
606 o.Write ('\\');
607 }
608 escape = false;
609 if (token == -1)
610 o.Write (c);
611 break;
612 }
613 }
614 }
615
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
616 void Render (TextWriter o, IList entries, int start, int end, string blog_base, bool include_daily_anchor)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
617 {
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
618 bool navigation = start + 1 == end;
619
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
620 for (int i = start; i < end; i++){
621 int idx = entries.Count - i - 1;
622 if (idx < 0)
623 return;
624
a41ba97 * lb.cs:
Jonathan Pryor authored
625 Render (o, entries, idx, blog_base, include_daily_anchor, navigation);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
626 }
627 }
628
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
629 void RenderArticleList (TextWriter o)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
630 {
631 foreach (Article a in articles){
0d54b2b @migueldeicaza Flush my changes
migueldeicaza authored
632 o.WriteLine ("<a href=\"{0}\">{1}</a><br>", a.url, a.caption);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
633 }
634 }
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
635
636 string GetPageNavigation (int start, int end)
637 {
638 StringBuilder nav = new StringBuilder ();
639
640 if (start != 0)
641 nav.Append (string.Format ("<a href=\"{0}\">&laquo; Newer entries</a>", LB.GetOutputFileAtOffset (start)));
642
643 if (end + Config.EntriesPerPage < entries.Count){
644 if (nav.Length > 0)
645 nav.Append (" | ");
646 nav.Append (string.Format ("<a href=\"{0}\">Older entries &raquo;</a>", LB.GetOutputFileAtOffset (end+1)));
647 }
648
649 return nav.ToString ();
650 }
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
651
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
652 void RenderHtml (string output, string blog_base, IList entries, int start, int end, bool include_page_navigation)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
653 {
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
654 using (FileStream o = CreateFile (output)){
7577226 * lb.cs: Read input & output file encodings from the config file
Jonathan Pryor authored
655 StreamWriter w = new StreamWriter (o, GetOutputEncoding ());
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
656
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
657 StringWriter blog_entries = new StringWriter ();
1c55267 @gonzalop 2006-02-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
gonzalop authored
658 Render (blog_entries, entries, start, end, blog_base, Path.GetFileName (output) == "all.html");
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
659
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
660 StringWriter blog_articles = new StringWriter ();
661 RenderArticleList (blog_articles);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
662
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
663 string page_navigation = "";
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
664
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
665 string title;
666 if (Math.Abs (start - end) == 1){
667 DayEntry d = (DayEntry) entries [entries.Count - start - 1];
668 title = String.Format ("{0} - {1}", d.Caption, config.Title);
669 } else {
670 title = config.Title;
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
671
672 if (include_page_navigation)
673 page_navigation = GetPageNavigation (start, end);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
674 }
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
675
676 Hashtable substitutions = new Hashtable ();
677 substitutions.Add ("@BLOG_ENTRIES@", blog_entries.ToString ());
498904d @migueldeicaza Add code that was removed by Jon
migueldeicaza authored
678 substitutions.Add ("@ANALYTICS@", analytics);
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
679 substitutions.Add ("@BLOG_ENTRY_INDEX@", CreateEntryIndex (entries, start, end));
680 substitutions.Add ("@BLOG_ARTICLES@", blog_articles.ToString ());
681 substitutions.Add ("@BASEDIR@", blog_base);
682 substitutions.Add ("@TITLE@", title);
683 substitutions.Add ("@DESCRIPTION@", config.Description);
684 substitutions.Add ("@RSSFILENAME@", config.RSSFileName);
685 substitutions.Add ("@EDITOR@", config.ManagingEditor);
74206fc @migueldeicaza Unleash the fury!
migueldeicaza authored
686 substitutions.Add ("@BLOGWEBDIR@", config.BlogWebDirectory);
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
687 substitutions.Add ("@ARCHIVE_NAVIGATOR@", archive_navigator);
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
688 substitutions.Add ("@PAGE_NAVIGATION@", page_navigation);
689
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
690 Translate (template, w, substitutions);
691
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
692 w.Flush ();
693 }
694 }
695
7577226 * lb.cs: Read input & output file encodings from the config file
Jonathan Pryor authored
696 // The default Encoding.GetEncoding ("utf-8") includes the BOM,
697 // which we don't want
698 Encoding GetOutputEncoding ()
699 {
700 string encoding = config.OutputEncoding;
701 Encoding e;
702 if (encoding != null && encoding.ToLower().Replace ("-", "_").Equals ("utf_8"))
703 e = new UTF8Encoding (false);
704 else
705 e = Encoding.GetEncoding (encoding);
706 return e;
707 }
708
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
709 string CreateEntryIndex (IList entries, int start, int end)
710 {
711 StringBuilder sb = new StringBuilder ();
712 sb.Append ("<ul class=\"blog-index\">\n");
713 for (int i = start; i < end; ++i) {
714 int idx = entries.Count - i - 1;
715 if (idx < 0)
716 break;
717 DayEntry d = (DayEntry) entries [idx];
718 sb.AppendFormat (" <li class=\"blog-index-item\"><a href=\"#{0}\">{1}</a></li>\n",
719 d.Id, d.Caption);
720 }
721 sb.Append ("</ul>\n");
722 return sb.ToString ();
723 }
724
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
725 public void RenderHtml (string output, int start, int end, string blog_base, bool include_page_navigation)
a41ba97 * lb.cs:
Jonathan Pryor authored
726 {
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
727 RenderHtml (output, blog_base, entries, start, end, include_page_navigation);
a41ba97 * lb.cs:
Jonathan Pryor authored
728 }
729
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
730 public void RenderArchive ()
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
731 {
732 for (int i = 0; i < Entries; i++){
733 DayEntry d = (DayEntry) entries [i];
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
734
a41ba97 * lb.cs:
Jonathan Pryor authored
735 string parent_dir = "../..";
736 if (d.Category.Length > 0)
737 parent_dir += Regex.Replace (d.Category, "[^/]+", "..");
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
738 RenderHtml (Path.Combine (config.Prefix, d.PermaLink),
739 entries.Count - i - 1, entries.Count - i, parent_dir, false);
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
740 if (d.Images == null)
741 continue;
742 foreach (string filename in d.Images) {
743 string file = Path.GetFileName (filename);
744 string thumbnail = Path.Combine (LB.GetEntryPath (config.Prefix + "/", d), LB.GetThumbnailName (file));
745 string thumbnail_target = Path.Combine (
746 LB.GetEntryPath (config.Prefix + "/", d), file);
747
748 file = Path.Combine (config.ImageDirectory, filename);
749 if (!File.Exists (file)) {
750 Console.Error.WriteLine ("lb: Missing file for #thumbnail {0}, ({1}).",
751 filename, file);
752 continue;
753 }
754 if (!File.Exists (thumbnail_target))
755 File.Copy (file, thumbnail_target);
756 if (!File.Exists (thumbnail)) {
757 ProcessStartInfo psi = new ProcessStartInfo (config.ThumbnailCommandFileName);
758 psi.Arguments = string.Format (config.ThumbnailCommandArguments, file, thumbnail);
759 Process p = Process.Start (psi);
760 p.WaitForExit ();
761 if (p.ExitCode != 0)
762 Console.Error.WriteLine ("lb: error running command: {0} {1}",
763 psi.FileName, psi.Arguments);
764 }
765 }
a41ba97 * lb.cs:
Jonathan Pryor authored
766 }
767
768 foreach (DictionaryEntry de in category_entries) {
769 string category = de.Key.ToString ();
770 IList entries = (IList) de.Value;
771 string parent_dir = ".." + Regex.Replace (category, "[^/]+", "..");
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
772 RenderHtml (Path.Combine (config.Prefix, "archive" + category + config.BlogFileName),
773 parent_dir, entries, 0, entries.Count, false);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
774 }
775 }
776
777 RssChannel MakeChannel ()
778 {
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
779 RssChannel c = new RssChannel (config.Title, config.Description, new Uri (config.BlogWebDirectory + "/" + config.BlogFileName));
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
780
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
781 c.Copyright = config.Copyright;
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
782 c.Generator = "lb#";
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
783 c.ManagingEditor = config.ManagingEditor;
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
784 // c.PubDate = System.DateTime.Now;
785 c.PubDate = pubDate;
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
786
787 return c;
788 }
789
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
790 public void RenderArchiveRss (RssVersion version, string output, int end)
791 {
792 foreach (DictionaryEntry de in category_entries) {
793 string category = de.Key.ToString ();
794 IList entries = (IList) de.Value;
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
795 RenderRSS (Path.Combine (config.Prefix, "archive" + category + output),
796 entries, 0, Math.Min (end, entries.Count));
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
797 }
798 }
799
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
800 public void RenderRSS (string output, IList entries, int start, int end)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
801 {
802 RssChannel channel = MakeChannel ();
803
804 for (int i = start; i < end; i++){
805 int idx = entries.Count - i - 1;
806 if (idx < 0)
807 continue;
808
809 DayEntry d = (DayEntry) entries [idx];
810
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
811 Hashtable substitutions = new Hashtable ();
995d841 @migueldeicaza Fix last commit, due to merge conflicts
migueldeicaza authored
812 FillEntrySubstitutions (substitutions, d, config.BlogWebDirectory, false);
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
813 StringWriter description = new StringWriter (new StringBuilder (d.Body.Length));
814 Translate (d.Body, description, substitutions);
815
816 StringWriter sw = new StringWriter (new StringBuilder (d.Body.Length));
817 Render (sw, entries, idx, "", false, false);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
818 RssItem item = new RssItem ();
fa15eda * lb.cs: Configuration data are now loaded from config.xml
Martin Willemoes Hansen authored
819 item.Author = config.Author;
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
820 item.Description = description.ToString ();
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
821 item.Guid = new RssGuid ();
6882b82 @migueldeicaza Update
migueldeicaza authored
822 item.Guid.Name = config.BlogWebDirectory + d.PermaLink;
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
823 item.Link = new Uri (item.Guid.Name);
824 item.Guid.PermaLink = DBBool.True;
05dabaf @migueldeicaza Correct
migueldeicaza authored
825
826 item.PubDate = d.Date.ToUniversalTime ();
b04d21b @migueldeicaza Multiple blog entries per day.
migueldeicaza authored
827 if (d.Caption == ""){
828 Console.WriteLine ("No caption for: " + d.DateCaption);
829 d.Caption = d.DateCaption;
830 }
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
831 item.Title = d.Caption;
832
833 channel.Items.Add (item);
834 }
835
a41ba97 * lb.cs:
Jonathan Pryor authored
836 FileStream o = CreateFile (output);
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
837 XmlTextWriter xtw = new XmlTextWriter (o, new UTF8Encoding (false));
838 Rss20Writer w = new Rss20Writer (xtw);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
839
840 w.Write (channel);
841 w.Close ();
842 }
7c028b5 @migueldeicaza Flushage
migueldeicaza authored
843
844 public void RenderRSS (string output, int start, int end)
845 {
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
846 RenderRSS (output + ".rss2", entries, start, end);
7c028b5 @migueldeicaza Flushage
migueldeicaza authored
847 }
848
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
849 public class Article {
850 public string url, caption;
851
852 public Article (string u, string c)
853 {
854 url = u;
855 caption = c;
856 }
857 }
858
859 ArrayList articles = new ArrayList ();
860
861 public void AddArticle (string url, string caption)
862 {
863 articles.Add (new Article (url, caption));
864 }
865
866
a41ba97 * lb.cs:
Jonathan Pryor authored
867 FileStream CreateFile (string file)
868 {
869 FileInfo info = new FileInfo (file);
870 if (!info.Directory.Exists)
871 info.Directory.Create ();
3d8c83c @migueldeicaza Update
migueldeicaza authored
872 try {
873 return File.Create (file);
874 } catch (Exception e){
875 Console.WriteLine (e);
876 return null;
877 }
a41ba97 * lb.cs:
Jonathan Pryor authored
878 }
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
879 }
880
881 class LB {
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
882 public static Config config;
883
884 static public string GetOutputFileAtOffset (int offset)
885 {
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
886 return offset == 0 ? config.BlogFileName : String.Format ("page{0}.html", offset / Config.EntriesPerPage);
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
887 }
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
888
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
889 static void Main (string[] args)
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
890 {
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
891 var basicConfig = new Config ();
892 if (!basicConfig.Parse (args))
893 return;
894
895 config = (Config) new XmlSerializer (typeof (Config)).Deserialize (new XmlTextReader (basicConfig.ConfigFile));
896
897 if (args.Length > 1)
898 config.Prefix = args [1];
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
899
5ccb9a2 @migueldeicaza Flush
migueldeicaza authored
900 if (config.BlogImageBasedir == null || config.BlogImageBasedir == "")
901 config.BlogImageBasedir = config.BlogWebDirectory;
1b62266 @gonzalop 2006-02-09 Gonzalo Paniagua Javier <gonzalo@ximian.com>
gonzalop authored
902 if (config.Prefix == null || config.Prefix == "")
903 config.Prefix = Environment.CurrentDirectory;
d4d4a6c @migueldeicaza Make this work without forcing people to edit their config.xml files
migueldeicaza authored
904 if (config.BlogTemplate == null || config.BlogTemplate == "")
905 config.BlogTemplate = "template";
906 if (config.EntryTemplate == null || config.EntryTemplate == "")
907 config.EntryTemplate = "entry";
908
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
909 if (!config.Parse (args))
910 return;
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
911
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
912 string template = File.OpenText (config.BlogTemplate).ReadToEnd ();
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
913
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
914 Blog b = new Blog (config, template);
915
916 //
917 // Renders the main page (index.html) and the various pageNN.html
918 //
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
919 for (int start = 0; start < b.Entries; start += Config.EntriesPerPage){
920 string output = GetOutputFileAtOffset (start);
921
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
922 b.RenderHtml (Path.Combine (config.Prefix, output), start, start + Config.EntriesPerPage, "", true);
4044ba9 @migueldeicaza 2008-05-04 Miguel de Icaza <miguel@novell.com>
migueldeicaza authored
923 }
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
924
925 //
926 // Renders the year/month dinguses
927 //
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
928
7fc7ea5 @migueldeicaza Bunch of changes, forgot what
migueldeicaza authored
929 //
930 // Legacy render: all.html
931 //
932 b.RenderHtml (Path.Combine (config.Prefix, "all.html"), 0, b.Entries, "", false);
933
934 //
935 // Renders each individual blog entry into the archive
936 //
937 b.RenderArchive ();
938
939 //
940 // The RSS feed
941 //
09a61dc * config.cs: Add Prefix (base directory for output), BlogTemplate
Jonathan Pryor authored
942 b.RenderRSS (Path.Combine (config.Prefix, config.RSSFileName), 0, 30);
943 b.RenderArchiveRss (RssVersion.RSS20, config.RSSFileName + ".rss2", 30);
944
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
945 if (File.Exists ("log-style.css")) {
946 File.Copy ("log-style.css", "texts/log-style.css", true);
b2903b9 @migueldeicaza Small changes to try to merge a wordpress theme
migueldeicaza authored
947 File.Copy ("log-style.css", Path.Combine (config.Prefix, "log-style.css"), true);
6785c45 * lb.cs: .rss descriptions should be Translate()d and Render()d.
Jonathan Pryor authored
948 }
949 }
950
951 public static string GetThumbnailName (string filename)
952 {
953 return Regex.Replace (filename, @"\.(.*?)$", @"-web.$1");
954 }
955
956 public static string GetEntryPath (string blog_base, DayEntry d)
957 {
958 return string.Format ("{0}archive{1}{2:yyyy}/", blog_base,
959 d.Category, d.Date);
59a9375 @migueldeicaza Initial revision
migueldeicaza authored
960 }
961 }
Something went wrong with that request. Please try again.