Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved the migration logic out of Post and into MiddlemanPost
- Loading branch information
1 parent
abb9224
commit 0c5c2e8
Showing
7 changed files
with
200 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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\">"YOU DO IT FOR ME" 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.