wp2middleman 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b67863aef362146f51783f4a7ced9cdeb3c0c2f
4
- data.tar.gz: 19c5c8e3928ca0105aeb94401d16cb7dc04793e9
3
+ metadata.gz: 6bf5c2076748c910b3515e473912c17758f0be58
4
+ data.tar.gz: 45ca770a54f40a300656253b68c0db60ad48e880
5
5
  SHA512:
6
- metadata.gz: 35557bbabfae2b1a3a018e42e2697ae94fa2212399a22ec8ecfa58af9948fec267de57c18b260709f18ede882bc692e71eff582ce9be304af32db6777fe36307
7
- data.tar.gz: f5fa2d3f54cd1814e86f9336e378635a143c5b112d221d6fe0695cc5c42505b137dfbed2915559ccd0259bcedd592454ac07cf8e6efaf87f2879439c7845de4d
6
+ metadata.gz: 1c4f3e75ba32a0912cbc858b426d87fe580c2a83cf6721acb64913b0aba589819e2fe9d3267f958930794ff8b9325fe68883e087681a564fb0b0a8a97f2cbb14
7
+ data.tar.gz: 34571e5da1978978e4e8d04b8aec6f262744d06f7b145b271e89232c987ee7ab2b90f93acab6e2af7e42a27a8c712254b22e910ae73770f028265b54a215be05
data/.rspec CHANGED
@@ -1 +1,2 @@
1
1
  --color
