Module: Jekyll::Drupal

Defined in:
lib/jekyll/migrators/drupal.rb

Constant Summary collapse

QUERY =

Reads a MySQL database via Sequel and creates a post file for each post in wp_posts that has post_status = ‘publish’. This restriction is made because ‘draft’ posts are not guaranteed to have valid dates.

"SELECT n.nid, \
       n.title, \
       nr.body, \
       n.created, \
       n.status \
FROM node AS n, \
     node_revisions AS nr \
WHERE (n.type = 'blog' OR n.type = 'story') \
AND n.vid = nr.vid"

Class Method Summary collapse

Class Method Details

.process(dbname, user, pass, host = 'localhost', prefix = '') ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jekyll/migrators/drupal.rb', line 27

def self.process(dbname, user, pass, host = 'localhost', prefix = '')
  db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8')

  if prefix != ''
    QUERY[" node "] = " " + prefix + "node "
    QUERY[" node_revisions "] = " " + prefix + "node_revisions "
  end

  FileUtils.mkdir_p "_posts"
  FileUtils.mkdir_p "_drafts"

  # Create the refresh layout
  # Change the refresh url if you customized your permalink config
  File.open("_layouts/refresh.html", "w") do |f|
    f.puts <<EOF
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url={{ page.refresh_to_post_id }}.html" />
</head>
</html>
EOF
  end

  db[QUERY].each do |post|
    # Get required fields and construct Jekyll compatible name
    node_id = post[:nid]
    title = post[:title]
    content = post[:body]
    created = post[:created]
    time = Time.at(created)
    is_published = post[:status] == 1
    dir = is_published ? "_posts" : "_drafts"
    slug = title.strip.downcase.gsub(/(&|&amp;)/, ' and ').gsub(/[\s\.\/\\]/, '-').gsub(/[^\w-]/, '').gsub(/[-_]{2,}/, '-').gsub(/^[-_]/, '').gsub(/[-_]$/, '')
    name = time.strftime("%Y-%m-%d-") + slug + '.md'

    # Get the relevant fields as a hash, delete empty fields and convert
    # to YAML for the header
    data = {
       'layout' => 'post',
       'title' => title.to_s,
       'created' => created,
     }.delete_if { |k,v| v.nil? || v == ''}.to_yaml

    # Write out the data and content to file
    File.open("#{dir}/#{name}", "w") do |f|
      f.puts data
      f.puts "---"
      f.puts content
    end

    # Make a file to redirect from the old Drupal URL
    if is_published
      aliases = db["SELECT dst FROM #{prefix}url_alias WHERE src = ?", "node/#{node_id}"].all

      aliases.push(:dst => "node/#{node_id}")

      aliases.each do |url_alias|
        FileUtils.mkdir_p url_alias[:dst]
        File.open("#{url_alias[:dst]}/index.md", "w") do |f|
          f.puts "---"
          f.puts "layout: refresh"
          f.puts "refresh_to_post_id: /#{time.strftime("%Y/%m/%d/") + slug}"
          f.puts "---"
        end
      end
    end
  end

  # TODO: Make dirs & files for nodes of type 'page'
    # Make refresh pages for these as well

  # TODO: Make refresh dirs & files according to entries in url_alias table
end