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.
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.
ruby
defsyntax_highlighter(html)
doc = Nokogiri::HTML(html)
doc.search("//pre[@lang]").each do |pre|
pre.replace Albino.colorize(pre.text.rstrip, pre[:lang])
end
doc.at_css("body").inner_html.to_s
end
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 rails c with Nokogiri::HTML("<p>content</p>").to_s
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 -v command 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.
ruby
defsyntax_highlighter(html)
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css("pre[@lang]").each do |pre|
pre.replace Albino.colorize(pre.text.rstrip, pre[:lang])
end
doc.to_s
end
I too can verify this problem exists with the code Ryan. The outputted code looks like the following for me.
html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"><html><body><p>This is a <strong>markdown</strong> enabled text</p><divclass="highlight"><pre><spanclass="nb">puts</span><spanclass="s2">"Hello World"</span></pre></div></body></html>
I fixed this by changing the sytnax_highlighter method to
ruby
defsyntax_highlight(html)
doc = Nokogiri::HTML(html)
doc.search("//pre[@lang]").each do |pre|
pre.replace Albino.colorize(pre.text.rstrip, pre[:lang])
end
doc.css('body').inner_html.to_s
end
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!
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:
pygmentize -l ruby -f html -O encoding=utf-8 test.rb
*** Error while highlighting:
TypeError: must be str, not bytes
(file "/usr/lib/python3.2/codecs.py", line 356, in write)
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
ruby
defsyntax_highlighter(html)
doc = Nokogiri::HTML(html)
doc.search("//pre[@lang]").each do |pre|
pre.replace Albino.colorize(pre.text.rstrip, pre[:lang])
end
doc.css('body').inner_html.to_s
end
instead of
ruby
defsyntax_highlighter(html)
doc = Nokogiri::HTML::DocumentFragment.parse(html)
doc.css("pre[@lang]").each do |pre|
pre.replace Albino.colorize(pre.text.rstrip, pre[:lang])
end
doc.to_s
end
when I put this without parsing it with Nokogiri/Albino/Pyg, then it appears correct (using markdown only)
@ 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
@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
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
Rendering with the Markdown object is done through Markdown#render. Unlike in the RedCloth API, the text to render is passed as an argument and not stored inside the Markdown instance, to encourage reusability.
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 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:
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.
I'm calling markdown_unsafe for publicly created comments, hence, it filters out as much as possible; still doing Albino.colorize() instead of Albino.safe_colorize() as I haven't upgraded it yet.
include AutoHtmldefmarkdown(text)
text = auto_html(text) do
youtube ( width:400, height:250 )
vimeo ( width:400, height:250 )
end
extensions = [tables:true, autolink:true, strikethrough:true]
Redcarpet::Markdown.new(Redcarpet::Render::XHTML, *extensions).render(text).html_safe
end
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:
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!
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:
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
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.
ruby
defmarkdown(blogtext)
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new({hard_wrap:true, filter_html:true}), {autolink:true, no_intra_emphasis:true})
markdown.render(blogtext).htm_safe
end
Check their github site to find out which options belongs in which options hash.
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! :)
Hello Ryan,
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
rails c
withNokogiri::HTML("<p>content</p>").to_s
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 -v
command 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
wrapper ...
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
https://github.com/ZhangHanDong/rails_markitup
I am getting the following error in my log when i try and use
ruby
block...ActionView::TemplateError (No such file or directory - posix_spawnp)
Any ideas why? or how to fix?
:D
I'm getting the same error, in production. Did you figure it out?
Thanks Ryan.
Is it possible to use pygments on Heroku?
@Paolo, Heroku can't use pygments.
It seems there are workarounds: http://pygments.heroku.com/
Also this:
http://github.com/trevorturk/pygments
@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
instead of
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.
[source]
http://stackoverflow.com/questions/6325416/maruku-incorrectly-parsing-second-line-of-code-blocks
Thank you for the link, I was encountering this issue too.
Hi,
when I try
I get
but not get
<pre lang="ruby">
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
Hello,
I would like to know if there is a way to show the line numbers for the code blocks generated through Albino.
Thanks.
@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.
markdown.render(@policies.introduction)
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:
<%= raw markdown(@policies.introduction) %>
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
I'm calling
markdown_unsafe
for publicly created comments, hence, it filters out as much as possible; still doingAlbino.colorize()
instead ofAlbino.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
Thanks.
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?
https://github.com/tanoku/redcarpet
The repo has moved here: https://github.com/vmg/redcarpet
nice article
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:
http://www.hamcois.com/articles/4
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
<%= markdown(@blog.body) %>
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.