2
+ --format documentation
data/README.md CHANGED
@@ -1,16 +1,16 @@
1
1
  [![Build Status](https://travis-ci.org/mdb/wp2middleman.png?branch=master)](https://travis-ci.org/mdb/wp2middleman)
2
+ [![Code Climate](https://codeclimate.com/github/mdb/wp2middleman/badges/gpa.svg)](https://codeclimate.com/github/mdb/wp2middleman)
2
3
 
3
4
  # wp2middleman
4
5
 
5
- Migrate the posts contained in a Wordpress XML export file to Middleman-style markdown files.
6
+ A command line tool to help move a Wordpress blog to [Middleman](http://middlemanapp.com).
7
+
8
+ wp2middleman migrates the posts contained in a Wordpress XML export file to Middleman-style markdown files.
6
9
 
7
10
  ## Installation
8
11
 
9
12
  ```
10
- git clone http://github.com/mdb/wp2middleman
11
- cd wp2middleman
12
- bundle install
13
- rake install
13
+ gem install wp2middleman
14
14
  ```
15
15
 
16
16
  ## Commandline Usage
@@ -35,7 +35,14 @@ tags: foo, bar
35
35
  </ul>
36
36
  ```
37
37
 
38
- ### Optional parameters
38
+ ## Optional Parameters
39
+
40
+ ```
41
+ --body_to_markdown converts to markdown
42
+ --include_fields=FIELD_ONE FIELD_TWO ETC includes specific fields in frontmatter
43
+ ```
44
+
45
+ ### Convert to Markdown
39
46
 
40
47
  ```
41
48
  wp2mm some_wordpress_export.xml --body_to_markdown true
@@ -55,3 +62,28 @@ The post content in markdown or text, depending on how it was saved to Wordpress
55
62
  * list item
56
63
  * another list item
57
64
  ```
65
+
66
+ ### Include specific post fields
67
+
68
+ ```
69
+ wp2mm some_wordpress_export.xml --include_fields wp:post_id link
70
+ ```
71
+
72
+ Pulls the specified key/values out of the post xml and includes it in frontmatter:
73
+
74
+ ```
75
+ ---
76
+ title: 'Some Title'
77
+ date: YYYY-MM-DD
78
+ tags: foo, bar
79
+ wp:post_id: '280'
80
+ link: http://somewebsite.com/2012/10/some-title
81
+ ---
82
+
83
+ <p>The post content in HTML or text, depending on how it was saved to Wordpress.</p>
84
+ <ul>
85
+ <li>list item</li>
86
+ <li>another list item</li>
87
+ </ul>
88
+ ```
89
+
@@ -5,7 +5,9 @@ module WP2Middleman
5
5
  default_task :wp2mm
6
6
 
7
7
  desc "WORDPRESS XML EXPORT FILE", "Migrate Wordpress posts to Middleman-style markdown files"
8
- option :body_to_markdown
8
+ option :body_to_markdown, :type => :boolean
9
+ option :include_fields, :type => :array
10
+
9
11
  def wp2mm(wp_xml_export = nil)
10
12
  return usage unless wp_xml_export
11
13
 
@@ -14,7 +16,8 @@ module WP2Middleman
14
16
  exit 1
15
17
  end
16
18
 
17
- WP2Middleman.migrate(wp_xml_export, options[:body_to_markdown])
19
+ include_fields = options[:include_fields] || []
20
+ WP2Middleman.migrate(wp_xml_export, options[:body_to_markdown], include_fields)
18
21
 
19
22
  say "Successfully migrated #{wp_xml_export}", "\033[32m"
20
23
  end
@@ -0,0 +1,34 @@
1
+ require 'yaml'
2
+
3
+ module WP2Middleman
4
+ class Frontmatter
5
+ def initialize(post, include_fields: [])
6
+ @post = post
7
+ @include_fields = include_fields
8
+ end
9
+
10
+ def post_data
11
+ data = {
12
+ 'title' => post.title,
13
+ 'date' => post.date_published,
14
+ 'tags' => post.tags
15
+ }
16
+
17
+ data['published'] = false if !post.published?
18
+
19
+ include_fields.each do |field|
20
+ data[field] = post.field(field)
21
+ end
22
+
23
+ data
24
+ end
25
+
26
+ def to_yaml
27
+ post_data.to_yaml.strip
28
+ end
29
+
30
+ private
31
+
32
+ attr_reader :post, :include_fields
33
+ end
34
+ end
@@ -0,0 +1,71 @@
1
+ module WP2Middleman
2
+ class MiddlemanPost
3
+ def initialize(wp_post, body_to_markdown: false, include_fields: [])
4
+ @wp_post = wp_post
5
+ @body_to_markdown = body_to_markdown
6
+ @include_fields = include_fields
7
+ end
8
+
9
+ def title
10
+ wp_post.title
11
+ end
12
+
13
+ def title_for_filename
14
+ title.gsub(/[^\w\s_-]+/, '')
15
+ .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
16
+ .gsub(/\s+/, '-')
17
+ end
18
+
19
+ def filename
20
+ "#{date_published}-#{title_for_filename}"
21
+ end
22
+
23
+ def date_published
24
+ wp_post.date_published
25
+ end
26
+
27
+ def full_filename output_path
28
+ "#{output_path}#{filename}.html.markdown"
29
+ end
30
+
31
+ def file_content
32
+ <<-EOS.gsub(/^ {8}/, '')
33
+ #{frontmatter.to_yaml}
34
+ ---
35
+
36
+ #{formatted_post_content}
37
+ EOS
38
+ end
39
+
40
+ def formatted_post_content
41
+ if body_to_markdown
42
+ markdown_content
43
+ else
44
+ content
45
+ end
46
+ end
47
+
48
+ def content
49
+ wp_post.content
50
+ end
51
+
52
+ def markdown_content
53
+ html = HTMLPage.new :contents => content
54
+ html.comment do |node,_|
55
+ "#{node}"
56
+ end
57
+ html.iframe do |node,_|
58
+ "#{node}"
59
+ end
60
+ html.markdown
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :wp_post, :body_to_markdown, :include_fields
66
+
67
+ def frontmatter
68
+ @frontmatter ||= Frontmatter.new(wp_post, include_fields: include_fields)
69
+ end
70
+ end
71
+ end
@@ -2,69 +2,23 @@ require 'yaml'
2
2
 
3
3
  module WP2Middleman
4
4
  class Migrator
5
-
6
5
  attr_reader :posts
7
6
 
8
- def initialize(wp_xml_export_file, body_to_markdown: false)
9
- @body_to_markdown = body_to_markdown
10
- @posts = WP2Middleman::PostCollection.new(wp_xml_export_file).posts
7
+ def initialize(wp_xml_export_file, body_to_markdown: false, include_fields: [])
8
+ @posts = WP2Middleman::PostCollection.from_file(wp_xml_export_file)
9
+ .without_attachments
10
+ .only_valid
11
+ .to_middleman(body_to_markdown: body_to_markdown, include_fields: include_fields)
11
12
  end
12
13
 
13
14
  def migrate
14
15
  ensure_export_directory
15
16
 
16
- @posts.each do |post|
17
- write_file(post)
17
+ posts.each do |post|
18
+ File.write(post.full_filename(output_path), post.file_content)
18
19
  end
19
20
  end
20
21
 
21
- def write_file(post)
22
- if valid_post_data(post)
23
- File.open(full_filename(post), "w") do |file|
24
- file.write(file_content(post))
25
- end
26
- end
27
- end
28
-
29
- def file_content(post)
30
- yaml = frontmatter(post).to_yaml.strip
31
-
32
- <<-EOS.gsub(/^ {8}/, '')
33
- #{yaml}
34
- ---
35
-
36
- #{formatted_post_content(post)}
37
- EOS
38
- end
39
-
40
- def frontmatter(post)
41
- data = {
42
- 'title' => post.title,
43
- 'date' => post.date_published,
44
- 'tags' => post.tags
45
- }
46
-
47
- data['published'] = false if !post.published?
48
-
49
- data
50
- end
51
-
52
- def formatted_post_content(post)
53
- if @body_to_markdown
54
- post.markdown_content
55
- else
56
- post.content
57
- end
58
- end
59
-
60
- def full_filename(post)
61
- "#{output_path}#{post.filename}.html.markdown"
62
- end
63
-
64
- def valid_post_data(post)
65
- !(post.post_date.nil? || post.title.nil? || post.date_published.nil? || post.content.nil?)
66
- end
67
-
68
22
  def output_path
69
23
  "#{Dir.pwd}/export/"
70
24
  end
@@ -12,14 +12,16 @@ module WP2Middleman
12
12
  post.css('title').text
13
13
  end
14
14
 
15
- def title_for_filename
16
- title.gsub(/[^\w\s_-]+/, '')
17
- .gsub(/(^|\b\s)\s+($|\s?\b)/, '\\1\\2')
18
- .gsub(/\s+/, '-')
15
+ def valid?
16
+ !(post_date.nil? || title.nil? || date_published.nil? || content.nil?)
19
17
  end
20
18
 
21
- def filename
22
- "#{date_published}-#{title_for_filename}"
19
+ def attachment?
20
+ type == 'attachment'
21
+ end
22
+
23
+ def field(field)
24
+ post.xpath(field).first.inner_text
23
25
  end
24
26
 
25
27
  def post_date
@@ -46,17 +48,6 @@ module WP2Middleman
46
48
  post.at_xpath(".//content:encoded").inner_text
47
49
  end
48
50
 
49
- def markdown_content
50
- html = HTMLPage.new :contents => content
51
- html.comment do |node,_|
52
- "#{node}"
53
- end
54
- html.iframe do |node,_|
55
- "#{node}"
56
- end
57
- html.markdown
58
- end
59
-
60
51
  def tags
61
52
  tags = []
62
53
  categories = post.xpath("category")
@@ -2,14 +2,44 @@ require 'nokogiri'
2
2
 
3
3
  module WP2Middleman
4
4
  class PostCollection
5
- def initialize(wp_xml_export_file)
6
- @xml = Nokogiri::XML(File.open("#{Dir.pwd}/#{wp_xml_export_file}"))
5
+ include Enumerable
6
+
7
+ def self.from_file(wp_xml_export_file)
8
+ xml = Nokogiri::XML(File.open("#{Dir.pwd}/#{wp_xml_export_file}"))
9
+ new xml.css('item').collect { |raw_wp_post| WP2Middleman::Post.new(raw_wp_post) }
10
+ end
11
+
12
+ def initialize(posts=[])
13
+ @posts = posts
14
+ end
15
+
16
+ def each(&block)
17
+ posts.each &block
18
+ end
19
+
20
+ def [] key
21
+ posts[key]
7
22
  end
8
23
 
9
- def posts
10
- @xml.css('item')
11
- .map { |post| WP2Middleman::Post.new(post) }
12
- .reject { |post| post.type == 'attachment' }
24
+ def empty?
25
+ posts.empty?
13
26
  end
27
+
28
+ def without_attachments
29
+ self.class.new(reject(&:attachment?))
30
+ end
31
+
32
+ def only_valid
33
+ self.class.new(select(&:valid?))
34
+ end
35
+
36
+ def to_middleman(body_to_markdown: false, include_fields: [])
37
+ middleman_posts = collect { |p| WP2Middleman::MiddlemanPost.new(p, body_to_markdown: body_to_markdown, include_fields: include_fields) }
38
+ self.class.new(middleman_posts)
39
+ end
40
+
41
+ private
42
+
43
+ attr_reader :posts
14
44
  end
15
45
  end
@@ -1,3 +1,3 @@
1
1
  module WP2Middleman
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/wp2middleman.rb CHANGED
@@ -1,12 +1,16 @@
1
1
  require 'wp2middleman/version'
2
2
  require 'wp2middleman/post'
3
+ require 'wp2middleman/middleman_post'
3
4
  require 'wp2middleman/post_collection'
5
+ require 'wp2middleman/frontmatter'
4
6
  require 'wp2middleman/migrator'
5
7
  require 'wp2middleman/cli'
6
8
 
7
9
  module WP2Middleman
8
- def self.migrate(wp_xml_export_file, body_to_markdown = false)
9
- migrator = WP2Middleman::Migrator.new wp_xml_export_file, body_to_markdown: body_to_markdown
10
+ def self.migrate(wp_xml_export_file, body_to_markdown = false, include_fields = [])
11
+ migrator = WP2Middleman::Migrator.new wp_xml_export_file,
12
+ body_to_markdown: body_to_markdown,
13
+ include_fields: include_fields
10
14
  migrator.migrate
11
15
  end
12
16
  end
@@ -7,51 +7,71 @@ describe WP2Middleman::CLI do
7
7
 
8
8
  describe "#wp2mm" do
9
9
  before do
10
- cli.stub :say
10
+ allow(cli).to receive(:say)
11
11
  end
12
12
 
13
13
  context "it's not passed any arguments" do
14
14
  it "returns usage details" do
15
- cli.should_receive(:usage).exactly(1).times
15
+ expect(cli).to receive(:usage).exactly(1).times
16
16
  cli.wp2mm
17
17
  end
18
18
  end
19
19
 
20
20
  context "it's passed a Wordpress XML export file that does not exist" do
21
21
  it "reports that it was passed an invalid directory and exits with an exit code of 1" do
22
- Kernel.stub(:exit).and_return true
23
- File.stub(:file?).and_return false
24
- cli.should_receive(:error).with("foo is not a valid file")
25
- lambda { cli.wp2mm 'foo' }.should exit_with_code(1)
22
+ allow(Kernel).to receive(:exit) { true }
23
+ allow(File).to receive(:file?) { false }
24
+ expect(cli).to receive(:error).with("foo is not a valid file")
25
+
26
+ begin
27
+ cli.wp2mm('foo')
28
+ rescue SystemExit => e
29
+ expect(e.status).to eq(1)
30
+ end
26
31
  end
27
32
  end
28
33
 
29
34
  context "it's passed a valid Wordpress XML export file" do
30
35
  before :each do
31
- WP2Middleman.stub(:migrate).and_return false
32
- File.stub(:file?).and_return true
36
+ allow(WP2Middleman).to receive(:migrate) { false }
37
+ allow(File).to receive(:file?) { true }
33
38
  end
34
39
 
35
40
  it "migrates the posts listed in the XML file" do
36
- WP2Middleman.should_receive(:migrate).with "foo", nil
41
+ expect(WP2Middleman).to receive(:migrate).with "foo", nil, []
37
42
  cli.wp2mm "foo"
38
43
  end
39
44
 
40
45
  it "reports that the directory has been successfully uploaded" do
41
- cli.should_receive(:say).with("Successfully migrated foo", "\e[32m")
46
+ expect(cli).to receive(:say).with("Successfully migrated foo", "\e[32m")
42
47
  cli.wp2mm "foo"
43
48
  end
44
49
  end
50
+
51
+ context "sets include_fields" do
52
+ before :each do
53
+ allow(WP2Middleman).to receive(:migrate) { false }
54
+ allow(File).to receive(:file?) { true }
55
+ end
56
+
57
+ it "deserializes the values into an array" do
58
+ expect(WP2Middleman).to receive(:migrate).with "foo", nil, ['wp:post_id', 'guid']
59
+
60
+ capture :stdout do
61
+ WP2Middleman::CLI.start %w[wp2mm foo --include_fields=wp:post_id guid]
62
+ end
63
+ end
64
+ end
45
65
  end
46
66
 
47
67
  describe "#usage" do
48
68
  subject(:usage) { cli.usage }
49
69
 
50
70
  it "displays version info, GitHub info, and help" do
51
- cli.should_receive(:say).with('wp2middleman 0.0.1')
52
- cli.should_receive(:say).with('https://github.com/mdb/wp2middleman')
53
- cli.should_receive(:say).with("\n")
54
- cli.should_receive(:help)
71
+ expect(cli).to receive(:say).with('wp2middleman 0.0.1')
72
+ expect(cli).to receive(:say).with('https://github.com/mdb/wp2middleman')
73
+ expect(cli).to receive(:say).with("\n")
74
+ expect(cli).to receive(:help)
55
75
 
56
76
  usage
57
77
  end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe WP2Middleman::Migrator do
4
+ def post(attributes = {})
5
+ defaults = {title: "mytitle", date_published: "mydate", tags: "mytags", published?: false}
6
+ @post ||= double(defaults.merge attributes)
7
+ end
8
+
9
+ it "includes the title, date, and tags from the post" do
10
+ frontmatter = WP2Middleman::Frontmatter.new(post).post_data
11
+
12
+ expect(frontmatter["title"]).to eq("mytitle")
13
+ expect(frontmatter["date"]).to eq("mydate")
14
+ expect(frontmatter["tags"]).to eq("mytags")
15
+ end
16
+
17
+ it "sets published to false for unpublished posts" do
18
+ frontmatter = WP2Middleman::Frontmatter.new(post).post_data
19
+
20
+ expect(frontmatter["published"]).to be_falsey
21
+ end
22
+
23
+ it "sets published to nil for published posts" do
24
+ frontmatter = WP2Middleman::Frontmatter.new(post published?: true).post_data
25
+
26
+ expect(frontmatter["published"]).to be_nil
27
+ end
28
+
29
+ it "includes fields specified in include_fields" do
30
+ allow(post).to receive(:field).with("field1") { "value1" }
31
+ allow(post).to receive(:field).with("field2") { "value2" }
32
+
33
+ frontmatter = WP2Middleman::Frontmatter.new(post, include_fields: ["field1", "field2"]).post_data
34
+
35
+ expect(frontmatter["field1"]).to eq("value1")
36
+ expect(frontmatter["field2"]).to eq("value2")
37
+ end
38
+ end
@@ -0,0 +1,121 @@
1
+ require 'spec_helper'
2
+
3
+ describe WP2Middleman::MiddlemanPost do
4
+ let(:post_one) { double :post,
5
+ title: "A Title",
6
+ date_published: Date.new(2012,6,8).to_s,
7
+ content: "Paragraph one.\n\n Paragraph two.\n ",
8
+ tags: [],
9
+ published?: true
10
+ }
11
+
12
+ let(:post_two) { double :post,
13
+ title: "A second title",
14
+ date_published: Date.new(2011,7,25).to_s,
15
+ content: " <strong>Foo</strong>",
16
+ tags: ["some_tag", "another tag", "tag"],
17
+ published?: true
18
+ }
19
+
20
+ let(:post_three) { double :post,
21
+ title: "A third title: With colon",
22
+ date_published: Date.new(2011,7,26).to_s,
23
+ content: "Foo",
24
+ tags: ["some_tag", "another tag", "tag"],
25
+ published?: false
26
+ }
27
+
28
+ let(:post_with_iframe) { double :post,
29
+ title: "A fourth item with iframe and comment",
30
+ date_published: Date.new(2011,7,26).to_s,
31
+ 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>",
32
+ tags: ["some_tag", "another tag", "tag"],
33
+ published?: false
34
+ }
35
+
36
+ it "has a title formatted for a filename" do
37
+ wp_post = double :post, title: "A Title"
38
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
39
+
40
+ expect(post.title_for_filename).to eq "A-Title"
41
+ end
42
+
43
+ it "removes odd characters (like colons) from title for filename" do
44
+ wp_post = double :post, title: "A third title: With colon"
45
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
46
+
47
+ expect(post.title_for_filename).to eq "A-third-title-With-colon"
48
+ end
49
+
50
+ it "prepends the date published onto the title for the filename" do
51
+ wp_post = double :post, title: "A Title", date_published: Date.new(2012,6,8)
52
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
53
+
54
+ expect(post.filename).to eq "2012-06-08-A-Title"
55
+ end
56
+
57
+ it "returns the full filename for a Middleman-style markdown post" do
58
+ wp_post = double :post, title: "A Title", date_published: Date.new(2012,6,8)
59
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
60
+
61
+ expect(post.full_filename('/some/path/')).to eq("/some/path/2012-06-08-A-Title.html.markdown")
62
+ end
63
+
64
+ it "converts the content to markdown" do
65
+ wp_post = double :post, content: "<strong>Foo</strong>"
66
+ post = WP2Middleman::MiddlemanPost.new(wp_post)
67
+
68
+ expect(post.markdown_content).to eq "**Foo**"
69
+ end
70
+
71
+ it "has no formatting change to post body by default" do
72
+ post = WP2Middleman::MiddlemanPost.new post_one
73
+
74
+ expect(post.file_content).to eq(
75
+ "---\ntitle: A Title\ndate: '2012-06-08'\ntags: []\n---\n\nParagraph one.\n\n Paragraph two.\n \n"
76
+ )
77
+ end
78
+
79
+ it "formats the post body as markdown" do
80
+ post = WP2Middleman::MiddlemanPost.new post_two, body_to_markdown: true
81
+
82
+ expect( post.file_content ).to eq(
83
+ "---\ntitle: A second title\ndate: '2011-07-25'\ntags:\n- some_tag\n- another tag\n- tag\n---\n\n**Foo**\n"
84
+ )
85
+ end
86
+
87
+ it "ignores iframe and comment tags when converting to markdown" do
88
+ post = WP2Middleman::MiddlemanPost.new post_with_iframe, body_to_markdown: true
89
+
90
+ 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")
91
+ end
92
+
93
+ it "appends included fields in with frontmatter" do
94
+ expect(post_two).to receive(:field).with('wp:post_id').and_return('209')
95
+ post = WP2Middleman::MiddlemanPost.new post_two, include_fields: ['wp:post_id']
96
+
97
+ expect( post.file_content ).to eq(
98
+ "---\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"
99
+ )
100
+ end
101
+
102
+ it "reports 'published: false' in the post's frontmatter when post is not published" do
103
+ post = WP2Middleman::MiddlemanPost.new post_three
104
+
105
+ expect( post.file_content ).to eq(
106
+ "---\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"
107
+ )
108
+ end
109
+
110
+ it "does no formatting by default" do
111
+ post = WP2Middleman::MiddlemanPost.new post_two
112
+
113
+ expect( post.formatted_post_content ).to eq(" <strong>Foo</strong>")
114
+ end
115
+
116
+ it "does markdown formatting" do
117
+ post = WP2Middleman::MiddlemanPost.new post_two, body_to_markdown: true
118
+
119
+ expect( post.formatted_post_content ).to eq("**Foo**")
120
+ end
121
+ end
@@ -5,148 +5,67 @@ describe WP2Middleman::Migrator do
5
5
  let(:migrator) { WP2Middleman::Migrator.new(file) }
6
6
 
7
7
  it "exists as a class within the WP2Middleman module" do
8
- WP2Middleman::Migrator.class.should eq Class
8
+ expect(WP2Middleman::Migrator.class).to eq Class
9
9
  end
10
10
 
11
11
  describe "#migrate" do
12
-
13
12
  before :each do
14
- FileUtils.stub :mkdir_p
15
- File.stub :write
13
+ allow(FileUtils).to receive(:mkdir_p)
14
+ allow(File).to receive(:write)
16
15
  end
17
16
 
18
17
  it "ensures there is an export directory" do
19
- File.stub :open
20
- migrator.should_receive :ensure_export_directory
21
- migrator.migrate
22
- end
18
+ allow(File).to receive(:open)
19
+
20
+ expect(migrator).to receive :ensure_export_directory
23
21
 
24
- it "writes a middleman markdown file for each post" do
25
- migrator.should_receive(:write_file).exactly(4).times
26
22
  migrator.migrate
27
23
  end
28
- end
29
24
 
30
- describe "#write_file" do
31
- before :each do
32
- @post = migrator.posts[0]
33
- end
25
+ it "writes a middleman markdown file for each post" do
26
+ expect(File).to receive(:write).exactly(4).times
34
27
 
35
- it "ensures that the post it's passed contains valid data" do
36
- migrator.should_receive(:valid_post_data).with(@post)
37
- migrator.write_file(@post)
28
+ migrator.migrate
38
29
  end
39
30
 
40
31
  it "writes the proper markdown file" do
41
- File.should_receive(:open).with( "#{Dir.pwd}/export/2012-06-08-A-Title.html.markdown", "w")
42
- migrator.write_file(@post)
43
- end
32
+ post = migrator.posts.first
44
33
 
45
- # pending
46
- xit "writes the proper markdown file" do
47
- File.should_receive(:write).with(migrator.file_content(@post))
48
- migrator.write_file(@post)
49
- end
50
- end
51
-
52
- describe "#file_content" do
53
- it "properly formats a post as a Middleman-style post" do
54
- 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")
55
- end
56
-
57
- context "its behavior if @body_to_markdown is true" do
58
- let(:migrator) { WP2Middleman::Migrator.new(file, body_to_markdown: true) }
34
+ allow(post).to receive(:file_content) { "content" }
35
+ allow(migrator).to receive(:valid_posts) { [post] }
59
36
 
60
- it "formats the post body as markdown" do
61
- 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")
62
- end
37
+ expect(File).to receive(:write).with("#{Dir.pwd}/export/2012-06-08-A-Title.html.markdown", "content")
63
38
 
64
- it "includes iframe and comment" do
65
- 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")
66
- end
67
- end
68
-
69
- context "the post is not published" do
70
- it "reports 'published: false' in the post's frontmatter" do
71
- 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")
72
- end
73
- end
74
- end
75
-
76
- describe "#formatted_post_content" do
77
- it "returns the content of the post it's passed" do
78
- expect(migrator.formatted_post_content(migrator.posts[1])).to eq(" <strong>Foo</strong>")
79
- end
80
-
81
- context "its behavior if @body_to_markdown is true" do
82
- let(:migrator) { WP2Middleman::Migrator.new(file, body_to_markdown: true) }
83
-
84
- it "returns the content of the post it's passed as markdown" do
85
- expect(migrator.formatted_post_content(migrator.posts[1])).to eq("**Foo**")
86
- end
87
- end
88
- end
89
-
90
- describe "#full_filename" do
91
- it "returns the full filename for a Middleman-style markdown post" do
92
- expect(migrator.full_filename(migrator.posts[0])).to eq("#{Dir.pwd}/export/2012-06-08-A-Title.html.markdown")
93
- end
94
- end
95
-
96
- describe "#valid_post_data" do
97
- context "the post's #post_date, #title, #date_published, and #content are not nil" do
98
- let(:post) {
99
- double('Post',
100
- :post_date => 'post_date',
101
- :title => 'title',
102
- :date_published => 'date_published',
103
- :content => 'content'
104
- )
105
- }
106
-
107
- it "returns true" do
108
- expect(migrator.valid_post_data(post)).to eq(true)
109
- end
110
- end
111
-
112
- context "the post's #post_date, #title, #date_published, or #content is nil" do
113
- let(:post) {
114
- double('Post',
115
- :post_date => nil,
116
- :title => 'title',
117
- :date_published => 'date_published',
118
- :content => 'content'
119
- )
120
- }
121
-
122
- it "returns false" do
123
- expect(migrator.valid_post_data(post)).to eq(false)
124
- end
39
+ migrator.migrate
125
40
  end
126
41
  end
127
42
 
128
43
  describe "#output_path" do
129
44
  subject { migrator.output_path }
130
45
 
131
- it { should eq("#{Dir.pwd}/export/") }
46
+ let(:export_path) { migrator.output_path }
47
+
48
+ it "reports the proper path to the export directory" do
49
+ expect(export_path).to eq "#{Dir.pwd}/export/"
50
+ end
132
51
  end
133
52
 
134
53
  describe "#ensure_export_directory" do
135
54
  it "makes the export directory if it's not already there" do
136
- File.stub(:directory?).and_return false
55
+ allow(File).to receive(:directory?) { false }
137
56
 
138
- FileUtils.should receive(:mkdir_p).with("#{Dir.pwd}/export/")
57
+ expect(FileUtils).to receive(:mkdir_p).with("#{Dir.pwd}/export/")
139
58
 
140
59
  migrator.ensure_export_directory
141
60
  end
142
61
 
143
62
  context "the export directory is already there" do
144
63
  it "does not create it" do
145
- File.stub(:directory?).and_return true
64
+ allow(File).to receive(:directory?) { true }
146
65
 
147
66
  migrator.ensure_export_directory
148
67
 
149
- FileUtils.should_not receive(:mkdir_p).with("#{Dir.pwd}/export/")
68
+ expect(FileUtils).not_to receive(:mkdir_p).with("#{Dir.pwd}/export/")
150
69
  end
151
70
  end
152
71
  end
@@ -2,15 +2,27 @@ require 'spec_helper'
2
2
 
3
3
  describe WP2Middleman::PostCollection do
4
4
  let(:file) { 'spec/fixtures/fixture.xml' }
5
- let(:posts) { WP2Middleman::PostCollection.new(file).posts }
6
5
 
7
6
  it "exists as a class within the WP2Middleman module" do
8
- WP2Middleman::PostCollection.class.should eq Class
7
+ expect(WP2Middleman::PostCollection.class).to eq Class
9
8
  end
10
9
 
11
- describe "#title" do
12
- it "returns an array of posts" do
13
- posts.class.should eq Array
10
+ it "contains a Post object for every word press item" do
11
+ posts = WP2Middleman::PostCollection.from_file(file)
12
+
13
+ expect(posts).to_not be_empty
14
+
15
+ posts.each do |post|
16
+ expect(post).to be_a WP2Middleman::Post
14
17
  end
15
18
  end
19
+
20
+ it "can reject attachments" do
21
+ post = double :post, attachment?: false
22
+ attachment = double :post, attachment?: true
23
+
24
+ post_collection = WP2Middleman::PostCollection.new([post, attachment]).without_attachments
25
+
26
+ expect(post_collection.to_a).to eq([post])
27
+ end
16
28
  end
@@ -7,60 +7,48 @@ describe WP2Middleman::Post do
7
7
  let(:post_three) { WP2Middleman::Post.new(file.css('item')[2]) }
8
8
 
9
9
  it "exists as a class within the WP2Middleman module" do
10
- WP2Middleman::Post.class.should eq Class
10
+ expect(WP2Middleman::Post.class).to eq Class
11
11
  end
12
12
 
13
13
  describe "#title" do
14
14
  subject { post_one.title }
15
15
 
16
- it { should eq "A Title" }
17
- end
18
-
19
- describe "#title_for_filename" do
20
- subject { post_one.title_for_filename }
21
-
22
- it { should eq "A-Title" }
23
- end
24
-
25
- describe "#filename" do
26
- subject { post_one.filename }
27
-
28
- it { should eq "2012-06-08-A-Title" }
29
-
30
- context "post titles with odd characters such as colons" do
31
- subject { post_three.filename }
32
-
33
- it { should eq "2011-07-26-A-third-title-With-colon" }
34
- end
16
+ it { is_expected.to eq "A Title" }
35
17
  end
36
18
 
37
19
  describe "#post_date" do
38
20
  subject { post_one.post_date }
39
21
 
40
- it { should eq "2012-06-08 03:21:41" }
22
+ it { is_expected.to eq "2012-06-08 03:21:41" }
41
23
  end
42
24
 
43
25
  describe "#date_published" do
44
26
  subject { post_one.date_published }
45
27
 
46
- it { should eq "2012-06-08" }
28
+ it { is_expected.to eq "2012-06-08" }
47
29
  end
48
30
 
49
31
  describe "#status" do
50
32
  subject { post_three.status }
51
33
 
52
- it { should eq "private" }
34
+ it { is_expected.to eq "private" }
35
+ end
36
+
37
+ describe "#field" do
38
+ subject { post_one.field('wp:post_id') }
39
+
40
+ it { is_expected.to eq "84" }
53
41
  end
54
42
 
55
43
  describe "#published?" do
56
44
  subject { post_one.published? }
57
45
 
58
- it { should eq true }
46
+ it { is_expected.to eq true }
59
47
 
60
48
  context "#status is not 'publish'" do
61
49
  subject { post_three.published? }
62
50
 
63
- it { should eq false }
51
+ it { is_expected.to eq false }
64
52
 
65
53
  end
66
54
  end
@@ -68,24 +56,51 @@ describe WP2Middleman::Post do
68
56
  describe "#content" do
69
57
  subject { post_one.content }
70
58
 
71
- it { should eq "Paragraph one.\n\n Paragraph two.\n " }
72
- end
73
-
74
- describe "#markdown_content" do
75
- subject { post_two.markdown_content }
76
-
77
- it { should eq "**Foo**" }
59
+ it { is_expected.to eq "Paragraph one.\n\n Paragraph two.\n " }
78
60
  end
79
61
 
80
62
  describe "#tags" do
81
63
  subject { post_two.tags }
82
64
 
83
- it { should eq ["some_tag", "another tag", "tag"] }
65
+ it { is_expected.to eq ["some_tag", "another tag", "tag"] }
84
66
 
85
67
  context "the post only has an 'Uncategorized' tag" do
86
68
  subject { post_one.tags }
87
69
 
88
- it { should eq [] }
70
+ it { is_expected.to eq [] }
71
+ end
72
+ end
73
+
74
+ describe "#valid?" do
75
+ def post(post_date: Date.new(2014,2,19), title: "Title", date_published: Date.new(2014,2,19), content: "content")
76
+ post = WP2Middleman::Post.new(double)
77
+
78
+ allow(post).to receive(:post_date) { post_date }
79
+ allow(post).to receive(:title) { title }
80
+ allow(post).to receive(:date_published) { date_published }
81
+ allow(post).to receive(:content) { content }
82
+
83
+ post
84
+ end
85
+
86
+ it "is valid with post_date, title, date_published, and content" do
87
+ expect(post).to be_valid
88
+ end
89
+
90
+ it "is not valid without post_date" do
91
+ expect(post(post_date: nil)).to_not be_valid
92
+ end
93
+
94
+ it "is not valid without a title" do
95
+ expect(post(title: nil)).to_not be_valid
96
+ end
97
+
98
+ it "is not valid without a date_published" do
99
+ expect(post(date_published: nil)).to_not be_valid
100
+ end
101
+
102
+ it "is not valid without content" do
103
+ expect(post(content: nil)).to_not be_valid
89
104
  end
90
105
  end
91
106
  end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe WP2Middleman do
4
4
  it "exists as a module" do
5
- WP2Middleman.class.should eq Module
5
+ expect(WP2Middleman.class).to eq Module
6
6
  end
7
7
 
8
8
  describe ".migrate" do
@@ -11,8 +11,8 @@ describe WP2Middleman do
11
11
  end
12
12
 
13
13
  it "migrates the posts in the wordpress XML export file it's passed" do
14
- WP2Middleman::Migrator.should_receive(:new).with('foo', body_to_markdown: false).and_return(@migrator_double)
15
- @migrator_double.should_receive(:migrate)
14
+ expect(WP2Middleman::Migrator).to receive(:new).with('foo', body_to_markdown: false, include_fields: []).and_return(@migrator_double)
15
+ expect(@migrator_double).to receive(:migrate)
16
16
 
17
17
  WP2Middleman.migrate 'foo'
18
18
  end
data/spec/spec_helper.rb CHANGED
@@ -10,5 +10,5 @@ SimpleCov.start
10
10
  require 'wp2middleman'
11
11
 
12
12
  RSpec.configure do |config|
13
- # some (optional) config here
13
+ config.include CLIHelpers
14
14
  end
@@ -0,0 +1,17 @@
1
+ $0 = "wp2mm"
2
+ ARGV.clear
3
+
4
+ module CLIHelpers
5
+ def capture(stream)
6
+ begin
7
+ stream = stream.to_s
8
+ eval "$#{stream} = StringIO.new"
9
+ yield
10
+ result = eval("$#{stream}").string
11
+ ensure
12
+ eval("$#{stream} = #{stream.upcase}")
13
+ end
14
+
15
+ result
16
+ end
17
+ end
@@ -10,12 +10,12 @@ RSpec::Matchers.define :exit_with_code do |exp_code|
10
10
  actual and actual == exp_code
11
11
  end
12
12
 
13
- failure_message_for_should do |block|
13
+ failure_message do |block|
14
14
  "expected block to call exit(#{exp_code}) but exit" +
15
15
  (actual.nil? ? " not called" : "(#{actual}) was called")
16
16
  end
17
17
 
18
- failure_message_for_should_not do |block|
18
+ failure_message_when_negated do |block|
19
19
  "expected block not to call exit(#{exp_code})"
20
20
  end
21
21
 
data/wp2middleman.gemspec CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
8
8
  spec.version = WP2Middleman::VERSION
9
9
  spec.authors = ["Mike Ball"]
10
10
  spec.email = ["mikedball@gmail.com"]
11
- spec.description = %q{Migrate a Wordpress export XML file to middleman}
12
- spec.summary = %q{Migrate your Wordpress blog posts to middleman}
11
+ spec.description = %q{Migrate a Wordpress export XML file to Middleman-style markdown files}
12
+ spec.summary = %q{Migrate Wordpress blog posts to Middleman-style markdown files}
13
13
  spec.homepage = "http://github.com/mdb/wp2middleman"
14
14
  spec.license = "MIT"
15
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wp2middleman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Ball
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-20 00:00:00.000000000 Z
11
+ date: 2015-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri
@@ -108,7 +108,7 @@ dependencies:
108
108
  - - '>='
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
- description: Migrate a Wordpress export XML file to middleman
111
+ description: Migrate a Wordpress export XML file to Middleman-style markdown files
112
112
  email:
113
113
  - mikedball@gmail.com
114
114
  executables:
@@ -127,17 +127,22 @@ files:
127
127
  - bin/wp2mm
128
128
  - lib/wp2middleman.rb
129
129
  - lib/wp2middleman/cli.rb
130
+ - lib/wp2middleman/frontmatter.rb
131
+ - lib/wp2middleman/middleman_post.rb
130
132
  - lib/wp2middleman/migrator.rb
131
133
  - lib/wp2middleman/post.rb
132
134
  - lib/wp2middleman/post_collection.rb
133
135
  - lib/wp2middleman/version.rb
134
136
  - spec/fixtures/fixture.xml
135
137
  - spec/lib/wp2middleman/cli_spec.rb
138
+ - spec/lib/wp2middleman/frontmatter_spec.rb
139
+ - spec/lib/wp2middleman/middleman_post_spec.rb
136
140
  - spec/lib/wp2middleman/migrator_spec.rb
137
141
  - spec/lib/wp2middleman/post_collection_spec.rb
138
142
  - spec/lib/wp2middleman/post_spec.rb
139
143
  - spec/lib/wp2middleman_spec.rb
140
144
  - spec/spec_helper.rb
145
+ - spec/support/cli_helpers.rb
141
146
  - spec/support/matchers/exit_with_code.rb
142
147
  - wp2middleman.gemspec
143
148
  homepage: http://github.com/mdb/wp2middleman
@@ -160,17 +165,19 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
165
  version: '0'
161
166
  requirements: []
162
167
  rubyforge_project:
163
- rubygems_version: 2.0.6
168
+ rubygems_version: 2.2.2
164
169
  signing_key:
165
170
  specification_version: 4
166
- summary: Migrate your Wordpress blog posts to middleman
171
+ summary: Migrate Wordpress blog posts to Middleman-style markdown files
167
172
  test_files:
168
173
  - spec/fixtures/fixture.xml
169
174
  - spec/lib/wp2middleman/cli_spec.rb
175
+ - spec/lib/wp2middleman/frontmatter_spec.rb
176
+ - spec/lib/wp2middleman/middleman_post_spec.rb
170
177
  - spec/lib/wp2middleman/migrator_spec.rb
171
178
  - spec/lib/wp2middleman/post_collection_spec.rb
172
179
  - spec/lib/wp2middleman/post_spec.rb
173
180
  - spec/lib/wp2middleman_spec.rb
174
181
  - spec/spec_helper.rb
182
+ - spec/support/cli_helpers.rb
175
183
  - spec/support/matchers/exit_with_code.rb
176
- has_rdoc: