Class: WP2Middleman::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/wp2middleman/migrator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wp_xml_export_file, body_to_markdown: false) ⇒ Migrator

Returns a new instance of Migrator.



8
9
10
11
# File 'lib/wp2middleman/migrator.rb', line 8

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
end

Instance Attribute Details

#postsObject (readonly)

Returns the value of attribute posts.



6
7
8
# File 'lib/wp2middleman/migrator.rb', line 6

def posts
  @posts
end

Instance Method Details

#ensure_export_directoryObject



72
73
74
75
76
# File 'lib/wp2middleman/migrator.rb', line 72

def ensure_export_directory
  unless File.directory? output_path
    FileUtils.mkdir_p output_path
  end
end

#file_content(post) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/wp2middleman/migrator.rb', line 29

def file_content(post)
  yaml = frontmatter(post).to_yaml.strip

  "    \#{yaml}\n    ---\n\n    \#{formatted_post_content(post)}\n  EOS\nend\n".gsub(/^ {8}/, '')

#formatted_post_content(post) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/wp2middleman/migrator.rb', line 52

def formatted_post_content(post)
  if @body_to_markdown
    post.markdown_content
  else
    post.content
  end
end

#frontmatter(post) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/wp2middleman/migrator.rb', line 40

def frontmatter(post)
  data = {
    'title' => post.title,
    'date' => post.date_published,
    'tags' => post.tags
  }

  data['published'] = false if !post.published?

  data
end

#full_filename(post) ⇒ Object



60
61
62
# File 'lib/wp2middleman/migrator.rb', line 60

def full_filename(post)
  "#{output_path}#{post.filename}.html.markdown"
end

#migrateObject



13
14
15
16
17
18
19
# File 'lib/wp2middleman/migrator.rb', line 13

def migrate
  ensure_export_directory

  @posts.each do |post|
    write_file(post)
  end
end

#output_pathObject



68
69
70
# File 'lib/wp2middleman/migrator.rb', line 68

def output_path
  "#{Dir.pwd}/export/"
end

#valid_post_data(post) ⇒ Object



64
65
66
# File 'lib/wp2middleman/migrator.rb', line 64

def valid_post_data(post)
  !(post..nil? || post.title.nil? || post.date_published.nil? || post.content.nil?)
end

#write_file(post) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/wp2middleman/migrator.rb', line 21

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