-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-md-dir.pl
72 lines (61 loc) · 1.79 KB
/
generate-md-dir.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Path;
# use File::Slurp;
# input and output directory
my $html_dir = '/mnt/cephfs/ie-web/ie/';
my $md_dir = '/mnt/cephfs/ie-web/ie-hugo/content/ja/';
my @files = <>;
chop @files;
foreach my $file ( @files ) {
find(\&wanted, $html_dir . $file);
}
sub wanted {
# Only proceed for HTML files
if (m/\.html$/) {
my $html_file = $File::Find::name;
# Generate output markdown filename
my $md_file = $html_file;
$md_file =~ s/index.html/_index.html/;
$md_file =~ s/^$html_dir/$md_dir/;
$md_file =~ s/\.html$/.md/;
$md_file =~ s/%([0-9A-Fa-f]{2})/pack('C', hex($1))/ge;
# Create directory path if it doesn't exist
my $md_dirname = $md_file;
$md_dirname =~ s/\/[^\/]+$//;
mkpath($md_dirname) unless (-d $md_dirname);
system "perl /ie-ryukyu/podman/web-servers/web-to-md/html-to-md.pl $html_file > $md_file\n";
# we have to remove empty index.html file
# if ($md_file =~ /pros/) {
# print "pros\n";
# }
open(my $md_fh, '<', $md_file) or die "Unable to open $md_file: $!";
my $rm = 1;
my $skip = 6;
while (<$md_fh>) {
if ($skip) {
$skip--;
next;
}
next if (/^\[\]/);
next if (/^受験生の方へ/);
next if (/^\s*$/);
$rm = 0;
last;
}
close($md_fh);
if ($rm) {
print "rm $md_file\n";
unlink $md_file or die "Unable to remove $md_file: $!";
} else {
# Get the modification time of the original file
my $orig_time = (stat $html_file)[9];
# Set the modification time of the markdown file to match the original
utime $orig_time, $orig_time, $md_file or die "Couldn't touch $md_file: $!";
print "generated $md_file\n";
}
}
}
print "Conversion complete\n";