Skip to content

Instantly share code, notes, and snippets.

@ravasthi
Created February 14, 2012 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravasthi/1829476 to your computer and use it in GitHub Desktop.
Save ravasthi/1829476 to your computer and use it in GitHub Desktop.
Jekyll Rakefile modifications
require "rubygems"
require "rake"
require "fileutils"
SOURCE = "."
CONFIG = {
'themes' => File.join(SOURCE, "_includes", "themes"),
'layouts' => File.join(SOURCE, "_layouts"),
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md"
}
# Usage: rake post title="A Title"
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
mkdir_p CONFIG['posts']
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(/\s+/, '-').gsub(/[^\w-]/, '')
filename = File.join(CONFIG['posts'], "#{Time.now.strftime('%Y-%m-%d')}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts "author: #{ENV['USER']}"
post.puts "tags: []"
post.puts "css_id: "
post.puts "css_classes: [ permalink ]"
post.puts "---"
post.puts
post.puts
post.puts "<!-- more -->"
post.puts
post.puts
end
end # task :post
# Usage: rake page name="about.html"
# You can also specify a sub-directory path.
# If you don't specify a file extention we create an index.html at the path specified
desc "Create a new page."
task :page do
name = ENV["name"] || "new-page.md"
filename = File.join(SOURCE, "#{name}")
filename = File.join(filename, "index.html") if File.extname(filename) == ""
title = File.basename(filename, File.extname(filename)).gsub(/[\W\_]/, " ").gsub(/\b\w/){$&.upcase}
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
mkdir_p File.dirname(filename)
puts "Creating new page: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: page"
post.puts "title: \"#{title}\""
draft.puts "css_id: "
draft.puts "css_classes: [ permalink ]"
post.puts "---"
post.puts "{% include JB/setup %}"
end
end # task :page
desc "Switch between Jekyll-bootstrap themes."
task :switch_theme do
theme_name = ENV["name"].to_s
theme_path = File.join(CONFIG['themes'], theme_name)
settings_file = File.join(theme_path, "settings.yml")
non_layout_files = ["settings.yml"]
abort("rake aborted: name cannot be blank") if theme_name.empty?
abort("rake aborted: '#{theme_path}' directory not found.") unless FileTest.directory?(theme_path)
abort("rake aborted: '#{CONFIG['layouts']}' directory not found.") unless FileTest.directory?(CONFIG['layouts'])
Dir.glob("#{theme_path}/*") do |filename|
next if non_layout_files.include?(File.basename(filename).downcase)
puts "Generating '#{theme_name}' layout: #{File.basename(filename)}"
open(File.join(CONFIG['layouts'], File.basename(filename)), 'w') do |page|
if File.basename(filename, ".html").downcase == "default"
page.puts "---"
page.puts File.read(settings_file) if File.exist?(settings_file)
page.puts "---"
else
page.puts "---"
page.puts "layout: default"
page.puts "---"
end
page.puts "{% include JB/setup %}"
page.puts "{% include themes/#{theme_name}/#{File.basename(filename)} %}"
end
end
end # task :switch_theme
desc "Launch preview environment"
task :preview do
system "jekyll --auto --server"
end # task :preview
def ask(message, valid_options)
if valid_options
answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
else
answer = get_stdin(message)
end
answer
end
def get_stdin(message)
print message
STDIN.gets.chomp
end
require "fileutils"
SOURCE = "."
CONFIG = {
'themes' => File.join(SOURCE, "_includes", "themes"),
'layouts' => File.join(SOURCE, "_layouts"),
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md"
}
# Usage: rake post title="A Title"
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
mkdir_p CONFIG['posts']
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(/\s+/, '-').gsub(/[^\w-]/, '')
filename = File.join(CONFIG['posts'], "#{Time.now.strftime('%Y-%m-%d')}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts "author: #{ENV['USER']}"
post.puts "tags: []"
post.puts "css_id: "
post.puts "css_classes: [ permalink ]"
post.puts "---"
post.puts
post.puts
post.puts "<!-- more -->"
post.puts
post.puts
end
end # task :post
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment