[typo] blosxom entry importer

Justus Pendleton justus at ryoohki.net
Thu Aug 4 16:12:30 EDT 2005


Attached is the importer I wrote to get my blosxom entries into Typo.
It is my first ruby script and largely based on the other files in
db/converters.  Blosxom has a ton of plugins and this script only
understands the few I use: meta-markup, meta-author, entries cache
(meta-creation_date), seemore.  You're on your own for comments and
flavours :)
-------------- next part --------------
#!/usr/bin/env ruby
# vim: ts=2 sw=2

require File.dirname(__FILE__) + '/../../config/environment'
require 'optparse'
require 'etc'
require 'article'
require 'category'
require 'html_engine'

class BlosxomMigrate
  attr_accessor :options

  def initialize
    self.options = {}
    self.parse_options
    self.convert_entries
  end

  def convert_entries
		blosxom_entry_extension = "txt"
		Dir.chdir(self.options[:dir])
		Find.find(".") { |filename|
			if filename =~ /(.*)\.#{blosxom_entry_extension}$/
				category = File.dirname($1[2..-1])
				entryname = File.basename($1[2..-1])

				puts("Parsing #{filename}")

				if category == '.'
					categories = nil
				else
					categories = category.split('/')
				end

				if categories
					categories.each do |cat|
						# there was a category associated with this post so add it to the DB
						Category.create('name' => cat) unless Category.find_by_name(cat)
					end
				end

				a = Article.new

				# use the blosxom filename for the permalink
				a.permalink = entryname

				uid = File.stat(filename).uid
				user = Etc.getpwuid(uid).name

				# these can be overridden by meta keywords
				# but we use these for defaults
				a.author = user
				a.created_at = File.stat(filename).mtime
				a.text_filter = 'none'

				fp = File.open(filename, "r")
				title = fp.readline
				title.chomp!
				a.title = title

				meta = true
				do_extended = false
				body = ''
				extended = ''

				fp.readlines.each { |line|
					if meta == true and line =~ /^\s*$/
						meta = false
						next
					end

					if meta == true
						case line
							when /meta\-markup: (.*)$/
								case $1
									when /textile/: a.text_filter = 'textile'
									when /markdown/: a.text_filter = 'markdown smartypants'
								end
							when /meta\-creation_date: (.*)$/
								# 23/3/2005 02:31:50
								$1 =~ /(\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)/
								time = Time.mktime($3, $2, $1, $4, $5, $6)
								a.created_at = time
							when /meta\-author: (.*)$/
								a.author = $1
							else
								puts("Unknown meta: #{line}")
						end
					else
						if line =~ /\<\!\-\- more \-\-\>/
							do_extended = true
							next
						end

						if do_extended == true
							extended << line	
						else
							body << line
						end
					end
				}

				a.body = body

				if extended != ''
					a.extended = extended
				end

				a.save

				# make sure we add with categories
				if categories
					categories.each do |cat|
						a.categories.push_with_attributes(Category.find_by_name(cat), :is_primary => 1)
					end
				end

				# a.comments.create (author,email,url,body,created_at,updated_at)
				# a.trackbacks.create (title,excerpt,url,ip,blog_name,created_at,updated_at)

			end

		}
	end

  def parse_options
    OptionParser.new do |opt|
      opt.banner = 'Usage: blosxom.rb [options]'

      opt.on('-d', '--dir DIR', 'blosxom datadir to import') do |d|
        self.options[:dir] = d
      end

      opt.on_tail('-h', '--help', 'Show this message.') do
        puts opt
        exit
      end

      opt.parse!(ARGV)
    end

    unless self.options.include?(:dir)
      puts 'See blosxom.rb --help for help.'
      exit
    end
  end
end

BlosxomMigrate.new


More information about the Typo-list mailing list