Permalink
Browse files

* lb.cs: .rss descriptions should be Translate()d and Render()d.

	  The entry can contain @...@ strings which need to be substituted; 
	  the RSS description is incorrect if this isn't done.  Make sure
	  log-style.css exists before trying to copy it.  Add a #thumbnail
	  command to simplify thumbnail graphic generation.
	* config.cs: Add -t and -thumbnail-command command-line arguments.
	* README: Document #thumbnail.

svn path=/trunk/lb/; revision=85451
  • Loading branch information...
1 parent b6d92bf commit 6785c45c650d63e2666dba5c9f6f1486bfcbe622 Jonathan Pryor committed Sep 6, 2007
Showing with 133 additions and 14 deletions.
  1. +10 −0 ChangeLog
  2. +21 −0 README
  3. +20 −0 config.cs
  4. +82 −14 lb.cs
View
@@ -1,3 +1,13 @@
+2007-09-06 Jonathan Pryor <jonpryor@vt.edu>
+
+ * lb.cs: .rss descriptions should be Translate()d and Render()d.
+ The entry can contain @...@ strings which need to be substituted;
+ the RSS description is incorrect if this isn't done. Make sure
+ log-style.css exists before trying to copy it. Add a #thumbnail
+ command to simplify thumbnail graphic generation.
+ * config.cs: Add -t and -thumbnail-command command-line arguments.
+ * README: Document #thumbnail.
+
2007-01-07 Alp Toker <alp@atoker.com>
* template: Make it valid XML, fix 'Picassa' spelling.
View
21 README
@@ -35,6 +35,8 @@ This is Lame Blog.
OutputEncoding="utf-8"
AnalyticsStub="analytics"
CommentsStub="comments"
+ ImageDirectory="/path/to/images"
+ ThumbnailCommand="shell-command-for-thumbnail source={0} thumbnail={1}"
/>
The various parameters should be self explanatory.
@@ -90,6 +92,9 @@ This is Lame Blog.
Will generate the code to center the image stored in
filename with the given caption.
+ This emits an HTML anchor to "pic.php" providing filename and
+ caption as GET parameters.
+
#comment, #comments
Will enable the entry to have comments. To activate
@@ -98,5 +103,21 @@ This is Lame Blog.
the file referenced in the config.xml CommentsStub
field
+ #thumbnail filename caption
+
+ A static alternative to #pic. This looks for `filename'
+ underneath ImageDirectory, copies `filename' into the archive
+ directory, creates a thumbnail for `filename' by invoking
+ ThumbnailCommand, and generates HTML that is an <a><img/></a> to
+ the thumbnail which targets the full-size image.
+
+ Caption is used as the alt and title text of the <img/>.
+
+ A possible ThumbnailCommand value is:
+
+ convert -size 400x300 {0} -resize 400x300 +profile '&quot;*&quot;' {1}
+
+ which generates a 400x300 pixel thumbnail of the input file.
+
Enjoy,
Miguel de Icaza (miguel@gnu.org)
View
@@ -22,6 +22,11 @@ public class Config {
[XmlAttribute] public string EntryTemplate;
[XmlAttribute] public string CommentsStub;
[XmlAttribute] public string EntrySpecific;
+ [XmlAttribute] public string ImageDirectory;
+ [XmlAttribute] public string ThumbnailCommand;
+
+ internal string ThumbnailCommandFileName;
+ internal string ThumbnailCommandArguments;
public bool Parse (string[] args)
{
@@ -51,6 +56,10 @@ public bool Parse (string[] args)
if (NextArgument (args, ref i, ref RSSFileName))
break;
return false;
+ case "-t": case "--thumbnail-command":
+ if (NextArgument (args, ref i, ref ThumbnailCommand))
+ break;
+ return false;
default:
if (ExtractArgument ("-p", arg, ref Prefix))
break;
@@ -72,10 +81,18 @@ public bool Parse (string[] args)
break;
if (ExtractArgument ("--rss-filename", arg, ref RSSFileName))
break;
+ if (ExtractArgument ("-t", arg, ref ThumbnailCommand))
+ break;
+ if (ExtractArgument ("--thumbnail-command", arg, ref ThumbnailCommand))
+ break;
Error ("unrecognized option `{0}'", arg);
return false;
}
}
+ if (ThumbnailCommand != null) {
+ ThumbnailCommandFileName = ThumbnailCommand.Split (' ')[0];
+ ThumbnailCommandArguments = ThumbnailCommand.Substring (ThumbnailCommandFileName.Length+1);
+ }
return true;
}
@@ -90,6 +107,9 @@ private static void PrintHelp ()
-b, --blog-template=FILE Blog template file .
-e, --entry-template=FILE Entry template file.
-x, --rss-filename=FILE Basename for RSS filename.
+ -t, --thumbnail-command=CMD Command to use to generate thumbnails.
+ {0} is the input file.
+ {1} is the input file.
-h, --help Display this message and exit.
");
}
View
96 lb.cs
@@ -23,6 +23,8 @@
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
+using System.Collections.Generic;
+using System.Diagnostics;
using System.Globalization;
using System.Web;
using System.Xml;
@@ -37,6 +39,7 @@ class DayEntry : IComparable {
public string Category = "";
public bool Comments = DateTime.Now > new DateTime (2007, 1, 1);
public string RenderedComment;
+ public List<string> Images;
Blog blog;
public string extra = "";
@@ -225,6 +228,20 @@ void Load (StreamReader i, bool is_html)
} else if (s.StartsWith ("#comment")){
Comments = true;
continue;
+ } else if (s.StartsWith ("#thumbnail")) {
+ Match m = Regex.Match (s, @"^#thumbnail\s+(?<filename>[^\s]+)\s+(?<desc>.*)$");
+ if (m.Groups.Count > 0) {
+ string filename = Path.GetFileName (m.Groups ["filename"].Value);
+ string thumbnail = LB.GetThumbnailName (filename);
+ sb.AppendFormat ("<blockquote><p><a href=\"@ENTRY_PATH@{0}\"><img " +
+ "src=\"@ENTRY_PATH@{1}\" title=\"{2}\" " +
+ "alt=\"{2}\" /></a></p></blockquote>\n",
+ filename, thumbnail,
+ m.Groups.Count == 1 ? "" : m.Groups ["desc"].Value);
+ Images = Images ?? new List<string> ();
+ Images.Add (m.Groups ["filename"].Value);
+ continue;
+ }
}
sb.Append (s);
}
@@ -391,15 +408,27 @@ void Render (TextWriter o, IList entries, int idx, string blog_base, bool includ
entry_specific = config.EntrySpecific;
}
+ Hashtable substitutions = new Hashtable ();
+ substitutions.Add ("@ENTRY_ANCHOR@", entry_anchor);
+ substitutions.Add ("@ENTRY_NAVIGATION@", navigation);
+ substitutions.Add ("@ENTRY_SPECIFIC@", entry_specific);
+
+ FillEntrySubstitutions (substitutions, d, blog_base);
+
+ StringWriter body = new StringWriter (new StringBuilder (d.Body.Length));
+ Translate (d.Body, body, substitutions);
+
+ substitutions.Add ("@ENTRY_BODY@", body.ToString ());
+ Translate (entry_template, o, substitutions);
+ }
+
+ void FillEntrySubstitutions (Hashtable substitutions, DayEntry d, string blog_base)
+ {
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_path = LB.GetEntryPath (blog_base, d);
- Hashtable substitutions = new Hashtable ();
substitutions.Add ("@ENTRY_ID@", d.Id);
- substitutions.Add ("@ENTRY_ANCHOR@", entry_anchor);
substitutions.Add ("@ENTRY_PATH@", entry_path);
- substitutions.Add ("@ENTRY_NAVIGATION@", navigation);
substitutions.Add ("@ENTRY_PERMALINK@", d.PermaLink);
substitutions.Add ("@ENTRY_CAPTION@", d.Caption);
substitutions.Add ("@ENTRY_CAPTION_ENC@", HttpUtility.UrlEncode (d.Caption));
@@ -411,7 +440,6 @@ void Render (TextWriter o, IList entries, int idx, string blog_base, bool includ
substitutions.Add ("@ENTRY_CATEGORY_PATHS@", category_paths);
substitutions.Add ("@BLOGWEBDIR@", config.BlogWebDirectory);
substitutions.Add ("@ENTRY_URL_PERMALINK@", Path.Combine (config.BlogWebDirectory, d.PermaLink));
- substitutions.Add ("@ENTRY_SPECIFIC@", entry_specific);
if (d.Comments){
StringWriter rendered_comment = new StringWriter (new StringBuilder (comments.Length));
@@ -420,12 +448,6 @@ void Render (TextWriter o, IList entries, int idx, string blog_base, bool includ
d.RenderedComment = rendered_comment.ToString ();
} else
substitutions.Add ("@COMMENTS@", "");
-
- StringWriter body = new StringWriter (new StringBuilder (d.Body.Length));
- Translate (d.Body, body, substitutions);
-
- substitutions.Add ("@ENTRY_BODY@", body.ToString ());
- Translate (entry_template, o, substitutions);
}
string GetEntryNavigation (IList entries, int idx, string blog_base)
@@ -633,6 +655,32 @@ public void RenderArchive (string template)
parent_dir += Regex.Replace (d.Category, "[^/]+", "..");
RenderHtml (template, Path.Combine (config.Prefix, d.PermaLink),
entries.Count - i - 1, entries.Count - i, parent_dir);
+ if (d.Images == null)
+ continue;
+ foreach (string filename in d.Images) {
+ string file = Path.GetFileName (filename);
+ string thumbnail = Path.Combine (LB.GetEntryPath (config.Prefix + "/", d), LB.GetThumbnailName (file));
+ string thumbnail_target = Path.Combine (
+ LB.GetEntryPath (config.Prefix + "/", d), file);
+
+ file = Path.Combine (config.ImageDirectory, filename);
+ if (!File.Exists (file)) {
+ Console.Error.WriteLine ("lb: Missing file for #thumbnail {0}, ({1}).",
+ filename, file);
+ continue;
+ }
+ if (!File.Exists (thumbnail_target))
+ File.Copy (file, thumbnail_target);
+ if (!File.Exists (thumbnail)) {
+ ProcessStartInfo psi = new ProcessStartInfo (config.ThumbnailCommandFileName);
+ psi.Arguments = string.Format (config.ThumbnailCommandArguments, file, thumbnail);
+ Process p = Process.Start (psi);
+ p.WaitForExit ();
+ if (p.ExitCode != 0)
+ Console.Error.WriteLine ("lb: error running command: {0} {1}",
+ psi.FileName, psi.Arguments);
+ }
+ }
}
foreach (DictionaryEntry de in category_entries) {
@@ -683,9 +731,16 @@ public void RenderRSS (RssVersion version, string output, IList entries, int sta
DayEntry d = (DayEntry) entries [idx];
+ Hashtable substitutions = new Hashtable ();
+ FillEntrySubstitutions (substitutions, d, config.BlogWebDirectory);
+ StringWriter description = new StringWriter (new StringBuilder (d.Body.Length));
+ Translate (d.Body, description, substitutions);
+
+ StringWriter sw = new StringWriter (new StringBuilder (d.Body.Length));
+ Render (sw, entries, idx, "", false, false);
RssItem item = new RssItem ();
item.Author = config.Author;
- item.Description = d.Body + (d.RenderedComment ?? "nothing");
+ item.Description = description.ToString ();
item.Guid = new RssGuid ();
item.Guid.Name = config.BlogWebDirectory + d.PermaLink;
item.Link = new Uri (item.Guid.Name);
@@ -769,6 +824,19 @@ static void Main (string[] args)
b.RenderRSS (Path.Combine (config.Prefix, config.RSSFileName), 0, 30);
b.RenderArchiveRss (RssVersion.RSS20, config.RSSFileName + ".rss2", 30);
- File.Copy ("log-style.css", Path.Combine (config.Prefix, "texts/log-style.css"), true);
+ if (File.Exists ("log-style.css")) {
+ File.Copy ("log-style.css", "texts/log-style.css", true);
+ }
+ }
+
+ public static string GetThumbnailName (string filename)
+ {
+ return Regex.Replace (filename, @"\.(.*?)$", @"-web.$1");
+ }
+
+ public static string GetEntryPath (string blog_base, DayEntry d)
+ {
+ return string.Format ("{0}archive{1}{2:yyyy}/", blog_base,
+ d.Category, d.Date);
}
}

0 comments on commit 6785c45

Please sign in to comment.