lib/wp2middleman/migrator.rb in wp2middleman-0.0.1 vs lib/wp2middleman/migrator.rb in wp2middleman-0.0.2
- old
+ new
@@ -1,69 +1,23 @@
require 'yaml'
module WP2Middleman
class Migrator
-
attr_reader :posts
- def initialize(wp_xml_export_file, body_to_markdown: false)
- @body_to_markdown = body_to_markdown
- @posts = WP2Middleman::PostCollection.new(wp_xml_export_file).posts
+ def initialize(wp_xml_export_file, body_to_markdown: false, include_fields: [])
+ @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
ensure_export_directory
- @posts.each do |post|
- write_file(post)
+ posts.each do |post|
+ File.write(post.full_filename(output_path), post.file_content)
end
- end
-
- def write_file(post)
- if valid_post_data(post)
- File.open(full_filename(post), "w") do |file|
- file.write(file_content(post))
- end
- end
- end
-
- def file_content(post)
- yaml = frontmatter(post).to_yaml.strip
-
- <<-EOS.gsub(/^ {8}/, '')
- #{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?
-
- data
- end
-
- def formatted_post_content(post)
- if @body_to_markdown
- post.markdown_content
- else
- post.content
- end
- end
-
- def full_filename(post)
- "#{output_path}#{post.filename}.html.markdown"
- end
-
- def valid_post_data(post)
- !(post.post_date.nil? || post.title.nil? || post.date_published.nil? || post.content.nil?)
end
def output_path
"#{Dir.pwd}/export/"
end