allow converters to be registered through subclassing much like railties in rails

This commit is contained in:
Kris Brown 2010-02-27 23:31:54 +00:00
parent cedda3afa3
commit 315f4c9222
6 changed files with 58 additions and 29 deletions

View File

@ -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.

50
lib/jekyll/converter.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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