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

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

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:
RAILS_ENV=production ./script/wp_export.rb > out.wxr


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

#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../config/boot'
require 'environment'

require 'application'

# we need a controller to generate html output
class FakeController < ApplicationController
  include ActionView::Helpers::TextHelper
  include ActionView::Helpers::TagHelper
end
fake_controller = FakeController.new

articles = Article.find(:all)

xml = Builder::XmlMarkup.new(:target => STDOUT, :indent => 2)

xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.rss 'version' => "2.0",
        'xmlns:content' => "http://purl.org/rss/1.0/modules/content/",
        'xmlns:wfw' => "http://wellformedweb.org/CommentAPI/",
        'xmlns:dc' => "http://purl.org/dc/elements/1.1/",
        'xmlns:wp' => "http://wordpress.org/export/1.0/" do
  xml.channel do
    xml.title "The Robot Co-op"
    xml.link "http://www.robotcoop.com"
    xml.language "en-us"
    xml.ttl "40"
    xml.description "Robot coop blog"

    articles.each do |a|
        xml.item do
          xml.title a.title
          xml.content(:encoded) { |x| x << a.html(fake_controller, :all) }
          xml.pubDate a.published_at.rfc2822
          xml.guid "urn:uuid:{a.guid}", "isPermaLink" => "false"
          author = a.user.name rescue a.author
          xml.author author
          xml.dc :creator, author
          for category in a.categories
            xml.category category.name
          end
          for tag in a.tags
            xml.category tag.display_name
          end
          xml.wp :post_id, a.id
          xml.wp :post_date, a.published_at.strftime("%Y-%m-%d %H:%M:%S")
          xml.wp :comment_status, 'closed'
          xml.wp :ping_status, 'closed'
          xml.wp :post_name, a.permalink
          xml.wp :status, 'publish'
          xml.wp :post_parent, '0'
          xml.wp :post_type, 'post'
          for comment in a.comments
            xml.wp(:comment) do
              xml.wp :comment_id, comment.id
              xml.wp :comment_author, comment.author
              xml.wp :comment_author_email, comment.email
              xml.wp :comment_author_url, comment.url
              xml.wp :comment_author_IP, comment.ip
              xml.wp :comment_date, comment.published_at.strftime("%Y-%m-%d %H:%M:%S")
              xml.wp(:comment_content) { |x| x << comment.body }
              xml.wp :comment_approved, '1'
              xml.wp :comment_parent, '0'
            end
          end
       end
    end
  end
end

Typo export to WordPress WXR

If you want to move from Typo to Wordpress, you can use this to export the strange Wordpress RSS file format (comments and all).

Just add this to the XmlController :

  def wp
    @articles = Article.find( :all, :include => [:categories, :tags, :user, :blog])
  end


And then add this file (app/views/xml/wp.rxml):

// insert code here..
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"

xml.rss 'version' => "2.0", 'xmlns:content' => "http://purl.org/rss/1.0/modules/content/", 'xmlns:wfw' => "http://wellformedweb.org/CommentAPI/", 'xmlns:dc' => "http://purl.org/dc/elements/1.1/", 'xmlns:wp' => "http://wordpress.org/export/1.0/" do
  xml.channel do
    xml.title @feed_title
    xml.link @link
    xml.language "en-us"
    xml.ttl "40"
    xml.description this_blog.blog_subtitle

    @articles.each do |a|
        xml.item do
          xml.title post_title(a)
          xml.content(:encoded) { |x| x << '<![CDATA[' + a.full_html + ']]>' }
          xml.pubDate a.published_at.rfc2822
          xml.guid "urn:uuid:#{a.guid}", "isPermaLink" => "false"
          author = a.user.name rescue a.author
          xml.author author
          xml.dc :creator, author
          for category in a.categories
            xml.category category.name
          end
          for tag in a.tags
            xml.category tag.display_name
          end
          xml.wp :post_id, a.id
          xml.wp :post_date, a.published_at.strftime("%Y-%m-%d %H:%M:%S")
          xml.wp :comment_status, 'closed'
          xml.wp :ping_status, 'closed'
          xml.wp :post_name, a.permalink
          xml.wp :status, 'publish'
          xml.wp :post_parent, '0'
          xml.wp :post_type, 'post'
          for comment in a.comments
            xml.wp(:comment) do
              xml.wp :comment_id, comment.id
              xml.wp :comment_author, comment.author
              xml.wp :comment_author_email, comment.email
              xml.wp :comment_author_url, comment.url
              xml.wp :comment_author_IP, comment.ip
              xml.wp :comment_author_date, comment.published_at.strftime("%Y-%m-%d %H:%M:%S")
              xml.wp(:comment_content) { |x| x << comment.full_html }
              xml.wp :comment_approved, '1'
              xml.wp :comment_parent, '0'
            end
          end
        end
    end
