Guest User

Untitled

a guest
Apr 24th, 2018
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. # Author: Toby DiPasquale <toby@cbcg.net>
  2. # Modified by: László Bácsi <lackac@lackac.hu>
  3. require 'fileutils'
  4. require 'rubygems'
  5. require 'sequel'
  6. require 'htmlentities'
  7.  
  8. begin
  9. require 'ya2yaml'
  10. $KCODE = 'u'
  11. YAML_METHOD = :ya2yaml
  12. rescue LoadError
  13. YAML_METHOD = :to_yaml
  14. end
  15.  
  16. class String
  17. def decode_entities
  18. coder = HTMLEntities.new
  19. coder.decode(self)
  20. end
  21. end
  22.  
  23. module Jekyll
  24. module Typo
  25. # this SQL *should* work for both MySQL and PostgreSQL, but I haven't
  26. # tested PostgreSQL yet (as of 2008-12-16)
  27. SQL = <<-EOS
  28. SELECT c.id id,
  29. c.type type,
  30. c.title title,
  31. COALESCE(c.permalink, c.name) slug,
  32. c.body body,
  33. c.extended extended,
  34. c.keywords tags,
  35. c.published_at date,
  36. c.state state,
  37. COALESCE(tf.name, 'html') filter
  38. FROM contents c
  39. LEFT OUTER JOIN text_filters tf
  40. ON c.text_filter_id = tf.id
  41. EOS
  42.  
  43. def self.process dbname, user, pass, host='localhost'
  44. FileUtils.mkdir_p '_posts'
  45. db = Sequel.mysql dbname, :user => user, :password => pass, :host => host
  46. db[SQL].each do |post|
  47. puts "#{post[:type]}: #{post[:title]}"
  48.  
  49. name = post[:slug].strip.tr('/', '-')
  50. if post[:type] == "Article"
  51. name = "#{post[:date].strftime("%Y-%m-%d")}-#{name}"
  52. dir = '_posts'
  53. layout = 'post'
  54. else
  55. post[:filter] = "html"
  56. dir = 'pages'
  57. layout = 'default'
  58. end
  59.  
  60. # Can have more than one text filter in this field, but we just want
  61. # the first one for this
  62. name += '.' + post[:filter].split(' ')[0]
  63.  
  64. File.open("#{dir}/#{name}", 'w') do |f|
  65. f.puts({ 'layout' => layout,
  66. 'title' => post[:title].to_s,
  67. 'created_at' => post[:date],
  68. 'tags' => post[:tags],
  69. 'typo_id' => post[:id]
  70. }.delete_if { |k, v| v.nil? || v == '' }.send(YAML_METHOD))
  71. f.puts '---'
  72. f.puts post[:body].delete("\r").decode_entities
  73. if post[:extended]
  74. f.puts post[:extended].delete("\r").decode_entities
  75. end
  76. end
  77. end
  78. end
  79.  
  80. end # module Typo
  81. end # module Jekyll
  82.  
  83. if __FILE__ == $PROGRAM_NAME
  84. unless ARGV.size < 3
  85. Jekyll::Typo.process(*ARGV)
  86. else
  87. puts "Usage: #{__FILE__} typo_db db_user db_pass [db_host]"
  88. exit 1
  89. end
  90. end
Add Comment
Please, Sign In to add comment