dg-x/build.pl

228 lines
6.4 KiB
Perl
Executable file

#!/usr/bin/env perl
use Cwd qw(getcwd);
use File::Basename qw(dirname basename);
use File::Copy::Recursive qw(dircopy);
use File::Find qw(finddepth);
use File::Path qw(make_path);
use Markdent::Simple::Fragment;
use POSIX qw(strftime);
use Text::Template;
use strict;
use utf8;
use constant {
OUT_PATH => "/out",
POSTS_PATH => "/posts",
ASSETS_PATH => "/assets",
PAGES_PATH => "/pages",
TEMPLATES_PATH => "/templates",
};
# globals
my $cwd = getcwd;
my $post_dir = $cwd . POSTS_PATH;
my $pages_dir = $cwd . PAGES_PATH;
my $tmpl_dir = $cwd . TEMPLATES_PATH;
my $post_tmpl = Text::Template->new(SOURCE => "$tmpl_dir/post.html.tmpl");
my $assets_path = $cwd . ASSETS_PATH;
my $out_path = $cwd . OUT_PATH;
my $postout_path = $out_path . POSTS_PATH;
# Converts a post file to a metadata hash.
#
# Takes one argument, the path to the post file.
#
# Returns a hash with the relevant metadata (body text is stored as
# $metadata{"content"}, desired filename is stored as
# $metadata{"fname"}).
sub post_to_meta {
defined(my $fname = shift) or warn "No filename argument!";
my %metadata;
open(MDIN, '<', $fname) or die "Unable to open $fname: " . $!;
# "parsing" loop
my $contentlevel = "";
my %content;
while (<MDIN>) {
chomp;
if (/^--(.+)?--$/) {
$contentlevel = $1;
$content{$contentlevel} = "";
} elsif ($contentlevel eq "") {
if (/^(.+)?=(.*)$/) {
$metadata{$1} = $2;
} else {
warn basename($fname) . ":" . $. . ": malformed line; ignored"
}
} else {
$content{$contentlevel} .= "$_\n";
}
}
close(MDIN);
if (exists $content{"body"}) {
my $ifn = 0;
my %fn;
my $parser = Markdent::Simple::Fragment->new;
# perl regexes give people like me too much power
$content{"body"} =~ s{\$fn:(.+)\$}{
$fn{$1} = ++$ifn;
"<a class=\"fn\" name=\"fn-$1\" href=\"#fn-$1-text\">[" . $ifn . "]</a>"
}egm;
$metadata{content} = $parser->markdown_to_html(
dialects => 'GitHub',
markdown => $content{"body"}
);
# oh also, parse the footnotes
if (exists $content{"footnotes"}) {
$metadata{content} .= "\n<hr />\n";
$content{"footnotes"} =~ s{^fn:(.+):}{
"<a name=\"fn-$1-text\" href=\"#fn-$1\">^</a>" . $fn{$1} . ":"
}egm;
$metadata{content} .= $parser->markdown_to_html(
dialects => 'GitHub',
markdown => $content{"footnotes"}
);
}
}
# HACK: Stuffing the basename in the metadata because I don't want
# to deal with hashes of hashes
$metadata{fname} = basename($fname) unless exists($metadata{"fname"});
%metadata;
}
# Gets an array of all posts for a directory.
#
# Takes one argument, the path to the posts directory.
#
# Returns an array of metadata hashes (see post_to_meta above for
# more on what those hashes look like).
sub all_posts_for_dir {
defined(my $postdir = shift) or warn "No directory argument!";
my @posts;
opendir(PD, $postdir) or die $!;
while (my $fname = readdir(PD)) {
next if ($fname =~ /^\.+$/);
my %post = &post_to_meta("$postdir/$fname");
push @posts, \%post;
}
closedir(PD);
sort { $b->{created} cmp $a->{created} } @posts;
}
sub all_pages_for_dir {
defined(my $pagedir = shift) or warn "No directory argument!";
my @pages;
finddepth(sub {
return if ($File::Find::name =~ /^\.+$/);
my %page;
$_ = $File::Find::name;
if (/^$pagedir\/(.+).tmpl$/) {
$page{fname} = $1;
$page{tmpl} = Text::Template->new(SOURCE => $File::Find::name);
push @pages, \%page;
}
}, $pagedir);
@pages;
}
sub mkpath {
make_path( shift, { error => \my $path_err } );
if ($path_err && @$path_err) {
print "Errors occurred while making posts directory!\n";
foreach my $err (@$path_err) {
my ($path, $msg) = %$err;
if ($path eq '') {
print " $msg\n";
} else {
print " $path: $msg\n";
}
}
# effectively die
return 0;
} else {
return 1;
}
}
sub include_tmpl {
my $tmpl_name = shift;
# TODO: do we want to cache snippet-type templates like this?
my $tmpl = Text::Template->new(SOURCE => "$tmpl_dir/$tmpl_name.tmpl");
if (defined $tmpl) {
my $props = shift;
$tmpl->fill_in(HASH => { props => \$props });
} else {
"error: $tmpl_dir/$tmpl_name.tmpl is missing"
}
}
## End subroutines
# subroutines used in templates
my %utils = (
strftime => \&strftime,
include_tmpl => \&include_tmpl,
);
# make posts dir or die
mkpath($postout_path) or die "Unable to create directory.";
my @posts = all_posts_for_dir $post_dir;
print "Generating blog posts...\n";
foreach my $post (@posts) {
if (!exists $post->{content}) {
print " $post->{fname} has no content, not making a page...\n";
next;
}
print " Processing $post->{fname}...\n";
my %post_hash = %utils;
$post_hash{post} = \$post;
my $post_content = $post_tmpl->fill_in(HASH => \%post_hash);
if (defined($post_content)) {
open(POSTOUT, '>', "$postout_path/$post->{fname}.html") or die("Unable to write $post->{fname}.html: $!");
print POSTOUT $post_content;
close(POSTOUT);
} else {
die "Failed to process $post->{fname}: $Text::Template::ERROR";
}
}
print "Generating pages...\n";
my @pages = all_pages_for_dir $pages_dir;
foreach my $pg (@pages) {
mkpath(dirname("$out_path/" . $pg->{fname})) or die "Unable to create directory.";
print " Processing $pg->{fname}...\n";
my %page_hash = %utils;
$page_hash{posts} = \@posts;
my $page_content = $pg->{tmpl}->fill_in(HASH => \%page_hash);
if (defined($page_content)) {
open(PGOUT, '>', "$out_path/$pg->{fname}") or die("Unable to write $pg->{fname}: $!");
print PGOUT $page_content;
close(PGOUT);
} else {
die "Failed to process $pg->{fname}: $Text::Template::ERROR";
}
}
print "Copying static assets...\n";
local $File::Copy::Recursive::RMTrgDir = 2;
dircopy($assets_path, "$out_path/" . ASSETS_PATH) or die "Unable to copy assets: $!";
print "Done! Artifacts have been stored in $out_path.\n";