build: Use MultiMarkdown + Text::FrontMatter::YAML

This commit is contained in:
snow flurry 2024-02-07 10:39:09 -08:00
parent 4c5cb560c8
commit e89cb21c82
2 changed files with 24 additions and 54 deletions

View file

@ -9,7 +9,9 @@ build.pl uses the following modules:
* Cwd
* File::Copy::Recursive
* File::Path
* Markdent
* File::Slurp
* Text::FrontMatter::YAML
* Text::MultiMarkdown
* Text::Template
ptouch.pl uses the following modules:

View file

@ -5,9 +5,11 @@ 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 File::Slurp;
use POSIX qw(strftime);
use Text::FrontMatter::YAML;
use Text::Template;
use Text::MultiMarkdown 'markdown';
use strict;
use utf8;
@ -39,68 +41,34 @@ my $postout_path = $out_path . POSTS_PATH;
# $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: " . $!;
my $fdata = read_file($fname);
my $mdfm = Text::FrontMatter::YAML->new(
document_string => $fdata
);
# "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);
my $metadata = $mdfm->frontmatter_hashref;
if (exists $content{"body"}) {
my $ifn = 0;
my %fn;
my $parser = Markdent::Simple::Fragment->new;
if (chomp($mdfm->body_text) ne "") {
my $parser = Text::MultiMarkdown->new(
disable_bibliography => 1,
use_metadata => 0,
document_format => 'fragment'
);
# perl regexes give people like me too much power
$content{"body"} =~ s{\$fn:(.+)\$}{
$fn{$1} = ++$ifn;
"<a class=\"fn\" onclick=\"fnClick(this)\" name=\"fn-$1\" href=\"#fn-$1-text\">[" . $ifn . "]</a>"
}egm;
$content{"body"} =~ s{\$cn\$}{
# for very funny bits, i assure you
$mdfm->body_text =~ s{\$cn\$}{
"<a class=\"fn\">[citation needed]</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"}
);
}
$metadata->{content} = $parser->markdown( $mdfm->body_text );
}
# 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->{fname} = basename($fname) unless exists($metadata->{"fname"});
%metadata;
$metadata;
}
# Gets an array of all posts for a directory.
@ -116,8 +84,8 @@ sub all_posts_for_dir {
opendir(PD, $postdir) or die $!;
while (my $fname = readdir(PD)) {
next if ($fname =~ /^\.+$/);
my %post = &post_to_meta("$postdir/$fname");
push @posts, \%post;
my $post = &post_to_meta("$postdir/$fname");
push @posts, $post;
}
closedir(PD);