diff --git a/README.md b/README.md index 3661938..dca1a2b 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/build.pl b/build.pl index 27a7d72..b264e02 100755 --- a/build.pl +++ b/build.pl @@ -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 () { - 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; - "[" . $ifn . "]" - }egm; - - $content{"body"} =~ s{\$cn\$}{ + # for very funny bits, i assure you + $mdfm->body_text =~ s{\$cn\$}{ "[citation needed]" }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"} - ); - } + $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);