basic textile and markdown
Chris Wanstrath chris@ozmm.org
Fri, 30 Oct 2009 18:14:48 -0700
2 files changed,
70 insertions(+),
0 deletions(-)
M
lib/github/markup.rb
→
lib/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.rb
→
test/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