Skip to content

Instantly share code, notes, and snippets.

@kejadlen
Created July 28, 2012 02:42
Show Gist options
  • Save kejadlen/3191500 to your computer and use it in GitHub Desktop.
Save kejadlen/3191500 to your computer and use it in GitHub Desktop.
task :create_month_index, :year, :month do |t,args|
args.with_defaults(:year => Time.now.strftime('%Y'), :month => Time.now.strftime('%m'))
year = args.year
month = args.month
# ... snip ...
Webby::Builder.create(fn, :from => tmpl,
:locals => {:title => month, :directory => dir, :dirty => ("#{year}.#{month}" == Time.now.strftime('%Y.%m'))})
end
end
task :create_year_index, :year do |t,args|
args.with_defaults(:year => Time.now.strftime('%Y'))
year = args.year
# ... snip ...
Webby::Builder.create(fn, :from => tmpl,
:locals => {:title => year, :directory => dir, :dirty => (year == Time.now.strftime('%Y'))})
end
end
desc "Import blog posts"
task :import_posts, :dbfile do |t,args|
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:dbfile => args.dbfile
)
class Content < ActiveRecord::Base; end
class Page < Content; end
class Article < Content; end
Article.find(:all, :conditions => { :published => true }).each do |post|
published_at = post.published_at
page = ::Webby::Resources.basename(post.title).to_url
title = post.title
dir = "#{Webby.site.blog_dir}/#{published_at.strftime('%Y/%m/%d')}"
::Webby.site.args = OpenStruct.new(:raw => [title], :page => page, :title => title, :dir => '')
year = published_at.strftime('%Y')
month = published_at.strftime('%m')
Rake::Task['blog:create_year_index'].execute(Rake::TaskArguments.new([:year], [year]))
Rake::Task['blog:create_month_index'].execute(Rake::TaskArguments.new([:year, :month], [year, month]))
page = File.join(dir, File.basename(page))
# Colons in the title isn't correct YAML
title = "\"#{title}\"" if title =~ /:/
body = post.body
# Convert the Ultraviolet textfilter blocks
body.gsub!(/<typo:ultraviolet(.*?)>/, '<%% uv\1 do -%>')
body.gsub!('</typo:ultraviolet>', '<%% end -%>')
body.gsub!(/<%% uv (.*) do -%>/) do
s = $1.split(' ').map {|i| i.gsub(/(.*)="(.*)[/) { ](#$1) => '#$2'" }}.join(', ')
s.sub!('linenumber', 'line_numbers')
"<%% uv #{s} do -%>"
end
# Remove extra carriage returns from the SQLite3 string.
body.gsub!("\r", '')
Webby::Builder.create(page,
:from => File.join(Webby.site.template_dir, 'blog', 'post.erb'),
:locals => {
:title => title,
:directory => dir,
:body => body,
:created_at => published_at.to_y })
end
end
title: <%= title %>
created_at: <%= Time.now.to_y %>
filter: erb
<%= 'dirty: true' if dirty %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment