9 'themes' => File.join(SOURCE, "_includes", "themes"),
10 'layouts' => File.join(SOURCE, "_layouts"),
11 'posts' => File.join(SOURCE, "_posts"),
13 'theme_package_version' => "0.1.0"
16 # Path configuration helper
21 :layouts => "_layouts",
22 :themes => "_includes/themes",
23 :theme_assets => "assets/themes",
24 :theme_packages => "_theme_packages",
32 # build a path relative to configured path settings.
33 def self.build(path, opts = {})
34 opts[:root] ||= SOURCE
35 path = "#{opts[:root]}/#{Paths[path.to_sym]}/#{opts[:node]}".split("/")
37 File.__send__ :join, path
43 # Usage: rake post title="A Title" [date="2012-02-09"] [tags=[tag1,tag2]] [category="category"]
44 desc "Begin a new post in #{CONFIG['posts']}"
46 abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
47 title = ENV["title"] || "new-post"
48 tags = ENV["tags"] || "[]"
49 category = ENV["category"] || ""
50 category = "\"#{category.gsub(/-/,' ')}\"" if !category.empty?
51 slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
53 date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d')
55 puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!"
58 filename = File.join(CONFIG['posts'], "#{date}-#{slug}.#{CONFIG['post_ext']}")
59 if File.exist?(filename)
60 abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
63 puts "Creating new post: #{filename}"
64 open(filename, 'w') do |post|
66 post.puts "layout: post"
67 post.puts "title: \"#{title.gsub(/-/,' ')}\""
68 post.puts 'description: ""'
69 post.puts "category: #{category}"
70 post.puts "tags: #{tags}"
72 post.puts "{% include JB/setup %}"
76 # Usage: rake page name="about.html"
77 # You can also specify a sub-directory path.
78 # If you don't specify a file extention we create an index.html at the path specified
79 desc "Create a new page."
81 name = ENV["name"] || "new-page.md"
82 filename = File.join(SOURCE, "#{name}")
83 filename = File.join(filename, "index.html") if File.extname(filename) == ""
84 title = File.basename(filename, File.extname(filename)).gsub(/[\W\_]/, " ").gsub(/\b\w/){$&.upcase}
85 if File.exist?(filename)
86 abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
89 mkdir_p File.dirname(filename)
90 puts "Creating new page: #{filename}"
91 open(filename, 'w') do |post|
93 post.puts "layout: page"
94 post.puts "title: \"#{title}\""
95 post.puts 'description: ""'
97 post.puts "{% include JB/setup %}"
101 desc "Launch preview environment"
103 system "jekyll serve -w"
106 # Public: Alias - Maintains backwards compatability for theme switching.
107 task :switch_theme => "theme:switch"
111 # Public: Switch from one theme to another for your blog.
113 # name - String, Required. name of the theme you want to switch to.
114 # The theme must be installed into your JB framework.
118 # rake theme:switch name="the-program"
120 # Returns Success/failure messages.
121 desc "Switch between Jekyll-bootstrap themes."
123 theme_name = ENV["name"].to_s
124 theme_path = File.join(CONFIG['themes'], theme_name)
125 settings_file = File.join(theme_path, "settings.yml")
126 non_layout_files = ["settings.yml"]
128 abort("rake aborted: name cannot be blank") if theme_name.empty?
129 abort("rake aborted: '#{theme_path}' directory not found.") unless FileTest.directory?(theme_path)
130 abort("rake aborted: '#{CONFIG['layouts']}' directory not found.") unless FileTest.directory?(CONFIG['layouts'])
132 Dir.glob("#{theme_path}/*") do |filename|
133 next if non_layout_files.include?(File.basename(filename).downcase)
134 puts "Generating '#{theme_name}' layout: #{File.basename(filename)}"
136 open(File.join(CONFIG['layouts'], File.basename(filename)), 'w') do |page|
138 page.puts File.read(settings_file) if File.exist?(settings_file)
139 page.puts "layout: default" unless File.basename(filename, ".html").downcase == "default"
141 page.puts "{% include JB/setup %}"
142 page.puts "{% include themes/#{theme_name}/#{File.basename(filename)} %}"
146 puts "=> Theme successfully switched!"
147 puts "=> Reload your web-page to check it out =)"
150 # Public: Install a theme using the theme packager.
151 # Version 0.1.0 simple 1:1 file matching.
153 # git - String, Optional path to the git repository of the theme to be installed.
154 # name - String, Optional name of the theme you want to install.
155 # Passing name requires that the theme package already exist.
159 # rake theme:install git="https://github.com/jekyllbootstrap/theme-twitter.git"
160 # rake theme:install name="cool-theme"
162 # Returns Success/failure messages.
166 manifest = theme_from_git_url(ENV["git"])
167 name = manifest["name"]
169 name = ENV["name"].to_s.downcase
172 packaged_theme_path = JB::Path.build(:theme_packages, :node => name)
175 => ERROR: 'name' cannot be blank") if name.empty?
177 => ERROR: '#{packaged_theme_path}' directory not found.
178 => Installable themes can be added via git. You can find some here: http://github.com/jekyllbootstrap
179 => To download+install run: `rake theme:install git='[PUBLIC-CLONE-URL]'`
180 => example : rake theme:install git='git@github.com:jekyllbootstrap/theme-the-program.git'
181 ") unless FileTest.directory?(packaged_theme_path)
183 manifest = verify_manifest(packaged_theme_path)
185 # Get relative paths to packaged theme files
186 # Exclude directories as they'll be recursively created. Exclude meta-data files.
187 packaged_theme_files = []
188 FileUtils.cd(packaged_theme_path) {
189 Dir.glob("**/*.*") { |f|
190 next if ( FileTest.directory?(f) || f =~ /^(manifest|readme|packager)/i )
191 packaged_theme_files << f
195 # Mirror each file into the framework making sure to prompt if already exists.
196 packaged_theme_files.each do |filename|
197 file_install_path = File.join(JB::Path.base, filename)
198 if File.exist? file_install_path and ask("#{file_install_path} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
201 mkdir_p File.dirname(file_install_path)
202 cp_r File.join(packaged_theme_path, filename), file_install_path
206 puts "=> #{name} theme has been installed!"
208 if ask("=> Want to switch themes now?", ['y', 'n']) == 'y'
209 system("rake switch_theme name='#{name}'")
213 # Public: Package a theme using the theme packager.
214 # The theme must be structured using valid JB API.
215 # In other words packaging is essentially the reverse of installing.
217 # name - String, Required name of the theme you want to package.
221 # rake theme:package name="twitter"
223 # Returns Success/failure messages.
226 name = ENV["name"].to_s.downcase
227 theme_path = JB::Path.build(:themes, :node => name)
228 asset_path = JB::Path.build(:theme_assets, :node => name)
230 abort("rake aborted: name cannot be blank") if name.empty?
231 abort("rake aborted: '#{theme_path}' directory not found.") unless FileTest.directory?(theme_path)
232 abort("rake aborted: '#{asset_path}' directory not found.") unless FileTest.directory?(asset_path)
234 ## Mirror theme's template directory (_includes)
235 packaged_theme_path = JB::Path.build(:themes, :root => JB::Path.build(:theme_packages, :node => name))
236 mkdir_p packaged_theme_path
237 cp_r theme_path, packaged_theme_path
239 ## Mirror theme's asset directory
240 packaged_theme_assets_path = JB::Path.build(:theme_assets, :root => JB::Path.build(:theme_packages, :node => name))
241 mkdir_p packaged_theme_assets_path
242 cp_r asset_path, packaged_theme_assets_path
244 ## Log packager version
245 packager = {"packager" => {"version" => CONFIG["theme_package_version"].to_s } }
246 open(JB::Path.build(:theme_packages, :node => "#{name}/packager.yml"), "w") do |page|
247 page.puts packager.to_yaml
250 puts "=> '#{name}' theme is packaged and available at: #{JB::Path.build(:theme_packages, :node => name)}"
253 end # end namespace :theme
255 # Internal: Download and process a theme from a git url.
256 # Notice we don't know the name of the theme until we look it up in the manifest.
257 # So we'll have to change the folder name once we get the name.
259 # url - String, Required url to git repository.
261 # Returns theme manifest hash
262 def theme_from_git_url(url)
263 tmp_path = JB::Path.build(:theme_packages, :node => "_tmp")
264 abort("rake aborted: system call to git clone failed") if !system("git clone #{url} #{tmp_path}")
265 manifest = verify_manifest(tmp_path)
266 new_path = JB::Path.build(:theme_packages, :node => manifest["name"])
267 if File.exist?(new_path) && ask("=> #{new_path} theme package already exists. Override?", ['y', 'n']) == 'n'
269 abort("rake aborted: '#{manifest["name"]}' already exists as theme package.")
272 remove_dir(new_path) if File.exist?(new_path)
273 mv(tmp_path, new_path)
277 # Internal: Process theme package manifest file.
279 # theme_path - String, Required. File path to theme package.
281 # Returns theme manifest hash
282 def verify_manifest(theme_path)
283 manifest_path = File.join(theme_path, "manifest.yml")
284 manifest_file = File.open( manifest_path )
285 abort("rake aborted: repo must contain valid manifest.yml") unless File.exist? manifest_file
286 manifest = YAML.load( manifest_file )
291 def ask(message, valid_options)
293 answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer)
295 answer = get_stdin(message)
300 def get_stdin(message)
305 #Load custom rake scripts
306 Dir['_rake/*.rake'].each { |r| load r }