Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Tom Pesman's Profile

GitHub User: tompesman

Site: http://tnux.net

Comments by Tom Pesman

Avatar

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
def syntax_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 
Avatar

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:

warnings: []
ruby:
engine: mri
version: 1.8.7
description: ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]
platform: universal-darwin10.0
libxml:
loaded: 2.7.3
binding: extension
compiled: 2.7.3
nokogiri: 1.4.6

Avatar

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.

ruby
def syntax_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?