Class: JekyllImport::Importers::Tumblr

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

Class Method Summary collapse

Methods inherited from JekyllImport::Importer

inherited, run, stringify_keys, subclasses

Class Method Details

.add_syntax_highlights(content, redirect_dir) ⇒ Object

Adds pygments highlight tags to code blocks in posts that use markdown format. This doesn’t guess the language of the code block, so you should modify this to suit your own content. For example, my code block only contain Python and JavaScript, so I can assume the block is JavaScript if it contains a semi-colon.



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/jekyll-import/importers/tumblr.rb', line 257

def add_syntax_highlights(content, redirect_dir)
  lines  = content.split("\n")
  block  = false
  indent = %r!^    !
  lang   = nil
  start  = nil
  lines.each_with_index do |line, i|
    if !block && line =~ indent
      block = true
      lang  = "python"
      start = i
    elsif block
      lang  = "javascript" if %r!;$!.match?(line)
      block = line =~ indent && i < lines.size - 1 # Also handle EOF
      unless block
        lines[start] = "{% highlight #{lang} %}"
        lines[i - 1] = "{% endhighlight %}"
      end
      FileUtils.cp(redirect_dir + "index.html", redirect_dir + "../" + "index.html")
      lines[i] = lines[i].sub(indent, "")
    end
  end
  lines.join("\n")
end

.extract_json(contents) ⇒ Object



71
72
73
74
75
76
# File 'lib/jekyll-import/importers/tumblr.rb', line 71

def extract_json(contents)
  beginning = contents.index("{")
  ending    = contents.rindex("}") + 1
  json_data = contents[beginning...ending] # Strip Tumblr's JSONP chars.
  JSON.parse(json_data)
end

.fetch_photo(post) ⇒ Object

Attempts to fetch the largest version of a photo available for a post. If that file fails, it tries the next smaller size until all available photo URLs are exhausted. If they all fail, the import is aborted.



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/jekyll-import/importers/tumblr.rb', line 181

def fetch_photo(post)
  sizes = post.keys.map { |k| k.gsub("photo-url-", "").to_i }
  sizes.sort! { |a, b| b <=> a }

  _ext_key, ext_val = post.find do |k, v|
    k =~ %r!^photo-url-! && v.split("/").last =~ %r!\.!
  end
  ext = "." + ext_val.split(".").last

  sizes.each do |size|
    url = post["photo-url"] || post["photo-url-#{size}"]
    next if url.nil?

    begin
      return +"<img src=\"#{save_photo(url, ext)}\"/>"
    rescue OpenURI::HTTPError
      Jekyll.logger.warn "Failed to grab photo"
    end
  end

  abort "Failed to fetch photo for post #{post["url"]}"
end

.html_to_markdown(content) ⇒ Object

Convert preserving HTML tables as per the markdown docs.



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/jekyll-import/importers/tumblr.rb', line 236

def html_to_markdown(content)
  preserve = %w(table tr th td)
  preserve.each do |tag|
    content.gsub!(%r!<#{tag}!i, "$$#{tag}")
    content.gsub!(%r!</#{tag}!i, "||#{tag}")
  end

  content = ReverseMarkdown.convert(content)

  preserve.each do |tag|
    content.gsub!("$$#{tag}", "<#{tag}")
    content.gsub!("||#{tag}", "</#{tag}")
  end
  content
end

.post_to_hash(post, format) ⇒ Object

Converts each type of Tumblr post to a hash with all required data for Jekyll.



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

def post_to_hash(post, format)
  case post["type"]
  when "regular"
    title, content = post.values_at("regular-title", "regular-body")
  when "link"
    title   = post["link-text"] || post["link-url"]
    content = "<a href=\"#{post["link-url"]}\">#{title}</a>"
    content << "<br/>#{post["link-description"]}" unless post["link-description"].nil?
  when "photo"
    title = post["slug"].tr("-", " ")
    if post["photos"].size > 1
      content = +""
      post["photos"].each do |post_photo|
        photo = fetch_photo post_photo
        content << "#{photo}<br/>"
        content << post_photo["caption"]
      end
    else
      content = fetch_photo post
    end
    content << "<br/>#{post["photo-caption"]}"
  when "audio"
    if !post["id3-title"].nil?
      title, content = post.values_at("id3-title", "audio-player")
      content << "<br/>#{post["audio-caption"]}"
    else
      title, content = post.values_at("audio-caption", "audio-player")
    end
  when "quote"
    title   = post["quote-text"]
    content = "<blockquote>#{post["quote-text"]}</blockquote>"
    content << "&#8212;#{post["quote-source"]}" unless post["quote-source"].nil?
  when "conversation"
    title   = post["conversation-title"]
    content = "<section><dialog>"
    post["conversation"].each do |line|
      content << "<dt>#{line["label"]}</dt><dd>#{line["phrase"]}</dd>"
    end
    content << "</dialog></section>"
  when "video"
    title, content = post.values_at("video-title", "video-player")
    unless post["video-caption"].nil?
      if content
        content << "<br/>#{post["video-caption"]}"
      else
        content = post["video-caption"]
      end
    end
  when "answer"
    title, content = post.values_at("question", "answer")
  end

  date  = Date.parse(post["date"]).to_s
  title = Nokogiri::HTML(title).text
  title = "no title" if title.empty?
  slug  = if post["slug"] && post["slug"].strip != ""
            post["slug"]
          elsif title && title.downcase.gsub(%r![^a-z0-9\-]!, "") != "" && title != "no title"
            slug = title.downcase.strip.tr(" ", "-").gsub(%r![^a-z0-9\-]!, "")
            slug.length > 200 ? slug.slice(0..200) : slug
          else
            post["id"]
          end
  {
    :name    => "#{date}-#{slug}.#{format}",
    :header  => {
      "layout"     => "post",
      "title"      => title,
      "date"       => Time.parse(post["date"]).xmlschema,
      "tags"       => (post["tags"] || []),
      "tumblr_url" => post["url-with-slug"],
    },
    :content => content,
    :url     => post["url"],
    :slug    => post["url-with-slug"],
  }
end

.process(options) ⇒ Object



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

def process(options)
  url            = options.fetch("url")
  format         = options.fetch("format", "html")
  grab_images    = options.fetch("grab_images", false)
  add_highlights = options.fetch("add_highlights", false)
  rewrite_urls   = options.fetch("rewrite_urls", false)

  @grab_images = grab_images
  FileUtils.mkdir_p "_posts/tumblr"
  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_url     = api_feed_url(url, current_page, per_page)
    Jekyll.logger.info "Fetching #{feed_url}"

    feed     = URI.parse(feed_url).open
    contents = feed.readlines.join("\n")
    blog     = extract_json(contents)
    Jekyll.logger.info "Page: #{current_page + 1} - Posts: #{blog["posts"].size}"

    batch = blog["posts"].map { |post| post_to_hash(post, format) }

    # If we're rewriting, save the posts for later.  Otherwise, go ahead and dump these to
    # disk now
    if rewrite_urls
      posts += batch
    else
      batch.each { |post| write_post(post, format == "md", add_highlights) }
    end
  end until blog["posts"].size < per_page

  # Rewrite URLs, create redirects and write out out posts if necessary
  if rewrite_urls
    posts = rewrite_urls_and_redirects posts
    posts.each { |post| write_post(post, format == "md", add_highlights) }
  end
end

.require_depsObject



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

def require_deps
  JekyllImport.require_with_fallback(%w(
    rubygems
    fileutils
    open-uri
    nokogiri
    json
    uri
    time
    jekyll
    reverse_markdown
  ))
end

.rewrite_urls_and_redirects(posts) ⇒ Object

Create a Hash of old urls => new urls, for rewriting and redirects, and replace urls in each post. Instantiate Jekyll site/posts to get the correct permalink format.



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/jekyll-import/importers/tumblr.rb', line 206

def rewrite_urls_and_redirects(posts)
  site = Jekyll::Site.new(Jekyll.configuration({}))
  urls = Hash[posts.map do |post|
    # Create an initial empty file for the post so that we can instantiate a post object.
    relative_path = "_posts/tumblr/#{post[:name]}"
    File.write(relative_path, "")
    tumblr_url = URI.parse(URI.encode(post[:slug])).path
    jekyll_url = if Jekyll.const_defined? :Post
                   Jekyll::Post.new(site, site.source, "", "tumblr/#{post[:name]}").url
                 else
                   Jekyll::Document.new(site.in_source_dir(relative_path), :site => site, :collection => site.posts).url
                 end
    redirect_dir = tumblr_url.sub(%r!\/!, "") + "/"
    FileUtils.mkdir_p redirect_dir
    File.open(redirect_dir + "index.html", "w") do |f|
      f.puts "<html><head><link rel=\"canonical\" href=\"" \
        "#{jekyll_url}\"><meta http-equiv=\"refresh\" content=\"0; " \
        "url=#{jekyll_url}\"></head><body></body></html>"
    end
    [tumblr_url, jekyll_url]
  end]
  posts.map do |post|
    urls.each do |tumblr_url, jekyll_url|
      post[:content].gsub!(%r!#{tumblr_url}!i, jekyll_url)
    end
    post
  end
end

.save_photo(url, ext) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/jekyll-import/importers/tumblr.rb', line 282

def save_photo(url, ext)
  return url unless @grab_images

  path = "tumblr_files/#{url.split("/").last}"
  path += ext unless %r!#{ext}$!.match?(path)
  FileUtils.mkdir_p "tumblr_files"

  # Don't fetch if we've already cached this file
  unless File.size? path
    Jekyll.logger.info "Fetching photo #{url}"
    File.open(path, "wb") { |f| f.write(URI.parse(url).read) }
  end
  "/#{path}"
end

.specify_options(c) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/jekyll-import/importers/tumblr.rb', line 21

def specify_options(c)
  c.option "url",            "--url URL",        "Tumblr URL."
  c.option "format",         "--format FORMAT",  "Output format. (default: 'html')"
  c.option "grab_images",    "--grab_images",    "Whether to grab images. (default: false)"
  c.option "add_highlights", "--add_highlights", "Whether to add highlights. (default: false)"
  c.option "rewrite_urls",   "--rewrite_urls",   "Whether to rewrite URLs. (default: false)"
end

.write_post(post, use_markdown, add_highlights) ⇒ Object

Writes a post out to disk



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/jekyll-import/importers/tumblr.rb', line 79

def write_post(post, use_markdown, add_highlights)
  content = post[:content]
  return unless content

  if use_markdown
    content = html_to_markdown content
    if add_highlights
      tumblr_url   = URI.parse(post[:slug]).path
      redirect_dir = tumblr_url.sub(%r!\/!, "") + "/"
      FileUtils.mkdir_p redirect_dir
      content = add_syntax_highlights(content, redirect_dir)
    end
  end

  File.open("_posts/tumblr/#{post[:name]}", "w") do |f|
    f.puts post[:header].to_yaml + "---\n" + content
  end
end