Created
January 27, 2009 04:15
-
-
Save reagent/53174 to your computer and use it in GitHub Desktop.
Convert old-style blurt content to new
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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