+sub chapters_in_order {
+ my $dir = shift;
+
+ opendir DIR, $dir or die "opendir($dir) failed: $!\n";
+ my @numeric = sort grep {/^ch\d+\.html$/} readdir(DIR);
+ closedir(DIR) or die "closedir($dir) failed: $!\n";
+
+ my @results = map {
+ $_ = File::Spec->catfile($dir, $_);
+ if (-l $_) {
+ my $t;
+ eval { $t = readlink $_ };
+ $_ = File::Spec->rel2abs($t, $dir) if defined $t;
+ }
+ $_
+ } @numeric;
+ return @results;
+}
+
+