#272 Markdown with Redcarpet
Redcarpet is an easy-to-use gem which interprets Markdown. Here I show how to customize it and add syntax highlighting through Pygments and Albino.
- source codeProject Files in Zip (204 KB)
- mp4Full Size H.264 Video (12.4 MB)
- m4vSmaller H.264 Video (7.75 MB)
- webmFull Size VP8 Video (8.32 MB)
- ogvFull Size Theora Video (19.8 MB)
How come you didn't do something like this? Was there any reason?
I think Ryan just wanted to demonstrate a basic helper so he didn't do it the way you described. Of course, yours is much more flexible than Ryan's version.
I prefer not to add these kinds of abstractions until I need them. Usually if I need to customize the options it will be to add or remove one option which will cause a lot of duplication. I think this kind of abstraction would be better, but too much to show in the episode.
Then you pass true/false options hash to modify the defaults. But the point is there are different ways to abstract this out, and we can't tell the best way without seeing how it needs to be customized.
Hi Ryan and all,
how can I get in my textmate to show up new folder icon, I've tried everything I still get the same old icon, I'm using TM 1.5.9
@Ainars Same old icon? Not sure if you're referring to how Ryans drawer looks like, but he's using the TextMate MissingDrawer plugin.
Ryan, thanks for the plug. Create episode as always!
actually I needed to replace FolderRef.tiff, the old one is out of date, for those who use Snow Leopard... and @Ryan all your screencasts are great! :)
Thanks for the screencast! When I use your example or the source code Nokogiri inserts HTML headers to the output of the syntax_highlighter method. I've fixed it by modifying the last line of that method.
Why is it that the output in the screencast is different?
That is very strange, it is not behaving this way for me. Maybe it's a different version of Nokogiri, what version are you using? I am using 1.4.6 in the screencast.
If I use
it will output:
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\">\n<html><body><p>content</p></body></html>\n"
I'm using Nokogiri version 1.4.6, maybe it's something with the libraries Nokogiri uses?
When I give the
/Library/Ruby/Gems/1.8/bin/nokogiri -vcommand I get:
It works fine here, the only difference is I'm using libxml version 2.7.8. Perhaps try updating libxml, I know nokogiri has had issues with some past versions
Last night I've done some testing on an Ubuntu installation with the same results. I've modified the Nokogiri part so it will (hopefully) work on both installations. I've used the DocumentFragment class of Nokogiri.
Thanks for the tip ... I was noticing the same problem ... Every article I displayed on a single page had its own
Nokogiri does that by default when using the ::HTML call. You need to use .fragment
doc = Nokogiri::HTML.fragment(html)
I too can verify this problem exists with the code Ryan. The outputted code looks like the following for me.
Using Nokogiri 1.4.6
I fixed this by changing the sytnax_highlighter method to
Love the 'casts...Thanks, Ryan!
Another great cast. You have an uncanny ability to cover topics as I need them. I got RedCloth going this morning, but upon seeing this cast I will be switching ASAP. Thanks Ryan!
Thanks for your screen.
A Markdown TextEditor with markitup and redcarpet ,support rails 3
I am getting the following error in my log when i try and use
ActionView::TemplateError (No such file or directory - posix_spawnp)
Any ideas why? or how to fix?
I'm getting the same error, in production. Did you figure it out?
Is it possible to use pygments on Heroku?
@Paolo, Heroku can't use pygments.
It seems there are workarounds: http://pygments.heroku.com/
@Paolo Well, this is very slow, and sometimes hanging for 10 sec. Use pygmentize gem instead
@Alex, yes it can via pygmentize gem
at least for Cedar stack...
Great screencast. Thanks Ryan.
Again, Just awesome, and Just in time.???
Is there a ruby alternative to pigments? Seems strange to use a python library.
I cant install redcarpet, im using windows :(! "Error: Failed to build gem native extensions." Someone could?
To do this with heroku, change syntax_highlighter to the following:
If you are having problems with the Albino gem returning nothing, make sure you are running Pygmentize under Python2.
I was encountering this issue from the command line:
In Arch Linux for example the default Python is Python3.2, installing Pygmentize under Python2.7 solved this for me.
Anyone knows why indentation is not correct, or which part is responsible for that, Albino or pygmentize?
it looks like this
when I put this without parsing it with Nokogiri/Albino/Pyg, then it appears correct (using markdown only)
I do have the same problem, using Markdown also.
Would be happy to found a solution :/
Hey, if anyone if having the same problem, you need to use preserve before sending the data.
Thank you for the link, I was encountering this issue too.
when I try
but not get
Thanks for the podcast.
I'm having the same problem with any form submited data, works fine if i manually enter it into my database
so i got mine to work simple quirk just needs a space newline between the code block and preview text
@ Everyone trying to to this on Heroku I know I am late but I set up a new appspot app that uses the new version of Pygments and wrote a little blog post about how to use it at http://iamaust.in/posts/3.
I hope this helps people, now you can colorize things like coffeescript and sass for your rails 3.1 apps
I would like to know if there is a way to show the line numbers for the code blocks generated through Albino.
@nathan le Ray you can pull from my github a modified version of albino that allows you to pass options to pygments. https://github.com/austinbv/albino. To use this version of albion in your gemfile just add
@Austin Thank you, I'll try that.
I started with the pre-release version of the new redcarpet gem and then went about modifying the helper you outlined in your screencast. Based on the following from the wiki for the gem
I tried to change your helper. This is the basics of what i tried. I clearly don't know enough to see what must be obvious to experienced programmers.
I have a policy model, so in my show view I tried something like.
Needless to say, this isn't working correctly for me. Thoughts, help?
I just ran into the same problem, using Redcarpet 2 on a Rails 3.1.3 app. Took me a little bit to figure out what was going on, but I updated the helper method to be:
So, in your view you would have:
I've instantiate a Redcarpet object and call its 'render" method both inside the 'markdown' helper method. That value will be return to the view, but since it will be escaped you'll need to pass it through the 'raw' method so that it comes out as actual html.
Here's my solution for upgrading to Redcarpet 2
markdown_unsafefor publicly created comments, hence, it filters out as much as possible; still doing
Albino.safe_colorize()as I haven't upgraded it yet.
I just refactored my helper as the previous code was a 'quick and dirty' test of my approach.
If no valid renderer is specified (as the first method argument) it will assume unsafe content and do the utmost to strip and filter HTML etc.
Final and further updates can be found here.
You are the bomb.
That solved my issues with Redcarpet.
Hi there, is it possible to embed a video (e.g. youtube video) using redcarpet?
I've just come up against this issue too. Does anyone have any ideas?
as an easy solution try out the gem dejan/auto_html
I've implemented this months ago without any pain. But today when I moved my application to a newer debian-server, it told me "easy_install not found.
If you face the same trouble use:
Here's how I did it for a blog project using Rails 3.2 and Redcarpet 2.1 (minus the syntax highlighting). Some of the configuration options have been moved into the HTML renderer, while the more generic ones are still at the markdown layer:
Albino has been deprecated in favor of pygments.rb. This code combines both redcarpet 2.1 and pygments.rb
Great update for redcloth 2.0 and pygments. One quick comment though. I needed to add ".html_safe" after the "render(text)" method call. Not a biggie. Thanks!
The github repository for redcarpet has gone away, does anyone know what's happening with it?
The repo has moved here: https://github.com/vmg/redcarpet
Given the changes with Redcarpet, I was not able to get the steps in this Railscast to work. But, I used it to help me understand what I was looking at in figuring out what to do next. I got Redcarpet working with my Rails 4, RC1 blog. If you'd like steps, you can peek at it here:
How to syntax highlight the code? I see class='ruby' in the view code. Where is the CSS?
Hey all I had a question about this, I'm using 3.2 and was wondering how would I implement live preview using the example above?? i have it working where when I enter the new information it reads the markdown
then i enter
in the bottom of the view.... its listening and showing the words but no markdown syntax is applied... how would I do this?
Update for anyone using RedCarpet 3.0.0
Using this RailsCast, just change the helper method to this:
Then in view just use
Notice they changed to sets of hashes for options. One hash of options for the Redcarpet::Render::HTML.new(options hash here)
and the second options hash for the Redcarpet::Markdown.new(renderer(this is where the renderer goes), markdown hash here). To understand, here are both without removing options into two variables.
Check their github site to find out which options belongs in which options hash.
Thank you so much SimplizIT. That was very helpful. Please keep it up.