Class: JekyllImport::Importers::WordpressDotCom

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

Defined Under Namespace

Classes: Item

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



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

def self.download_images(title, post_hpricot, assets_folder)
  images = (post_hpricot / "img")
  return if images.empty?

  Jekyll.logger.info "Downloading images for ", title
  images.each do |i|
    uri = i["src"]

    i["src"] = format("{{ site.baseurl }}/%s/%s", assets_folder, File.basename(uri))
    dst = File.join(assets_folder, File.basename(uri))
    Jekyll.logger.info uri
    if File.exist?(dst)
      Jekyll.logger.info "Already in cache. Clean assets folder if you want a redownload."
      next
    end
    begin
      OpenURI.open_uri(uri, :allow_redirections => :safe) do |f|
        File.open(dst, "wb") do |out|
          out.puts f.read
        end
      end
      Jekyll.logger.info "OK!"
    rescue StandardError => e
      Jekyll.logger.error "Error: #{e.message}"
      Jekyll.logger.error e.backtrace.join("\n")
    end
  end
end

.process(options) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 139

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

    metas = {}
    node.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.text_for("dc:creator").strip

    header = {
      "layout"     => item.post_type,
      "title"      => item.title,
      "date"       => item.published_at,
      "type"       => item.post_type,
      "parent_id"  => item.parent_id,
      "published"  => item.published?,
      "password"   => item.post_password,
      "status"     => item.status,
      "categories" => categories,
      "tags"       => tags,
      "meta"       => metas,
      "author"     => authors[],
      "permalink"  => item.permalink,
    }

    begin
      content = Hpricot(item.text_for("content:encoded"))
      header["excerpt"] = item.excerpt if item.excerpt

      download_images(item.title, content, assets_folder) if fetch

      FileUtils.mkdir_p item.directory_name
      File.open(File.join(item.directory_name, item.file_name), "w") do |f|
        f.puts header.to_yaml
        f.puts "---"
        f.puts Util.wpautop(content.to_html)
      end
    rescue StandardError => e
      Jekyll.logger.error "Couldn't import post!"
      Jekyll.logger.error "Title: #{item.title}"
      Jekyll.logger.error "Name/Slug: #{item.file_name}\n"
      Jekyll.logger.error "Error: #{e.message}"
      next
    end

    import_count[item.post_type] += 1
  end

  import_count.each do |key, value|
    Jekyll.logger.info "Imported #{value} #{key}s"
  end
end

.require_depsObject



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

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

.sluggify(title) ⇒ Object



218
219
220
# File 'lib/jekyll-import/importers/wordpressdotcom.rb', line 218

def self.sluggify(title)
  title.gsub(%r![^[:alnum:]]+!, "-").downcase
end

.specify_options(c) ⇒ Object



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

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