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