Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Laurel Fan http://blog.gorgorg.org

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

truncate Rails development/test logs

In rails applications development.log and test.log like to grow forever, which takes up space and makes them slow to grep. If I just delete them running processes with logs open might get confused. So I can use truncate instead:

   1  
   2  truncate ~/www/*/log/*.log


Even easier, ask cron to do it for me every night:
   1  
   2  4 22 * * * * truncate -s 0k  ~/www/*/log/*.log

Typo export to Wordpress WXR (script version)

I tried to use this method here: http://snippets.dzone.com/posts/show/3264 however my typo blog was too slow and it kept timing out (which is why we're switching to wordpress anyway...). So I stole the code and hacked it up a bit so it could be run as a script.

Put this in $RAILS_ROOT/script/wp_export.rb, and run it like:
   1  
   2  RAILS_ENV=production ./script/wp_export.rb > out.wxr


Import it with the "Import Wordpress" tool in your wordpress blog's admin interface.

   1  
   2  #!/usr/bin/env ruby
   3  require File.dirname(__FILE__) + '/../config/boot'
   4  require 'environment'
   5  
   6  require 'application'
   7  
   8  # we need a controller to generate html output
   9  class FakeController < ApplicationController
  10    include ActionView::Helpers::TextHelper
  11    include ActionView::Helpers::TagHelper
  12  end
  13  fake_controller = FakeController.new
  14  
  15  articles = Article.find(:all)
  16  
  17  xml = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)
  18  
  19  xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
  20  xml.rss 'version' => "2.0",
  21          'xmlns:content' => "http://purl.org/rss/1.0/modules/content/",
  22          'xmlns:wfw' => "http://wellformedweb.org/CommentAPI/",
  23          'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
  24          'xmlns:wp' => "http://wordpress.org/export/1.0/" do
  25    xml.channel do
  26      xml.title "The Robot Co-op"
  27      xml.link "http://www.robotcoop.com"
  28      xml.language "en-us"
  29      xml.ttl "40"
  30      xml.description "Robot coop blog"
  31  
  32      articles.each do |a|
  33          xml.item do
  34            xml.title a.title
  35            xml.content(:encoded) { |x| x << a.html(fake_controller, :all) }
  36            xml.pubDate a.published_at.rfc2822
  37            xml.guid "urn:uuid:{a.guid}", "isPermaLink" => "false"
  38            author = a.user.name rescue a.author
  39            xml.author author
  40            xml.dc :creator, author
  41            for category in a.categories
  42              xml.category category.name
  43            end
  44            for tag in a.tags
  45              xml.category tag.display_name
  46            end
  47            xml.wp :post_id, a.id
  48            xml.wp :post_date, a.published_at.strftime("%Y-%m-%d %H:%M:%S")
  49            xml.wp :comment_status, 'closed'
  50            xml.wp :ping_status, 'closed'
  51            xml.wp :post_name, a.permalink
  52            xml.wp :status, 'publish'
  53            xml.wp :post_parent, '0'
  54            xml.wp :post_type, 'post'
  55            for comment in a.comments
  56              xml.wp(:comment) do
  57                xml.wp :comment_id, comment.id
  58                xml.wp :comment_author, comment.author
  59                xml.wp :comment_author_email, comment.email
  60                xml.wp :comment_author_url, comment.url
  61                xml.wp :comment_author_IP, comment.ip
  62                xml.wp :comment_date, comment.published_at.strftime("%Y-%m-%d %H:%M:%S")
  63                xml.wp(:comment_content) { |x| x << comment.body }
  64                xml.wp :comment_approved, '1'
  65                xml.wp :comment_parent, '0'
  66              end
  67            end
  68         end
  69      end
  70    end
  71  end
  72  
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS