Class: CodeStock::CodeRayWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/codestock/cdweb/lib/coderay_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, filename, match_lines = []) ⇒ CodeRayWrapper

Returns a new instance of CodeRayWrapper.



16
17
18
19
20
21
22
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 16

def initialize(content, filename, match_lines = [])
  @content = content
  @filename = filename
  @match_lines = match_lines
  @highlight_lines = match_lines.map{|v|v.index+1}
  @line_number_start = 1
end

Instance Attribute Details

#line_number_startObject (readonly)

Returns the value of attribute line_number_start.



14
15
16
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 14

def line_number_start
  @line_number_start
end

Instance Method Details

#codestock_ornament(html) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 49

def codestock_ornament(html)
  a = html.split("\n")

  line_number = @line_number_start
  is_code_content = false

  a.each_with_index do |l, index|
    if (l =~ /  <td class="code"><pre (.*?)>(.*)<tt>/)
      a[index] = "  <td class=\"code\"><pre #{$1}><span #{line_attr(line_number)}>#{$2}</span><tt>"
      is_code_content = true
      line_number += 1
      next
    elsif (l =~ %r|</tt></pre></td>|)
      is_code_content = false
    end

    if (is_code_content)
      if (l =~ %r|</tt>(.*)<tt>|)
        a[index] = "</tt><span #{line_attr(line_number)}>#{$1}</span><tt>"
        line_number += 1
      end
    end
  end
      
  a.join("\n") + "\n"
end

#file_typeObject



76
77
78
79
80
81
82
83
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 76

def file_type
  case File.extname(@filename)
  when ".el"
    :scheme
  else
    CodeRay::FileType.fetch @filename, :plaintext
  end
end

#limit_range(range, array) ⇒ Object



31
32
33
34
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 31

def limit_range(range, array)
  Range.new(range.first < 0 ? 0 : range.first,
            range.last >= array.size ? array.size - 1 : range.last)
end

#line_attr(no) ⇒ Object



85
86
87
88
89
90
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 85

def line_attr(no)
  r = []
  r << "id=\"#{no}\""
  r << "class=\"highlight-line\"" if @highlight_lines.include?(no)
  r.join(" ")
end

#set_range(range) ⇒ Object



24
25
26
27
28
29
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 24

def set_range(range)
  content_a = @content.to_a
  range = limit_range(range, content_a)
  @content = content_a[range]
  @line_number_start = range.first + 1
end

#to_htmlObject



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/codestock/cdweb/lib/coderay_wrapper.rb', line 36

def to_html
  html = CodeRay.scan(@content, file_type).
    html(
         :wrap => nil,
         :line_numbers => :table,
         :css => :class,
         :highlight_lines => @highlight_lines,
         :line_number_start => @line_number_start
         )

  codestock_ornament(html)
end