From c38561f9a9871407797bce770f7b2095b42ce9a5 Mon Sep 17 00:00:00 2001 From: snow flurry Date: Mon, 21 Mar 2022 19:01:21 -0700 Subject: [PATCH] commit code crimes for better footnote formatting --- build.pl | 58 +++++++++++++++++++++++++++++++++++++++++-------------- ptouch.pl | 2 +- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/build.pl b/build.pl index a9eeed0..034fc4f 100755 --- a/build.pl +++ b/build.pl @@ -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 () { chomp; - if (/^(.+)?=(.*)$/) { - $metadata{$1} = $2; - } elsif (/^---$/) { - last; + if (/^--(.+)?--$/) { + $contentlevel = $1; + $content{$contentlevel} = ""; + } elsif ($contentlevel eq "") { + if (/^(.+)?=(.*)$/) { + $metadata{$1} = $2; + } else { + warn basename($fname) . ":" . $. . ": malformed line; ignored" + } } else { - warn basename($fname) . ":" . $. . ": malformed line; ignored" + $content{$contentlevel} .= "$_\n"; } } - - my $body = do { local $/; }; 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; + "[" . $ifn . "]" + }egm; + + $metadata{content} = $parser->markdown_to_html( + dialects => 'GitHub', + markdown => $content{"body"} + ); + + # oh also, parse the footnotes + if (exists $content{"footnotes"}) { + $metadata{content} .= "\n
\n"; + $content{"footnotes"} =~ s{^fn:(.+):}{ + "^" . $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; } diff --git a/ptouch.pl b/ptouch.pl index 291e13b..0330b38 100755 --- a/ptouch.pl +++ b/ptouch.pl @@ -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++; }