Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 67 lines (57 sloc) 2.098 kb
f798374 @Tigraine Initial version of the importer
authored
1 require 'rexml/document'
2 require 'date'
061914d @Tigraine Almost finished blog importer script
authored
3 require 'htmlentities'
4 require 'fileutils'
f798374 @Tigraine Initial version of the importer
authored
5 include REXML
6
061914d @Tigraine Almost finished blog importer script
authored
7 postsPath = "./_posts"
83ce381 @Tigraine renamed wordpress export file
authored
8 wpsource = "wordpress.xml"
061914d @Tigraine Almost finished blog importer script
authored
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
22 doc = REXML::Document.new file
23 counter = 0
061914d @Tigraine Almost finished blog importer script
authored
24 skipped = 0
f798374 @Tigraine Initial version of the importer
authored
25 doc.elements.each("*/channel/item") do |element|
26 postname = element.elements["wp:post_name"].text
061914d @Tigraine Almost finished blog importer script
authored
27 title = element.elements["title"].text
28 postid = element.elements["wp:post_id"].text
2a589b4 @Tigraine Initial posts work now
authored
29 guid = element.elements["guid"].text
061914d @Tigraine Almost finished blog importer script
authored
30 #Encoding of Post title
31 coder = HTMLEntities.new
32 title = coder.encode(title, :named)
f798374 @Tigraine Initial version of the importer
authored
33 postdate = element.elements["wp:post_date"].text
061914d @Tigraine Almost finished blog importer script
authored
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
35 next
36 end
061914d @Tigraine Almost finished blog importer script
authored
37 counter += 1
f798374 @Tigraine Initial version of the importer
authored
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
41 skipped += 1
f798374 @Tigraine Initial version of the importer
authored
42 end
43
44 postdate = postdate.strftime("%Y-%m-%d")
45
061914d @Tigraine Almost finished blog importer script
authored
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
50 targetFile.puts("guid: #{guid}")
51 targetFile.puts("postid: #{postid}")
e71af0c @Tigraine Updated importer for categories
authored
52 targetFile.puts("categories:")
f3e80fb @Tigraine Updated importer to include categories
authored
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
58 targetFile.puts("- #{category.attributes["nicename"]}")
f3e80fb @Tigraine Updated importer to include categories
authored
59 end
60
061914d @Tigraine Almost finished blog importer script
authored
61 targetFile.puts("---")
f798374 @Tigraine Initial version of the importer
authored
62 targetFile.write(element.elements["content:encoded"].text)
63 targetFile.close
64 end
061914d @Tigraine Almost finished blog importer script
authored
65
66 puts "Finished migration of #{counter} posts. Skipped #{skipped} posts"
Something went wrong with that request. Please try again.