now with 100% more chunky bacon!

Posted by phillip Sat, 21 Jan 2006 12:48:00 GMT

the foxes

switched to typo

today i migrated my site from a home-brewn php/one-html-page solution to the ruby on rails powered weblog typo (which is not to be confused with typo3). what had to be done:

  • locally update ruby on rails to version 1.0 and install typo.
  • parse the existing html page with all the log entries, and feed the extracted posts into the ruby on rails database.
  • find a nice template and tweak the stylesheet here and there.
  • fix all image links to be absolute, convert the log entries to utf-8 (had to install the iconv library for ruby).
  • upload the locally prepared installation to the webserver, create and fill the online database.
  • compile ruby, ruby-iconv, ruby-fcgi, ruby-mysql, pcre and lighttpd on the webserver; then install ruby on rails.
  • configure apache to forward (proxy) all requests to lighttpd, configure lighttpd to embed ruby via fcgi
  • write a start/stop-script for lighttpd and make sure lighttpd is run on startup.

all of this took about twelve hours to complete. more than half of the time was spent preparing the webserver — finding out what was needed to get ruby and lighttpd work together over fcgi.

if you’d like to see conversion scripts, take a look at the extended content of this post.

(i am) happy rubying!

try ruby if you like – an interactive ruby shell that runs directly your web browser.

you could also visit why’s hilarious blog, or his poignant guide to ruby, where you’ll find many more cartoon foxes!

convert.rb — parse the existing index.php and insert all posts into the database:


require File.dirname(__FILE__) + "/../config/environment"
require 'script/String_utf8'

def run
  file = File.read('script/index.php')
  body = file.chomp.split('</h1>')[1].chomp.split('<script type="text/javascript" src="js/splash.js">')[0]
  posts = body.split('<h2>')
  posts.each { |post|
    parse_post(post) unless (post == posts.first)
    puts "\n----------------------------------------\n"
  }
end

def parse_post(post)  
  post = post.to_utf8
  a = Article.new
  a.author = 'phillip'
  a.allow_comments = 1
  a.allow_pings = 1
  a.user_id = 1 
  a.text_filter = 'none'
  a.title = post[/^(.+)<\/h2>/, 1] 
  date = post[/<span class="date">(.+)<\/span>/, 1]
  date = date.split('-')   
  a.created_at = Time.local(date[2], date[1], date[0], 0, 0, 0, 0)
  a.body = post.chomp.split(/<span class="date">.+<\/span>/)[1].strip
  # debug output
  a.attributes.each_pair { |name, value|
    puts name.to_s + " --> " + value.to_s
  }
  a.save
end

run

fixup.rb — after the posts had been created in the database, edit them to fix the image links. thanks to rails, that was the hard part ;-)


require File.dirname(__FILE__) + "/../config/environment"

Article.find(:all).each { |a|
  a.body = a.body.gsub('"img', '"/img')
  a.body = a.body.gsub("'img", "'/img")
  a.body = a.body.gsub('"images', '"/img')
  a.body = a.body.gsub('"../img', '"/img')
  a.save
}

utf conversion — i used some code written by clbustos to convert to utf-8 - the source can be found on his website

disclaimer: i am new to ruby plus this is use-once-then-throw-away-code; i know the ruby-fu rating is extremly low. i’m working on it.

Posted in , |