Posted:

Blogofile hacks

The Blogofile documentation is reasonably good, so getting it setup and figuring out how the controllers and filters work was pretty straight-forward. Googling around to see what others did to setup and customize Blogofile, I found some fixes that looked useful. Especially, zzzeek's improved slug generation.

I tweaked it some more and ended up with a diff that looks like this:

@@ -61,6 +61,31 @@
     "filename"   :"Reserved internally"
     }

+def fmtSlug_original(source):
+    return re.sub("[ ?]", "-", source).lower()
+
+# From http://techspot.zzzeek.org/2010/12/06/my-blogofile-hacks/
+def fmtSlug(source):
+    """
+    Generate a 'slug' from a source string.  A slug is a slightly cleaned up
+    string suitable for use in a URL or filename (e.g., from a post title).
+    """
+    slug = source.lower()
+
+    # convert ellipses to a space
+    slug = re.sub(r'\.{2,}', ' ', slug)
+
+    # flatten everything ugly into a single -
+    slug = re.sub(r'[^-0-9a-zA-Z\._]+', '-', slug)
+
+    # trim off leading/trailing -
+    slug = re.sub(r'^-+|-+$', '', slug)
+
+    # trim off leading/trailing spaces
+    slug = re.sub(r'^ +| +$', '', slug)
+
+    return slug
+

 class PostParseException(Exception):

@@ -166,7 +191,7 @@
                     datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))

         if not self.slug:
-            self.slug = re.sub("[ ?]", "-", self.title).lower()
+            self.slug = fmtSlug(self.title)

         if not self.date:
             self.date = datetime.datetime.now(pytz.timezone(self.__timezone))
@@ -190,7 +215,6 @@
             self.permalink = \
                     re.sub(":title", self.slug, self.permalink)

-            # TODO: slugification should be abstracted out somewhere reusable
             self.permalink = re.sub(
                     ":filename", re.sub(
                             "[ ?]", "-", self.filename).lower(), self.permalink)
@@ -282,9 +306,8 @@

     def __init__(self, name):
         self.name = unicode(name)
-        # TODO: slugification should be abstracted out somewhere reusable
         # TODO: consider making url_name and path read-only properties?
-        self.url_name = self.name.lower().replace(" ", "-")
+        self.url_name = fmtSlug(self.name)
         self.path = bf.util.site_path_helper(
                 bf.config.controllers.blog.path,
                 bf.config.controllers.blog.category_dir,

Comments or Questions?

Subscribe via RSS | Atom Feed