add special cased markdown implementation
Charlie Somerville charlie@charliesomerville.com
Wed, 04 Dec 2013 11:25:39 +1100
2 files changed,
58 insertions(+),
20 deletions(-)
A
lib/github/markup/markdown.rb
@@ -0,0 +1,56 @@
+require "github/markup/implementation" + +module GitHub + module Markup + class Markdown < Implementation + MARKDOWN_GEMS = { + "github/markdown" => proc { |content| + GitHub::Markdown.render(content) + }, + "redcarpet" => proc { |content| + RedcarpetCompat.new(content).to_html + }, + "rdiscount" => proc { |content| + RDiscount.new(content).to_html + }, + "maruku" => proc { |content| + Maruku.new(content).to_html + }, + "kramdown" => proc { |content| + Kramdown::Document.new(content).to_html + }, + "bluecloth" => proc { |content| + BlueCloth.new(content).to_html + }, + } + + def initialize + super(/md|mkdn?|mdwn|mdown|markdown|litcoffee/) + end + + def load + return if @renderer + MARKDOWN_GEMS.each do |gem_name, renderer| + if try_require(gem_name) + @renderer = renderer + return + end + end + raise LoadError, "no suitable markdown gem found" + end + + def render(content) + load + @renderer.call(content) + end + + private + def try_require(file) + require file + true + rescue LoadError + false + end + end + end +end
M
lib/github/markups.rb
→
lib/github/markups.rb
@@ -1,24 +1,6 @@
-MD_FILES = /md|mkdn?|mdwn|mdown|markdown|litcoffee/ +require "github/markup/markdown" -if markup('github/markdown', MD_FILES, eager: true) do |content| - GitHub::Markdown.render(content) - end -elsif markup(:redcarpet, MD_FILES, eager: true) do |content| - RedcarpetCompat.new(content).to_html - end -elsif markup(:rdiscount, MD_FILES, eager: true) do |content| - RDiscount.new(content).to_html - end -elsif markup(:maruku, MD_FILES, eager: true) do |content| - Maruku.new(content).to_html - end -elsif markup(:kramdown, MD_FILES, eager: true) do |content| - Kramdown::Document.new(content).to_html - end -elsif markup(:bluecloth, MD_FILES, eager: true) do |content| - BlueCloth.new(content).to_html - end -end +@@markups << GitHub::Markup::Markdown.new markup(:redcloth, /textile/) do |content| RedCloth.new(content).to_html