Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 5 commits
  • 4 files changed
  • 0 commit comments
  • 3 contributors
View
1  lib/wp2middleman.rb
@@ -1,6 +1,7 @@
require 'wp2middleman/version'
require 'wp2middleman/post'
require 'wp2middleman/post_collection'
+require 'wp2middleman/frontmatter'
require 'wp2middleman/migrator'
require 'wp2middleman/cli'
View
34 lib/wp2middleman/frontmatter.rb
@@ -0,0 +1,34 @@
+require 'yaml'
+
+module WP2Middleman
+ class Frontmatter
+ def initialize(post, include_fields: [])
+ @post = post
+ @include_fields = include_fields
+ end
+
+ def post_data
+ data = {
+ 'title' => post.title,
+ 'date' => post.date_published,
+ 'tags' => post.tags
+ }
+
+ data['published'] = false if !post.published?
+
+ include_fields.each do |field|
+ data[field] = post.field(field)
+ end
+
+ data
+ end
+
+ def to_yaml
+ post_data.to_yaml.strip
+ end
+
+ private
+
+ attr_reader :post, :include_fields
+ end
+end
View
20 lib/wp2middleman/migrator.rb
@@ -26,32 +26,16 @@ def write_file(post)
end
def file_content(post)
- yaml = frontmatter(post).to_yaml.strip
+ frontmatter = Frontmatter.new(post, include_fields: @include_fields)
<<-EOS.gsub(/^ {8}/, '')
- #{yaml}
+ #{frontmatter.to_yaml}
---
#{formatted_post_content(post)}
EOS
end
- def frontmatter(post)
- data = {
- 'title' => post.title,
- 'date' => post.date_published,
- 'tags' => post.tags
- }
-
- data['published'] = false if !post.published?
-
- @include_fields.each do |field|
- data[field] = post.field(field)
- end
-
- data
- end
-
def formatted_post_content(post)
if @body_to_markdown
post.markdown_content
View
38 spec/lib/wp2middleman/frontmatter_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe WP2Middleman::Migrator do
+ def post(attributes = {})
+ defaults = {title: "mytitle", date_published: "mydate", tags: "mytags", published?: false}
+ @post ||= double(defaults.merge attributes)
+ end
+
+ it "includes the title, date, and tags from the post" do
+ frontmatter = WP2Middleman::Frontmatter.new(post).post_data
+
+ expect(frontmatter["title"]).to eq("mytitle")
+ expect(frontmatter["date"]).to eq("mydate")
+ expect(frontmatter["tags"]).to eq("mytags")
+ end
+
+ it "sets published to false for unpublished posts" do
+ frontmatter = WP2Middleman::Frontmatter.new(post).post_data
+
+ expect(frontmatter["published"]).to be_false
+ end
+
+ it "sets published to nil for published posts" do
+ frontmatter = WP2Middleman::Frontmatter.new(post published?: true).post_data
+
+ expect(frontmatter["published"]).to be_nil
+ end
+
+ it "includes fields specified in include_fields" do
+ allow(post).to receive(:field).with("field1") { "value1" }
+ allow(post).to receive(:field).with("field2") { "value2" }
+
+ frontmatter = WP2Middleman::Frontmatter.new(post, include_fields: ["field1", "field2"]).post_data
+
+ expect(frontmatter["field1"]).to eq("value1")
+ expect(frontmatter["field2"]).to eq("value2")
+ end
+end

No commit comments for this range

Something went wrong with that request. Please try again.