forked from clstokes/wordpress-octopress-migration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordpress-octopress-migration.rb
66 lines (49 loc) · 2.13 KB
/
wordpress-octopress-migration.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
require 'fileutils'
require 'date'
require 'yaml'
require 'rexml/document'
require 'net/http'
include REXML
CURRENT_IMG_HOST = "cameronstokes.com"
doc = Document.new File.new(ARGV[0])
FileUtils.mkdir_p "_posts"
doc.elements.each("rss/channel/item[wp:status = 'publish' and wp:post_type = 'post']") do |e|
post = e.elements
slug = post['wp:post_name'].text
date = DateTime.parse(post['wp:post_date'].text)
name = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day, slug]
category = post[ "category[not(@domain)]" ].text
puts "Converting: #{name}"
puts "- Category: #{category}"
content = post['content:encoded'].text
content = content.gsub(/<code>(.*?)<\/code>/m, "``` \n \\1 \n```")
content = content.gsub(/<pre class="brush: ([^"]*)">(.*?)<\/pre>/m, "``` \\1 \n \\2 \n```")
content = content.gsub(/<pre lang="([^"]*)">(.*?)<\/pre>/m, '')
content = content.gsub(/\[caption[^\]]+\](.*?)\[\/caption\]/m, "\\1")
content.scan( /<img class="[^"]*" title="[^"]*" src="([^"]*)" alt="[^"]*" width="[^"]*" height="[^"]*" \/>/m, ).each do |w|
img = w[0].scan( /http\:\/\/(#{CURRENT_IMG_HOST})\/((.*)\/.*)/m )
puts "- Downloading image: #{img[0][0]}/#{img[0][1]}"
FileUtils.mkdir_p "#{img[0][2]}"
Net::HTTP.start( img[0][0] ) do |http|
resp = http.get( "\/#{img[0][1]}" )
open("#{img[0][1]}", "wb") do |file|
file.write(resp.body)
end
end
end
content = content.gsub(/<a href="[^"]*">[ ]*<img class="[^"]*" title="([^"]*)" src="http[s]?:\/\/#{CURRENT_IMG_HOST}([^"]*)" alt="[^"]*" width="([^"]*)" height="([^"]*)" \/>[ ]*<\/a>/m, "{% img \\2 \\3 \\4 \\1 %}")
(1..3).each do |i|
content = content.gsub(/<h#{i}>([^<]*)<\/h#{i}>/, ('#'*i) + ' \1')
end
data = {
'layout' => 'post',
'title' => post['title'].text,
'excerpt' => post['excerpt:encoded'].text,
'categories' => category
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml
File.open("_posts/#{name}", "w") do |f|
f.puts data
f.puts "---"
f.puts content
end
end