Get a text backup of your Typo

After using Typo for some time now, I wanted a raw backup of my many posts. So I used some Ruby to extract of that information. First I wanted a raw text file of my content.

$ script/runner -e production "Content.find(:all, :conditions => 'type = \"Article\"'). \
each {|i| puts '##########'; puts i.title; puts ''; puts i.body; puts '' }" >> ~/ta-text.txt

Next I needed some script to convert this to a separate text file fore each post. I reused a bit from my GTD and iPod Notes post. This script will take the output from the above command and create a ‘txt’ file for each post. I have stripped out the header comments for here but you can find the original version here

#!/usr/bin/env ruby
###############################################################################
# COPYRIGHT 2006 Eric Davis ("eric" + "at.to_sym" + "theadmin.org")
###############################################################################

if ARGV.length < 1
  puts "You must enter the name of the file to split"
  exit
end

require 'fileutils'

# Make a folder split
FileUtils.mkdir_p 'split'
FileUtils.cd 'split'

# Read the todo from the origonal folder
file = IO.readlines("../#{ARGV[0]}")

# Use a tmp variable so I can skip the first close
first_run = true

# Use this to track if a new file is needed
new_file = false

f = String.new
posts = 0

file.each do |line|
  if line =~ /^[#]{10,10}$/
    f.close if not :first_run
    new_file = true
  else
    if new_file == true
      posts += 1
      new_file_name = line.strip.gsub(/ /, '_').gsub(/\//, '-').downcase + '.txt'
      f = File.new(new_file_name, "w") 
      first_run = false
      new_file = false
    end
    f.puts line
  end

end
f.close
puts "Converted #{posts} posts"

Now all you have to do is save that file somewhere as typo-split.rb and run ruby typo-split.rb output.txt and out will pop your posts.

Enjoy!

Eric Davis