From cb77a5287b82349bc662a3cd9d2093eee783b991 Mon Sep 17 00:00:00 2001 From: Tom Preston-Werner Date: Fri, 18 Jun 2010 17:55:17 -0700 Subject: [PATCH] load plugins under safe mode --- History.txt | 1 + bin/jekyll | 4 ++++ lib/jekyll.rb | 2 ++ lib/jekyll/converters/identity.rb | 2 ++ lib/jekyll/converters/markdown.rb | 2 ++ lib/jekyll/converters/textile.rb | 2 ++ lib/jekyll/generators/pagination.rb | 1 + lib/jekyll/plugin.rb | 16 +++++++++++++++- lib/jekyll/site.rb | 24 +++++++++++++++++++----- 9 files changed, 48 insertions(+), 6 deletions(-) diff --git a/History.txt b/History.txt index daef64d3..2ca6f0e7 100644 --- a/History.txt +++ b/History.txt @@ -1,6 +1,7 @@ == Edge * Major Enhancements * Proper plugin system (#19, #100) + * Add safe mode so unsafe converters/generators can be added * Minor Enhancements * Inclusion/exclusion of future dated posts (#59) * Generation for a specific time (#59) diff --git a/bin/jekyll b/bin/jekyll index 37a7934c..b33d4247 100755 --- a/bin/jekyll +++ b/bin/jekyll @@ -23,6 +23,10 @@ options = {} opts = OptionParser.new do |opts| opts.banner = help + opts.on("--safe", "Safe mode (default unsafe)") do + options['safe'] = true + end + opts.on("--auto", "Auto-regenerate") do options['auto'] = true end diff --git a/lib/jekyll.rb b/lib/jekyll.rb index dfedd4cd..ad6cb62a 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -49,12 +49,14 @@ module Jekyll # Default options. Overriden by values in _config.yml or command-line opts. # (Strings rather symbols used for compatability with YAML). DEFAULTS = { + 'safe' => false, 'auto' => false, 'server' => false, 'server_port' => 4000, 'source' => '.', 'destination' => File.join('.', '_site'), + 'plugins' => File.join('.', '_plugins'), 'future' => true, 'lsi' => false, diff --git a/lib/jekyll/converters/identity.rb b/lib/jekyll/converters/identity.rb index 8d2da491..7d9628ca 100644 --- a/lib/jekyll/converters/identity.rb +++ b/lib/jekyll/converters/identity.rb @@ -1,6 +1,8 @@ module Jekyll class IdentityConverter < Converter + safe true + priority :lowest def matches(ext) diff --git a/lib/jekyll/converters/markdown.rb b/lib/jekyll/converters/markdown.rb index 6fc218d5..0210c786 100644 --- a/lib/jekyll/converters/markdown.rb +++ b/lib/jekyll/converters/markdown.rb @@ -1,6 +1,8 @@ module Jekyll class MarkdownConverter < Converter + safe true + pygments_prefix '\n' pygments_suffix '\n' diff --git a/lib/jekyll/converters/textile.rb b/lib/jekyll/converters/textile.rb index d301387b..60f2153b 100644 --- a/lib/jekyll/converters/textile.rb +++ b/lib/jekyll/converters/textile.rb @@ -1,6 +1,8 @@ module Jekyll class TextileConverter < Converter + safe true + pygments_prefix '' pygments_suffix '' diff --git a/lib/jekyll/generators/pagination.rb b/lib/jekyll/generators/pagination.rb index 83bf3e9f..59d71093 100644 --- a/lib/jekyll/generators/pagination.rb +++ b/lib/jekyll/generators/pagination.rb @@ -1,6 +1,7 @@ module Jekyll class Pagination < Generator + safe true def generate(site) site.pages.dup.each do |page| diff --git a/lib/jekyll/plugin.rb b/lib/jekyll/plugin.rb index 899a6235..208472f0 100644 --- a/lib/jekyll/plugin.rb +++ b/lib/jekyll/plugin.rb @@ -25,7 +25,7 @@ module Jekyll @subclasses ||= [] end - # Get or set the priority of this converter. When called without an + # Get or set the priority of this plugin. When called without an # argument it returns the priority. When an argument is given, it will # set the priority. # @@ -40,6 +40,20 @@ module Jekyll @priority || :normal end + # Get or set the safety of this plugin. When called without an argument + # it returns the safety. When an argument is given, it will set the + # safety. + # + # safe - The Boolean safety (default: nil). + # + # Returns the safety Boolean. + def self.safe(safe = nil) + if safe + @safe = safe + end + @safe || false + end + # Spaceship is priority [higher -> lower] # # other - The class to be compared. diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 7b5fa683..3d4137a3 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -1,9 +1,9 @@ module Jekyll class Site - attr_accessor :config, :layouts, :posts, :pages, :static_files, :categories, :exclude, - :source, :dest, :lsi, :pygments, :permalink_style, :tags, :time, - :future + attr_accessor :config, :layouts, :posts, :pages, :static_files, + :categories, :exclude, :source, :dest, :lsi, :pygments, + :permalink_style, :tags, :time, :future, :safe, :plugins attr_accessor :converters, :generators # Initialize the site @@ -13,8 +13,10 @@ module Jekyll def initialize(config) self.config = config.clone + self.safe = config['safe'] self.source = File.expand_path(config['source']) self.dest = config['destination'] + self.plugins = File.expand_path(config['plugins']) self.lsi = config['lsi'] self.pygments = config['pygments'] self.permalink_style = config['permalink'].to_sym @@ -38,11 +40,23 @@ module Jekyll def setup require 'classifier' if self.lsi - self.converters = Jekyll::Converter.subclasses.map do |c| + # If safe mode is off, load in any ruby files under the plugins + # directory. + unless self.safe + Dir[File.join(self.plugins, "**/*.rb")].each do |f| + require f + end + end + + self.converters = Jekyll::Converter.subclasses.select do |c| + !self.safe || c.safe + end.map do |c| c.new(self.config) end - self.generators = Jekyll::Generator.subclasses.map do |c| + self.generators = Jekyll::Generator.subclasses.select do |c| + !self.safe || c.safe + end.map do |c| c.new(self.config) end end