To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / .svn / pristine / 1f / 1f572940b49da993424874b4b9bed1d524fcea35.svn-base @ 1297:0a574315af3e

History | View | Annotate | Download (2.92 KB)

1
module CodeRay
2
  
3
  # A little hack to enable CodeRay highlighting in RedCloth.
4
  # 
5
  # Usage:
6
  #  require 'coderay'
7
  #  require 'coderay/for_redcloth'
8
  #  RedCloth.new('@[ruby]puts "Hello, World!"@').to_html
9
  # 
10
  # Make sure you have RedCloth 4.0.3 activated, for example by calling
11
  #  require 'rubygems'
12
  # before RedCloth is loaded and before calling CodeRay.for_redcloth.
13
  module ForRedCloth
14
    
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)  # :nodoc:
34
          replacements = {
35
            '&' => '&',
36
            '"' => '"',
37
            '>' => '>',
38
            '&lt;' => '<',
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].lang == :text
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
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)  # :nodoc:
78
          if @in_bc ||= nil
79
            text
80
          else
81
            html_esc(text, :html_escape_preformatted)
82
          end
83
        end
84
      end
85
    end
86

    
87
    module TextileDoc  # :nodoc:
88
      attr_accessor :filter_coderay
89
    end
90
    
91
  end
92
  
93
end
94

    
95
CodeRay::ForRedCloth.install