|
| 1 | +require "rubygems" |
| 2 | +require 'rake' |
| 3 | +require 'yaml' |
| 4 | +require 'time' |
| 5 | + |
| 6 | +SOURCE = "." |
| 7 | +CONFIG = { |
| 8 | + 'version' => "0.3.0", |
| 9 | + 'themes' => File.join(SOURCE, "_includes", "themes"), |
| 10 | + 'layouts' => File.join(SOURCE, "_layouts"), |
| 11 | + 'posts' => File.join(SOURCE, "_posts"), |
| 12 | + 'post_ext' => "md", |
| 13 | + 'theme_package_version' => "0.1.0" |
| 14 | +} |
| 15 | + |
| 16 | +# Path configuration helper |
| 17 | +module JB |
| 18 | + class Path |
| 19 | + SOURCE = "." |
| 20 | + Paths = { |
| 21 | + :layouts => "_layouts", |
| 22 | + :themes => "_includes/themes", |
| 23 | + :theme_assets => "assets/themes", |
| 24 | + :theme_packages => "_theme_packages", |
| 25 | + :posts => "_posts" |
| 26 | + } |
| 27 | + |
| 28 | + def self.base |
| 29 | + SOURCE |
| 30 | + end |
| 31 | + |
| 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("/") |
| 36 | + path.compact! |
| 37 | + File.__send__ :join, path |
| 38 | + end |
| 39 | + |
| 40 | + end #Path |
| 41 | +end #JB |
| 42 | + |
| 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']}" |
| 45 | +task :post do |
| 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-]/, '') |
| 52 | + begin |
| 53 | + date = (ENV['date'] ? Time.parse(ENV['date']) : Time.now).strftime('%Y-%m-%d') |
| 54 | + rescue => e |
| 55 | + puts "Error - date format must be YYYY-MM-DD, please check you typed it correctly!" |
| 56 | + exit -1 |
| 57 | + end |
| 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' |
| 61 | + end |
| 62 | + |
| 63 | + puts "Creating new post: #{filename}" |
| 64 | + open(filename, 'w') do |post| |
| 65 | + post.puts "---" |
| 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}" |
| 71 | + post.puts "---" |
| 72 | + post.puts "{% include JB/setup %}" |
| 73 | + end |
| 74 | +end # task :post |
| 75 | + |
| 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." |
| 80 | +task :page do |
| 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' |
| 87 | + end |
| 88 | + |
| 89 | + mkdir_p File.dirname(filename) |
| 90 | + puts "Creating new page: #{filename}" |
| 91 | + open(filename, 'w') do |post| |
| 92 | + post.puts "---" |
| 93 | + post.puts "layout: page" |
| 94 | + post.puts "title: \"#{title}\"" |
| 95 | + post.puts 'description: ""' |
| 96 | + post.puts "---" |
| 97 | + post.puts "{% include JB/setup %}" |
| 98 | + end |
| 99 | +end # task :page |
| 100 | + |
| 101 | +desc "Launch preview environment" |
| 102 | +task :preview do |
| 103 | + system "jekyll serve -w" |
| 104 | +end # task :preview |
| 105 | + |
| 106 | +# Public: Alias - Maintains backwards compatability for theme switching. |
| 107 | +task :switch_theme => "theme:switch" |
| 108 | + |
| 109 | +namespace :theme do |
| 110 | + |
| 111 | + # Public: Switch from one theme to another for your blog. |
| 112 | + # |
| 113 | + # name - String, Required. name of the theme you want to switch to. |
| 114 | + # The theme must be installed into your JB framework. |
| 115 | + # |
| 116 | + # Examples |
| 117 | + # |
| 118 | + # rake theme:switch name="the-program" |
| 119 | + # |
| 120 | + # Returns Success/failure messages. |
| 121 | + desc "Switch between Jekyll-bootstrap themes." |
| 122 | + task :switch do |
| 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"] |
| 127 | + |
| 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']) |
| 131 | + |
| 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)}" |
| 135 | + |
| 136 | + open(File.join(CONFIG['layouts'], File.basename(filename)), 'w') do |page| |
| 137 | + page.puts "---" |
| 138 | + page.puts File.read(settings_file) if File.exist?(settings_file) |
| 139 | + page.puts "layout: default" unless File.basename(filename, ".html").downcase == "default" |
| 140 | + page.puts "---" |
| 141 | + page.puts "{% include JB/setup %}" |
| 142 | + page.puts "{% include themes/#{theme_name}/#{File.basename(filename)} %}" |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + puts "=> Theme successfully switched!" |
| 147 | + puts "=> Reload your web-page to check it out =)" |
| 148 | + end # task :switch |
| 149 | + |
| 150 | + # Public: Install a theme using the theme packager. |
| 151 | + # Version 0.1.0 simple 1:1 file matching. |
| 152 | + # |
| 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. |
| 156 | + # |
| 157 | + # Examples |
| 158 | + # |
| 159 | + # rake theme:install git="https://github.com/jekyllbootstrap/theme-twitter.git" |
| 160 | + # rake theme:install name="cool-theme" |
| 161 | + # |
| 162 | + # Returns Success/failure messages. |
| 163 | + desc "Install theme" |
| 164 | + task :install do |
| 165 | + if ENV["git"] |
| 166 | + manifest = theme_from_git_url(ENV["git"]) |
| 167 | + name = manifest["name"] |
| 168 | + else |
| 169 | + name = ENV["name"].to_s.downcase |
| 170 | + end |
| 171 | + |
| 172 | + packaged_theme_path = JB::Path.build(:theme_packages, :node => name) |
| 173 | + |
| 174 | + abort("rake aborted! |
| 175 | + => ERROR: 'name' cannot be blank") if name.empty? |
| 176 | + abort("rake aborted! |
| 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='[email protected]:jekyllbootstrap/theme-the-program.git' |
| 181 | + ") unless FileTest.directory?(packaged_theme_path) |
| 182 | + |
| 183 | + manifest = verify_manifest(packaged_theme_path) |
| 184 | + |
| 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 |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 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' |
| 199 | + next |
| 200 | + else |
| 201 | + mkdir_p File.dirname(file_install_path) |
| 202 | + cp_r File.join(packaged_theme_path, filename), file_install_path |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + puts "=> #{name} theme has been installed!" |
| 207 | + puts "=> ---" |
| 208 | + if ask("=> Want to switch themes now?", ['y', 'n']) == 'y' |
| 209 | + system("rake switch_theme name='#{name}'") |
| 210 | + end |
| 211 | + end |
| 212 | + |
| 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. |
| 216 | + # |
| 217 | + # name - String, Required name of the theme you want to package. |
| 218 | + # |
| 219 | + # Examples |
| 220 | + # |
| 221 | + # rake theme:package name="twitter" |
| 222 | + # |
| 223 | + # Returns Success/failure messages. |
| 224 | + desc "Package theme" |
| 225 | + task :package do |
| 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) |
| 229 | + |
| 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) |
| 233 | + |
| 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 |
| 238 | + |
| 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 |
| 243 | + |
| 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 |
| 248 | + end |
| 249 | + |
| 250 | + puts "=> '#{name}' theme is packaged and available at: #{JB::Path.build(:theme_packages, :node => name)}" |
| 251 | + end |
| 252 | + |
| 253 | +end # end namespace :theme |
| 254 | + |
| 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. |
| 258 | +# |
| 259 | +# url - String, Required url to git repository. |
| 260 | +# |
| 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' |
| 268 | + remove_dir(tmp_path) |
| 269 | + abort("rake aborted: '#{manifest["name"]}' already exists as theme package.") |
| 270 | + end |
| 271 | + |
| 272 | + remove_dir(new_path) if File.exist?(new_path) |
| 273 | + mv(tmp_path, new_path) |
| 274 | + manifest |
| 275 | +end |
| 276 | + |
| 277 | +# Internal: Process theme package manifest file. |
| 278 | +# |
| 279 | +# theme_path - String, Required. File path to theme package. |
| 280 | +# |
| 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 ) |
| 287 | + manifest_file.close |
| 288 | + manifest |
| 289 | +end |
| 290 | + |
| 291 | +def ask(message, valid_options) |
| 292 | + if valid_options |
| 293 | + answer = get_stdin("#{message} #{valid_options.to_s.gsub(/"/, '').gsub(/, /,'/')} ") while !valid_options.include?(answer) |
| 294 | + else |
| 295 | + answer = get_stdin(message) |
| 296 | + end |
| 297 | + answer |
| 298 | +end |
| 299 | + |
| 300 | +def get_stdin(message) |
| 301 | + print message |
| 302 | + STDIN.gets.chomp |
| 303 | +end |
| 304 | + |
| 305 | +#Load custom rake scripts |
| 306 | +Dir['_rake/*.rake'].each { |r| load r } |
0 commit comments