A little hack to enable CodeRay highlighting in RedCloth.
Usage:
require 'coderay' require 'coderay/for_redcloth' RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
Make sure you have RedCloth 4.0.3 activated, for example by calling
require 'rubygems'
before RedCloth is loaded and before calling CodeRay.for_redcloth.
# File lib/coderay/for_redcloth.rb, line 15 15: def self.install 16: gem 'RedCloth', '>= 4.0.3' if defined? gem 17: require 'redcloth' 18: unless RedCloth::VERSION.to_s >= '4.0.3' 19: if defined? gem 20: raise 'CodeRay.for_redcloth needs RedCloth version 4.0.3 or later. ' + 21: "You have #{RedCloth::VERSION}. Please gem install RedCloth." 22: else 23: $".delete 'redcloth.rb' # sorry, but it works 24: require 'rubygems' 25: return install # retry 26: end 27: end 28: unless RedCloth::VERSION.to_s >= '4.2.2' 29: warn 'CodeRay.for_redcloth works best with RedCloth version 4.2.2 or later.' 30: end 31: RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc 32: RedCloth::Formatters::HTML.module_eval do 33: def unescape(html) 34: replacements = { 35: '&' => '&', 36: '"' => '"', 37: '>' => '>', 38: '<' => '<', 39: } 40: html.gsub(/&(?:amp|quot|[gl]t);/) { |entity| replacements[entity] } 41: end 42: undef code, bc_open, bc_close, escape_pre 43: def code(opts) # :nodoc: 44: opts[:block] = true 45: if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0' 46: # simulating pre-4.2 behavior 47: if opts[:text].sub!(/\A\[(\w+)\]/, '') 48: if CodeRay::Scanners[$1].plugin_id == 'plaintext' 49: opts[:text] = $& + opts[:text] 50: else 51: opts[:lang] = $1 52: end 53: end 54: end 55: if opts[:lang] && !filter_coderay 56: require 'coderay' 57: @in_bc ||= nil 58: format = @in_bc ? :div : :span 59: opts[:text] = unescape(opts[:text]) unless @in_bc 60: highlighted_code = CodeRay.encode opts[:text], opts[:lang], format, :stream => true 61: highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) } 62: highlighted_code 63: else 64: "<code#{pba(opts)}>#{opts[:text]}</code>" 65: end 66: end 67: def bc_open(opts) # :nodoc: 68: opts[:block] = true 69: @in_bc = opts 70: opts[:lang] ? '' : "<pre#{pba(opts)}>" 71: end 72: def bc_close(opts) # :nodoc: 73: opts = @in_bc 74: @in_bc = nil 75: opts[:lang] ? '' : "</pre>\n" 76: end 77: def escape_pre(text) 78: if @in_bc ||= nil 79: text 80: else 81: html_esc(text, :html_escape_preformatted) 82: end 83: end 84: end 85: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.