Class: Heel::DirectoryIndexer

Inherits:
Object
  • Object
show all
Defined in:
lib/heel/directory_indexer.rb

Overview

generate html index pages of a directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_file, options) ⇒ DirectoryIndexer

Returns a new instance of DirectoryIndexer.



17
18
19
20
21
22
# File 'lib/heel/directory_indexer.rb', line 17

def initialize( template_file, options )
  @template         = nil
  @template_file    = template_file
  @options          = options
  reload_template
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/heel/directory_indexer.rb', line 13

def options
  @options
end

#templateObject (readonly)

Returns the value of attribute template.



15
16
17
# File 'lib/heel/directory_indexer.rb', line 15

def template
  @template
end

#template_fileObject (readonly)

Returns the value of attribute template_file.



14
15
16
# File 'lib/heel/directory_indexer.rb', line 14

def template_file
  @template_file
end

Instance Method Details

#highlighting?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/heel/directory_indexer.rb', line 35

def highlighting?
  @options.fetch( :highlighting, false)
end

#index_page_for(req) ⇒ Object

generate the directory index html page of a directory



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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, :highlighting => highlighting? )
  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_mapObject



31
32
33
# File 'lib/heel/directory_indexer.rb', line 31

def mime_map
  @mime_map ||= Heel::MimeMap.new
end

#num_to_bytes(num, fmt = "%.2f") ⇒ Object

essentially this is strfbytes from facets



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/heel/directory_indexer.rb', line 39

def reload_on_template_change?
  @options.fetch( :reload_template_on_change, false )
end

#reload_templateObject



47
48
49
50
51
52
53
# 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) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
29
# 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?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/heel/directory_indexer.rb', line 43

def using_icons?
  @options.fetch( :using_icons, false )
end