ruby-on-railsrubyhamlmarkdownbluecloth

Haml Markdown Filter Breaking on Example Ruby Code in Rails


I'm using Haml in rails and have been writting with the :markdown filter (bluecloth gem), but a piece of example code is in ruby and the page tries to execute the #{values}, how can I stop this?

Here is the breaking bit of code:

:markdown
  like_frags = [fields].flatten.map { |f| "LOWER(#{f}) LIKE :word#{count}" }
    or_frags << "(#{like_frags.join(" OR ")})"
    binds["word#{count}".to_sym] = "%#{word.to_s.downcase}%"
    count += 1
  end

The error that is returned:

undefined local variable or method `f'

I have found a solution myself but it's far from ideal:

- f=nil;count=nil;like_frags=[];word=nil;

I place this before the :markdown filter begins.

I know it can be done because stackoverflow didn't break when I wrote that, so how can I achieve this too?

Thanks in advance!


Solution

  • Per the Haml documentation, interpolation within filters is normal and expected. To circumvent it, you can just escape the #{} interpolation syntax by prepending a backslash:

    :markdown
      like_frags = [fields].flatten.map { |f| "LOWER(\#{f}) LIKE :word\#{count}" }
        or_frags << "(\#{like_frags.join(" OR ")})"
        binds["word\#{count}".to_sym] = "%\#{word.to_s.downcase}%"
        count += 1
      end