Class: JekyllImport::Importers::WordpressDotCom

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

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.download_images(title, post_hpricot, assets_folder) ⇒ Object

Will modify post DOM tree



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
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 27

def self.download_images(title, post_hpricot, assets_folder)
  images = (post_hpricot/"img")
  if images.length == 0
    return
  end
  puts "Downloading images for " + title
  images.each do |i|
    uri = i["src"]

    i["src"] = assets_folder + "/" + File.basename(uri)
    dst = File.join(assets_folder, File.basename(uri))
    puts "  " + uri
    if File.exist?(dst)
      puts "    Already in cache. Clean assets folder if you want a redownload."
      next
    end
    begin
      open(uri) {|f|
        File.open(dst, "wb") do |out|
          out.puts f.read
        end
      }
      puts "    OK!"
    rescue => e
      puts "    Errorr: #{e.message}"
    end
  end
end

.process(options) ⇒ Object



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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 56

def self.process(options)
  source        = options.fetch('source', "wordpress.xml")
  fetch         = !options.fetch('no_fetch_images', false)
  assets_folder = options.fetch('assets_folder', 'assets')
  FileUtils.mkdir_p(assets_folder)

  import_count = Hash.new(0)
  doc = Hpricot::XML(File.read(source))
  # Fetch authors data from header
  authors = Hash[
    (doc/:channel/'wp:author').map do |author|
    [author.at("wp:author_login").inner_text.strip, {
      "login" => author.at("wp:author_login").inner_text.strip,
      "email" => author.at("wp:author_email").inner_text,
      "display_name" => author.at("wp:author_display_name").inner_text,
      "first_name" => author.at("wp:author_first_name").inner_text,
      "last_name" => author.at("wp:author_last_name").inner_text
    }]
    end
  ] rescue {}

  (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 = sluggify(title)
    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
    categories = item.search('category[@domain="category"]').map{|c| c.inner_text}.reject{|c| c == 'Uncategorized'}.uniq
    tags = item.search('category[@domain="post_tag"]').map{|t| t.inner_text}.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

     = item.at('dc:creator').inner_text.strip

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

    begin
      content = Hpricot(item.at('content:encoded').inner_text)

      if fetch
        download_images(title, content, assets_folder)
      end

      FileUtils.mkdir_p "_#{type}s"
      File.open("_#{type}s/#{name}", "w") do |f|
        f.puts header.to_yaml
        f.puts '---'
        f.puts Util.wpautop(content.to_html)
      end
    rescue => e
      puts "Couldn't import post!"
      puts "Title: #{title}"
      puts "Name/Slug: #{name}\n"
      puts "Error: #{e.message}"
      next
    end

    import_count[type] += 1
  end

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

.require_depsObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 8

def self.require_deps
  JekyllImport.require_with_fallback(%w[
    rubygems
    sequel
    fileutils
    safe_yaml
    hpricot
    time
    open-uri
  ])
end

.sluggify(title) ⇒ Object



150
151
152
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 150

def self.sluggify(title)
  title.gsub(/[^[:alnum:]]+/, '-').downcase
end

.specify_options(c) ⇒ Object



20
21
22
23
24
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 20

def self.specify_options(c)
  c.option 'source', '--source FILE', 'WordPress export XML file (default: "wordpress.xml")'
  c.option 'no_fetch_images', '--no-fetch-images', 'Do not fetch the images referenced in the posts'
  c.option 'assets_folder', '--assets_folder FOLDER', 'Folder where assets such as images will be downloaded to (default: assets)'
end