class Milkode::SearchGotoLine

Constants

COL_LIMIT
DISP_NUM
LIMIT_NUM
MATH_FILE_DISP
MATH_FILE_LIMIT
MatchRecord
NTH

Attributes

page[R]
total_records[R]

Public Class Methods

new(path, params, query, suburl) click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 25
def initialize(path, params, query, suburl)
  @path    = path
  @params  = params
  @q       = query
  @page    = params[:page].to_i || 0
  @offset  = params[:offset].to_i
  @suburl  = suburl
  @homeurl = @suburl + "/home/"

  # 検索クエリを解析
  @gotolines = Util.parse_gotoline(@q.keywords)

  # レコードをピックアップ
  @records = []
  @gotolines.each do |v|
    @records << Database.instance.record(v[0][0][1..-1])
  end
  @total_records = @records.size

  # 検索結果を表示
  grep_contents
end

Public Instance Methods

data_range() click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 56
def data_range
  @offset..(@offset + @end_index)
end
html_contents() click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 60
    def html_contents
      match_groups = @match_records.reduce([]) do |g, m|
        # 近接マッチ無効
        g << [m]
      end
      
      <<EOF
#{match_groups.map{|g|result_match_record(g)}.join}
EOF
    end
html_pagination() click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 71
    def html_pagination
      return "" if @q.empty?
      return "" if next_offset >= @total_records

      return <<EOF
<div class='pagination pagination-centered'>
#{pagination_link(next_offset, @next_line, "next >>")}
</div>
EOF
    end
match_num() click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 82
def match_num
  @match_records.size
end
next_offset() click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 52
def next_offset
  @offset + @next_index
end
query() click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 48
def query
  @q.query_string
end

Private Instance Methods

grep_contents() click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 90
def grep_contents
  @match_records = []
  @end_index = @next_index = @records.size
  @next_line = nil

  @records.each_with_index do |record, index|
    @match_records << MatchRecord.new(record, Grep::MatchLineResult.new(@gotolines[index][1] - 1, nil))

    if @match_records.size >= DISP_NUM
      @end_index = index
      @next_index = index + 1
      break
    end
  end
end
pagination_span(content) click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 135
def pagination_span(content)
  "<ul><li>#{content}</li></ul>\n"
end
result_match_record(match_group) click to toggle source
# File lib/milkode/cdweb/lib/search_gotoline.rb, line 106
    def result_match_record(match_group)
      record = match_group[0].record

      first_index = match_group[0].match_line.index - NTH
      last_index  = match_group[-1].match_line.index + NTH
      match_lines = match_group.map{|m| m.match_line}

      coderay = CodeRayWrapper.new(record.content, record.shortpath, match_lines, @q.keywords)
      coderay.col_limit(COL_LIMIT)
      coderay.set_range(first_index..last_index)

      url = @homeurl + record_link(record)
      
      <<EOS
    <dt class='result-record'><a href='#{url + "#n#{coderay.highlight_lines[0]}"}'>#{Util.relative_path record.shortpath, @path}</a></dt>
    <dd>
#{coderay.to_html_anchorlink(url)}
    </dd>
EOS
    end