Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only complete write_post on Tumblr posts w/ non-nil content #118

Merged
merged 5 commits into from
Jan 9, 2014
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions jekyll-import.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Gem::Specification.new do |s|

s.name = 'jekyll-import'
s.version = '0.1.0'
s.date = '2013-12-18'
s.date = '2014-01-01'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please revert this change?

s.rubyforge_project = 'jekyll-import'

s.summary = "Import command for Jekyll (static site generator)."
Expand Down Expand Up @@ -34,7 +34,7 @@ Gem::Specification.new do |s|

# test dependencies:
s.add_development_dependency('redgreen', "~> 1.2")
s.add_development_dependency('shoulda', "~> 3.3.2")
s.add_development_dependency('shoulda', "~> 3.5.0")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept bumping my head into warning: already initialized constant Mocha until I discovered that shoulda might be due for an update

s.add_development_dependency('rr', "~> 1.0")
s.add_development_dependency('simplecov', "~> 0.7")
s.add_development_dependency('simplecov-gem-adapter', "~> 1.0.1")
Expand Down
20 changes: 13 additions & 7 deletions lib/jekyll-import/importers/tumblr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ def self.process(options)
# Writes a post out to disk
def self.write_post(post, use_markdown, add_highlights)
content = post[:content]
if use_markdown
content = html_to_markdown content
content = add_syntax_highlights content if add_highlights
end

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

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

Expand Down Expand Up @@ -125,6 +128,9 @@ def self.post_to_hash(post, format)
unless post["video-caption"].nil?
content << "<br/>" + post["video-caption"]
end
when "answer"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now properly dealing with answer posts

title = post["question"]
content = post["answer"]
end
date = Date.parse(post['date']).to_s
title = Nokogiri::HTML(title).text
Expand All @@ -140,7 +146,7 @@ def self.post_to_hash(post, format)
"layout" => "post",
"title" => title,
"date" => Time.parse(post['date']).xmlschema,
"tags" => post["tags"],
"tags" => (post["tags"] or []),
"tumblr_url" => post["url-with-slug"]
},
:content => content,
Expand Down
133 changes: 81 additions & 52 deletions test/test_tumblr_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,59 +3,88 @@

class TestTumblrImporter < Test::Unit::TestCase

context "A Tumblr blog" do
setup do
context "A Tumblr blog" do
setup do
Importers::Tumblr.require_deps
@jsonPayload = '{"tumblelog":{"title":"JekyllImport","description":"Jekyll Importer Test.","name":"JekyllImport","timezone":"Canada\/Atlantic","cname":"https://github.com/jekyll/jekyll-import/","feeds":[]},"posts-start":0,"posts-total":"1","posts-type":false,"posts":[{"id":54759400073,"url":"https:\/\/github.com\/post\/54759400073","url-with-slug":"http:\/\/github.com\/post\/54759400073\/jekyll-test","type":"regular","date-gmt":"2013-07-06 16:27:23 GMT","date":"Sat, 06 Jul 2013 13:27:23","bookmarklet":null,"mobile":null,"feed-item":"","from-feed-id":0,"unix-timestamp":1373128043,"format":"html","reblog-key":"0L6yPcHr","slug":"jekyll-test","regular-title":"Jekyll: Test","regular-body":"<p>Testing...<\/p>","tags":["jekyll"]}]}'
@posts = JSON.parse(@jsonPayload)
end
@jsonPayload = '{"tumblelog":{"title":"JekyllImport","description":"Jekyll Importer Test.","name":"JekyllImport","timezone":"Canada\/Atlantic","cname":"https://github.com/jekyll/jekyll-import/","feeds":[]},"posts-start":0,"posts-total":"2","posts-type":false,"posts":[{"id":54759400073,"url":"https:\/\/github.com\/post\/54759400073","url-with-slug":"http:\/\/github.com\/post\/54759400073\/jekyll-test","type":"regular","date-gmt":"2013-07-06 16:27:23 GMT","date":"Sat, 06 Jul 2013 13:27:23","bookmarklet":null,"mobile":null,"feed-item":"","from-feed-id":0,"unix-timestamp":1373128043,"format":"html","reblog-key":"0L6yPcHr","slug":"jekyll-test","regular-title":"Jekyll: Test","regular-body":"<p>Testing...<\/p>","tags":["jekyll"]},{"id":"71845593082","url":"http:\/\/example.com\/post\/71845593082","url-with-slug":"http:\/\/example.com\/post\/71845593082\/knock-knock","type":"answer","date-gmt":"2014-01-01 14:08:45 GMT","date":"Wed, 01 Jan 2014 09:08:45","bookmarklet":0,"mobile":0,"feed-item":"","from-feed-id":0,"unix-timestamp":1388585325,"format":"html","reblog-key":"jPfWHFnT","slug":"knock-knock","question":"Knock knock?","answer":"<p>Who is there?<\/p>"}]}'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the answer format from my actual Tumblr feed. Took the courtesy to mask the urls for fictitious urls, so I'm pretty sure that this will work. I've confirmed that the code works by importing my real Tumblr feed.

@posts = JSON.parse(@jsonPayload)
@batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Took the liberty to drop build the batch list in the setup as we're using it in nearly every test 😉, hope it's okay. I also dropped all the \t's in exhange for soft-tabs. Expect it to be accepted as I'm just following the coding style in the Ruby Styleguide which is referred to on Jekyll Contributing.

end

should "have a post" do
assert_equal(1, @posts["posts"].size)
end
should "have a post" do
assert_equal(2, @posts["posts"].size)
end

should "convert post into hash" do
refute_nil(@batch, "a batch with a valid post should exist")
end

should "have a regular post" do
assert_equal("regular", @posts['posts'][0]['type'])
end
context "post" do
should "have a corresponding type" do
assert_equal("regular", @posts['posts'][0]['type'])
end

should "have a hash with a valid name" do
assert_equal("2013-07-06-jekyll-test.html", @batch[0][:name])
end

should "have a hash with a valid layout" do
assert_equal("post", @batch[0][:header]['layout'])
end

should "have a hash with a valid title" do
assert_equal("Jekyll: Test", @batch[0][:header]['title'])
end

should "have a hash with valid tags" do
assert_equal("jekyll", @batch[0][:header]['tags'][0])
end

should "have a hash with valid content" do
assert_equal("<p>Testing...</p>", @batch[0][:content])
end

should "have a hash with a valid url" do
assert_equal("https://github.com/post/54759400073", @batch[0][:url])
end

should "have a hash with a valid slug" do
assert_equal("http://github.com/post/54759400073/jekyll-test", @batch[0][:slug])
end
end

should "convert post into hash" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
refute_nil(batch, "a batch with a valid post should exist")
end

should "have a hash with a valid name" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
assert_equal("2013-07-06-jekyll-test.html", batch[0][:name])
end

should "have a hash with a valid layout" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
assert_equal("post", batch[0][:header]['layout'])
end

should "have a hash with a valid title" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
assert_equal("Jekyll: Test", batch[0][:header]['title'])
end

should "have a hash with valid tags" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
assert_equal("jekyll", batch[0][:header]['tags'][0])
end

should "have a hash with valid content" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
assert_equal("<p>Testing...</p>", batch[0][:content])
end

should "have a hash with a valid url" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
assert_equal("https://github.com/post/54759400073", batch[0][:url])
end

should "have a hash with a valid slug" do
batch = @posts["posts"].map { |post| Importers::Tumblr.post_to_hash(post, 'html') }
assert_equal("http://github.com/post/54759400073/jekyll-test", batch[0][:slug])
end
end
end
context "answer" do
should "have a corresponding type" do
assert_equal("answer", @posts['posts'][1]['type'])
end

should "have a hash with a valid name" do
assert_equal("2014-01-01-knock-knock.html", @batch[1][:name])
end

should "have a hash with a valid layout" do
assert_equal("post", @batch[1][:header]['layout'])
end

should "have a hash with a valid title" do
assert_equal("Knock knock?", @batch[1][:header]['title'])
end

should "have a hash with valid tags" do
assert_equal([], @batch[1][:header]['tags'])
end

should "have a hash with valid content" do
assert_equal("<p>Who is there?</p>", @batch[1][:content])
end

should "have a hash with a valid url" do
assert_equal("http://example.com/post/71845593082", @batch[1][:url])
end

should "have a hash with a valid slug" do
assert_equal("http://example.com/post/71845593082/knock-knock", @batch[1][:slug])
end
end
end
end