Guest User

Untitled

a guest
Apr 9th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. # Quickly hacked together my Michael Ivey
  2. # Based on mt.rb by Nick Gerakines, open source and publically
  3. # available under the MIT license. Use this module at your own risk.
  4.  
  5. require 'rubygems'
  6. require 'sequel'
  7. require 'fastercsv'
  8. require 'fileutils'
  9. require File.join(File.dirname(__FILE__),"csv.rb")
  10.  
  11. # NOTE: This converter requires Sequel and the MySQL gems.
  12. # The MySQL gem can be difficult to install on OS X. Once you have MySQL
  13. # installed, running the following commands should work:
  14. # $ sudo gem install sequel
  15. # $ sudo gem install mysql -- --with-mysql-config=/usr/local/mysql/bin/mysql_config
  16.  
  17. module Jekyll
  18. module Mephisto
  19. #Accepts a hash with database config variables, exports mephisto posts into a csv
  20. #export PGPASSWORD if you must
  21. def self.postgres(c)
  22. sql = <<-SQL
  23. BEGIN;
  24. CREATE TEMP TABLE jekyll AS
  25. SELECT title, permalink, body, published_at, filter FROM contents
  26. WHERE user_id = 1 AND type = 'Article' ORDER BY published_at;
  27. COPY jekyll TO STDOUT WITH CSV HEADER;
  28. ROLLBACK;
  29. SQL
  30. command = %Q(psql -h #{c[:host] || "localhost"} -c "#{sql.strip}" #{c[:database]} #{c[:username]} -o #{c[:filename] || "posts.csv"})
  31. puts command
  32. `#{command}`
  33. CSV.process
  34. end
  35.  
  36. # This query will pull blog posts from all entries across all blogs. If
  37. # you've got unpublished, deleted or otherwise hidden posts please sift
  38. # through the created posts to make sure nothing is accidently published.
  39.  
  40. # QUERY = "SELECT id, permalink, body, published_at, title FROM contents WHERE user_id = 1 AND type = 'Article' AND published_at IS NOT NULL ORDER BY published_at"
  41.  
  42. QUERY = "SELECT contents.id, contents.permalink, contents.body, contents.published_at, contents.title, GROUP_CONCAT(tags.name) AS tags FROM taggings JOIN (contents,tags) ON (tag_id=tags.id AND taggable_id=contents.id) WHERE contents.user_id = 1 AND contents.type = 'Article' AND contents.published_at IS NOT NULL GROUP BY contents.permalink"
  43.  
  44.  
  45. def self.process(dbname, user, pass, host = 'localhost')
  46. db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host)
  47.  
  48. FileUtils.mkdir_p "_posts"
  49.  
  50. db[QUERY].each do |post|
  51. title = post[:title]
  52. slug = post[:permalink]
  53. date = post[:published_at]
  54. content = post[:body]
  55. # more_content = ''
  56.  
  57. # Be sure to include the body and extended body.
  58. # if more_content != nil
  59. # content = content + " \n" + more_content
  60. # end
  61.  
  62. # Ideally, this script would determine the post format (markdown, html
  63. # , etc) and create files with proper extensions. At this point it
  64. # just assumes that markdown will be acceptable.
  65. name = [date.year, date.month, date.day, slug].join('-') + ".markdown"
  66.  
  67. data = {
  68. 'layout' => 'post',
  69. 'title' => title.to_s,
  70. 'mt_id' => post[:entry_id],
  71. 'categories' => post[:tags].to_s.split(','),
  72. }.delete_if { |k,v| v.nil? || v == ''}.to_yaml
  73.  
  74. File.open("_posts/#{name}", "w") do |f|
  75. f.puts data
  76. f.puts "---"
  77. f.puts content
  78. end
  79. end
  80.  
  81. end
  82. end
  83. end
Add Comment
Please, Sign In to add comment