Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax highlighting in HAML's markdown filter

I'm using HAML's markdown filter, like this:

:markdown
  This is markdown text, yay!

but I want to do syntax highlighting for code inside that text, something like:

:markdown
  This is markdown text, yay!
  <code lang="ruby">
    def hello(world)
      puts "Hello #{world}"
    end
  </code>

Any ideas how to do it? I know how to use CodeRay, but I don't see how to grab ahold of that text.

like image 655
pupeno Avatar asked Feb 22 '26 05:02

pupeno


1 Answers

Markdown does support code blocks out of the box, but does not support syntax highlighting.

see http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#markdown-filter for what markdown libraries haml looks for to parse your markdown.

If you want to pass your code to a filter that makes it highlighted you can write your own filter extension to haml. It is actually quite easy.

http://haml-lang.com/docs/yardoc/Haml/Filters/Base.html

If I were you though, I would just put the code in as a block and use a javascript library like GeSHi to highlight the code. http://qbnz.com/highlighter/index.php

So you can do something like:

  %pre.ruby 
    puts "this is now syntax highlighted"

And if you have geshi included, it now highlighted.

like image 122
Michael Papile Avatar answered Feb 23 '26 18:02

Michael Papile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!