Today I had to add technical documentation pages to a Rails app. I chose to write those in Markdown as that’s what I’m used to and it’s a widely-used format. This post describes how to render Markdown views in a Rails application with Pygments for code syntax highlighting.
The markdown-rails gem enables
.html.md views. Redcarpet is a better parser which you’ll want to use to have features like GitHub Flavored Markdown. Finally, pyments.rb will handle the syntax highlighting.
Add those gems to your
Gemfile and run
1 2 3
To configure the parser and enable syntax highlighting, you can add this initializer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Note that you must have Pygments’
pygmentize command installed and in your path. You may install it on most platforms with
Well, that was pretty painless.