Skip to content

Commit

Permalink
move converters to classes
Browse files Browse the repository at this point in the history
  • Loading branch information
krisb committed Feb 27, 2010
1 parent ff3ca30 commit cedda3a
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 90 deletions.
3 changes: 3 additions & 0 deletions lib/jekyll.rb
Expand Up @@ -27,6 +27,9 @@
require 'jekyll/tags/include'
require 'jekyll/albino'
require 'jekyll/static_file'
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.
Expand Down
27 changes: 27 additions & 0 deletions lib/jekyll/converters/identity.rb
@@ -0,0 +1,27 @@
module Jekyll

class IdentityConverter

def initialize(config = {})

end

def content_type
nil
end

def matches(ext)
true
end

def output_ext(ext)
ext
end

def convert(content)
content
end

end

end
69 changes: 69 additions & 0 deletions lib/jekyll/converters/markdown.rb
@@ -0,0 +1,69 @@
module Jekyll

class MarkdownConverter

def initialize(config = {})
# Set the Markdown interpreter (and Maruku self.config, if necessary)
case config['markdown']
when 'rdiscount'
begin
require 'rdiscount'

def convert(content)
RDiscount.new(content).to_html
end

rescue LoadError
puts 'You must have the rdiscount gem installed first'
end
when 'maruku'
begin
require 'maruku'

def convert(content)
Maruku.new(content).to_html
end

if config['maruku']['use_divs']
require 'maruku/ext/div'
puts 'Maruku: Using extended syntax for div elements.'
end

if config['maruku']['use_tex']
require 'maruku/ext/math'
puts "Maruku: Using LaTeX extension. Images in `#{config['maruku']['png_dir']}`."

# Switch off MathML output
MaRuKu::Globals[:html_math_output_mathml] = false
MaRuKu::Globals[:html_math_engine] = 'none'

# Turn on math to PNG support with blahtex
# Resulting PNGs stored in `images/latex`
MaRuKu::Globals[:html_math_output_png] = true
MaRuKu::Globals[:html_png_engine] = config['maruku']['png_engine']
MaRuKu::Globals[:html_png_dir] = config['maruku']['png_dir']
MaRuKu::Globals[:html_png_url] = config['maruku']['png_url']
end
rescue LoadError
puts "The maruku gem is required for markdown support!"
end
else
raise "Invalid Markdown processor: '#{config['markdown']}' -- did you mean 'maruku' or 'rdiscount'?"
end
end

def content_type
"markdown"
end

def matches(ext)
ext =~ /(markdown|mkdn?|md)/i
end

def output_ext(ext)
".html"
end

end

end
27 changes: 27 additions & 0 deletions lib/jekyll/converters/textile.rb
@@ -0,0 +1,27 @@
module Jekyll

class TextileConverter

def initialize(config = {})

end

def content_type
"textile"
end

def matches(ext)
ext =~ /textile/i
end

def output_ext(ext)
".html"
end

def convert(content)
RedCloth.new(content).to_html
end

end

end
34 changes: 10 additions & 24 deletions lib/jekyll/convertible.rb
Expand Up @@ -22,54 +22,40 @@ def to_s
# Returns nothing
def read_yaml(base, name)
self.content = File.read(File.join(base, name))

if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)/m
self.content = self.content[($1.size + $2.size)..-1]

self.data = YAML.load($1)
end

self.data ||= {}
end

# Transform the contents based on the content type.
#
# Returns nothing
def transform
case self.content_type
when 'textile'
self.content = self.site.textile(self.content)
when 'markdown'
self.content = self.site.markdown(self.content)
end
self.content = converter.convert(self.content)
end

# Determine the extension depending on content_type
#
# Returns the extensions for the output file
def output_ext
case self.content_type
when 'textile'
".html"
when 'markdown'
".html"
else
self.ext
end
converter.output_ext(self.ext)
end

# Determine which formatting engine to use based on this convertible's
# extension
#
# Returns one of :textile, :markdown or :unknown
def content_type
case self.ext[1..-1]
when /textile/i
return 'textile'
when /markdown/i, /mkdn/i, /md/i, /mkd/i
return 'markdown'
end
return 'unknown'
converter.content_type
end

def converter
@converter ||= self.site.converters.find { |c| c.matches(self.ext) }
end

