-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_index.pl
More file actions
301 lines (256 loc) · 10.1 KB
/
make_index.pl
File metadata and controls
301 lines (256 loc) · 10.1 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use Encode qw(decode_utf8 encode_utf8);
use File::Basename;
use Cwd qw(cwd abs_path);
use Getopt::Long qw(:config pass_through);
binmode STDOUT, ':encoding(UTF-8)';
# ---------------------------------------------------------------------------
# Translations
# ---------------------------------------------------------------------------
my %i18n = (
en => { index_of => 'Index of', folders => 'folder(s)', files => 'file(s)' },
fr => { index_of => 'Index de', folders => 'dossier(s)', files => 'fichier(s)' },
de => { index_of => 'Index von', folders => 'Ordner', files => 'Datei(en)' },
es => { index_of => 'Índice de', folders => 'carpeta(s)', files => 'archivo(s)' },
it => { index_of => 'Indice di', folders => 'cartella(e)',files => 'file' },
pt => { index_of => 'Índice de', folders => 'pasta(s)', files => 'arquivo(s)' },
nl => { index_of => 'Index van', folders => 'map(pen)', files => 'bestand(en)' },
pl => { index_of => 'Indeks', folders => 'folder(y)', files => 'plik(i)' },
ru => { index_of => 'Индекс', folders => 'папок', files => 'файлов' },
ja => { index_of => 'インデックス:', folders => 'フォルダ', files => 'ファイル' },
zh => { index_of => '索引:', folders => '个文件夹', files => '个文件' },
);
# ---------------------------------------------------------------------------
# Options
# ---------------------------------------------------------------------------
my ($custom_title, $no_recursive, $lang_code, $dark, $help);
GetOptions(
't|title=s' => \$custom_title,
'n|no-recursive' => \$no_recursive,
'l|language=s' => \$lang_code,
'd|dark' => \$dark,
'h|help' => \$help,
);
$lang_code //= 'en';
unless (exists $i18n{$lang_code}) {
warn "Unknown language '$lang_code'. Available: " . join(', ', sort keys %i18n) . ". Falling back to 'en'.\n";
$lang_code = 'en';
}
my %t = %{ $i18n{$lang_code} };
if ($help) {
(my $s = basename($0)) =~ s/\.pl$//;
print <<HELP;
$s — Generates an HTML index page listing files and directories.
Usage: $s [options] [directory]
Options:
-t, --title TITLE Title displayed at the top of the page (default: folder name)
-n, --no-recursive Do not recurse into subdirectories (default: recursive)
-l, --language CODE Language for page output: en fr de es it pt nl pl ru ja zh (default: en)
-d, --dark Use dark theme
-h, --help Show this help
Examples:
$s # current directory, recursive
$s -n # current directory, top level only
$s --no-recursive --title "My files" /my/path
$s -l fr -t "Mes fichiers"
HELP
exit 0;
}
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
my $root = @ARGV ? abs_path($ARGV[0]) : cwd();
$root =~ s|/+$||;
my $title = $custom_title // basename($root);
my $heading = $custom_title ? $custom_title : "$t{index_of} $title";
my $outfile = "$root/index.html";
my $script = basename($0);
my ($total_dirs, $total_files) = (0, 0);
sub human_size {
my ($bytes) = @_;
my @units = ('B', 'KB', 'MB', 'GB', 'TB');
my $i = 0;
while ($bytes >= 1024 && $i < $#units) { $bytes /= 1024; $i++ }
return $i == 0 ? "${bytes} $units[$i]" : sprintf("%.1f %s", $bytes, $units[$i]);
}
sub file_icon {
my ($name) = @_;
my ($ext) = $name =~ /\.([^.]+)$/;
$ext = lc($ext // '');
my %icons = (
# Images
jpg => '🖼️', jpeg => '🖼️', png => '🖼️', gif => '🖼️',
bmp => '🖼️', svg => '🖼️', webp => '🖼️', ico => '🖼️',
tiff => '🖼️', tif => '🖼️', heic => '🖼️', raw => '🖼️',
# Videos
mp4 => '🎬', avi => '🎬', mkv => '🎬', mov => '🎬',
wmv => '🎬', flv => '🎬', webm => '🎬', m4v => '🎬',
mpg => '🎬', mpeg => '🎬', ts => '🎬', divx => '🎬',
# Audio
mp3 => '🎵', wav => '🎵', flac => '🎵', ogg => '🎵',
m4a => '🎵', aac => '🎵', wma => '🎵', opus => '🎵',
# PDF
pdf => '📕',
# Text documents
doc => '📝', docx => '📝', odt => '📝', rtf => '📝',
# Spreadsheets
xls => '📊', xlsx => '📊', ods => '📊', csv => '📊',
# Presentations
ppt => '📋', pptx => '📋', odp => '📋',
# Archives
zip => '📦', tar => '📦', gz => '📦', bz2 => '📦',
'7z' => '📦', rar => '📦', xz => '📦', zst => '📦',
tgz => '📦', tbz2 => '📦',
# Code
pl => '💻', py => '💻', js => '💻', ts => '💻',
php => '💻', c => '💻', cpp => '💻', h => '💻',
java => '💻', rb => '💻', go => '💻', rs => '💻',
cs => '💻', swift=> '💻', kt => '💻', lua => '💻',
r => '💻', m => '💻',
# Web
html => '🌐', htm => '🌐', css => '🌐', jsx => '🌐',
tsx => '🌐', vue => '🌐',
# Shell scripts
sh => '⚙️', bash => '⚙️', zsh => '⚙️', fish => '⚙️',
bat => '⚙️', ps1 => '⚙️',
# Config / data
json => '🔧', yaml => '🔧', yml => '🔧', toml => '🔧',
xml => '🔧', ini => '🔧', conf => '🔧', cfg => '🔧',
env => '🔧',
# Databases
db => '🗄️', sqlite => '🗄️', sql => '🗄️',
# Plain text / docs
txt => '📄', md => '📄', rst => '📄', log => '📄',
# Fonts
ttf => '🔤', otf => '🔤', woff => '🔤', woff2 => '🔤',
# Executables / binaries
exe => '🖥️', run => '🖥️', bin => '🖥️', apk => '🖥️',
deb => '🖥️', rpm => '🖥️', dmg => '🖥️', iso => '🖥️',
);
return $icons{$ext} // '📄';
}
sub url_encode {
my ($str) = @_; # decoded Unicode string
my $bytes = encode_utf8($str); # back to UTF-8 bytes
$bytes =~ s/([^A-Za-z0-9\-_.~\/])/sprintf("%%%02X", ord($1))/ge;
return $bytes;
}
# Scans $abs_dir, writes to $fh, links relative to $root
sub scan_dir {
my ($fh, $abs_dir, $indent) = @_;
my $pad = " " x $indent;
opendir(my $dh, $abs_dir) or do { warn "Cannot open '$abs_dir': $!\n"; return };
my @entries = sort grep { !/^\./ } readdir($dh);
closedir($dh);
my @dirs = grep { -d "$abs_dir/$_" } @entries;
my @files = grep { -f "$abs_dir/$_" } @entries;
for my $d (@dirs) {
my $d_display = decode_utf8($d);
$total_dirs++;
if (!$no_recursive) {
print $fh "$pad<li><span class=\"dir\">📁 $d_display</span>\n$pad <ul>\n";
scan_dir($fh, "$abs_dir/$d", $indent + 2);
print $fh "$pad </ul>\n$pad</li>\n";
} else {
print $fh "$pad<li><span class=\"dir\">📁 $d_display</span></li>\n";
}
}
for my $f (@files) {
next if $abs_dir eq $root && ($f eq 'index.html' || $f eq $script);
my $rel = abs_path("$abs_dir/$f") =~ s|^\Q$root\E/||r;
my $href = url_encode(decode_utf8($rel));
my $f_display = decode_utf8($f);
my $size = human_size(-s "$abs_dir/$f");
$total_files++;
my $icon = file_icon($f);
print $fh "$pad<li>$icon <a href=\"$href\">$f_display</a><span class=\"size\">$size</span></li>\n";
}
}
open(my $fh, '>:encoding(UTF-8)', $outfile) or die "Cannot write '$outfile': $!\n";
print $fh <<"HTML";
<!DOCTYPE html>
<html lang="$lang_code">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>$heading</title>
<style>
:root {
--bg: #ffffff;
--fg: #222222;
--link: #0969da;
--dir: #444444;
--size: #888888;
--border: #cccccc;
--tree: #bbbbbb;
--footer: #888888;
}
body.dark {
--bg: #0d1117;
--fg: #e6edf3;
--link: #58a6ff;
--dir: #adbac7;
--size: #768390;
--border: #30363d;
--tree: #4d5a6e;
--footer: #768390;
}
body { font-family: sans-serif; max-width: 900px; margin: 2em auto;
color: var(--fg); background: var(--bg); }
h1 { border-bottom: 2px solid var(--border); padding-bottom: .4em; }
ul.tree, ul.tree ul {
list-style: none;
padding-left: 1.6em;
margin: .05em 0;
}
ul.tree { padding-left: .4em; }
ul.tree li {
position: relative;
padding: .15em 0;
}
/* horizontal arm ─── */
ul.tree li::before {
content: "";
position: absolute;
left: -1.3em;
top: .68em;
width: 1.1em;
border-top: 1px solid var(--tree);
}
/* vertical bar │ — half-height on last child, full on others */
ul.tree li::after {
content: "";
position: absolute;
left: -1.3em;
top: 0;
height: .68em;
border-left: 1px solid var(--tree);
}
ul.tree li:not(:last-child)::after { height: 100%; }
/* no connector lines on root items */
ul.tree > li::before,
ul.tree > li::after { display: none; }
a { text-decoration: none; color: var(--link); }
a:hover { text-decoration: underline; }
.dir { font-weight: bold; color: var(--dir); }
.size { color: var(--size); font-size: .85em; margin-left: .8em; }
footer { margin-top: 2em; font-size: .8em; color: var(--footer); }
</style>
</head>
<body${\ ($dark ? ' class="dark"' : '')}>
<h1>$heading</h1>
<ul class="tree">
HTML
scan_dir($fh, $root, 2);
print $fh <<"HTML";
</ul>
<footer>$total_dirs $t{folders}, $total_files $t{files}</footer>
</body>
</html>
HTML
close($fh);
print "index.html generated in: $outfile\n";
print " $total_dirs folder(s), $total_files file(s)\n";