Class: Milkode::CodeRayWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/milkode/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.



20
21
22
23
24
25
26
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 20

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

#highlight_linesObject (readonly)

Returns the value of attribute highlight_lines.



18
19
20
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 18

def highlight_lines
  @highlight_lines
end

#line_number_startObject (readonly)

Returns the value of attribute line_number_start.



17
18
19
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 17

def line_number_start
  @line_number_start
end

Instance Method Details

#col_limit(limit_num) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 28

def col_limit(limit_num)
  content_a = @content.split("\n")

  @content = content_a.map{|v|
    if (v.length > limit_num)
      v[0...limit_num] + " ..."
    else
      v
    end
  }.join("\n")
end

#file_typeObject



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 79

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

#limit_range(range, array) ⇒ Object



47
48
49
50
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 47

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

#set_range(range) ⇒ Object



40
41
42
43
44
45
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 40

def set_range(range)
  content_a = @content.split("\n")
  range = limit_range(range, content_a)
  @content = content_a[range].join("\n")
  @line_number_start = range.first + 1
end

#to_htmlObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 52

def to_html
  CodeRay.scan(@content, file_type).
    html2(
          :wrap => nil,
          :line_numbers => :table,
          :css => :class,
          :highlight_lines => @highlight_lines,
          :line_number_start => @line_number_start,
          :line_number_anchors => false,
          :onclick_copy_line_number => true,
          :onclick_copy_prefix => "/#{@filename}:"
          )
end


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 66

def to_html_anchorlink(url)
  CodeRay.scan(@content, file_type).
    html2(
          :wrap => nil,
          :line_numbers => :table,
          :css => :class,
          :highlight_lines => @highlight_lines,
          :line_number_start => @line_number_start,
          :line_number_anchors => 'n',
          :line_number_anchor_url => url
          )
end