Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
require 'rubygems'
require 'sequel'
require 'fileutils'
module Jekyll
module Drupal
QUERY = "
select n.nid node_id,
n.title post_title,
n.created post_date,
nr.body post_content,
nr.format format,
ff.name format_name
from node n, node_revisions nr, filter_formats ff
where n.status = 1
and n.nid = nr.nid
and n.vid = nr.vid
and nr.format = ff.format
order by n.changed
"
# TODO status = 0 to drafts
def self.process(dbname, user, pass, host = 'localhost')
db = Sequel.mysql(dbname, :user => user, :password => pass, :host => host)
FileUtils.mkdir_p "_posts"
db[QUERY].each do |post|
# Get required fields and construct Jekyll compatible name
title = post[:post_title]
slug = post[:node_id].to_s
date = Time.at(post[:post_date])
content = post[:post_content]
format_name = post[:format_name]
if format_name.downcase == "markdown"
format = "markdown"
else
format = "html"
if format_name.downcase == "filtered html" or format_name.downcase == "full html"
process_filtered_html!(content)
end
end
name = "%02d-%02d-%02d-%s.%s" % [date.year, date.month, date.day,
slug, format]
# Get the relevant fields as a hash, delete empty fields and convert
# to YAML for the header
data = {
'layout' => 'post',
'title' => title.to_s,
'drupal_format' => format_name
}.delete_if { |k,v| v.nil? || v == ''}.to_yaml
# Write out the data and content to file
File.open("_posts/#{name}", "w") do |f|
f.puts data
f.puts "---"
f.puts content
end
end
end
def self.process_filtered_html!(content)
content.gsub!(/\n/, "<br>\n")
end
def self.process_source_tags!(content)
content.gsub!(/<lisp>/, "{% highlight cl %}")
content.gsub!(/<\/lisp>/, "{% endhighlight %}")
content.gsub!(/<bash>/, "{% highlight bash %}")
content.gsub!(/<\/bash>/, "{% endhighlight %}")
content.gsub!(/<code language="java5">/, "{% highlight java %}")
# TODO content.gsub!(/<\/code>/, "{% endhighlight %}")
content.gsub!(/<code language="java">/, "{% highlight java %}")
# TODO content.gsub!(/<\/code>/, "{% endhighlight %}")
content.gsub!(/<python>/, "{% highlight python %}")
content.gsub!(/<\/python>/, "{% endhighlight %}")
end
end
end
# Replace the following with your database info
Jekyll::Drupal.process("databasename", "user", "password", "hostname")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment