Sha256: e88529ac94d696c2ca69231b1db4e7c7d4c4214c40d4d1eee3cfb349211b2fce

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

# Author: Toby DiPasquale <toby@cbcg.net>
require 'fileutils'
require 'rubygems'
require 'sequel'
require 'safe_yaml'

module JekyllImport
  module Typo
    # This SQL *should* work for both MySQL and PostgreSQL.
    SQL = <<-EOS
    SELECT c.id id,
           c.title title,
           c.permalink slug,
           c.body body,
           c.extended extended,
           c.published_at date,
           c.state state,
           COALESCE(tf.name, 'html') filter
      FROM contents c
           LEFT OUTER JOIN text_filters tf
                        ON c.text_filter_id = tf.id
    EOS

    def self.process server, dbname, user, pass, host='localhost'
      FileUtils.mkdir_p '_posts'
      case server.intern
      when :postgres
        db = Sequel.postgres(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8')
      when :mysql
        db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8')
      else
        raise "Unknown database server '#{server}'"
      end
      db[SQL].each do |post|
        next unless post[:state] =~ /published/

        if post[:slug] == nil
          post[:slug] = "no slug"
        end

        if post[:extended]
          post[:body] << "\n<!-- more -->\n"
          post[:body] << post[:extended]
        end

        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.
        name += '.' + post[:filter].split(' ')[0]

        File.open("_posts/#{name}", 'w') do |f|
          f.puts({ 'layout'   => 'post',
                   'title'    => post[:title].to_s,
                   'typo_id'  => post[:id]
                 }.delete_if { |k, v| v.nil? || v == '' }.to_yaml)
          f.puts '---'
          f.puts post[:body].delete("\r")
        end
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-import-0.1.0.beta1 lib/jekyll/jekyll-import/typo.rb