Module: Jekyll::WordpressDotCom

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

Overview

This importer takes a wordpress.xml file, which can be exported from your wordpress.com blog (/wp-admin/export.php).

Class Method Summary collapse

Class Method Details

.process(filename = "wordpress.xml") ⇒ Object



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/jekyll/migrators/wordpressdotcom.rb', line 13

def self.process(filename = "wordpress.xml")
  import_count = Hash.new(0)
  doc = Hpricot::XML(File.read(filename))

  (doc/:channel/:item).each do |item|
    title = item.at(:title).inner_text.strip
    permalink_title = item.at('wp:post_name').inner_text
    # Fallback to "prettified" title if post_name is empty (can happen)
    if permalink_title == ""
      permalink_title = title.downcase.split.join('-')
    end

    date = Time.parse(item.at('wp:post_date').inner_text)
    status = item.at('wp:status').inner_text

    if status == "publish" 
      published = true
    else
      published = false
    end

    type = item.at('wp:post_type').inner_text
    tags = (item/:category).map{|c| c.inner_text}.reject{|c| c == 'Uncategorized'}.uniq

    metas = Hash.new
    item.search("wp:postmeta").each do |meta|
      key = meta.at('wp:meta_key').inner_text
      value = meta.at('wp:meta_value').inner_text
      metas[key] = value;
    end

    name = "#{date.strftime('%Y-%m-%d')}-#{permalink_title}.html"
    header = {
      'layout' => type,
      'title'  => title,
      'tags'   => tags,
      'status'   => status,
      'type'   => type,
      'published' => published,
      'meta'   => metas
    }

    FileUtils.mkdir_p "_#{type}s"
    File.open("_#{type}s/#{name}", "w") do |f|
      f.puts header.to_yaml
      f.puts '---'
      f.puts item.at('content:encoded').inner_text
    end

    import_count[type] += 1
  end

  import_count.each do |key, value|
    puts "Imported #{value} #{key}s"
  end
end