Skip to content

Instantly share code, notes, and snippets.

@reagent
Created January 27, 2009 04:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reagent/53174 to your computer and use it in GitHub Desktop.
Save reagent/53174 to your computer and use it in GitHub Desktop.
Convert old-style blurt content to new
namespace :content do
desc "Convert old-style code blocks to new format"
task :convert_code => :environment do
require 'hpricot'
Post.all.each do |post|
doc = Hpricot(post.body)
(doc/'code').each do |node|
header = node['lang'] ? " #lang:#{node['lang']}\n" : ''
lines = node.inner_html.sub(/^\n+/, '').split("\n")
matches = lines.first.match(/^( +)/)
indent = matches ? (' ' * matches[1].length) : ''
block = lines.map do |line|
line.sub(/^#{indent}/, ' ')
end.join("\n")
node.swap "#{header}#{block}"
end
post.body = doc.to_s
post.send(:update_without_timestamps)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment