Module: Jekyll::Tumblr

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

Class Method Summary collapse

Class Method Details

.process(url, format = "html", grab_images = false, add_highlights = false, rewrite_urls = true) ⇒ Object



12
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
# File 'lib/jekyll/migrators/tumblr.rb', line 12

def self.process(url, format = "html", grab_images = false,
                 add_highlights = false, rewrite_urls = true)
  @grab_images = grab_images
  FileUtils.mkdir_p "_posts/tumblr"
  url += "/api/read/json/"
  per_page = 50
  posts = []
  # Two passes are required so that we can rewrite URLs.
  # First pass builds up an array of each post as a hash.
  begin
    current_page = (current_page || -1) + 1
    feed = open(url + "?num=#{per_page}&start=#{current_page * per_page}")
    json = feed.readlines.join("\n")[21...-2]  # Strip Tumblr's JSONP chars.
    blog = JSON.parse(json)
    puts "Page: #{current_page + 1} - Posts: #{blog["posts"].size}"
    posts += blog["posts"].map { |post| post_to_hash(post, format) }
  end until blog["posts"].size < per_page
  # Rewrite URLs and create redirects.
  posts = rewrite_urls_and_redirects posts if rewrite_urls
  # Second pass for writing post files.
  posts.each do |post|
    if format == "md"
      post[:content] = html_to_markdown post[:content]
      post[:content] = add_syntax_highlights post[:content] if add_highlights
    end
    File.open("_posts/tumblr/#{post[:name]}", "w") do |f|
      f.puts post[:header].to_yaml + "---\n" + post[:content]
    end
  end
end