# -*- coding: utf-8 -*- # # @file # @brief # @author ongaeshi # @date 2011/06/25 require 'rubygems' require 'sinatra' if ENV['MILKODE_SINATRA_RELOADER'] require 'sinatra/reloader' also_reload '../../**/*.rb' end require 'sass' require 'haml' $LOAD_PATH.unshift '../..' require 'milkode/cdweb/lib/database' require 'milkode/cdweb/lib/command' require 'milkode/cdweb/lib/mkurl' require 'milkode/cdweb/lib/web_setting' require 'milkode/cdweb/lib/package_list' require 'milkode/common/util' set :haml, :format => :html5 get '/' do if Database.validate? @setting = WebSetting.new @version = "0.9.9" @package_num = Database.instance.yaml_package_num @file_num = Database.instance.totalRecords @package_list = PackageList.new(Database.instance.grndb) haml :index, :layout => false else <Setup Error! Database Directory: #{Database.dbdir}
EOF end end def package_path(path) path.split('/')[0,3].join('/') end post '/search*' do path = unescape(params[:pathname]) if params[:clear] redirect Mkurl.new("#{path}", params).inherit_shead else case params[:shead] when 'all' path = "/home" when 'package' path = package_path(path) when 'directory' # do nothing else path = package_path(path) end query = Query.new(params[:query]) # gotolineモードで1つだけ渡された時は直接ジャンプ if query.keywords.size == 1 && Milkode::Util::gotoline_keyword?(query.keywords[0]) gotoline = Milkode::Util::parse_gotoline(query.keywords)[0] path2 = File.join('/home', gotoline[0][0]) redirect Mkurl.new(path2, params).inherit_query_shead + "#n#{gotoline[1]}" else redirect Mkurl.new("#{path}", params).inherit_query_shead end end end post '/command' do case params[:kind] when 'update' before = Time.now if (params[:name] == '') result = Database.instance.update_all update_result_str(result, before) else result = Database.instance.update(params[:name]) update_result_str(result, before) end end end get '/home*' do |path| before = Time.now path = path.sub(/^\//, "") record = Database.instance.record(path) if path.empty? if (params[:query] and !params[:query].empty?) search(path, params, before) else packages(params, before) end elsif (record) view(record, params, before) else if (params[:query] and !params[:query].empty?) search(path, params, before) else filelist(path, params, before) end end end get %r{/help} do @setting = WebSetting.new haml :help end # -- helper function -- helpers do # -- escape functions -- alias h escape_html alias escape_url escape def escape_path(src) escape_url(src).gsub("%2F", '/') end # -- utility -- def link(query) "#{query}" end def create_select_shead(value) value ||= "package" data = [ ['all' , '全て' ], ['package' , 'パッケージ' ], ['directory', 'ディレクトリ'], ] < #{data.map{|v| ""}.join} EOF end def create_select_package(path) value = package_name(path) value = '---' if value == "root" data = ['---'] + Database.instance.packages(nil) < #{data.map{|v| ""}.join} EOF end def create_select_package_home value = '---' data = ['---'] + Database.instance.packages(nil) < #{data.map{|v| ""}.join} EOF end def create_checkbox(name, value, label) str = (value) ? 'checked' : '' "" end def create_headmenu(path, query, flistpath = '') href = Mkurl.new('/home/' + path, params).inherit_query_shead flist = File.join("/home/#{path}", flistpath) package_name = "" modal_body = "全てのパッケージを更新しますか?" if (path != "") package_name = path.split('/')[0] modal_body = "#{package_name} を更新しますか?" end <ホーム #{headicon('document-new-4.png')} タブを複製 #{headicon('directory.png')} ディレクトリ #{headicon('view-refresh-4.png')} パッケージを更新 #{headicon('help.png')} ヘルプ EOF end def headicon(name) "" end def topic_path(path, params) href = '' path = File.join('home', path) path.split('/').map_with_index {|v, index| href += '/' + v "#{v}" }.join('/') end HASH = { '.h' => ['.c', '.cpp', '.m', '.mm'], '.c' => ['.h'], '.hpp' => ['.cpp'], '.cpp' => ['.hpp', '.h'], '.m' => ['.h'], '.mm' => ['.h'], } def additional_info(path, parms) suffix = File.extname path cadet = HASH[suffix] if (cadet) result = cadet.find do |v| Database.instance.record(path.gsub(/#{suffix}\Z/, v)) end if (result) path2 = path.gsub(/#{suffix}\Z/, result) " (#{result}) " else '' end else '' end end def package_name(path) (path == "") ? 'root' : path.split('/')[0] end def current_name(path) (path == "") ? 'root' : File.basename(path) end def path_title(path) (path == "") ? "root" : path end def filelist_title(path) (path == "") ? "Package List" : path end def update_result_str(result, before) r = [] r << "#{result.package_count} packages" if result.package_count > 1 r << "#{result.file_count} records" r << "#{result.add_count} add" r << "#{result.update_count} update" "#{r.join(', ')} (#{Time.now - before} sec)" end # .search-summary に追加情報を表示したい時はこの関数をオーバーライド def search_summary_hook(path) "" end end class Array def map_with_index! each_with_index do |e, idx| self[idx] = yield(e, idx); end end def map_with_index(&block) dup.map_with_index!(&block) end end