annotate vendor/gems/coderay-1.0.0/lib/coderay/for_redcloth.rb @ 1022:f2ec92061fca browsing

Merge from live branch
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Tue, 13 Nov 2012 10:35:40 +0000
parents cbb26bc654de
children
rev   line source
Chris@909 1 module CodeRay
Chris@909 2
Chris@909 3 # A little hack to enable CodeRay highlighting in RedCloth.
Chris@909 4 #
Chris@909 5 # Usage:
Chris@909 6 # require 'coderay'
Chris@909 7 # require 'coderay/for_redcloth'
Chris@909 8 # RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
Chris@909 9 #
Chris@909 10 # Make sure you have RedCloth 4.0.3 activated, for example by calling
Chris@909 11 # require 'rubygems'
Chris@909 12 # before RedCloth is loaded and before calling CodeRay.for_redcloth.
Chris@909 13 module ForRedCloth
Chris@909 14
Chris@909 15 def self.install
Chris@909 16 gem 'RedCloth', '>= 4.0.3' if defined? gem
Chris@909 17 require 'redcloth'
Chris@909 18 unless RedCloth::VERSION.to_s >= '4.0.3'
Chris@909 19 if defined? gem
Chris@909 20 raise 'CodeRay.for_redcloth needs RedCloth version 4.0.3 or later. ' +
Chris@909 21 "You have #{RedCloth::VERSION}. Please gem install RedCloth."
Chris@909 22 else
Chris@909 23 $".delete 'redcloth.rb' # sorry, but it works
Chris@909 24 require 'rubygems'
Chris@909 25 return install # retry
Chris@909 26 end
Chris@909 27 end
Chris@909 28 unless RedCloth::VERSION.to_s >= '4.2.2'
Chris@909 29 warn 'CodeRay.for_redcloth works best with RedCloth version 4.2.2 or later.'
Chris@909 30 end
Chris@909 31 RedCloth::TextileDoc.send :include, ForRedCloth::TextileDoc
Chris@909 32 RedCloth::Formatters::HTML.module_eval do
Chris@909 33 def unescape(html) # :nodoc:
Chris@909 34 replacements = {
Chris@909 35 '&amp;' => '&',
Chris@909 36 '&quot;' => '"',
Chris@909 37 '&gt;' => '>',
Chris@909 38 '&lt;' => '<',
Chris@909 39 }
Chris@909 40 html.gsub(/&(?:amp|quot|[gl]t);/) { |entity| replacements[entity] }
Chris@909 41 end
Chris@909 42 undef code, bc_open, bc_close, escape_pre
Chris@909 43 def code(opts) # :nodoc:
Chris@909 44 opts[:block] = true
Chris@909 45 if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0'
Chris@909 46 # simulating pre-4.2 behavior
Chris@909 47 if opts[:text].sub!(/\A\[(\w+)\]/, '')
Chris@909 48 if CodeRay::Scanners[$1].lang == :text
Chris@909 49 opts[:text] = $& + opts[:text]
Chris@909 50 else
Chris@909 51 opts[:lang] = $1
Chris@909 52 end
Chris@909 53 end
Chris@909 54 end
Chris@909 55 if opts[:lang] && !filter_coderay
Chris@909 56 require 'coderay'
Chris@909 57 @in_bc ||= nil
Chris@909 58 format = @in_bc ? :div : :span
Chris@909 59 opts[:text] = unescape(opts[:text]) unless @in_bc
Chris@909 60 highlighted_code = CodeRay.encode opts[:text], opts[:lang], format
Chris@909 61 highlighted_code.sub!(/\A<(span|div)/) { |m| m + pba(@in_bc || opts) }
Chris@909 62 highlighted_code
Chris@909 63 else
Chris@909 64 "<code#{pba(opts)}>#{opts[:text]}</code>"
Chris@909 65 end
Chris@909 66 end
Chris@909 67 def bc_open(opts) # :nodoc:
Chris@909 68 opts[:block] = true
Chris@909 69 @in_bc = opts
Chris@909 70 opts[:lang] ? '' : "<pre#{pba(opts)}>"
Chris@909 71 end
Chris@909 72 def bc_close(opts) # :nodoc:
Chris@909 73 opts = @in_bc
Chris@909 74 @in_bc = nil
Chris@909 75 opts[:lang] ? '' : "</pre>\n"
Chris@909 76 end
Chris@909 77 def escape_pre(text) # :nodoc:
Chris@909 78 if @in_bc ||= nil
Chris@909 79 text
Chris@909 80 else
Chris@909 81 html_esc(text, :html_escape_preformatted)
Chris@909 82 end
Chris@909 83 end
Chris@909 84 end
Chris@909 85 end
Chris@909 86
Chris@909 87 module TextileDoc # :nodoc:
Chris@909 88 attr_accessor :filter_coderay
Chris@909 89 end
Chris@909 90
Chris@909 91 end
Chris@909 92
Chris@909 93 end
Chris@909 94
Chris@909 95 CodeRay::ForRedCloth.install