# Add any necessary layouts to this convertible document
Expand Down
3 changes: 2 additions & 1 deletion lib/jekyll/post.rb
Expand Up @@ -124,7 +124,8 @@ def url
"month" => date.strftime("%m"),
"day" => date.strftime("%d"),
"title" => CGI.escape(slug),
"categories" => categories.join('/')
"categories" => categories.join('/'),
"output_ext" => self.output_ext
}.inject(template) { |result, token|
result.gsub(/:#{token.first}/, token.last)
}.gsub(/\/\//, "/")
Expand Down
57 changes: 6 additions & 51 deletions lib/jekyll/site.rb
Expand Up @@ -3,7 +3,7 @@ module Jekyll
class Site
attr_accessor :config, :layouts, :posts, :pages, :static_files, :categories, :exclude,
:source, :dest, :lsi, :pygments, :permalink_style, :tags, :time,
:future
:future, :converters

# Initialize the site
# +config+ is a Hash containing site configurations details
Expand All @@ -19,6 +19,7 @@ def initialize(config)
self.permalink_style = config['permalink'].to_sym
self.exclude = config['exclude'] || []
self.future = config['future']
self.converters = []

self.reset
self.setup
Expand All @@ -38,57 +39,11 @@ def setup
# Check to see if LSI is enabled.
require 'classifier' if self.lsi

# Set the Markdown interpreter (and Maruku self.config, if necessary)
case self.config['markdown']
when 'rdiscount'
begin
require 'rdiscount'
# converters
converters << Jekyll::MarkdownConverter.new(self.config)
converters << Jekyll::TextileConverter.new(self.config)
converters << Jekyll::IdentityConverter.new(self.config)

def markdown(content)
RDiscount.new(content).to_html
end

rescue LoadError
puts 'You must have the rdiscount gem installed first'
end
when 'maruku'
begin
require 'maruku'

def markdown(content)
Maruku.new(content).to_html
end

if self.config['maruku']['use_divs']
require 'maruku/ext/div'
puts 'Maruku: Using extended syntax for div elements.'
end

if self.config['maruku']['use_tex']
require 'maruku/ext/math'
puts "Maruku: Using LaTeX extension. Images in `#{self.config['maruku']['png_dir']}`."

# Switch off MathML output
MaRuKu::Globals[:html_math_output_mathml] = false
MaRuKu::Globals[:html_math_engine] = 'none'

# Turn on math to PNG support with blahtex
# Resulting PNGs stored in `images/latex`
MaRuKu::Globals[:html_math_output_png] = true
MaRuKu::Globals[:html_png_engine] = self.config['maruku']['png_engine']
MaRuKu::Globals[:html_png_dir] = self.config['maruku']['png_dir']
MaRuKu::Globals[:html_png_url] = self.config['maruku']['png_url']
end
rescue LoadError
puts "The maruku gem is required for markdown support!"
end
else
raise "Invalid Markdown processor: '#{self.config['markdown']}' -- did you mean 'maruku' or 'rdiscount'?"
end
end

def textile(content)
RedCloth.new(content).to_html
end

# Do the actual work of processing the site and generating the
Expand Down
19 changes: 5 additions & 14 deletions test/test_tags.rb
Expand Up @@ -2,26 +2,17 @@

class TestTags < Test::Unit::TestCase

def create_post(content, override = {}, markdown = true)
def create_post(content, override = {}, content_type = "markdown")
stub(Jekyll).configuration do
Jekyll::DEFAULTS.merge({'pygments' => true}).merge(override)
end
site = Site.new(Jekyll.configuration)
info = { :filters => [Jekyll::Filters], :registers => { :site => site } }

if markdown
payload = {"content_type" => "markdown"}
else
payload = {"content_type" => "textile"}
end
payload = {"content_type" => content_type}
@converter = site.converters.find { |c| c.content_type == content_type }

@result = Liquid::Template.parse(content).render(payload, info)

if markdown
@result = site.markdown(@result)
else
@result = site.textile(@result)
end
@result = @converter.convert(@result)
end

def fill_post(code, override = {})
Expand Down Expand Up @@ -82,7 +73,7 @@ def fill_post(code, override = {})

context "using Textile" do
setup do
create_post(@content, {}, false)
create_post(@content, {}, "textile")
end

# Broken in RedCloth 4.1.9
Expand Down

0 comments on commit cedda3a

Please sign in to comment.