Guest User

Untitled

a guest
Jun 4th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. require 'rubygems'
  2. require 'sequel'
  3. require 'fileutils'
  4.  
  5. module Jekyll
  6. module Drupal
  7.  
  8. QUERY = "
  9. select n.nid node_id,
  10. n.title post_title,
  11. n.created post_date,
  12. nr.body post_content,
  13. nr.format format,
  14. ff.name format_name
  15. from node n, node_revisions nr, filter_formats ff
  16. where n.status = 1
  17. and n.nid = nr.nid
  18. and n.vid = nr.vid
  19. and nr.format = ff.format
  20. order by n.changed
  21. "
  22. # TODO status = 0 to drafts
  23.  
  24. def self.process(dbname, user, pass, host = 'localhost')
  25. db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host)
  26.  
  27. FileUtils.mkdir_p "_posts"
  28.  
  29. db[QUERY].each do |post|
  30. # Get required fields and construct Jekyll compatible name
  31. title = post[:post_title]
  32. slug = post[:node_id].to_s
  33. date = Time.at(post[:post_date])
  34. content = post[:post_content]
  35. format_name = post[:format_name]
  36. if format_name.downcase == "markdown"
  37. format = "markdown"
  38. else
  39. format = "html"
  40. if format_name.downcase == "filtered html" or format_name.downcase == "full html"
  41. process_filtered_html!(content)
  42. end
  43. end
  44. name = "%02d-%02d-%02d-%s.%s" % [date.year, date.month, date.day,
  45. slug, format]
  46.  
  47. # Get the relevant fields as a hash, delete empty fields and convert
  48. # to YAML for the header
  49. data = {
  50. 'layout' => 'post',
  51. 'title' => title.to_s,
  52. 'drupal_format' => format_name
  53. }.delete_if { |k,v| v.nil? || v == ''}.to_yaml
  54.  
  55. # Write out the data and content to file
  56. File.open("_posts/#{name}", "w") do |f|
  57. f.puts data
  58. f.puts "---"
  59. f.puts content
  60. end
  61. end
  62. end
  63.  
  64. def self.process_filtered_html!(content)
  65. content.gsub!(/\n/, "<br>\n")
  66. end
  67.  
  68. def self.process_source_tags!(content)
  69. content.gsub!(/<lisp>/, "{% highlight cl %}")
  70. content.gsub!(/<\/lisp>/, "{% endhighlight %}")
  71. content.gsub!(/<bash>/, "{% highlight bash %}")
  72. content.gsub!(/<\/bash>/, "{% endhighlight %}")
  73. content.gsub!(/<code language="java5">/, "{% highlight java %}")
  74. # TODO content.gsub!(/<\/code>/, "{% endhighlight %}")
  75. content.gsub!(/<code language="java">/, "{% highlight java %}")
  76. # TODO content.gsub!(/<\/code>/, "{% endhighlight %}")
  77. content.gsub!(/<python>/, "{% highlight python %}")
  78. content.gsub!(/<\/python>/, "{% endhighlight %}")
  79. end
  80. end
  81. end
  82.  
  83. # Replace the following with your database info
  84. Jekyll::Drupal.process("databasename", "user", "password", "hostname")
Add Comment
Please, Sign In to add comment