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 * Cwd
* File::Copy::Recursive * File::Copy::Recursive
* File::Path * File::Path
* Markdent * File::Slurp
* Text::FrontMatter::YAML
* Text::MultiMarkdown
* Text::Template * Text::Template
ptouch.pl uses the following modules: 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::Copy::Recursive qw(dircopy);
use File::Find qw(finddepth); use File::Find qw(finddepth);
use File::Path qw(make_path); use File::Path qw(make_path);
use Markdent::Simple::Fragment; use File::Slurp;
use POSIX qw(strftime); use POSIX qw(strftime);
use Text::FrontMatter::YAML;
use Text::Template; use Text::Template;
use Text::MultiMarkdown 'markdown';
use strict; use strict;
use utf8; use utf8;
@ -39,68 +41,34 @@ my $postout_path = $out_path . POSTS_PATH;
# $metadata{"fname"}). # $metadata{"fname"}).
sub post_to_meta { sub post_to_meta {
defined(my $fname = shift) or warn "No filename argument!"; 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 $metadata = $mdfm->frontmatter_hashref;
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"}) { if (chomp($mdfm->body_text) ne "") {
my $ifn = 0; my $parser = Text::MultiMarkdown->new(
my %fn; disable_bibliography => 1,
my $parser = Markdent::Simple::Fragment->new; use_metadata => 0,
document_format => 'fragment'
);
# perl regexes give people like me too much power # for very funny bits, i assure you
$content{"body"} =~ s{\$fn:(.+)\$}{ $mdfm->body_text =~ s{\$cn\$}{
$fn{$1} = ++$ifn;
"<a class=\"fn\" onclick=\"fnClick(this)\" name=\"fn-$1\" href=\"#fn-$1-text\">[" . $ifn . "]</a>"
}egm;
$content{"body"} =~ s{\$cn\$}{
"<a class=\"fn\">[citation needed]</a>" "<a class=\"fn\">[citation needed]</a>"
}egm; }egm;
$metadata{content} = $parser->markdown_to_html( $metadata->{content} = $parser->markdown( $mdfm->body_text );
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 # HACK: Stuffing the basename in the metadata because I don't want
# to deal with hashes of hashes # 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. # Gets an array of all posts for a directory.
@ -116,8 +84,8 @@ sub all_posts_for_dir {
opendir(PD, $postdir) or die $!; opendir(PD, $postdir) or die $!;
while (my $fname = readdir(PD)) { while (my $fname = readdir(PD)) {
next if ($fname =~ /^\.+$/); next if ($fname =~ /^\.+$/);
my %post = &post_to_meta("$postdir/$fname"); my $post = &post_to_meta("$postdir/$fname");
push @posts, \%post; push @posts, $post;
} }
closedir(PD); closedir(PD);