Chris@909: module CodeRay
Chris@909:
Chris@909: # A little hack to enable CodeRay highlighting in RedCloth.
Chris@909: #
Chris@909: # Usage:
Chris@909: # require 'coderay'
Chris@909: # require 'coderay/for_redcloth'
Chris@909: # RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
Chris@909: #
Chris@909: # Make sure you have RedCloth 4.0.3 activated, for example by calling
Chris@909: # require 'rubygems'
Chris@909: # before RedCloth is loaded and before calling CodeRay.for_redcloth.
Chris@909: module ForRedCloth
Chris@909:
Chris@909: def self.install
Chris@909: gem 'RedCloth', '>= 4.0.3' if defined? gem
Chris@909: require 'redcloth'
Chris@909: unless RedCloth::VERSION.to_s >= '4.0.3'
Chris@909: if defined? gem
Chris@909: raise 'CodeRay.for_redcloth needs RedCloth version 4.0.3 or later. ' +
Chris@909: "You have #{RedCloth::VERSION}. Please gem install RedCloth."
Chris@909: else
Chris@909: $".delete 'redcloth.rb' # sorry, but it works
Chris@909: require 'rubygems'
Chris@909: return install # retry
Chris@909: end
Chris@909: end
Chris@909: unless RedCloth::VERSION.to_s >= '4.2.2'
Chris@909: warn 'CodeRay.for_redcloth works best with RedCloth version 4.2.2 or later.'
Chris@909: end
Chris@909: RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc
Chris@909: RedCloth::Formatters::HTML.module_eval do
Chris@909: def unescape(html) # :nodoc:
Chris@909: replacements = {
Chris@909: '&' => '&',
Chris@909: '"' => '"',
Chris@909: '>' => '>',
Chris@909: '<' => '<',
Chris@909: }
Chris@909: html.gsub(/&(?:amp|quot|[gl]t);/) { |entity| replacements[entity] }
Chris@909: end
Chris@909: undef code, bc_open, bc_close, escape_pre
Chris@909: def code(opts) # :nodoc:
Chris@909: opts[:block] = true
Chris@909: if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0'
Chris@909: # simulating pre-4.2 behavior
Chris@909: if opts[:text].sub!(/\A\[(\w+)\]/, '')
Chris@909: if CodeRay::Scanners[$1].lang == :text
Chris@909: opts[:text] = $& + opts[:text]
Chris@909: else
Chris@909: opts[:lang] = $1
Chris@909: end
Chris@909: end
Chris@909: end
Chris@909: if opts[:lang] && !filter_coderay
Chris@909: require 'coderay'
Chris@909: @in_bc ||= nil
Chris@909: format = @in_bc ? :div : :span
Chris@909: opts[:text] = unescape(opts[:text]) unless @in_bc
Chris@909: highlighted_code = CodeRay.encode opts[:text], opts[:lang], format
Chris@909: highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) }
Chris@909: highlighted_code
Chris@909: else
Chris@909: "#{opts[:text]}
"
Chris@909: end
Chris@909: end
Chris@909: def bc_open(opts) # :nodoc:
Chris@909: opts[:block] = true
Chris@909: @in_bc = opts
Chris@909: opts[:lang] ? '' : "
" Chris@909: end Chris@909: def bc_close(opts) # :nodoc: Chris@909: opts = @in_bc Chris@909: @in_bc = nil Chris@909: opts[:lang] ? '' : "\n" Chris@909: end Chris@909: def escape_pre(text) # :nodoc: Chris@909: if @in_bc ||= nil Chris@909: text Chris@909: else Chris@909: html_esc(text, :html_escape_preformatted) Chris@909: end Chris@909: end Chris@909: end Chris@909: end Chris@909: Chris@909: module TextileDoc # :nodoc: Chris@909: attr_accessor :filter_coderay Chris@909: end Chris@909: Chris@909: end Chris@909: Chris@909: end Chris@909: Chris@909: CodeRay::ForRedCloth.install