This repository has been archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
create_directory_listings
executable file
·133 lines (117 loc) · 3.81 KB
/
create_directory_listings
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env ruby
require 'erb'
# creates an index.html file in every directory below (and including) the current directory
# each index.html file contains a list of files and directories that are in its parent directory
def process_directory(path)
directories = []
files = []
sizes = {}
mtimes = {}
Dir.entries(path).sort.each do |filename|
full_path = "#{path}/#{filename}"
case filename
when '.' || '..'
when 'index.html'
else
case
when filename =~ /^\./
# ignore hidden files
when File.directory?(full_path)
directories << filename
when File.file?(full_path)
files << filename
sizes[full_path] = File.size(full_path)
mtimes[full_path] = File.mtime(full_path)
end
end # case filename
end
directories.sort.each do |directory|
process_directory("#{path}/#{directory}")
end
# output this directory's info:
write_index_file("#{path}/index.html", directories, files, sizes, mtimes)
puts "#{path}/index.html"
end
def write_index_file(path, directories, files, sizes, mtimes)
@basename = File.basename(path)
@dirname = File.dirname(path)
@directories = directories
@files = files
@sizes = sizes
@mtimes = mtimes
@levels_deep = @dirname.split('/').length - 1
# FIXME: make me look pretty
erb = ERB.new(<<EOS)
<html>
<head>
<title><%= @dirname %></title>
<meta name="generator" content="flapjack.io flapjack-omnibus create_directory_listings">
<link rel="stylesheet" href="http://flapjack.io/stylesheets/bootstrap.min.css"/>
<link rel="stylesheet" href="http://flapjack.io/stylesheets/api-screen.css" type="text/css" media="screen" />
<link rel="stylesheet" href="http://flapjack.io/stylesheets/api-syntax.css" type="text/css" media="screen" />
<link rel="stylesheet" href="http://flapjack.io/stylesheets/github-markdown.css" type="text/css">
<link rel="shortcut icon" href="http://flapjack.io/images/flapjack-2013-favicon-64-32-24-16.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
.markdown-body {
min-width: 200px;
max-width: 790px;
margin: 0 auto;
padding: 30px;
}
</style>
</head>
<body>
<div class="container content">
<div class="row">
<div class="col-md-offset-1 col-md-10">
<h1><a href="http://packages.flapjack.io">packages.flapjack.io</a></h1>
<% parts = @dirname.split('/') %>
<h2>
<% parts.each_with_index do |part, i| %>
<% uri_up = nil %>
<% j = i %>
<% while @levels_deep > j %>
<% uri_up = uri_up ? (uri_up + '../') : '../' %>
<% j += 1 %>
<% end %>
<% if uri_up %>
<a href="<%= uri_up %>"><%= part %></a> /
<% else %>
<%= part %> /
<% end %>
<% end %>
</h2>
<ul>
<li><a href="../">..</a></li>
<% @directories.each do |d| %>
<li><a href="<%= d %>"><%= d %>/</a></li>
<% end %>
<% @files.each do |thefile| %>
<li>
<% full_path = [@dirname, thefile].join('/') %>
<a href="<%= thefile %>"><%= thefile %></a>
- <%= @sizes[full_path] %> bytes - <%= @mtimes[full_path] %>
</li>
<% end %>
</ul>
</div>
</div>
</div>
</body>
</html>
EOS
File.open(path, 'w') do |index|
index.write erb.result(binding)
end
end
unless dir = ARGV[0]
puts "please specify a directory, eg:"
puts " #{$?} deb"
exit 1
end
unless File.directory?(dir)
puts "Error: can't see a directory at #{dir}"
exit 1
end
process_directory(dir)