Skip to content
Newer
Older
100644 67 lines (57 sloc) 2.05 KB
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
1 require 'rexml/document'
2 require 'date'
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
3 require 'htmlentities'
4 require 'fileutils'
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
5 include REXML
6
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
7 postsPath = "./_posts"
83ce381 @Tigraine renamed wordpress export file
authored Sep 29, 2010
8 wpsource = "wordpress.xml"
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
9 puts "Migrating Wordpress posts from #{wpsource} to #{postsPath}"
10
11 if (!File.exists? wpsource) then
12 puts "Cannot find file '#{wpsource}' please modify wp-source to match your Wordpress export file"
13 exit 1
14 end
15
16 if (!File.directory? postsPath) then
17 puts "Directory #{postsPath} does not exist. Creating it"
18 FileUtils.mkdir(postsPath)
19 end
20
21 file = File.new(wpsource)
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
22 doc = REXML::Document.new file
23 counter = 0
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
24 skipped = 0
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
25 doc.elements.each("*/channel/item") do |element|
26 postname = element.elements["wp:post_name"].text
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
27 title = element.elements["title"].text
28 postid = element.elements["wp:post_id"].text
2a589b4 @Tigraine Initial posts work now
authored Sep 29, 2010
29 guid = element.elements["guid"].text
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
30 #Encoding of Post title
31 coder = HTMLEntities.new
32 title = coder.encode(title, :named)
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
33 postdate = element.elements["wp:post_date"].text
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
34 if postdate == "0000-00-00 00:00:00" or element.elements["wp:post_type"].text != "post" then
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
35 next
36 end
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
37 counter += 1
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
38 postdate = DateTime.parse(postdate)
39 if postname.nil? or postdate.nil? then
40 puts "Post #{element.elements["wp:post_id"].text} is incomplete"
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
41 skipped += 1
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
42 end
43
44 postdate = postdate.strftime("%Y-%m-%d")
45
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
46 targetFile = File.open("./_posts/#{postdate}-#{postname}.html", "w:ASCII-8BIT")
47 targetFile.puts("---")
48 targetFile.puts("layout: post")
49 targetFile.puts("title: \"#{title}\"")
2a589b4 @Tigraine Initial posts work now
authored Sep 29, 2010
50 targetFile.puts("guid: #{guid}")
51 targetFile.puts("postid: #{postid}")
e71af0c @Tigraine Updated importer for categories
authored Sep 30, 2010
52 targetFile.puts("categories:")
f3e80fb @Tigraine Updated importer to include categories
authored Sep 29, 2010
53
54 element.elements.each("category") do |category|
55 if category.attributes["domain"] == nil then
56 next
57 end
e71af0c @Tigraine Updated importer for categories
authored Sep 30, 2010
58 targetFile.puts("- #{category.attributes["nicename"]}")
f3e80fb @Tigraine Updated importer to include categories
authored Sep 29, 2010
59 end
60
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
61 targetFile.puts("---")
f798374 @Tigraine Initial version of the importer
authored Sep 28, 2010
62 targetFile.write(element.elements["content:encoded"].text)
63 targetFile.close
64 end
061914d @Tigraine Almost finished blog importer script
authored Sep 29, 2010
65
66 puts "Finished migration of #{counter} posts. Skipped #{skipped} posts"
Something went wrong with that request. Please try again.