Skip to content

Instantly share code, notes, and snippets.

@sosedoff
Created May 10, 2011 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sosedoff/965598 to your computer and use it in GitHub Desktop.
Save sosedoff/965598 to your computer and use it in GitHub Desktop.
TextMarkup module for Ruby 1.9.2
require 'RedCloth'
require 'rdiscount'
require 'rdoc/markup/to_html' # ruby1.9.2 specific
module TextMarkup
extend self
# Auto-detect format from filename and render content
def render(filename, content)
name = File.basename(filename.to_s.strip)
raise ArgumentError, 'Filename required!' if name.empty?
format = detect_format(name)
format == :text ? content : self.send(format, content)
end
# Render content for Markdown
def markdown(content)
Markdown.new(content).to_html
end
# Render content for RDoc
def rdoc(content)
RDoc::Markup::ToHtml.new.convert(content)
end
# Render content for Textile
def textile(content)
RedCloth.new(content).to_html
end
protected
# Detect markup format from filename
def detect_format(filename)
case(filename)
when /\.rdoc/i
:rdoc
when /\.(md|mkdn?|mdown|markdown)/i
:markdown
when /\.textile/i
:textile
else
:text
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment