#! /usr/bin/ruby require 'rexml/document' require 'time' require 'fileutils' journal = '/home/users/aredridel/.logjam/servers/LiveJournal.com/users/aredridel/journal' outputdir = '/home/users/aredridel/web' class String def htmlentitydecode self.gsub('>', '>').gsub('<', '<').gsub('&', '&') end end class JournalEntry def initialize(subject, time, text, security, url) @subject = subject @time = time @text = text @security = security @url = url end attr_accessor :subject, :time, :text, :security, :url def to_s %{ #{title_as_html}

#{title_as_html}

#{text}

Comments.

} end def title_as_html o = time.strftime "%Y/%m/%d %H:%M" if subject o << ": #{subject}" end o end end class JournalDir < Dir class YearDir < Dir def each super do |e| je = File.join(path, e) if /^[0-9]{2}\.xml$/ =~ e monthfile = REXML::Document.new(File.read(je)) REXML::XPath.each(monthfile, '//entry') do |event| begin subject = event.elements["subject"].text rescue NoMethodError => e subject = nil end begin url = event.elements["url"].text rescue NoMethodError => e url = nil end begin security = event.elements["security"].attributes["type"] rescue NoMethodError => e security = 'public' end text = event.elements["event"].text.to_s.htmlentitydecode time = Time.parse(event.elements["time"].text.to_s) yield JournalEntry.new(subject, time, text, security, url) end end end end end def each super do |e| je = File.join(path, e) if (e[0] != ?.) and (File.directory? je) YearDir.open(je) do |d| yield d end end end end end def urlify(s) s.downcase.gsub(/[^-_a-z0-9' ]/, '').gsub(/[ _]/, '-') end JournalDir.open(journal) do |journaldir| journaldir.each do |year| year.each do |entry| puts entry.title_as_html tried = false monthdir = File.join(outputdir, "%02i" % entry.time.year, "%02i" % entry.time.month) begin entryfile = File.join(monthdir, "#{"%02i" % entry.time.day}-#{if entry.subject then urlify(entry.subject) else entry.time.strftime("%H:%M") end}.html") File.open(entryfile, 'w') do |f| f.puts(entry) end case entry.security when 'public' File.chmod(0644, entryfile) when 'friends' File.chmod 0640, entryfile when 'private' File.chmod 0600, entryfile end %x{touch -t #{entry.time.strftime("%Y%m%d%H%M.%S")} #{entryfile}} rescue Errno::ENOENT => e FileUtils.mkdir_p(monthdir) if !tried tried = true retry else raise end end end end end