From fab5a715c5c914bd3b939b7a000b410798f2a8a5 Mon Sep 17 00:00:00 2001 From: Jamie Macey Date: Sun, 29 Nov 2009 13:25:00 -0500 Subject: [PATCH] safely allow loading jekyll customizations from inside a site --- bin/jekyll | 8 ++++++++ features/site_configuration.feature | 18 ++++++++++++++++++ features/step_definitions/jekyll_steps.rb | 6 +++++- lib/jekyll.rb | 1 + lib/jekyll/site.rb | 10 +++++++++- 5 files changed, 41 insertions(+), 2 deletions(-) diff --git a/bin/jekyll b/bin/jekyll index 3e8469e1..7a31b80a 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -62,6 +62,14 @@ opts = OptionParser.new do |opts| end end + opts.on("--extensions", "Load Jekyll extensions from _lib directory") do + options['extensions'] = true + end + + opts.on("--no-extensions", "Do not load Jekyll extensions") do + options['extensions'] = false + end + opts.on("--version", "Display current version") do puts "Jekyll " + Jekyll.version exit 0 diff --git a/features/site_configuration.feature b/features/site_configuration.feature index 31b8c71e..a2b7d0ca 100644 --- a/features/site_configuration.feature +++ b/features/site_configuration.feature @@ -61,3 +61,21 @@ Feature: Site configuration When I run jekyll Then the _site directory should exist And I should see "puts 'Hello world!'" in "_site/index.html" + + Scenario: Load an extension from _lib + Given I have an "index.html" page that contains "{{ 'extension' | hello }}" + And I have a _lib directory + And I have a "_lib/hello.rb" file that contains "module Jekyll::Filters ; def hello(input) ; "hello #{input}" ; end ; end" + And I have a configuration file with "extensions" set to "true" + When I run jekyll + Then the _site directory should exist + And I should see "hello extension" in "_site/index.html" + + Scenario: Skip loading extensions from _lib + Given I have an "index.html" page that contains "{{ 'extension' | hello }}" + And I have a _lib directory + And I have a "_lib/hello.rb" file that contains "module Jekyll::Filters ; def hello(input) ; "hello #{input}" ; end ; end" + And I have a configuration file with "extensions" set to "false" + When I run jekyll + Then the _site directory should exist + And I should not see "hello extension" in "_site/index.html" diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index 3bf34736..8efdb8d6 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -25,7 +25,7 @@ EOF end end -Given /^I have an "(.*)" file that contains "(.*)"$/ do |file, text| +Given /^I have an? "(.*)" file that contains "(.*)"$/ do |file, text| File.open(file, 'w') do |f| f.write(text) f.close @@ -123,6 +123,10 @@ Then /^I should see "(.*)" in "(.*)"$/ do |text, file| assert_match Regexp.new(text), File.open(file).readlines.join end +Then /^I should not see "(.*)" in "(.*)"$/ do |text, file| + assert_no_match Regexp.new(text), File.open(file).readlines.join +end + Then /^the "(.*)" file should not exist$/ do |file| assert !File.exists?(file) end diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 8a27e161..e728a7ff 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -42,6 +42,7 @@ module Jekyll 'pygments' => false, 'markdown' => 'maruku', 'permalink' => 'date', + 'extensions' => false, 'maruku' => { 'use_tex' => false, diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index be275d88..11aa607b 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -2,7 +2,8 @@ module Jekyll class Site attr_accessor :config, :layouts, :posts, :categories, :exclude, - :source, :dest, :lsi, :pygments, :permalink_style, :tags + :source, :dest, :lsi, :pygments, :permalink_style, :tags, + :extensions # Initialize the site # +config+ is a Hash containing site configurations details @@ -17,6 +18,7 @@ module Jekyll self.pygments = config['pygments'] self.permalink_style = config['permalink'].to_sym self.exclude = config['exclude'] || [] + self.extensions = config['extensions'] self.reset self.setup @@ -80,6 +82,12 @@ module Jekyll else raise "Invalid Markdown processor: '#{self.config['markdown']}' -- did you mean 'maruku' or 'rdiscount'?" end + + # Load extensions from _lib folder + if self.extensions + # load instead of require so that jekyll --auto reloads changes + Dir["#{source}/_lib/*.rb"].each {|f| load f} + end end def textile(content)