aboutsummaryrefslogtreecommitdiff
path: root/template.pl
blob: 5c97e61196694c8b0a66800b4cc91e3e83adbe55 (plain)
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
#!/usr/bin/env perl

# this script is generally responsible for all html postprocessing

use v5.39;
use utf8;
use autodie;

use Mojo::DOM;
use File::Basename;

use open qw( :std :encoding(UTF-8) );

my $template = do {
    local $/;
    open my $fh, '<', 'template.html';
    <$fh>;
};

my $dom = Mojo::DOM->new($template);

my $content = `lowdown --html-no-head-ids $ARGV[0]`;

# inject content @ <article>
$dom->at('article')->content($content);

# replace <title> with the contents of the first <h1>
my $title_dom = $dom->at('h1');
$dom->at('title')->content($title_dom->text) if ($title_dom);

# makes the up button go up a directory
my $parent = dirname $ARGV[0];
$parent =~ s/^(?:.+?)(\/.*)/$1/;
$parent = dirname $parent if (basename($ARGV[0]) eq 'index.md' or $parent eq 'src');
$dom->at('#go-up')->attr(href => $parent);

# give title the same contents as alt if it exists
for my $img ($dom->find('img')->each) {
    my $alt = $img->attr('alt');
    $img->attr(title => $alt) if ($alt);
}

say "$dom";