class Object

Constants

HASH

Public Instance Methods

additional_info(path, parms) click to toggle source
# File lib/milkode/cdweb/app.rb, line 340
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)
      " (<a href='#{url_for Mkurl.new(File.join('/home', path2), params).inherit_query_shead}'>#{result}</a>) "
    else
      ''
    end
  else
    ''
  end
end
create_checkbox(name, value, label) click to toggle source
# File lib/milkode/cdweb/app.rb, line 239
def create_checkbox(name, value, label)
  str = (value) ? 'checked' : ''
  "<label class='checkbox inline'><input type='checkbox' name='#{name}' value='on' #{str}/>#{label}</label>"
end
create_favorite_list(package_list) click to toggle source
# File lib/milkode/cdweb/app.rb, line 305
  def create_favorite_list(package_list)
    return "" if package_list.favorite_list_size == 0
    
    <<EOF
      <div class="favorite_list">
        #{t(:favorite)}:
        #{package_list.favorite_list(params)}
      </div>
    </div>
EOF
  end
create_headmenu(path, query, flistpath = '') click to toggle source
# File lib/milkode/cdweb/app.rb, line 244
  def create_headmenu(path, query, flistpath = '')
    suburl = url_for ""

    href = Mkurl.new("#{suburl}/home/#{path}", params).inherit_query_shead
    flist = File.join("#{suburl}/home/#{path}", flistpath)

    package_name = ""
    modal_body = t(:update_all)

    if (path != "")
      package_name = path.split('/')[0]
      update_locale
      modal_body = I18n.t(:update_package, {package_name: package_name, locale: @locale})
    end

    info_path = "#{suburl}/info"
    info_path = File.join(info_path, package_name) if package_name != ""

    update_menu = "#{headicon('view-refresh-4.png', suburl)}<a href='#updateModal' class='headmenu' data-toggle='modal'>#{t(:update_packages)}</a>&nbsp;"
    update_menu = "" if @setting.hide_update_button

    <<EOF
    #{headicon('go-home-5.png', suburl)}<a href="#{suburl}/home" class="headmenu">#{t(:home)}</a>&nbsp;
    #{headicon('directory.png', suburl)}<a href="#{flist}" class="headmenu">#{t(:directory)}</a>&nbsp;
    #{update_menu}
    #{headicon('info.png', suburl)}<a href="#{info_path}" class="headmenu">#{t(:stats)}</a>&nbsp;
    #{headicon('help.png', suburl)}<a href="#{suburl}/help" class="headmenu">#{t(:help)}</a>

    <div id="updateModal" class="modal hide fade">
      <div class="modal-header">
        <a href="#" class="close" data-dismiss="modal">&times;</a>
        <h3>#{t(:update_packages)}</h3>
      </div>
      <div class="modal-body">
        <h4>#{modal_body}</h4>
      </div>
      <div class="modal-footer">
        <a href="#" id="updateCancel" class="btn" data-dismiss="modal">Cancel</a>
        <a href="#" id="updateOk" class="btn btn-primary" data-loading-text="Updating..." milkode-package-name="#{package_name}">OK</a>
      </div>
    </div>

    <div id="lineno-modal" class="modal hide">
      <div class="modal-header">
        <a href="#" class="close" data-dismiss="modal">&times;</a>
        <h3 id="lineno-path"></h3>
      </div>
      <div class="modal-body">
        <table class="CodeRay"><tr>
          <td class="code"><pre id="lineno-body">
          </pre></td>
        </tr></table>
    </div>
      <div class="modal-footer">
        <span id="lineno-copyall"></span>
        <a href="#" id="lineno-ok" class="btn" data-dismiss="modal">OK</a>
      </div>
    </div>
EOF
  end
