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 = [], keywords = []) ⇒ CodeRayWrapper

Returns a new instance of CodeRayWrapper.



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

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

Instance Attribute Details

#highlight_linesObject (readonly)

Returns the value of attribute highlight_lines.



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

def highlight_lines
  @highlight_lines
end

#line_number_startObject (readonly)

Returns the value of attribute line_number_start.



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

def line_number_start
  @line_number_start
end

Instance Method Details

#col_limit(limit_num) ⇒ Object



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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 91

def file_type
  @setting = WebSetting.new
  @extname = File.extname(@filename)
  @p_extname = "^\\#{@extname}$"
  if @setting.eliminate_extname.split(" ").grep(/#{@p_extname}/).size > 0
      @filename = File.basename(@filename, @extname)
      @extname = File.extname(@filename)
  end
  case @extname
  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



49
50
51
52
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 49

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



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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 54

def to_html
  setting = WebSetting.new
  layout_setting = setting.layout_setting

  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}:",
          :keywords => @keywords,
          :tab_width => layout_setting[:tab_width],
          )
end


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/milkode/cdweb/lib/coderay_wrapper.rb', line 73

def to_html_anchorlink(url)
  setting = WebSetting.new
  layout_setting = setting.layout_setting

  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,
          :keywords => @keywords,
          :tab_width => layout_setting[:tab_width],
          )
end