Write your Typo posts in Haml - A TextFilter plugin 6

Posted by jeff Fri, 07 Mar 2008 16:20:00 GMT

Update #1: This plugin is now hosted on rubyforge. Please see the official HamlFilter plugin page and the rubyforge page

Update #2: This plugin is mentioned (and slightly mocked) on the Rails Envy Podcast. I've long enjoyed the Rails Envy Podcasts, so I appreciate the nod and joke.

Haml (HTML Abstraction Markup Language) is awesome. If you don't know just how awesome Haml is, you need to check it out asap. It'll speed up your templating dramatically.

Typo is also awesome. As a typo noob I discovered that sadly the two don't play well with each other when it comes to writing posts. I wanted to type my blog posts in Haml. And now I'm in luck and you are too, because this post is written in 100% Haml.

Introducing the Haml textfilter for Typo

I'm still new to Typo, so the packaging and install is ... well, there isn't any. That will hopefully change soon, but until then, you can infuse your typo blog with Haml-y goodness by doing the following.

  1. sudo gem install haml
  2. Download typo_textfilter_hamlfilter-0.1.tar and extract the folder to /your_typo_install/vendor/plugins
  3. Run the following sql against your typo database
    insert into `text_filters` (`name`, `description`, `markup`, `filters`, `params`) values('hamlfilter','HamlFilter','hamlfilter','--- \r\n- :hamlfilter','--- {}\r\n');
  4. Restart your typo server
  5. Via the typo Administration page, select Settings->Write and then select HamlFilter as your Article Filter. You can now do nifty stuff like
  6. ='Hi!' * 10
    and get
    Hi!Hi!Hi!Hi!Hi!Hi!Hi!Hi!Hi!Hi!
    =Time.now
    and get
    Thu March 07 14:25:55 -0500 2008 
    Haml evaluates your ruby code as expected. Because of this, you can do some neat things, but if you're not careful and allow HamlFilter to control your comments, you're in trouble:
  7. Do not, I repeat, do not choose HamlFilter as your comment markup. If you do so, you're subjecting yourself to all kinds of malicious attacks.

After following these steps, everything should work as expected*. If your post is non-Haml-ized, there's probably a flaw in your spacing somewhere -Haml loves proper whitespace. You may want to write posts in your favorite text editor or something more Haml-friendly. A small price to pay for being free to Haml in Typo.

*One more note: Currently, to accomplish typo magic using things like <typo:code>, you'll need to use Haml's :preserve function like so:


  :preserve

      <typo:code lang="ruby">

      class Person

        attr_accessor :name



        #alias for readability joy

        alias knows? respond_to?



        def initialize(name)

          @name = name

        end

      end

      </typo:code>

gives you
class Person
  attr_accessor :name

  #alias for readability joy
  alias knows? respond_to?

  def initialize(name)
    @name = name
  end
end
This should be done automatically in future versions.

Enjoy and check back for updates as I formalize this thing. I'm hanging out in #typo on freenode trying to figure out how to better implement this, so hopefully I'll have a better solution soon.