From 315f4c9222ef0a6af5362751e897cda1ec1fcb10 Mon Sep 17 00:00:00 2001 From: Kris Brown Date: Sat, 27 Feb 2010 23:31:54 +0000 Subject: [PATCH] allow converters to be registered through subclassing much like railties in rails --- lib/jekyll.rb | 3 +- lib/jekyll/converter.rb | 50 +++++++++++++++++++++++++++++++ lib/jekyll/converters/identity.rb | 9 ++---- lib/jekyll/converters/markdown.rb | 8 +---- lib/jekyll/converters/textile.rb | 8 +---- lib/jekyll/site.rb | 9 ++---- 6 files changed, 58 insertions(+), 29 deletions(-) create mode 100644 lib/jekyll/converter.rb diff --git a/lib/jekyll.rb b/lib/jekyll.rb index 7938031d..53a9b84b 100644 --- a/lib/jekyll.rb +++ b/lib/jekyll.rb @@ -27,9 +27,10 @@ require 'jekyll/tags/highlight' require 'jekyll/tags/include' require 'jekyll/albino' require 'jekyll/static_file' +require 'jekyll/converter' +require 'jekyll/converters/identity' require 'jekyll/converters/markdown' require 'jekyll/converters/textile' -require 'jekyll/converters/identity' module Jekyll # Default options. Overriden by values in _config.yml or command-line opts. diff --git a/lib/jekyll/converter.rb b/lib/jekyll/converter.rb new file mode 100644 index 00000000..50965be1 --- /dev/null +++ b/lib/jekyll/converter.rb @@ -0,0 +1,50 @@ +module Jekyll + + class Converter + + PRIORITIES = { :lowest => -100, + :low => -10, + :normal => 0, + :high => 10, + :highest => 100 } + + class << self + def subclasses + @subclasses ||= [] + end + + def inherited(base) + subclasses << base + subclasses.sort! + end + + # priority order of this converter + def priority(priority = nil) + if priority && PRIORITIES.has_key?(priority) + @priority = priority + end + @priority || :normal + end + + # priority order of this converter + def content_type(content_type = nil) + @content_type = content_type if content_type + @content_type || self.name.downcase.gsub(/^.*::/, '').gsub(/converter$/, '') + end + + # Spaceship is priority [higher -> lower] + # + # Returns -1, 0, 1 + def <=>(other) + cmp = PRIORITIES[other.priority] <=> PRIORITIES[self.priority] + return cmp + end + end + + def content_type + self.class.content_type + end + + end + +end \ No newline at end of file diff --git a/lib/jekyll/converters/identity.rb b/lib/jekyll/converters/identity.rb index b21dda7b..ad21bb7b 100644 --- a/lib/jekyll/converters/identity.rb +++ b/lib/jekyll/converters/identity.rb @@ -1,15 +1,11 @@ module Jekyll - - class IdentityConverter + class IdentityConverter < Converter + priority :lowest def initialize(config = {}) end - def content_type - nil - end - def matches(ext) true end @@ -23,5 +19,4 @@ module Jekyll end end - end diff --git a/lib/jekyll/converters/markdown.rb b/lib/jekyll/converters/markdown.rb index 3c1aeb50..c7cdbbd5 100644 --- a/lib/jekyll/converters/markdown.rb +++ b/lib/jekyll/converters/markdown.rb @@ -1,6 +1,5 @@ module Jekyll - - class MarkdownConverter + class MarkdownConverter < Converter def initialize(config = {}) # Set the Markdown interpreter (and Maruku self.config, if necessary) @@ -52,10 +51,6 @@ module Jekyll end end - def content_type - "markdown" - end - def matches(ext) ext =~ /(markdown|mkdn?|md)/i end @@ -65,5 +60,4 @@ module Jekyll end end - end diff --git a/lib/jekyll/converters/textile.rb b/lib/jekyll/converters/textile.rb index 50d28276..79eb5476 100644 --- a/lib/jekyll/converters/textile.rb +++ b/lib/jekyll/converters/textile.rb @@ -1,15 +1,10 @@ module Jekyll - - class TextileConverter + class TextileConverter < Converter def initialize(config = {}) end - def content_type - "textile" - end - def matches(ext) ext =~ /textile/i end @@ -23,5 +18,4 @@ module Jekyll end end - end diff --git a/lib/jekyll/site.rb b/lib/jekyll/site.rb index 17243296..df7bc612 100644 --- a/lib/jekyll/site.rb +++ b/lib/jekyll/site.rb @@ -19,7 +19,6 @@ module Jekyll self.permalink_style = config['permalink'].to_sym self.exclude = config['exclude'] || [] self.future = config['future'] - self.converters = [] self.reset self.setup @@ -39,11 +38,7 @@ module Jekyll # Check to see if LSI is enabled. require 'classifier' if self.lsi - # converters - converters << Jekyll::MarkdownConverter.new(self.config) - converters << Jekyll::TextileConverter.new(self.config) - converters << Jekyll::IdentityConverter.new(self.config) - + self.converters = Jekyll::Converter.subclasses.collect { |c| c.new(self.config) } end # Do the actual work of processing the site and generating the @@ -135,7 +130,7 @@ module Jekyll end end - # Reads the directories and finds posts, pages and static files that will + # Reads the directories and finds posts, pages and static files that will # become part of the valid site according to the rules in +filter_entries+. # The +dir+ String is a relative path used to call this method # recursively as it descends through directories