~jbauer/paritybit.ca

paritybit.ca/tuptime-update.pl -rwxr-xr-x 1.8 KiB
c8b1e086Jake Bauer Publish new blog post 2 days ago
                                                                                
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
#!/bin/perl

use strict;
use warnings;
use HTML::TreeBuilder;
use File::Copy qw(copy move);

# Copy about-site file so we can work on it without disturbing live copy
copy '/var/www/html/about-site.html', './about-site.html';
my $outfile = './about-site.html.new';
my $infile = './about-site.html';

# Open both infile and outfile for reading/writing respectively
open(my $fh, '<:encoding(UTF-8)', $infile)
    or die "Could not open file '$infile' $!";
open my $out, '>', "$outfile" or die "Can't write new file: $!";

# Run the tuptime command to get output for putting into outfile
my $cmd = "tuptime";
my @output = `$cmd`;
chomp @output;

# Look through each line of the infile, write the line to outfile unless we are
# in between <pre></pre> in which case write out the output of tuptime.
my $inPreFlag = 0;
my $arrIndex = 0;
while (my $row = <$fh>) {
    chomp $row;
    # Create a new HTML tree to parse HTML tags from $row
    my $tr = HTML::TreeBuilder->new_from_content($row);

    # If this line contains a <pre> tag, set the proper flag to begin printing
    # output from tuptime.
    if ($tr->find_by_tag_name('pre')) {
        $inPreFlag = 1;
        print "Entering pre\n";
        print $out "<pre><code>";
        next;
    }
    # Check if we have printed out every line from the output of tuptime
    if ($arrIndex == scalar @output) {
        # Reset arrIndex so the above doesn't continue to resolve to true
        $arrIndex = 0;
        $inPreFlag = 0;
        print "Exiting pre\n";
        print $out "</code></pre>\n";
    }

    if ($inPreFlag) {
        print $out "$output[$arrIndex]\n";
        $arrIndex++;
    }
    else {
        print $out "$row\n";
    }
}

# Finally, overwrite the old about-site file with this new one:
move './about-site.html.new', '/var/www/html/about-site.html';
# And remove the copy of the old one
unlink('./about-site.html');