Created
April 22, 2009 09:12
-
-
Save Yasushi/99676 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Author: Toby DiPasquale <toby@cbcg.net> | |
require 'fileutils' | |
require 'rubygems' | |
require 'sequel' | |
require 'jekyll/filters' | |
module Jekyll | |
module Typo | |
class <<self | |
include Jekyll::Filters | |
end | |
def self.process dbname | |
FileUtils.mkdir_p '_posts' | |
db = Sequel.sqlite dbname | |
db[:contents].left_outer_join(:text_filters, :id => :text_filter_id).select(:contents.*,:text_filters__name.as(:filter)).each do |post| | |
p post[:id] | |
date = post[:published_at] || post[:created_at] | |
name = | |
case post[:type] | |
when "Article" | |
[ date.strftime('%Y-%m-%d'), | |
post[:permalink].strip ].join('-') | |
when "Page" | |
[ date.strftime('%Y-%m-%d'), | |
'page', | |
post[:name].gsub('/','-').strip].join('-') | |
end | |
# Can have more than one text filter in this field, but we just want | |
# the first one for this | |
name += '.' + (post[:filter] ? post[:filter].split(' ')[0] : 'html') | |
File.open("_posts/#{name}", 'w') do |f| | |
typo_attrs={} | |
[:type, :published_at, :created_at, | |
:updated_at, :keywords, :permalink, :name].each do |n| | |
typo_attrs[n.to_s]=post[n] | |
end | |
f.puts({ 'layout' => 'post', | |
'title' => post[:title].to_s, | |
'typo_id' => post[:id], | |
'published' => post[:published], | |
'typo_attrs' => typo_attrs | |
}.delete_if { |k, v| v.nil? || v == '' }.to_yaml) | |
f.puts '---' | |
f.puts post_filter(post[:body]).delete("\r") | |
f.puts '----' | |
f.puts post_filter(post[:extended]).delete("\r") if post[:extended] | |
end | |
end | |
end | |
def self.post_filter(content) | |
c=content.dup | |
c.gsub!(%r!<typo:code>(.*?)</typo:code>!m){|m| | |
"\n<pre><code>"+xml_escape(Regexp.last_match[1])+"\n</code></pre>\n" | |
} | |
c.gsub!(%r!<typo:code lang="(.*?)">(.*?)</typo:code>!m, | |
"\n{% highlight \\1 %}\n\\2\n{% endhighlight %}\n") | |
c.gsub!(%r!<typo:hide>(.*?)</typo:hide>!m, | |
"<span class=\"typo_hide\">\\1</span>") | |
c | |
end | |
end # module Typo | |
end # module Jekyll | |
Jekyll::Typo.process(ARGV[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment