commit code crimes for better footnote formatting

This commit is contained in:
snow flurry 2022-03-21 19:01:21 -07:00
parent c6fd874bc5
commit c38561f9a9
2 changed files with 44 additions and 16 deletions

View file

@ -42,31 +42,59 @@ sub post_to_meta {
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;
} elsif (/^---$/) {
last;
} else {
warn basename($fname) . ":" . $. . ": malformed line; ignored"
}
} else {
$content{$contentlevel} .= "$_\n";
}
}
my $body = do { local $/; <MDIN> };
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"});
if ($body ne "") {
my $parser = Markdent::Simple::Fragment->new;
$metadata{content} = $parser->markdown_to_html(
dialects => 'GitHub',
markdown => $body
);
}
%metadata;
}

View file

@ -31,7 +31,7 @@ for (@fharr) {
print "$fname:$.: Overwriting existing \`$metavar\'\n";
$done++;
}
if (s/^(---)$/$metavar=$timestamp\n$1/) {
if (s/^(--.+?--)$/$metavar=$timestamp\n$1/) {
print "$fname:$.: Inserting new \`$metavar\'\n";
$done++;
}