Author: jonpryor
Date: 2006-01-20 09:47:10 -0500 (Fri, 20 Jan 2006)
New Revision: 55834

Added:
   trunk/lb/entry.test
   trunk/lb/template.test
Modified:
   trunk/lb/ChangeLog
   trunk/lb/config.cs
   trunk/lb/lb.cs
   trunk/lb/makefile
Log:
  * config.cs: Add Prefix (base directory for output), BlogTemplate
    (previously hardcoded as the "template" file), EntryTemplate properties
    (previously hardcoded as the "entry" file).  Add support for command-line
    handling (used for regression tests).
  * entry.test, template.test: Test input files (used for regression tests).
  * lb.cs:
    - Use new Config fields to control entry & file templates, prefix for
      output directory;
    - Change the RSS publication date to be the latest file time found, not the
      current time (helps make regression tests more consistent, as the
      pubdate won't be changing all the time in the RSS file).
    - Move entry_id handling into DayEntry.Id property (it's used in 2 places).
    - Permit rendering to a TextWriter
    - Don't open/read/close the file template all the time, but cache the file
      in memory (slight performance improvement).
    - Change RenderHtml() to use Translate().  Allows greater code sharing,
      consistency, easily adding new variables for replacement.
    - Add @BLOG_ENTRY_INDEX@ variable, a <ul/> list of all entries in a given
      page.
    - Create a .rss2 file for each category.
  * makefile: Add check and check-update targets.  `check' runs the regression
    tests, while `check-update' updates the expected output.


Modified: trunk/lb/ChangeLog
===================================================================
--- trunk/lb/ChangeLog  2006-01-20 14:26:17 UTC (rev 55833)
+++ trunk/lb/ChangeLog  2006-01-20 14:47:10 UTC (rev 55834)
@@ -1,3 +1,31 @@
+2006-01-19  Jonathan Pryor  <[EMAIL PROTECTED]>
+
+       * config.cs: Add Prefix (base directory for output), BlogTemplate
+         (previously hardcoded as the "template" file), EntryTemplate 
properties
+         (previously hardcoded as the "entry" file).  Add support for 
command-line
+         handling (used for regression tests).
+       * entry.test, template.test: Test input files (used for regression 
tests).
+       * lb.cs: 
+         - Use new Config fields to control entry & file templates, prefix for 
+           output directory; 
+         - Change the RSS publication date to be the latest file time found, 
not the
+           current time (helps make regression tests more consistent, as the
+           pubdate won't be changing all the time in the RSS file).
+         - Move entry_id handling into DayEntry.Id property (it's used in 2 
places).
+         - Permit rendering to a TextWriter
+         - Don't open/read/close the file template all the time, but cache the 
file
+           in memory (slight performance improvement).
+         - Change RenderHtml() to use Translate().  Allows greater code 
sharing,
+           consistency, easily adding new variables for replacement.
+         - Add @BLOG_ENTRY_INDEX@ variable, a <ul/> list of all entries in a 
given
+           page.
+         - Create a .rss2 file for each category.
+       * makefile: Add check and check-update targets.  `check' runs the 
regression
+         tests, while `check-update' updates the expected output.
+       * test/in/2005/jul-20.txt, test/in/foo/bar/2005/jul-14.html: test input
+         files for regression tests.
+       * test/out/**: expected output for regression tests.
+
 2006-01-03  Miguel de Icaza  <[EMAIL PROTECTED]>
 
        * lb.cs: The THML title for individual archive blogs is now set to

Modified: trunk/lb/config.cs
===================================================================
--- trunk/lb/config.cs  2006-01-20 14:26:17 UTC (rev 55833)
+++ trunk/lb/config.cs  2006-01-20 14:47:10 UTC (rev 55834)
@@ -1,3 +1,4 @@
+using System;
 using System.Xml.Serialization;
 
 [XmlRoot("config")]
@@ -16,4 +17,110 @@
        [XmlAttribute] public string InputEncoding;
        [XmlAttribute] public string OutputEncoding;
        [XmlAttribute] public string AnalyticsStub;
+       [XmlAttribute] public string Prefix;
+       [XmlAttribute] public string BlogTemplate;
+       [XmlAttribute] public string EntryTemplate;
+
+       public bool Parse (string[] args)
+       {
+               for (int i = 0; i < args.Length; ++i) {
+                       string arg = args [i];
+                       switch (arg) {
+                       case "-h": case "--help":
+                               PrintHelp ();
+                               return false;
+                       case "-p": case "--prefix":
+                               if (NextArgument (args, ref i, ref Prefix))
+                                       break;
+                               return false;
+                       case "-d": case "--blog-directory":
+                               if (NextArgument (args, ref i, ref 
BlogDirectory))
+                                       break;
+                               return false;
+                       case "-b": case "--blog-template":
+                               if (NextArgument (args, ref i, ref 
BlogTemplate))
+                                       break;
+                               return false;
+                       case "-e": case "--entry-template":
+                               if (NextArgument (args, ref i, ref 
EntryTemplate))
+                                       break;
+                               return false;
+                       case "-x": case "--rss-filename":
+                               if (NextArgument (args, ref i, ref RSSFileName))
+                                       break;
+                               return false;
+                       default:
+                               if (ExtractArgument ("-p", arg, ref Prefix))
+                                       break;
+                               if (ExtractArgument ("--prefix", arg, ref 
Prefix))
+                                       break;
+                               if (ExtractArgument ("-b", arg, ref 
BlogTemplate))
+                                       break;
+                               if (ExtractArgument ("--blog-template", arg, 
ref BlogTemplate))
+                                       break;
+                               if (ExtractArgument ("-e", arg, ref 
EntryTemplate))
+                                       break;
+                               if (ExtractArgument ("--entry-template", arg, 
ref EntryTemplate))
+                                       break;
+                               if (ExtractArgument ("-d", arg, ref 
BlogDirectory))
+                                       break;
+                               if (ExtractArgument ("--blog-directory", arg, 
ref BlogDirectory))
+                                       break;
+                               if (ExtractArgument ("-x", arg, ref 
RSSFileName))
+                                       break;
+                               if (ExtractArgument ("--rss-filename", arg, ref 
RSSFileName))
+                                       break;
+                               Error ("unrecognized option `{0}'", arg);
+                               return false;
+                       }
+               }
+               return true;
+       }
+
+       private static void PrintHelp ()
+       {
+               Console.WriteLine ("Usage: lb [OPTION]*");
+               Console.WriteLine ("lb (Lame Blog) is a blog engine.");
+               Console.WriteLine (@"
+Options:
+  -p, --prefix=DIR            Root directory for generated files.
+  -d, --blog-directory=DIR    Where to find blog entry files (*.txt, *.html).
+  -b, --blog-template=FILE    Blog template file .
+  -e, --entry-template=FILE   Entry template file.
+  -x, --rss-filename=FILE     Basename for RSS filename.
+  -h, --help                  Display this message and exit.
+");
+       }
+
+       private void Error (string format, params object[] args)
+       {
+               Console.Write ("lb: ");
+               Console.WriteLine (format, args);
+               Console.WriteLine ("Try `lb --help' for more information.");
+       }
+
+       private bool ExtractArgument (string prefix, string argument, ref 
string value)
+       {
+               if (argument.Length - 1 <= prefix.Length)
+                       return false;
+               if (!argument.StartsWith (prefix))
+                       return false;
+
+               char delim = argument [prefix.Length];
+               if (delim != '=' && delim != ':')
+                       return false;
+
+               value = argument.Substring (prefix.Length+1);
+               return true;
+       }
+
+       private bool NextArgument (string[] args, ref int i, ref string value)
+       {
+               if ((i+1) >= args.Length) {
+                       Error ("missing argument for `{0}'", args [i]);
+                       return false;
+               }
+               value = args [++i];
+               return true;
+       }
 }

Added: trunk/lb/entry.test
===================================================================
--- trunk/lb/entry.test 2006-01-20 14:26:17 UTC (rev 55833)
+++ trunk/lb/entry.test 2006-01-20 14:47:10 UTC (rev 55834)
@@ -0,0 +1,30 @@
+<p>@ENTRY_NAVIGATION@</p>
+<h2 id="@ENTRY_ID@" name="@ENTRY_ANCHOR@">@ENTRY_CAPTION</h2>
[EMAIL PROTECTED]@
+<hr>
+<p>Test Variables:</p>
+<dl>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@BASEDIR@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@COPYRIGHT@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_ANCHOR@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_CAPTION@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_CATEGORY@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_CATEGORY_PATHS@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_DATECAPTION@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_ID@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_NAVIGATION@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_PATH@</dd>
+  <dt>[EMAIL PROTECTED]@:</dt>
+  <dd>@ENTRY_PERMALINK@</dd>
+</dl>
+

Modified: trunk/lb/lb.cs
===================================================================
--- trunk/lb/lb.cs      2006-01-20 14:26:17 UTC (rev 55833)
+++ trunk/lb/lb.cs      2006-01-20 14:47:10 UTC (rev 55834)
@@ -271,10 +271,18 @@
                        return String.Format 
("archive{2}{0:yyyy}/{0:MMM}-{0:dd}{1}.html", Date, extra, Category);
                }
        }
+
+       public string Id {
+               get {
+                       return string.Format ("entry{0}{1}{2}",
+                               Category.Replace ('/', '-'), Date.ToString 
("yyyy-MM-ddThh:mm:sstt"), extra);
+               }
+       }
 }
 
 class Blog {
        public Config config;
+       public DateTime pubDate = new DateTime (1, 1, 1);
        string entry_template;
        string analytics = "";
        Hashtable category_entries = new Hashtable ();
@@ -290,7 +298,7 @@
        public Blog (Config config)
        {
                this.config = config;
-               this.entry_template = File.OpenText ("entry").ReadToEnd ();
+               this.entry_template = File.OpenText 
(config.EntryTemplate).ReadToEnd ();
 
                LoadDirectory (new DirectoryInfo (config.BlogDirectory));
 
@@ -313,8 +321,11 @@
                        if (!(file.Name.EndsWith (".html") || 
file.Name.EndsWith (".txt")))
                                continue;
                        DayEntry de = DayEntry.Load (this, file.FullName);
-                       if (de != null)
+                       if (de != null) {
                                entries.Add (de);
+                               if (de.Date > pubDate)
+                                       pubDate = de.Date;
+                       }
                }
        }
 
@@ -339,7 +350,7 @@
        
        static DateTime LastDate = new DateTime (2004, 5, 19, 0, 0, 0);
        
-       void Render (StreamWriter o, IList entries, int idx, string blog_base, 
bool include_daily_anchor, bool include_navigation)
+       void Render (TextWriter o, IList entries, int idx, string blog_base, 
bool include_daily_anchor, bool include_navigation)
        {
                DayEntry d = (DayEntry) entries [idx];
 
@@ -356,12 +367,9 @@
                string category_paths = GetCategoryPaths (d, blog_base);
                string entry_path = string.Format ("{0}archive{1}{2:yyyy}/",
                        blog_base, d.Category, d.Date);
-               string entry_id = string.Format ("entry{0}{1}{2}",
-                       d.Category.Replace ('/', '-'), d.Date.ToString 
("yyyy-MM-ddThh:mm:sstt"),
-                       d.extra);
 
                Hashtable substitutions = new Hashtable ();
-               substitutions.Add ("@ENTRY_ID@", entry_id);
+               substitutions.Add ("@ENTRY_ID@", d.Id);
                substitutions.Add ("@ENTRY_ANCHOR@", entry_anchor);
                substitutions.Add ("@ENTRY_PATH@", entry_path);
                substitutions.Add ("@ENTRY_NAVIGATION@", navigation);
@@ -483,7 +491,7 @@
                }
        }
 
-       void Render (StreamWriter o, IList entries, int start, int end, string 
blog_base, bool include_daily_anchor)
+       void Render (TextWriter o, IList entries, int start, int end, string 
blog_base, bool include_daily_anchor)
        {
                bool navigation = start + 1 == end;
                
@@ -496,7 +504,7 @@
                }
        }
 
-       void RenderArticleList (StreamWriter o)
+       void RenderArticleList (TextWriter o)
        {
                foreach (Article a in articles){
                        o.WriteLine ("<a href=\"{0}\">{1}</a><br>", a.url, 
a.caption);
@@ -506,41 +514,35 @@
        void RenderHtml (string template, string output, string blog_base, 
IList entries,
                        int start, int end)
        {
-               using (FileStream i = File.OpenRead (template), o = CreateFile 
(output)){
-                       StreamReader s = new StreamReader (i, 
Encoding.GetEncoding (config.InputEncoding));
+               using (FileStream o = CreateFile (output)){
                        StreamWriter w = new StreamWriter (o, GetOutputEncoding 
());
-                       string line;
 
-                       while ((line = s.ReadLine ()) != null){
-                               switch (line){
-                               case "@BLOG_ENTRIES@":
-                                       Render (w, entries, start, end, 
blog_base, output == "all.html");
-                                       break;
-                               case "@BLOG_ARTICLES@":
-                                       RenderArticleList (w);
-                                       break;
+                       StringWriter blog_entries = new StringWriter ();
+                       Render (blog_entries, entries, start, end, blog_base, 
output == "all.html");
 
-                               default:
-                                       string title;
+                       StringWriter blog_articles = new StringWriter ();
+                       RenderArticleList (blog_articles);
 
-                                       if (Math.Abs (start - end) == 1){
-                                               DayEntry d = (DayEntry) entries 
[entries.Count - start - 1];
-                                               title = String.Format ("{0} - 
{1}", d.Caption, config.Title);
-                                       } else {
-                                               title = config.Title;
-                                       }
-                                       
-                                       line = line.Replace ("@BASEDIR@", 
blog_base);
-                                       line = line.Replace ("@ANALYTICS@", 
analytics);
-                                       line = line.Replace ("@TITLE@", title);
-                                       line = line.Replace ("@DESCRIPTION@", 
config.Description);
-                                       line = line.Replace ("@RSSFILENAME@", 
config.RSSFileName);
-                                       line = line.Replace ("@EDITOR@", 
config.ManagingEditor);
-                                       w.WriteLine (line);
-                                       break;
-                               }
+                       string title;
+                       if (Math.Abs (start - end) == 1){
+                               DayEntry d = (DayEntry) entries [entries.Count 
- start - 1];
+                               title = String.Format ("{0} - {1}", d.Caption, 
config.Title);
+                       } else {
+                               title = config.Title;
+                       }
 
-                       }
+                       Hashtable substitutions = new Hashtable ();
+                       substitutions.Add ("@BLOG_ENTRIES@", 
blog_entries.ToString ());
+                       substitutions.Add ("@BLOG_ENTRY_INDEX@", 
CreateEntryIndex (entries, start, end));
+                       substitutions.Add ("@BLOG_ARTICLES@", 
blog_articles.ToString ());
+                       substitutions.Add ("@BASEDIR@", blog_base);
+                       substitutions.Add ("@TITLE@", title);
+                       substitutions.Add ("@DESCRIPTION@", config.Description);
+                       substitutions.Add ("@RSSFILENAME@", config.RSSFileName);
+                       substitutions.Add ("@EDITOR@", config.ManagingEditor);
+
+                       Translate (template, w, substitutions);
+
                        w.Flush ();
                }
        }
@@ -558,6 +560,22 @@
                return e;
        }
 
+       string CreateEntryIndex (IList entries, int start, int end)
+       {
+               StringBuilder sb = new StringBuilder ();
+               sb.Append ("<ul class=\"blog-index\">\n");
+               for (int i = start; i < end; ++i) {
+                       int idx = entries.Count - i - 1;
+                       if (idx < 0)
+                               break;
+                       DayEntry d = (DayEntry) entries [idx];
+                       sb.AppendFormat ("  <li class=\"blog-index-item\"><a 
href=\"#{0}\">{1}</a></li>\n",
+                                       d.Id, d.Caption);
+               }
+               sb.Append ("</ul>\n");
+               return sb.ToString ();
+       }
+
        public void RenderHtml (string template, string output, int start, int 
end, string blog_base)
        {
                RenderHtml (template, output, blog_base, entries, start, end);
@@ -571,14 +589,16 @@
                        string parent_dir = "../..";
                        if (d.Category.Length > 0)
                                parent_dir += Regex.Replace (d.Category, 
"[^/]+", "..");
-                       RenderHtml (template, d.PermaLink, entries.Count - i - 
1, entries.Count - i, parent_dir);
+                       RenderHtml (template, Path.Combine (config.Prefix, 
d.PermaLink), 
+                                       entries.Count - i - 1, entries.Count - 
i, parent_dir);
                }
 
                foreach (DictionaryEntry de in category_entries) {
                        string category = de.Key.ToString ();
                        IList entries = (IList) de.Value;
                        string parent_dir = ".." + Regex.Replace (category, 
"[^/]+", "..");
-                       RenderHtml (template, "archive" + category + 
"index.html",
+                       RenderHtml (template, 
+                                       Path.Combine (config.Prefix, "archive" 
+ category + "index.html"),
                                        parent_dir, entries, 0, entries.Count);
                }
        }
@@ -593,13 +613,26 @@
                c.Copyright = config.Copyright;
                c.Generator = "lb#";
                c.ManagingEditor = config.ManagingEditor;
-               c.PubDate = System.DateTime.Now;
+               // c.PubDate = System.DateTime.Now;
+               c.PubDate = pubDate;
                
                return c;
        }
 
-       public void RenderRSS (RssVersion version, string output, int start, 
int end)
+       public void RenderArchiveRss (RssVersion version, string output, int 
end)
        {
+               foreach (DictionaryEntry de in category_entries) {
+                       string category = de.Key.ToString ();
+                       IList entries = (IList) de.Value;
+                       string parent_dir = ".." + Regex.Replace (category, 
"[^/]+", "..");
+                       RenderRSS (version, 
+                                       Path.Combine (config.Prefix, "archive" 
+ category + output),
+                                       entries, 0, Math.Min (end, 
entries.Count));
+               }
+       }
+
+       public void RenderRSS (RssVersion version, string output, IList 
entries, int start, int end)
+       {
                RssChannel channel = MakeChannel ();
 
                for (int i = start; i < end; i++){
@@ -638,7 +671,7 @@
 
        public void RenderRSS (string output, int start, int end)
        {
-               RenderRSS (RssVersion.RSS20, output + ".rss2", start, end);
+               RenderRSS (RssVersion.RSS20, output + ".rss2", entries, start, 
end);
        }
 
        public class Article {
@@ -670,20 +703,24 @@
 
 class LB {
 
-       static void Main ()
+       static void Main (string[] args)
        {
                Config config = (Config) 
                        new XmlSerializer (typeof (Config)).Deserialize (new 
XmlTextReader ("config.xml"));
                if (config.BlogImageBasedir == null || config.BlogImageBasedir 
== "")
                        config.BlogImageBasedir = config.BlogWebDirectory;
-
+               if (!config.Parse (args))
+                       return;
                Blog b = new Blog (config);
 
-               b.RenderHtml ("template", config.BlogFileName, 0, 30, "");
-               b.RenderHtml ("template", "all.html", 0, b.Entries, "");
-               b.RenderArchive ("template");
+               string template = File.OpenText (config.BlogTemplate).ReadToEnd 
();
+               b.RenderHtml (template, Path.Combine (config.Prefix, 
config.BlogFileName), 0, 30, "");
+               b.RenderHtml (template, Path.Combine (config.Prefix, 
"all.html"), 0, b.Entries, "");
+               b.RenderArchive (template);
                
-               b.RenderRSS (config.RSSFileName, 0, 30);
+               b.RenderRSS (Path.Combine (config.Prefix, config.RSSFileName), 
0, 30);
+               b.RenderArchiveRss (RssVersion.RSS20, config.RSSFileName + 
".rss2", 30);
+
                File.Copy ("log-style.css", "texts/log-style.css", true);
        }
 }

Modified: trunk/lb/makefile
===================================================================
--- trunk/lb/makefile   2006-01-20 14:26:17 UTC (rev 55833)
+++ trunk/lb/makefile   2006-01-20 14:47:10 UTC (rev 55834)
@@ -3,7 +3,7 @@
 ASSEMBLIES=    -r:RSS.NET.dll  \
                -r:System.Web   
 
-lb.exe: lb.cs
+lb.exe: lb.cs config.cs
        mcs $(ASSEMBLIES) -debug lb.cs config.cs -out:lb.exe 
 
 b: lb.exe
@@ -19,3 +19,16 @@
        log-style.css *.rss2 *.php index.html all.html          \
        $(REMOTE_DIRECTORY)
 
+check-update:
+       -rm -Rf test/out
+       mono --debug lb.exe --blog-directory=`pwd`/test/in --prefix=test/out \
+               --blog-template=template.test --entry-template=entry.test \
+               --rss-filename=test
+
+check:
+       -rm -Rf test/tmp
+       mono --debug lb.exe --blog-directory=`pwd`/test/in --prefix=test/tmp \
+               --blog-template=template.test --entry-template=entry.test \
+               --rss-filename=test
+       diff -ru test/out test/tmp 
+

Added: trunk/lb/template.test
===================================================================
--- trunk/lb/template.test      2006-01-20 14:26:17 UTC (rev 55833)
+++ trunk/lb/template.test      2006-01-20 14:47:10 UTC (rev 55834)
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd";>
+<html xml:lang="en">
+  <head>
+    <title>@TITLE@</title>
+    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+    <style type="text/css" title="default">
+    @import '../@[EMAIL PROTECTED]';
+    </style>
+  </head>
+  <body>
+    <h1>Lame Blog Test Template</h1>
+    <h2>Simple Substitutions:</h2>
+    <dl>
+      <dt>[EMAIL PROTECTED]@:</dt><dd>@BASEDIR@</dd>
+      <dt>[EMAIL PROTECTED]@:</dt><dd>@DESCRIPTION@</dd>
+      <dt>[EMAIL PROTECTED]@:</dt><dd>@EDITOR@</dd>
+      <dt>[EMAIL PROTECTED]@:</dt><dd>@RSSFILENAME@</dd>
+      <dt>[EMAIL PROTECTED]@:</dt><dd>@TITLE@</dd>
+    </dl>
+    <h2>Complex Substitutions:</h2>
+    <h3>[EMAIL PROTECTED]@:</h3>
+    <blockquote>
+    @BLOG_ENTRY_INDEX@
+    </blockquote>
+    <h3>[EMAIL PROTECTED]@:</h3>
+    <blockquote>
+    @BLOG_ENTRIES@
+    </blockquote>
+    <h3>[EMAIL PROTECTED]@:</h3>
+    <blockquote>
+    @BLOG_ARTICLES@
+    </blockquote>
+  </body>
+</html>

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to