all repos — markup @ 1a659c4aed737afd7a612265c79258417f0fd902

The code we use to render README.your_favorite_markup

basic textile and markdown
Chris Wanstrath chris@ozmm.org
Fri, 30 Oct 2009 18:14:48 -0700
commit

1a659c4aed737afd7a612265c79258417f0fd902

parent

2fc7da6d417acb1c79764d83a3d63c233c755529

2 files changed, 70 insertions(+), 0 deletions(-)

jump to
M lib/github/markup.rblib/github/markup.rb

@@ -1,4 +1,47 @@

+require 'rdiscount' +require 'redcloth' + module GitHub module Markup + extend self + @@markups = {} + + def markups + @@markups + end + + def add_markup(regexp, &block) + markups[regexp] = block + end + + def renderer(filename) + @@markups.each do |key, value| + if Regexp.compile("#{key}$") =~ filename + return value + end + end + end + + def render(filename, content) + renderer(filename)[content] + end end end + +begin + require 'rdiscount' + GitHub::Markup.add_markup(/md|mkdn?|markdown/) do |content| + Markdown.new(content).to_html + end +rescue LoadError + nil +end + +begin + require 'redcloth' + GitHub::Markup.add_markup(/textile/) do |content| + RedCloth.new(content).to_html + end +rescue LoadError + nil +end
M test/markup_test.rbtest/markup_test.rb

@@ -0,0 +1,27 @@

+$LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib" + +require 'github/markup' +require 'test/unit' + +class MarkupTest < Test::Unit::TestCase + def test_markdown + markdown = GitHub::Markup.render('README.markdown', "* One\n* Two") + assert_equal <<markdown, markdown +<ul> +<li>One</li> +<li>Two</li> +</ul> + +markdown + end + + def test_textile + textile = GitHub::Markup.render('README.textile', "* One\n* Two") + assert_equal <<textile.strip, textile +<ul> +\t<li>One</li> +\t<li>Two</li> +</ul> +textile + end +end