public
Created

  • Download Gist
migrate_content_to_webby.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
# Author: Stephane Busso based Toby DiPasquale <toby@cbcg.net> Jekyll import and other disqus import
require 'fileutils'
require 'rubygems'
require 'sequel'
require 'rake'
 
namespace :import
desc "import posts"
task :post do
# this SQL *should* work for both MySQL and PostgreSQL, but I haven't
db = Sequel.postgres dbname, :user => user, :password => pass, :host => host
SQL = <<-EOS
SELECT c.id as id,
c.title as title,
c.permalink as slug,
c.body as body,
c.published_at as date,
c.state as state,
c.keywords as keywords,
COALESCE(tf.name, 'html') as filter
FROM contents c
LEFT OUTER JOIN text_filters tf
ON c.text_filter_id = tf.id
EOS
db[SQL].each do |post|
next unless post[:state] =~ /Published/
dir = post[:date].strftime('content/blog/%Y/%m/%d/')
FileUtils.mkdir_p dir
 
name = [ sprintf("%.04d", post[:date].year),
sprintf("%.02d", post[:date].month),
sprintf("%.02d", post[:date].day),
post[:slug].strip ].join('-')
# Can have more than one text filter in this field, but we just want
# the first one for this
File.open(dir + name + '.textile', 'w') do |f|
f.puts({
'title' => post[:title].to_s,
'blog_post' => true,
'created_at' => post[:date],
'filter' => ['textile'],
'tags' => post[:keywords].gsub(",","").split(" ").compact
}.delete_if { |k, v| v.nil? || v == '' }.to_yaml)
f.puts '---'
f.puts post[:body].gsub("<typo:code>","<pre>").gsub("</typo:code>", "</pre>").delete("\r")
end
end
end # import:post
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.