class Milkode::SearchFuzzyGotoLine

Constants

COL_LIMIT
DISP_NUM
LIMIT_NUM
MatchRecord
NTH

Attributes

end_index[R]
match_records[R]
next_index[R]
total_records[R]

Public Class Methods

new(path, params, query, suburl) click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 24
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.gotolines + @q.keywords)
  @gotoline = gotolines[0]

  # 検索
  fpaths = @q.fpaths + @gotoline[0]
  @records, @total_records = Database.instance.search([], @q.multi_match_keywords, @q.packages, path, fpaths, @q.suffixs, @q.fpath_or_packages, @offset, LIMIT_NUM)

  # 検索結果を表示
  grep_contents
end

Public Instance Methods

data_range() click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 53
def data_range
  @offset..(@offset + @end_index)
end
directjump?() click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 83
def directjump?
  match_num == 1
end
directjump_url() click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 87
def directjump_url
  path   = @homeurl + @match_records[0].record.shortpath
  lineno = "#n#{@gotoline[1]}"
  Mkurl.new(path, @params).inherit_query_shead + lineno
end
html_contents() click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 57
    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_fuzzy_gotoline.rb, line 68
    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_fuzzy_gotoline.rb, line 79
def match_num
  @match_records.size
end
next_offset() click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 49
def next_offset
  @offset + @next_index
end
query() click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 45
def query
  @q.query_string
end

Private Instance Methods

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

  @records.each_with_index do |record, index|
    lineidx = @gotoline[1] - 1
    
    if (lineidx < record.content.split("\n").size)
      @match_records << MatchRecord.new(record, Grep::MatchLineResult.new(lineidx, nil))

      if @match_records.size >= DISP_NUM
        @end_index  = index
        @next_index = index + 1
        break
      end
    end
  end
end
pagination_span(content) click to toggle source
# File lib/milkode/cdweb/lib/search_fuzzy_gotoline.rb, line 146
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_fuzzy_gotoline.rb, line 117
    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