end


Then hit your typo blog at http://myblog/xml/wp and save the file to import through the WordPress interface.

measure map tags for typo

See my associated blog post.

# /app/views/articles/_article.rhtml
<script type="text/javascript"><!--
if(!mmposts){var mmposts=[];}mmposts[mmposts.length]="<%= article.id %>";
//--></script>
<!-- mmp mmid:<%= article.id %> mmdate:<%= article.created_at.to_s(:db) %> mmurl:<%=h article_url(article, true) %> mmtitle:<%=h article.title %> -->

# /app/views/articles/_comment.rhtml
<script type="text/javascript"><!--
if(!mmcomments){var mmcomments=[];}mmcomments[mmcomments.length]="<%= comment.id %>";
//--></script>
<!-- mmc mmid:<%= comment.id %> mmdate:<%= comment.created_at.to_s(:db) %> mmauthor:<%=h comment.author %> -->

# /app/views/articles/read.rhtml
# This is the same as the _article.rhtml snippet, but using the @article instance var instead of the article local var
<script type="text/javascript"><!--
if(!mmposts){var mmposts=[];}mmposts[mmposts.length]="<%= @article.id %>";
//--></script>
<!-- mmp mmid:<%= @article.id %> mmdate:<%= @article.created_at.to_s(:db) %> mmurl:<%=h article_url(@article, true) %> mmtitle:<%=h @article.title %> -->
</div>


There's also a javascript include to add to the bottom of your typo theme, but there's no customization required...

redirecting wordpress xml links to typo in lighttpd

Update: go for the simplest regex possible...

$HTTP["host"] =~ "awesomerailsapp.com" {
  server.error-handler-404 = "/dispatch.fcgi"
  server.document-root = "/awesomerailsapp/public/"
  url.redirect = (
    "^/feed/atom" => "http://awesomerailsapp.com/xml/atom/feed.xml",
    "^/feed" => "http://awesomerailsapp.com/xml/rss/feed.xml",
    "^/wp-rss2\.php" => "http://awesomerailsapp.com/xml/rss/feed.xml",
    "^/wp-atom\.php" => "http://awesomerailsapp.com/xml/atom/feed.xml",
    # my wp article format to typo's
    "^/(\d{4})/(\d{2})/(\d{2})/([\w-_]+)" => "/articles/$1/$2/$3/$4"
  )

  # bring on the rest of the config
}

Typo under lighttpd

Example vhost of a typo install under lighttpd

$HTTP["host"] =~ "typo.jasonhoffman.org" {
server.document-root = "/home/jah/apps/typo/trunk/public/"
server.error-handler-404   = "/dispatch.fcgi"
server.indexfiles           = ( "dispatch.fcgi")
server.errorlog = "/home/jah/logs/error.typo.log"
  fastcgi.server = ( ".fcgi" =>
     ( "localhost" =>
       ( "socket" => "/home/jah/tmp/typo-jah.socket",
                            "min-procs" => 1,
                            "max-procs" => 5,
         "bin-path" => "/home/jah/apps/typo/trunk/public/dispatch.fcgi",
         "bin-environment" => ( "RAILS_ENV" => "production" )
  )))
}

Pretty archive URLs in Typo with Routes

Tobi posted this as a new URL scheme for Typo.

# allow neat perma urls
map.connect 'articles/:year/:month/:day', :controller  => 'articles', 
     :action => 'find_by_date', 
     :year => /\d{4}/, :day => nil, :month => nil
map.connect 'articles/:year/:month/:day/:title', :controller  => 'articles', 
     :action => 'permalink', :year => /\d{4}/
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS