Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved the content methods in migrator to Post

  • Loading branch information...
commit b051e8135c48694ce1e4a5d93b931843b24c074e 1 parent 5c61ed8
@natedavisolds authored
View
30 lib/wp2middleman/migrator.rb
@@ -2,7 +2,6 @@
module WP2Middleman
class Migrator
-
attr_reader :posts
def initialize(wp_xml_export_file, body_to_markdown: false, include_fields: [])
@@ -15,33 +14,10 @@ def migrate
ensure_export_directory
posts.each do |post|
- File.write(full_filename(post), file_content(post))
- end
- end
-
- def file_content(post, include_fields: @include_fields)
- frontmatter = Frontmatter.new(post, include_fields: include_fields)
-
- <<-EOS.gsub(/^ {8}/, '')
- #{frontmatter.to_yaml}
- ---
-
- #{formatted_post_content(post)}
- EOS
- end
-
- def formatted_post_content(post)
- if @body_to_markdown
- post.markdown_content
- else
- post.content
+ File.write(post.full_filename(output_path), post.file_content(body_to_markdown: body_to_markdown, include_fields: include_fields))
end
end
- def full_filename(post)
- "#{output_path}#{post.filename}.html.markdown"
- end
-
def output_path
"#{Dir.pwd}/export/"
end
@@ -51,5 +27,9 @@ def ensure_export_directory
FileUtils.mkdir_p output_path
end
end
+
+ private
+
+ attr_reader :body_to_markdown, :include_fields
end
end
View
23 lib/wp2middleman/post.rb
@@ -30,10 +30,33 @@ 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(include_fields: [], body_to_markdown: false)
+ frontmatter = Frontmatter.new(self, include_fields: include_fields)
+
+ <<-EOS.gsub(/^ {8}/, '')
+ #{frontmatter.to_yaml}
+ ---
+
+ #{formatted_post_content(body_to_markdown: body_to_markdown)}
+ EOS
+ end
+
+ def formatted_post_content body_to_markdown: false
+ if body_to_markdown
+ markdown_content
+ else
+ content
+ end
+ end
+
def post_date
post.xpath("wp:post_date").first.inner_text
end
View
57 spec/lib/wp2middleman/migrator_spec.rb
@@ -27,62 +27,11 @@
it "writes the proper markdown file" do
post = migrator.posts.first
+ post.stub(:file_content).and_return("content")
migrator.stub(:valid_posts).and_return([post])
- File.should_receive(:write).with("#{Dir.pwd}/export/2012-06-08-A-Title.html.markdown", migrator.file_content(post))
- migrator.migrate
- end
- end
-
- describe "#file_content" do
- it "properly formats a post as a Middleman-style post" do
- expect(migrator.file_content(migrator.posts[1])).to eq("---\ntitle: A second title\ndate: '2011-07-25'\ntags:\n- some_tag\n- another tag\n- tag\n---\n\n <strong>Foo</strong>\n")
- end
-
- context "its behavior if @body_to_markdown is true" do
- let(:migrator) { WP2Middleman::Migrator.new(file, body_to_markdown: true) }
-
- it "formats the post body as markdown" do
- expect(migrator.file_content(migrator.posts[1])).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 "includes iframe and comment" do
- expect(migrator.file_content(migrator.posts[3])).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
- end
-
- context "has been passed include_fields" do
- let(:migrator) { WP2Middleman::Migrator.new(file, include_fields: ['wp:post_id']) }
-
- it "includes the property and value from the item's xml in the frontmatter" do
- expect(migrator.file_content(migrator.posts[1])).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
- end
-
- context "the post is not published" do
- it "reports 'published: false' in the post's frontmatter" do
- expect(migrator.file_content(migrator.posts[2])).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
- end
- end
-
- describe "#formatted_post_content" do
- it "returns the content of the post it's passed" do
- expect(migrator.formatted_post_content(migrator.posts[1])).to eq(" <strong>Foo</strong>")
- end
-
- context "its behavior if @body_to_markdown is true" do
- let(:migrator) { WP2Middleman::Migrator.new(file, body_to_markdown: true) }
-
- it "returns the content of the post it's passed as markdown" do
- expect(migrator.formatted_post_content(migrator.posts[1])).to eq("**Foo**")
- end
- end
- end
-
- describe "#full_filename" do
- it "returns the full filename for a Middleman-style markdown post" do
- expect(migrator.full_filename(migrator.posts[0])).to eq("#{Dir.pwd}/export/2012-06-08-A-Title.html.markdown")
+ File.should_receive(:write).with("#{Dir.pwd}/export/2012-06-08-A-Title.html.markdown", "content")
+ migrator.migrate
end
end
View
46 spec/lib/wp2middleman/post_spec.rb
@@ -34,6 +34,12 @@
end
end
+ describe "#full_filename" do
+ it "returns the full filename for a Middleman-style markdown post" do
+ expect(post_one.full_filename('/some/path/')).to eq("/some/path/2012-06-08-A-Title.html.markdown")
+ end
+ end
+
describe "#post_date" do
subject { post_one.post_date }
@@ -95,6 +101,46 @@
end
end
+ describe "#file_content" do
+ it "properly formats a post as a Middleman-style post" do
+ expected_content = "---\ntitle: A Title\ndate: '2012-06-08'\ntags: []\n---\n\nParagraph one.\n\n Paragraph two.\n \n"
+ expect(post_one.file_content).to eq(expected_content)
+ end
+
+ context "its behavior if @body_to_markdown is true" do
+ it "formats the post body as markdown" do
+ expect(post_two.file_content(body_to_markdown: true)).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 "includes iframe and comment" do
+ post = WP2Middleman::Post.new(file.css('item')[3])
+ expect(post.file_content(body_to_markdown: true)).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
+ end
+
+ context "has been passed include_fields" do
+ it "includes the property and value from the item's xml in the frontmatter" do
+ expect(post_two.file_content(include_fields: ['wp:post_id'])).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
+ end
+
+ context "the post is not published" do
+ it "reports 'published: false' in the post's frontmatter" do
+ expect(post_three.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
+ end
+ end
+
+ describe "#formatted_post_content" do
+ it "returns the content of the post it's passed" do
+ expect(post_two.formatted_post_content).to eq(" <strong>Foo</strong>")
+ end
+
+ it "returns the content as markdown" do
+ expect(post_two.formatted_post_content(body_to_markdown: true)).to eq("**Foo**")
+ end
+ end
+
describe "#valid?" do
def post(post_date: Date.new(2014,2,19), title: "Title", date_published: Date.new(2014,2,19), content: "content")
post = WP2Middleman::Post.new(double)

3 comments on commit b051e81

@mdb

@natedavisolds Thanks! I have some mixed feelings about this, namely that a Post now has the blended responsibilities of 1) providing a post interface and 2) writing a post to a markdown file. Previously, I liked that Post was simply an interface to data, and Migrator assumed the responsibilities of doing things with the data.

But I'm open to change. Maybe you see things differently? What are your thoughts?

@natedavisolds

@mdb I definitely agree about the Post class. I was think that that we are representing two types of Posts... the WPPost and the MiddlemanPost. Would that help the responsibilities?

How about using a PostMigrator to isolate the logic for a single post's transition?

@mdb

@natedavisolds I see. I suppose I conceive of Post as a Ruby interface to the Wordpress post. Given that, I think I most prefer your PostMigrator option, though I'm flexible.

...curious to hear your thoughts.

Please sign in to comment.
Something went wrong with that request. Please try again.