Class: BuntoImport::Importers::Typo
- Inherits:
-
BuntoImport::Importer
- Object
- BuntoImport::Importer
- BuntoImport::Importers::Typo
- Defined in:
- lib/bunto-import/importers/typo.rb
Constant Summary collapse
- SQL =
This SQL should work for both MySQL and PostgreSQL.
"SELECT c.id id,\n c.title title,\n c.permalink slug,\n c.body body,\n c.extended extended,\n c.published_at date,\n c.state state,\n c.keywords keywords,\n COALESCE(tf.name, 'html') filter\n FROM contents c\n LEFT OUTER JOIN text_filters tf\n ON c.text_filter_id = tf.id\n"
Class Method Summary collapse
Methods inherited from BuntoImport::Importer
inherited, run, stringify_keys, subclasses
Class Method Details
.process(options) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/bunto-import/importers/typo.rb', line 37 def self.process() server = .fetch('server') dbname = .fetch('dbname') user = .fetch('user') pass = .fetch('password', '') host = .fetch('host', "localhost") FileUtils.mkdir_p '_posts' case server.intern when :postgres db = Sequel.postgres(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8') when :mysql db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host, :encoding => 'utf8') else raise "Unknown database server '#{server}'" end db[SQL].each do |post| next unless post[:state] =~ /published/i if post[:slug] == nil post[:slug] = "no slug" end if post[:extended] post[:body] << "\n<!-- more -->\n" post[:body] << post[:extended] end name = [ sprintf("%.04d", post[:date].year), sprintf("%.02d", post[:date].month), sprintf("%.02d", post[:date].day), post[:slug].strip ].join('-') # Can have more than one text filter in this field, but we just want # the first one for this. name += '.' + post[:filter].split(' ')[0] File.open("_posts/#{name}", 'w') do |f| f.puts({ 'layout' => 'post', 'title' => (post[:title] and post[:title].to_s.force_encoding('UTF-8')), 'tags' => (post[:keywords] and post[:keywords].to_s.force_encoding('UTF-8')), 'typo_id' => post[:id] }.delete_if { |k, v| v.nil? || v == '' }.to_yaml) f.puts '---' f.puts post[:body].delete("\r") end end end |
.require_deps ⇒ Object
20 21 22 23 24 25 26 27 |
# File 'lib/bunto-import/importers/typo.rb', line 20 def self.require_deps BuntoImport.require_with_fallback(%w[ rubygems sequel fileutils safe_yaml ]) end |
.specify_options(c) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/bunto-import/importers/typo.rb', line 29 def self.(c) c.option 'server', '--server TYPE', 'Server type ("mysql" or "postgres")' c.option 'dbname', '--dbname DB', 'Database name' c.option 'user', '--user USER', 'Database user name' c.option 'password', '--password PW', "Database user's password (default: '')" c.option 'host', '--host HOST', 'Database host name' end |