create_select_package(path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 216
  def create_select_package(path)
    value = package_name(path)
    value = '---' if value == "root"
    data = ['---'] + Database.instance.packages(nil)

    <<EOF
<select name="package" id="package" onchange="select_package()">
#{data.map{|v| "<option value='#{v}' #{v == value ? 'selected' : ''}>#{v}</option>"}.join}
</select>
EOF
  end
create_select_package_home() click to toggle source
# File lib/milkode/cdweb/app.rb, line 228
  def create_select_package_home
    value = '---'
    data = ['---'] + Database.instance.packages(nil)

    <<EOF
<select name="package" id="package_home" onchange="select_package_home()">
#{data.map{|v| "<option value='#{v}' #{v == value ? 'selected' : ''}>#{v}</option>"}.join}
</select>
EOF
  end
create_select_shead(value) click to toggle source
# File lib/milkode/cdweb/app.rb, line 200
  def create_select_shead(value)
    value ||= "package"

    data = [
            ['all'      , t(:all)      ],
            ['package'  , t(:package)  ],
            ['directory', t(:directory)],
           ]

    <<EOF
<select name="shead" id="shead">
#{data.map{|v| "<option value='#{v[0]}' #{v[0] == value ? 'selected' : ''}>#{v[1]}</option>"}.join}
</select>
EOF
  end
current_name(path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 364
def current_name(path)
  (path == "") ? 'root' : File.basename(path)
end
escape_path(src) click to toggle source
# File lib/milkode/cdweb/app.rb, line 187
def escape_path(src)
  escape_url(src).gsub("%2F", '/')
end
favstar(path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 385
def favstar(path)
  pname   = package_name(path)

  if pname != "root"
    classes = Database.instance.fav?(pname) ? "star favorited" : "star"
    "<a href=\"javascript:\" class=\"#{classes}\" milkode-package-name=\"#{pname}\">Favorite Me</a>"
  else
    ""
  end
end
filelist_title(path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 372
def filelist_title(path)
  (path == "") ? "Package List" : path
end
goto_github_project(path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 396
def goto_github_project(path)
  return "" if (path == "")

  paths = path.split('/')
  package = Database.instance.yaml_package(paths[0])
  return "" unless package.options[:github]
  
  image_href = 'https://raw.github.com/github/media/master/octocats/blacktocat-16.png'
  url = "https://github.com/#{package.options[:github]}"

  if (paths.size == 1)
    "<a href='#{url}' target=\"_blank\"><img src='#{image_href}'></img></a>"
  else
    "<a href='#{url}/tree/master/#{paths[1..-1].join('/')}' target=\"_blank\"><img src='#{image_href}'></img></a>"
  end
end
headicon(name, suburl) click to toggle source
# File lib/milkode/cdweb/app.rb, line 317
def headicon(name, suburl)
  "<img alt='' style='vertical-align:center; border: 0px; margin: 0px;' src='#{suburl}/images/#{name}'>"
end
package_name(path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 360
def package_name(path)
  (path == "") ? 'root' : path.split('/')[0]
end
package_path(homeurl, path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 60
def package_path(homeurl, path)
  homeurl + path.sub(homeurl, "").split('/')[0,2].join('/')
end
path_title(path) click to toggle source
# File lib/milkode/cdweb/app.rb, line 368
def path_title(path)
  (path == "") ? "root" : path
end
search_summary_hook(path) click to toggle source

.search-summary に追加情報を表示したい時はこの関数をオーバーライド

# File lib/milkode/cdweb/app.rb, line 414
def search_summary_hook(path)
  goto_github_project(path)
end
t(*args) click to toggle source
# File lib/milkode/cdweb/app.rb, line 439
def t(*args)
  update_locale
  I18n.t(*args, locale: @locale)
end
topic_path(path, params) click to toggle source
# File lib/milkode/cdweb/app.rb, line 321
def topic_path(path, params)
  href = ''
  path = File.join('home', path)

  path.split('/').map_with_index {|v, index|
    href += '/' + v
    "<a id='topic_#{index}' href='#{url_for Mkurl.new(href, params).inherit_query_shead}' onclick='topic_path(\"topic_#{index}\");'>#{v}</a>"
  }.join('/')
end
ua_locale() click to toggle source

for I18N

# File lib/milkode/cdweb/app.rb, line 419
def ua_locale
  # Pulls the browser's language
  @env["HTTP_ACCEPT_LANGUAGE"][0,2]
end
update_locale() click to toggle source
# File lib/milkode/cdweb/app.rb, line 424
def update_locale
  unless @locale
    begin
      # Support session
      @locale = params[:locale] || session[:locale] || ua_locale || 'en'
      session[:locale] = @locale
    rescue NameError          # 'session' variable can't find during testing
      @locale = 'en'
    end
      
    # Reload with sinatra-reloader
    I18n.reload! if ENV['MILKODE_SINATRA_RELOADER']
  end
end
update_result_str(result, before) click to toggle source
# File lib/milkode/cdweb/app.rb, line 376
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