Permalink
Browse files

Moved the migration logic out of Post and into MiddlemanPost

  • Loading branch information...
1 parent abb9224 commit 0c5c2e8817188ceb722d050e02f81d561839cc5a @natedavisolds committed Feb 28, 2014
View
@@ -1,5 +1,6 @@
require 'wp2middleman/version'
require 'wp2middleman/post'
+require 'wp2middleman/middleman_post'
require 'wp2middleman/post_collection'
require 'wp2middleman/frontmatter'
require 'wp2middleman/migrator'
@@ -0,0 +1,71 @@
+module WP2Middleman
+ class MiddlemanPost
+ def initialize(wp_post, body_to_markdown: false, include_fields: [])
+ @wp_post = wp_post
+ @body_to_markdown = body_to_markdown
+ @include_fields = include_fields
+ end
+
+ def title
+ wp_post.title
+ end
+
+ def title_for_filename
+ title.gsub(/[^\w\s_-]+/, '')
+ .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
+ .gsub(/\s+/, '-')
+ end
+
+ def filename
+ "#{date_published}-#{title_for_filename}"
+ end
+
+ def date_published
+ wp_post.date_published
+ end
+
+ def full_filename output_path
+ "#{output_path}#{filename}.html.markdown"
+ end
+
+ def file_content
+ <<-EOS.gsub(/^ {8}/, '')
+ #{frontmatter.to_yaml}
+ ---
+
+ #{formatted_post_content}
+ EOS
+ end
+
+ def formatted_post_content
+ if body_to_markdown
+ markdown_content
+ else
+ content
+ end
+ end
+
+ def content
+ wp_post.content
+ end
+
+ def markdown_content
+ html = HTMLPage.new :contents => content
+ html.comment do |node,_|
+ "#{node}"
+ end
+ html.iframe do |node,_|
+ "#{node}"
+ end
+ html.markdown
+ end
+
+ private
+
+ attr_reader :wp_post, :body_to_markdown, :include_fields
+
+ def frontmatter
+ @frontmatter ||= Frontmatter.new(wp_post, include_fields: include_fields)
+ end
+ end
+end
@@ -5,11 +5,10 @@ class Migrator
attr_reader :posts
def initialize(wp_xml_export_file, body_to_markdown: false, include_fields: [])
- @posts = WP2Middleman::PostCollection.from_file(
- wp_xml_export_file,
- body_to_markdown: body_to_markdown,
- include_fields: include_fields
- ).without_attachments.only_valid
+ @posts = WP2Middleman::PostCollection.from_file(wp_xml_export_file)
+ .without_attachments
+ .only_valid
+ .to_middleman(body_to_markdown: body_to_markdown, include_fields: include_fields)
end
def migrate
View
@@ -4,22 +4,14 @@ module WP2Middleman
class Post
attr_accessor :post
- def initialize(nokogiri_post_doc, body_to_markdown: false, include_fields: [])
+ def initialize(nokogiri_post_doc)
@post = nokogiri_post_doc
- @body_to_markdown = body_to_markdown
- @include_fields = include_fields
end
def title
post.css('title').text
end
- def title_for_filename
- title.gsub(/[^\w\s_-]+/, '')
- .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
- .gsub(/\s+/, '-')
- end
-
def valid?
!(post_date.nil? || title.nil? || date_published.nil? || content.nil?)
end
@@ -28,35 +20,10 @@ def attachment?
type == 'attachment'
end
- def filename
- "#{date_published}-#{title_for_filename}"
- end
-
- def full_filename output_path
- "#{output_path}#{filename}.html.markdown"
- end
-
def field(field)
post.xpath(field).first.inner_text
end
- def file_content
- <<-EOS.gsub(/^ {8}/, '')
- #{frontmatter.to_yaml}
- ---
-
- #{formatted_post_content}
- EOS
- end
-
- def formatted_post_content
- if body_to_markdown
- markdown_content
- else
- content
- end
- end
-
def post_date
post.xpath("wp:post_date").first.inner_text
end
@@ -81,17 +48,6 @@ def content
post.at_xpath(".//content:encoded").inner_text
end
- def markdown_content
- html = HTMLPage.new :contents => content
- html.comment do |node,_|
- "#{node}"
- end
- html.iframe do |node,_|
- "#{node}"
- end
- html.markdown
- end
-
def tags
tags = []
categories = post.xpath("category")
@@ -104,13 +60,5 @@ def tags
tags
end
-
- private
-
- attr_reader :body_to_markdown, :include_fields
-
- def frontmatter
- @frontmatter ||= Frontmatter.new(self, include_fields: include_fields)
- end
end
end
@@ -4,9 +4,9 @@ module WP2Middleman
class PostCollection
include Enumerable
- def self.from_file(wp_xml_export_file, body_to_markdown: false, include_fields: [])
+ def self.from_file(wp_xml_export_file)
xml = Nokogiri::XML(File.open("#{Dir.pwd}/#{wp_xml_export_file}"))
- new xml.css('item').collect { |wp_post| WP2Middleman::Post.new(wp_post, body_to_markdown: body_to_markdown, include_fields: include_fields) }
+ new xml.css('item').collect { |raw_wp_post| WP2Middleman::Post.new(raw_wp_post) }
end
def initialize(posts=[])
@@ -33,6 +33,11 @@ def only_valid
self.class.new(select(&:valid?))
end
+ def to_middleman(body_to_markdown: false, include_fields: [])
+ middleman_posts = collect { |p| WP2Middleman::MiddlemanPost.new(p, body_to_markdown: body_to_markdown, include_fields: include_fields) }
+ self.class.new(middleman_posts)
+ end
+
private
attr_reader :posts
@@ -0,0 +1,116 @@
+require 'spec_helper'
+
+describe WP2Middleman::MiddlemanPost do
+ let(:post_one) { double :post,
+ title: "A Title",
+ date_published: Date.new(2012,6,8).to_s,
+ content: "Paragraph one.\n\n Paragraph two.\n ",
+ tags: [],
+ published?: true
+ }
+
+ let(:post_two) { double :post,
+ title: "A second title",
+ date_published: Date.new(2011,7,25).to_s,
+ content: " <strong>Foo</strong>",
+ tags: ["some_tag", "another tag", "tag"],
+ published?: true
+ }
+
+ let(:post_three) { double :post,
+ title: "A third title: With colon",
+ date_published: Date.new(2011,7,26).to_s,
+ content: "Foo",
+ tags: ["some_tag", "another tag", "tag"],
+ published?: false
+ }
+
+ let(:post_with_iframe) { double :post,
+ title: "A fourth item with iframe and comment",
+ date_published: Date.new(2011,7,26).to_s,
+ content: "Here's a post with an iframe and a comment.\n\n<!--more-->\n\n<iframe width=\"400\" height=\"100\" style=\"position: relative; display: block; width: 400px; height: 100px;\" src=\"http://bandcamp.com/EmbeddedPlayer/v=2/track=833121761/size=venti/bgcol=FFFFFF/linkcol=4285BB/\" allowtransparency=\"true\" frameborder=\"0\"><a href=\"http://dihannmoore.bandcamp.com/track/you-do-it-for-me\">&quot;YOU DO IT FOR ME&quot; by DIHANN MOORE</a></iframe>",
+ tags: ["some_tag", "another tag", "tag"],
+ published?: false
+ }
+
+ it "has a title formatted for a filename" do
+ wp_post = double :post, title: "A Title"
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
+ expect(post.title_for_filename).to eq "A-Title"
+ end
+
+ it "removes odd characters (like colons) from title for filename" do
+ wp_post = double :post, title: "A third title: With colon"
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
+ expect(post.title_for_filename).to eq "A-third-title-With-colon"
+ end
+
+ it "prepends the date published onto the title for the filename" do
+ wp_post = double :post, title: "A Title", date_published: Date.new(2012,6,8)
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
+ expect(post.filename).to eq "2012-06-08-A-Title"
+ end
+
+ it "returns the full filename for a Middleman-style markdown post" do
+ wp_post = double :post, title: "A Title", date_published: Date.new(2012,6,8)
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
+ expect(post.full_filename('/some/path/')).to eq("/some/path/2012-06-08-A-Title.html.markdown")
+ end
+
+ it "converts the content to markdown" do
+ wp_post = double :post, content: "<strong>Foo</strong>"
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
+ expect(post.markdown_content).to eq "**Foo**"
+ end
+
+ it "has no formatting change to post body by default" do
+ post = WP2Middleman::MiddlemanPost.new post_one
+
+ expect(post.file_content).to eq(
+ "---\ntitle: A Title\ndate: '2012-06-08'\ntags: []\n---\n\nParagraph one.\n\n Paragraph two.\n \n"
+ )
+ end
+
+ it "formats the post body as markdown" do
+ post = WP2Middleman::MiddlemanPost.new post_two, body_to_markdown: true
+
+ expect( post.file_content ).to eq(
+ "---\ntitle: A second title\ndate: '2011-07-25'\ntags:\n- some_tag\n- another tag\n- tag\n---\n\n**Foo**\n"
+ )
+ end
+
+ it "ignores iframe and comment tags when converting to markdown" do
+ post = WP2Middleman::MiddlemanPost.new post_with_iframe, body_to_markdown: true
+
+ expect(post.file_content).to eq("---\ntitle: A fourth item with iframe and comment\ndate: '2011-07-26'\ntags:\n- some_tag\n- another tag\n- tag\npublished: false\n---\n\nHere's a post with an iframe and a comment.\n\n\n<!--more-->\n\n\n<iframe width=\"400\" height=\"100\" style=\"position: relative; display: block; width: 400px; height: 100px;\" src=\"http://bandcamp.com/EmbeddedPlayer/v=2/track=833121761/size=venti/bgcol=FFFFFF/linkcol=4285BB/\" allowtransparency=\"true\" frameborder=\"0\"><a href=\"http://dihannmoore.bandcamp.com/track/you-do-it-for-me\">\"YOU DO IT FOR ME\" by DIHANN MOORE</a></iframe>\n")
+ end
+
+ it "appends included fields in with frontmatter" do
+ post_two.should_receive(:field).with('wp:post_id').and_return('209')
+ post = WP2Middleman::MiddlemanPost.new post_two, include_fields: ['wp:post_id']
+
+ expect( post.file_content ).to eq(
+ "---\ntitle: A second title\ndate: '2011-07-25'\ntags:\n- some_tag\n- another tag\n- tag\nwp:post_id: '209'\n---\n\n <strong>Foo</strong>\n"
+ )
+ end
+
+ it "reports 'published: false' in the post's frontmatter when post is not published" do
+ post = WP2Middleman::MiddlemanPost.new post_three
+
+ expect( post.file_content ).to eq(
+ "---\ntitle: 'A third title: With colon'\ndate: '2011-07-26'\ntags:\n- some_tag\n- another tag\n- tag\npublished: false\n---\n\nFoo\n"
+ )
+ end
+
+ it "does no formatting by default" do
+ post = WP2Middleman::MiddlemanPost.new post_two
+
+ expect( post.formatted_post_content ).to eq(" <strong>Foo</strong>")
+ end
+
+ it "does markdown formatting" do
+ post = WP2Middleman::MiddlemanPost.new post_two, body_to_markdown: true
+
+ expect( post.formatted_post_content ).to eq("**Foo**")
+ end
+end
Oops, something went wrong.

0 comments on commit 0c5c2e8

Please sign in to comment.