ruby 我用来将我的Wordpress博客中的帖子导入到新的Jekyll中的脚本。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 我用来将我的Wordpress博客中的帖子导入到新的Jekyll中的脚本。相关的知识,希望对你有一定的参考价值。

%w(rubygems sequel fileutils yaml active_support/inflector).each{|g| require g}

require File.join(File.dirname(__FILE__), "downmark_it")

module WordPress
  def self.import(database, user, password, table_prefix = "wp", host = 'localhost')
    db = Sequel.mysql(database, :user => user, :password => password, :host => host, :encoding => 'utf8')

    %w(_posts _drafts images/posts/featured).each{|folder| FileUtils.mkdir_p folder}

    query = <<-EOS

      SELECT 	post_title, post_name, post_date, post_content, post_excerpt, ID, guid, post_status, post_type, post_status, 
      		 (	SELECT 	guid 
      			FROM 	#{table_prefix}_posts
      			WHERE 	ID = (	SELECT 	meta_value 
      							FROM	#{table_prefix}_postmeta
      							WHERE 	post_id = post.ID AND meta_key = "_thumbnail_id") ) AS post_image

      FROM #{table_prefix}_posts AS post
      WHERE  post_type = 'post'

    EOS
    
    categories_and_tags_query = <<-EOS

      SELECT 		  t.taxonomy, term.name, term.slug
      FROM 		    #{table_prefix}_term_relationships AS tr
      INNER JOIN	#{table_prefix}_term_taxonomy AS t ON t.term_taxonomy_id = tr.term_taxonomy_id
      INNER JOIN 	#{table_prefix}_terms AS term ON term.term_id = t.term_id
      WHERE		    tr.object_id = %d
      ORDER BY	  tr.term_order
    
    EOS

    db[query].each do |post|
      title      = post[:post_title]
      slug       = post[:post_name]
      date       = post[:post_date]
      content    = DownmarkIt.to_markdown post[:post_content]
      status     = post[:post_status]
      name       = "%02d-%02d-%02d-%s.markdown" % [date.year, date.month, date.day, slug]
      image      = File.basename(post[:post_image]) rescue ""
      categories = []
      post_tags  = []
      
      puts title
      
      `wget -O "images/posts/featured/#{image}" "#{post[:post_image]}"` unless File::exists?("images/posts/featured/#{image}") || post[:post_image].nil?
      
      db[categories_and_tags_query % post[:ID]].each do |category_or_tag|
        eval(category_or_tag[:taxonomy].pluralize) << { 
          "title"    => category_or_tag[:name], 
          "slug"     => category_or_tag[:slug],
          "autoslug" => category_or_tag[:name].downcase.gsub(" ", "-")
        }
      end

      data = {
         'layout'        => 'post',
         'title'         => title.to_s,
         'excerpt'       => post[:post_excerpt].to_s,
         'image'         => image,
         'wordpress_id'  => post[:ID],
         'wordpress_url' => post[:guid],
         'categories'    => categories,
         'tags'          => post_tags
       }.delete_if { |k,v| v.nil? || v == ''}.to_yaml

      File.open("#{status == 'publish' ? '_posts' : '_drafts'}/#{name}", "w") do |f|
        f.puts data
        f.puts "---"
        f.puts content
      end
    end
  end
end

以上是关于ruby 我用来将我的Wordpress博客中的帖子导入到新的Jekyll中的脚本。的主要内容,如果未能解决你的问题,请参考以下文章

Wordpress 链接将我引导至 wamp 服务器配置页面(本地主机)

localhost中的Wordpress空白屏幕[关闭]

如何将我的运输信息添加到 WooCommerce (Wordpress) 中的“谢谢”页面

如何将 wordpress 博客迁移到 AWS 弹性 beantalk

建立数据库连接 Amazon EC2 时出错 - 移动我的 wordpress 博客后

将我的wordpress网站移动到主域名会导致一些错误