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

Correctly handle wordpressdotcom drafts #213

Merged
merged 3 commits into from Aug 16, 2015
Jump to file or symbol
Failed to load files and symbols.
+147 −20
Diff settings

Always

Just for now

Viewing a subset of changes. View all

Wrap WP's item nodes in an Item, that can extract its data.

  • Loading branch information...
danbernier committed Aug 10, 2015
commit c5c62dd8e9659c330f950b270a6a666f2249885e
@@ -50,6 +50,50 @@ def self.download_images(title, post_hpricot, assets_folder)
end
end
class Item
def initialize(node)
@node = node
end
def title
@node.at(:title).inner_text.strip
end
def permalink_title
p_title = @node.at('wp:post_name').inner_text
# Fallback to "prettified" title if post_name is empty (can happen)
if p_title == ""
p_title = WordpressDotCom.sluggify(title)
end
return p_title
end
def published_at
Time.parse(@node.at('wp:post_date').inner_text)
end
def status
@node.at('wp:status').inner_text
end
def post_type
@node.at('wp:post_type').inner_text
end
def file_name
"#{published_at.strftime('%Y-%m-%d')}-#{permalink_title}.html"
end
def directory_name
"_#{post_type}s"
end
def published?
status == 'publish'
end
end
def self.process(options)
source = options.fetch('source', "wordpress.xml")
fetch = !options.fetch('no_fetch_images', false)
@@ -72,23 +116,14 @@ def self.process(options)
] rescue {}
(doc/:channel/:item).each do |node|
title = node.at(:title).inner_text.strip
permalink_title = node.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(node.at('wp:post_date').inner_text)
status = node.at('wp:status').inner_text
if status == "publish"
published = true
else
published = false
end
type = node.at('wp:post_type').inner_text
item = Item.new(node)
title = item.title
permalink_title = item.permalink_title
date = item.published_at
status = item.status
published = item.published?
type = item.post_type
categories = node.search('category[@domain="category"]').map{|c| c.inner_text}.reject{|c| c == 'Uncategorized'}.uniq
tags = node.search('category[@domain="post_tag"]').map{|t| t.inner_text}.uniq
@@ -101,7 +136,7 @@ def self.process(options)
author_login = node.at('dc:creator').inner_text.strip
name = "#{date.strftime('%Y-%m-%d')}-#{permalink_title}.html"
name = item.file_name
header = {
'layout' => type,
'title' => title,
@@ -127,8 +162,8 @@ def self.process(options)
download_images(title, content, assets_folder)
end
FileUtils.mkdir_p "_#{type}s"
File.open("_#{type}s/#{name}", "w") do |f|
FileUtils.mkdir_p item.directory_name
File.open("#{item.directory_name}/#{name}", "w") do |f|
f.puts header.to_yaml
f.puts '---'
f.puts Util.wpautop(content.to_html)
@@ -6,3 +6,95 @@ class TestWordpressDotComMigrator < Test::Unit::TestCase
assert_equal("blogs-part-1-2", Importers::WordpressDotCom.sluggify(test_title))
end
end
class TestWordpressDotComItem < Test::Unit::TestCase
should "extract an item's title" do
node = Hpricot(%q{
<item>
<title>Dear Science</title>
</item>}).at('item')
item = Importers::WordpressDotCom::Item.new(node)
assert_equal('Dear Science', item.title)
end
should "use post_name for the permalink_title if it's there" do
node = Hpricot(%q{
<item>
<wp:post_name>cookie-mountain</wp:post_name>
<title>Dear Science</title>
</item>}).at('item')
item = Importers::WordpressDotCom::Item.new(node)
assert_equal('cookie-mountain', item.permalink_title)
end
should "sluggify title for the permalink_title if post_name is empty" do
node = Hpricot(%q{
<item>
<wp:post_name></wp:post_name>
<title>Dear Science</title>
</item>}).at('item')
item = Importers::WordpressDotCom::Item.new(node)
assert_equal('dear-science', item.permalink_title)
end
end
class TestWordpressDotComPublishedItem < TestWordpressDotComItem
def node
Hpricot(%q{
<item>
<wp:post_name>post-name</wp:post_name>
<wp:post_type>post</wp:post_type>
<wp:status>publish</wp:status>
<wp:post_date>2015-01-23 08:53:47</wp:post_date>
</item>}).at('item')
end
def item
Importers::WordpressDotCom::Item.new(node)
end
should "extract the date-time the item was published" do
assert_equal(Time.new(2015, 1, 23, 8, 53, 47), item.published_at)
end
should 'put the date in the file_name' do
assert_equal('2015-01-23-post-name.html', item.file_name)
end
should "put the file in ./_posts" do
assert_equal('_posts', item.directory_name)
end
should 'know its status' do
assert_equal('publish', item.status)
end
should 'be published' do
assert_equal(true, item.published?)
end
end
class TestWordpressDotComDraftItem < TestWordpressDotComItem
def node
Hpricot(%q{
<item>
<wp:post_name>post-name</wp:post_name>
<wp:post_type>post</wp:post_type>
<wp:status>draft</wp:status>
<wp:post_date>0000-00-00 00:00:00</wp:post_date>
</item>}).at('item')
end
def item
Importers::WordpressDotCom::Item.new(node)
end
should 'know its status' do
assert_equal('draft', item.status)
end
should 'not be published' do
assert_equal(false, item.published?)
end
end
ProTip! Use n and p to navigate between commits in a pull request.