Skip to content

Instantly share code, notes, and snippets.

@sbusso
Created March 29, 2010 18:42
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 sbusso/348230 to your computer and use it in GitHub Desktop.
Save sbusso/348230 to your computer and use it in GitHub Desktop.
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment