Please save me from myself and Haml (Use haml to render HTML code examples)

Once you write markup in haml, you'll do anything to avoid writing straight HTML ever again. This is an extreme example.

I needed to make a page that shows my tech-savvy users some example HTML and CSS.

So of course I wanted to use haml to render HTML that would then be escaped and whitespace preserved in a <pre> element.

First I read the reference and haml helper docs for ideas. All of these looked promising:

  • &= and ~
  • Combinations of the :plan, :escaped, and :preserve filters. They didn't seem to play together.
  • The #capture_haml, #preserve, and #escape_once haml helpers

.. but I couldn't get them to work in concert.  I unsuccessfully googled the following phrases (and many more) looking for help: haml render html in pre or code, haml plain html filter preserve white space, haml generate escaped html.

    The right solution would be to contribute a new filter for haml.  But here is my solution for now:  I read the haml source and found the following to work. I fear it's pretty ugly.

    The output, as rendered in a browser:

    As you can see, it works for Sass as well. Note that %[text across lines] is short hand for the %q{} ruby literals, which is a nice way to code readable multiline strings.  You need to be careful to left align that column of haml or sass (lines 4 and 11) with the preceeding variable definition to achieve proper indentation when parsed.

    If you know a cleaner way to achieve this, I'd love to learn from you. I'm a relative newcomer to software development.

    iMob hack for a constant stream of new mobbers

    I got semi-addicted to iMob.  It's a very fun MORPG for iPhone.  

    It's also rife with embarrassing software defects, the server experiences frequent downtime, and it's heavily cheated by jailbroken players editing client-side stats in .plist files.  What's worse: the devs are off creating their next app "Vampires" instead of fixing it or adding server validations to inhibit cheating.  And they solicit feedback but everyone complains they never listen. I agree. </bitch>

    One thing that helps you win fights is having people "join your mob".  Lovely UI - you have to enter a 9 digit code for each person you add, so there's tons of code posting sites like imobcodes.com to facilitate code sharing.  You can also try adding people physically near you via geolocation, but this never works for most people.

    I got tired of this.  Here's my simple solution, which I'm sharing because I have too many mobbers now. On *nix or mac, load a shell. Run crontab -e, and add this line, replacing YOURCODE with your 9 digit invite code.

    0 1-24/2 * * * curl -d code=YOURCODE http://imobcodes.com/add.php 1> /dev/null

    (PS: crontab -e usually opens the crontab file in vi[m].  You'll need to press 'i' to insert, paste the line, then <esc>:wq to write and exit)

    There. ~10 new mobbers every 2 hours, and THEY (imobcodes.com visitors) do the annoying job of typing in your code for you.