all repos — markup @ d65bb7ee9be795ae607d0d666e34fd4b33be0926

The code we use to render README.your_favorite_markup

lib/github/markup/markdown.rb (view raw)

 1require "github/markup/implementation"
 2
 3module GitHub
 4  module Markup
 5    class Markdown < Implementation
 6      MARKDOWN_GEMS = {
 7        "github/markdown" => proc { |content|
 8          GitHub::Markdown.render(content)
 9        },
10        "redcarpet" => proc { |content|
11          RedcarpetCompat.new(content).to_html
12        },
13        "rdiscount" => proc { |content|
14          RDiscount.new(content).to_html
15        },
16        "maruku" => proc { |content|
17          Maruku.new(content).to_html
18        },
19        "kramdown" => proc { |content|
20          Kramdown::Document.new(content).to_html
21        },
22        "bluecloth" => proc { |content|
23          BlueCloth.new(content).to_html
24        },
25      }
26
27      def initialize
28        super(/md|mkdn?|mdwn|mdown|markdown|litcoffee/)
29      end
30
31      def load
32        return if @renderer
33        MARKDOWN_GEMS.each do |gem_name, renderer|
34          if try_require(gem_name)
35            @renderer = renderer
36            return
37          end
38        end
39        raise LoadError, "no suitable markdown gem found"
40      end
41
42      def render(content)
43        load
44        @renderer.call(content)
45      end
46
47    private
48      def try_require(file)
49        require file
50        true
51      rescue LoadError
52        false
53      end
54    end
55  end
56end