Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: ggarron on Aug 13th, 2011  |  syntax: Rails  |  size: 3.19 KB  |  views: 290  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'rubygems'
  2. require 'sequel'
  3. require 'fileutils'
  4. require 'yaml'
  5.  
  6. # NOTE: This converter requires Sequel and the MySQL gems.
  7. # The MySQL gem can be difficult to install on OS X. Once you have MySQL
  8. # installed, running the following commands should work:
  9. # $ sudo gem install sequel
  10. # $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
  11.  
  12. module Jekyll
  13.   module Drupal
  14.     # Reads a MySQL database via Sequel and creates a post file for each post
  15.     # in wp_posts that has post_status = 'publish'. This restriction is made
  16.     # because 'draft' posts are not guaranteed to have valid dates.
  17.     QUERY = "SELECT node.nid, \
  18. node.title, \
  19. node_revisions.body, \
  20. node.created, \
  21. node.status, \
  22. url_alias.dst \
  23. FROM node, \
  24. node_revisions, \
  25. url_alias \
  26. WHERE (node.type = 'blog' OR node.type = 'story') \
  27. AND node.vid = node_revisions.vid \
  28. AND url_alias.src = concat('node/',node.nid)"
  29.  
  30.     def self.process(dbname, user, pass, host = 'localhost')
  31.       db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8')
  32.  
  33.       FileUtils.mkdir_p "_posts"
  34.       FileUtils.mkdir_p "_drafts"
  35.  
  36.       # Create the refresh layout
  37.       # Change the refresh url if you customized your permalink config
  38.       File.open("_layouts/refresh.html", "w") do |f|
  39.         f.puts <<EOF
  40. <!DOCTYPE html>
  41. <html>
  42. <head>
  43. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  44. <meta http-equiv="refresh" content="0;url={{ page.refresh_to_post_id }}" />
  45. </head>
  46. </html>
  47. EOF
  48.       end
  49.  
  50.       db[QUERY].each do |post|
  51.         # Get required fields and construct Jekyll compatible name
  52.         node_id = post[:nid]
  53.         title = post[:title]
  54.         content = post[:body]
  55.         created = post[:created]
  56.         time = Time.at(created)
  57.         is_published = post[:status] == 1
  58.         url = post[:dst]
  59.         dir = is_published ? "_posts" : "_drafts"
  60.         slug = title.strip.downcase.gsub(/(&|&amp;)/, ' and ').gsub(/[\s\.\/\\]/, '-').gsub(/[^\w-]/, '').gsub(/[-_]{2,}/, '-').gsub(/^[-_]/, '').gsub(/[-_]$/, '')
  61.         name = time.strftime("%Y-%m-%d-") + slug + '.md'
  62.  
  63.         # Get the relevant fields as a hash, delete empty fields and convert
  64.         # to YAML for the header
  65.         data = {
  66.            'permalink' => url,
  67.            'layout' => 'post',
  68.            'title' => title.to_s,
  69.            'created' => created,
  70.          }.delete_if { |k,v| v.nil? || v == ''}.to_yaml
  71.  
  72.         # Write out the data and content to file
  73.         File.open("#{dir}/#{name}", "w") do |f|
  74.           f.puts data
  75.           f.puts "---"
  76.           f.puts content
  77.         end
  78.  
  79.         # Make a file to redirect from the old Drupal URL
  80.         if is_published
  81.           FileUtils.mkdir_p "node/#{node_id}"
  82.           File.open("node/#{node_id}/index.md", "w") do |f|
  83.             f.puts "---"
  84.             f.puts "layout: refresh"
  85.             f.puts "refresh_to_post_id: /#{url}"
  86.             f.puts "---"
  87.           end
  88.         end
  89.       end
  90.  
  91.       # TODO: Make dirs & files for nodes of type 'page'
  92.         # Make refresh pages for these as well
  93.  
  94.       # TODO: Make refresh dirs & files according to entries in url_alias table
  95.     end
  96.   end
  97. end