Class: JekyllImport::Importers::Enki

Inherits:
JekyllImport::Importer show all
Defined in:
lib/jekyll-import/importers/enki.rb

Constant Summary collapse

SQL =
<<-EOS
        SELECT p.id,
               p.title,
               p.slug,
               p.body,
               p.published_at as date,
               p.cached_tag_list as tags
        FROM posts p
EOS

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.process(options) ⇒ Object

Just working with postgres, but can be easily adapted to work with both mysql and postgres.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/jekyll-import/importers/enki.rb', line 44

def self.process(options)
  dbname = options.fetch('dbname')
  user   = options.fetch('user')
  pass   = options.fetch('password', "")
  host   = options.fetch('host', "localhost")

  FileUtils.mkdir_p('_posts')
  db = Sequel.postgres(:database => dbname,
                       :user => user,
                       :password => pass,
                       :host => host,
                       :encoding => 'utf8')

  db[SQL].each do |post|
    name = [ sprintf("%.04d", post[:date].year),
             sprintf("%.02d", post[:date].month),
             sprintf("%.02d", post[:date].day),
             post[:slug].strip ].join('-')
    name += '.textile'

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

.require_depsObject



32
33
34
35
36
37
38
39
40
# File 'lib/jekyll-import/importers/enki.rb', line 32

def self.require_deps
  JekyllImport.require_with_fallback(%w[
    rubygems
    sequel
    fileutils
    pg
    yaml
  ])
end

.specify_options(c) ⇒ Object



25
26
27
28
29
30
# File 'lib/jekyll-import/importers/enki.rb', line 25

def self.specify_options(c)
  c.option 'dbname', '--dbname', 'Database name'
  c.option 'user', '--user', 'Database name'
  c.option 'password', '--password', 'Database name (default: "")'
  c.option 'host', '--host', 'Database name'
end

.validate(options) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/jekyll-import/importers/enki.rb', line 17

def self.validate(options)
  %w[dbname user].each do |option|
    if options[option].nil?
      abort "Missing mandatory option --#{option}."
    end
  end
end