Class Heel::DirectoryIndexer
In: lib/heel/directory_indexer.rb
lib/heel/directory_indexer.rb
Parent: Object

generate html index pages of a directory

Methods

Attributes

options  [R] 
options  [R] 
template  [R] 
template  [R] 
template_file  [R] 
template_file  [R] 

Public Class methods

[Source]

    # File lib/heel/directory_indexer.rb, line 18
18:     def initialize( template_file, options )
19:       @template_file    = template_file
20:       @options          = options
21:       reload_template
22:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 18
18:     def initialize( template_file, options )
19:       @template_file    = template_file
20:       @options          = options
21:       reload_template
22:     end

Public Instance methods

[Source]

    # File lib/heel/directory_indexer.rb, line 35
35:     def highlighting?
36:       @options[:highlighting]
37:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 35
35:     def highlighting?
36:       @options[:highlighting]
37:     end

generate the directory index html page of a directory

[Source]

    # File lib/heel/directory_indexer.rb, line 57
57:     def index_page_for(req)
58:       reload_template if reload_on_template_change?
59:       dir      = req.request_path
60:       base_uri = req.path_info
61:       entries = []
62:       Dir.entries(dir).each do |entry|
63:         next if should_ignore?(entry)
64:         next if dir == @options[:document_root] and entry == ".."
65: 
66:         stat            = File.stat(File.join(dir,entry))
67:         entry_data      = OpenStruct.new 
68: 
69:         entry_data.name          = entry == ".." ? "Parent Directory" : entry
70:         entry_data.link          = entry
71:         entry_data.size          = num_to_bytes(stat.size)
72:         entry_data.last_modified = stat.mtime.strftime("%Y-%m-%d %H:%M:%S")
73: 
74:         if stat.directory? then
75:           entry_data.content_type = "Directory"
76:           entry_data.size         = "-"
77:           entry_data.name        += "/"
78:           if using_icons? then
79:             entry_data.icon_url = File.join(options[:icon_url], MimeMap.icons_by_mime_type[:directory])
80:           end
81:         else
82:           entry_data.mime_type = mime_map.mime_type_of(entry)
83:           entry_data.content_type = entry_data.mime_type.content_type
84:           if using_icons? then
85:             entry_data.icon_url = File.join(options[:icon_url], mime_map.icon_for(entry_data.mime_type))
86:           end
87:         end
88:         entries << entry_data
89:       end
90: 
91:       entries = entries.sort_by { |e| e.link }
92:       return template.result(binding)
93:     end

generate the directory index html page of a directory

[Source]

    # File lib/heel/directory_indexer.rb, line 57
57:     def index_page_for(req)
58:       reload_template if reload_on_template_change?
59:       dir      = req.request_path
60:       base_uri = req.path_info
61:       entries = []
62:       Dir.entries(dir).each do |entry|
63:         next if should_ignore?(entry)
64:         next if dir == @options[:document_root] and entry == ".."
65: 
66:         stat            = File.stat(File.join(dir,entry))
67:         entry_data      = OpenStruct.new 
68: 
69:         entry_data.name          = entry == ".." ? "Parent Directory" : entry
70:         entry_data.link          = entry
71:         entry_data.size          = num_to_bytes(stat.size)
72:         entry_data.last_modified = stat.mtime.strftime("%Y-%m-%d %H:%M:%S")
73: 
74:         if stat.directory? then
75:           entry_data.content_type = "Directory"
76:           entry_data.size         = "-"
77:           entry_data.name        += "/"
78:           if using_icons? then
79:             entry_data.icon_url = File.join(options[:icon_url], MimeMap.icons_by_mime_type[:directory])
80:           end
81:         else
82:           entry_data.mime_type = mime_map.mime_type_of(entry)
83:           entry_data.content_type = entry_data.mime_type.content_type
84:           if using_icons? then
85:             entry_data.icon_url = File.join(options[:icon_url], mime_map.icon_for(entry_data.mime_type))
86:           end
87:         end
88:         entries << entry_data
89:       end
90: 
91:       entries = entries.sort_by { |e| e.link }
92:       return template.result(binding)
93:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 31
31:     def mime_map
32:       @mime_map ||= Heel::MimeMap.new
33:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 31
31:     def mime_map
32:       @mime_map ||= Heel::MimeMap.new
33:     end

essentially this is strfbytes from facets

[Source]

     # File lib/heel/directory_indexer.rb, line 97
 97:     def num_to_bytes(num,fmt="%.2f")
 98:       case
 99:       when num < 1024
100:               "#{num} bytes"
101:       when num < 1024**2
102:               "#{fmt % (num.to_f / 1024)} KB"
103:       when num < 1024**3
104:               "#{fmt % (num.to_f / 1024**2)} MB"
105:       when num < 1024**4
106:               "#{fmt % (num.to_f / 1024**3)} GB"
107:       when num < 1024**5
108:               "#{fmt % (num.to_f / 1024**4)} TB"
109:       else
110:               "#{num} bytes"
111:       end
112:     end

essentially this is strfbytes from facets

[Source]

     # File lib/heel/directory_indexer.rb, line 97
 97:     def num_to_bytes(num,fmt="%.2f")
 98:       case
 99:       when num < 1024
100:               "#{num} bytes"
101:       when num < 1024**2
102:               "#{fmt % (num.to_f / 1024)} KB"
103:       when num < 1024**3
104:               "#{fmt % (num.to_f / 1024**2)} MB"
105:       when num < 1024**4
106:               "#{fmt % (num.to_f / 1024**3)} GB"
107:       when num < 1024**5
108:               "#{fmt % (num.to_f / 1024**4)} TB"
109:       else
110:               "#{num} bytes"
111:       end
112:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 39
39:     def reload_on_template_change?
40:       @options[:reload_template_on_change]
41:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 39
39:     def reload_on_template_change?
40:       @options[:reload_template_on_change]
41:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 47
47:     def reload_template
48:       fstat = File.stat(@template_file)
49:       @template_mtime ||= fstat.mtime
50:       if @template.nil? or fstat.mtime > @template_mtime then
51:         @template = ::ERB.new(File.read(@template_file))
52:       end
53:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 47
47:     def reload_template
48:       fstat = File.stat(@template_file)
49:       @template_mtime ||= fstat.mtime
50:       if @template.nil? or fstat.mtime > @template_mtime then
51:         @template = ::ERB.new(File.read(@template_file))
52:       end
53:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 24
24:     def should_ignore?(fname)
25:       options[:ignore_globs].each do |glob|
26:         return true if ::File.fnmatch(glob,fname)
27:       end
28:       false 
29:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 24
24:     def should_ignore?(fname)
25:       options[:ignore_globs].each do |glob|
26:         return true if ::File.fnmatch(glob,fname)
27:       end
28:       false 
29:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 43
43:     def using_icons?
44:       @options[:using_icons]
45:     end

[Source]

    # File lib/heel/directory_indexer.rb, line 43
43:     def using_icons?
44:       @options[:using_icons]
45:     end

[Validate]