class Heel::DirectoryIndexer
generate html index pages of a directory
Attributes
options[R]
template[R]
template_file[R]
Public Class Methods
new( template_file, options )
click to toggle source
# File lib/heel/directory_indexer.rb, line 17 def initialize( template_file, options ) @template = nil @template_file = template_file @options = options reload_template end
Public Instance Methods
highlighting?()
click to toggle source
# File lib/heel/directory_indexer.rb, line 35 def highlighting? @options.fetch( :highlighting, false) end
index_page_for(req)
click to toggle source
generate the directory index html page of a directory
# File lib/heel/directory_indexer.rb, line 57 def index_page_for(req) reload_template if reload_on_template_change? dir = req.request_path entries = [] Dir.entries(dir).each do |entry| next if should_ignore?(entry) next if dir == @options[:document_root] and entry == ".." stat = File.stat(File.join(dir,entry)) entry_data = OpenStruct.new entry_data.name = entry == ".." ? "Parent Directory" : entry entry_data.link = ERB::Util.url_encode(entry) entry_data.size = num_to_bytes(stat.size) entry_data.last_modified = stat.mtime.strftime("%Y-%m-%d %H:%M:%S") if stat.directory? then entry_data.content_type = "Directory" entry_data.size = "-" entry_data.name += "/" if using_icons? then entry_data.icon_url = File.join(options[:icon_url], MimeMap.icons_by_mime_type[:directory]) end else entry_data.mime_type = mime_map.mime_type_of(entry) entry_data.content_type = entry_data.mime_type.content_type if using_icons? then entry_data.icon_url = File.join(options[:icon_url], mime_map.icon_for(entry_data.mime_type)) end end entries << entry_data end template_vars = TemplateVars.new( :base_uri => req.path_info ) template_vars.entries = entries.sort_by { |e| e.link } template_vars.homepage = Heel::Configuration::HOMEPAGE return template.result( template_vars.binding_for_template ) end
mime_map()
click to toggle source
# File lib/heel/directory_indexer.rb, line 31 def mime_map @mime_map ||= Heel::MimeMap.new end
num_to_bytes(num,fmt="%.2f")
click to toggle source
essentially this is strfbytes from facets
# File lib/heel/directory_indexer.rb, line 99 def num_to_bytes(num,fmt="%.2f") case when num < 1024 "#{num} bytes" when num < 1024**2 "#{fmt % (num.to_f / 1024)} KB" when num < 1024**3 "#{fmt % (num.to_f / 1024**2)} MB" when num < 1024**4 "#{fmt % (num.to_f / 1024**3)} GB" when num < 1024**5 "#{fmt % (num.to_f / 1024**4)} TB" else "#{num} bytes" end end
reload_on_template_change?()
click to toggle source
# File lib/heel/directory_indexer.rb, line 39 def reload_on_template_change? @options.fetch( :reload_template_on_change, false ) end
reload_template()
click to toggle source
# File lib/heel/directory_indexer.rb, line 47 def reload_template fstat = File.stat(@template_file) @template_mtime ||= fstat.mtime if @template.nil? or fstat.mtime > @template_mtime then @template = ::ERB.new(File.read(@template_file)) end end
should_ignore?(fname)
click to toggle source
# File lib/heel/directory_indexer.rb, line 24 def should_ignore?(fname) options[:ignore_globs].each do |glob| return true if ::File.fnmatch(glob,fname) end false end
using_icons?()
click to toggle source
# File lib/heel/directory_indexer.rb, line 43 def using_icons? @options.fetch( :using_icons, false ) end