Change Markdown templating engine
This commit is contained in:
parent
e89cb21c82
commit
b848bc58c9
129
build.pl
129
build.pl
|
@ -9,7 +9,7 @@ use File::Slurp;
|
||||||
use POSIX qw(strftime);
|
use POSIX qw(strftime);
|
||||||
use Text::FrontMatter::YAML;
|
use Text::FrontMatter::YAML;
|
||||||
use Text::Template;
|
use Text::Template;
|
||||||
use Text::MultiMarkdown 'markdown';
|
use Markdent::Simple::Fragment;
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use utf8;
|
use utf8;
|
||||||
|
@ -31,6 +31,69 @@ my $post_tmpl = Text::Template->new(SOURCE => "$tmpl_dir/post.html.tmpl");
|
||||||
my $assets_path = $cwd . ASSETS_PATH;
|
my $assets_path = $cwd . ASSETS_PATH;
|
||||||
my $out_path = $cwd . OUT_PATH;
|
my $out_path = $cwd . OUT_PATH;
|
||||||
my $postout_path = $out_path . POSTS_PATH;
|
my $postout_path = $out_path . POSTS_PATH;
|
||||||
|
my $return_link = qq{<img src="/assets/img/return.gif" />};
|
||||||
|
|
||||||
|
# used across a couple functions
|
||||||
|
my $parser = Markdent::Simple::Fragment->new;
|
||||||
|
|
||||||
|
sub make_fragment {
|
||||||
|
my $body = shift;
|
||||||
|
$parser->markdown_to_html(
|
||||||
|
dialect => 'GitHub',
|
||||||
|
markdown => $body,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
sub strip_id {
|
||||||
|
my $id = shift;
|
||||||
|
$id =~ s/[ \t]/-/g;
|
||||||
|
$id =~ s/[^A-Za-z0-9_-]//g;
|
||||||
|
return $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
sub do_footnotes {
|
||||||
|
my $text = shift;
|
||||||
|
|
||||||
|
my %footnotes;
|
||||||
|
my @used;
|
||||||
|
|
||||||
|
my $footnote_counter = 0;
|
||||||
|
|
||||||
|
# First grab the definitions
|
||||||
|
while ($text =~ s{
|
||||||
|
\n\[\^([^\n]+?)\]\:[ \t]*
|
||||||
|
\n?
|
||||||
|
(.*?)(?:\n{1,2} # end at new paragraph
|
||||||
|
(?=\n[ ]{0,4}\S)|\Z) # Lookahead for non-space at line-start, or end of doc
|
||||||
|
}{\n}sx) {
|
||||||
|
my $id = strip_id($1);
|
||||||
|
$footnotes{$1} = make_fragment(qq{$2 [$return_link](#fnref:$id)});
|
||||||
|
}
|
||||||
|
|
||||||
|
# then replace the inline footnotes
|
||||||
|
$text =~ s{
|
||||||
|
\[\^(.*?)\]
|
||||||
|
}{
|
||||||
|
my $result = '';
|
||||||
|
my $id = strip_id($1);
|
||||||
|
if (defined $footnotes{$id}) {
|
||||||
|
$footnote_counter++;
|
||||||
|
$result = qq{<sup>[<a href="#fn:$id" id="fnref:$id" class="footnote">$footnote_counter</a>]</sup>};
|
||||||
|
push(@used, $id);
|
||||||
|
}
|
||||||
|
$result;
|
||||||
|
}xsge;
|
||||||
|
|
||||||
|
my $fn_block = qq{<div id="footnotes"><hr /><ol>};
|
||||||
|
# finally, append the footnotes
|
||||||
|
foreach my $id (@used) {
|
||||||
|
my $footnote = $footnotes{$id};
|
||||||
|
$fn_block .= qq{<li id="fn:$id">$footnote</li>};
|
||||||
|
}
|
||||||
|
$fn_block .= qq{</ol></div>};
|
||||||
|
|
||||||
|
return ($text, $fn_block);
|
||||||
|
}
|
||||||
|
|
||||||
# Converts a post file to a metadata hash.
|
# Converts a post file to a metadata hash.
|
||||||
#
|
#
|
||||||
|
@ -49,19 +112,65 @@ sub post_to_meta {
|
||||||
|
|
||||||
my $metadata = $mdfm->frontmatter_hashref;
|
my $metadata = $mdfm->frontmatter_hashref;
|
||||||
|
|
||||||
if (chomp($mdfm->body_text) ne "") {
|
chomp(my $body = $mdfm->data_text);
|
||||||
my $parser = Text::MultiMarkdown->new(
|
if ($body ne "") {
|
||||||
disable_bibliography => 1,
|
|
||||||
use_metadata => 0,
|
|
||||||
document_format => 'fragment'
|
|
||||||
);
|
|
||||||
|
|
||||||
# for very funny bits, i assure you
|
# for very funny bits, i assure you
|
||||||
$mdfm->body_text =~ s{\$cn\$}{
|
$body =~ s{\[\^citation\sneeded\]}{
|
||||||
"<a class=\"fn\">[citation needed]</a>"
|
"<sup>[<i>citation needed</i>]</sup>"
|
||||||
}egm;
|
}egm;
|
||||||
|
|
||||||
$metadata->{content} = $parser->markdown( $mdfm->body_text );
|
my $tag_attrs = qr{
|
||||||
|
(?: # Match one attr name/value pair
|
||||||
|
\s+ # There needs to be at least some whitespace
|
||||||
|
# before each attribute name.
|
||||||
|
[\w.:_-]+ # Attribute name
|
||||||
|
\s*=\s*
|
||||||
|
(?:
|
||||||
|
".+?" # "Attribute value"
|
||||||
|
|
|
||||||
|
'.+?' # 'Attribute value'
|
||||||
|
|
|
||||||
|
[^\s]+? # AttributeValue (HTML5)
|
||||||
|
)
|
||||||
|
)* # Zero or more
|
||||||
|
}x;
|
||||||
|
my $markdown_attr = qr{ \s* markdown \s* = \s* (['"]) (.*?) \1 }xs;
|
||||||
|
my $empty_tag = qr{< \w+ $tag_attrs \s* />}oxms;
|
||||||
|
|
||||||
|
use Text::Balanced qw(gen_extract_tagged);
|
||||||
|
my $extract_block = gen_extract_tagged(qr{< div $tag_attrs \s* >}oxms,
|
||||||
|
undef,
|
||||||
|
undef,
|
||||||
|
{ ignore => [$empty_tag] });
|
||||||
|
|
||||||
|
$body =~ s{
|
||||||
|
(
|
||||||
|
<div.*?</div>
|
||||||
|
)
|
||||||
|
}{
|
||||||
|
my $return = $1;
|
||||||
|
my ($tag, $remainder, $prefix, $opening_tag, $text_in_tag, $closing_tag) = $extract_block->($return);
|
||||||
|
if ($tag) {
|
||||||
|
if ($opening_tag =~ s/$markdown_attr//i) {
|
||||||
|
my $markdown = $2;
|
||||||
|
if ($markdown =~ /^(1|on|yes)$/) {
|
||||||
|
$tag = $prefix . $opening_tag . "\n"
|
||||||
|
. make_fragment($text_in_tag) . "\n" . $closing_tag;
|
||||||
|
} else {
|
||||||
|
$tag = $prefix . $opening_tag . $text_in_tag . $closing_tag;
|
||||||
|
}
|
||||||
|
$return = $tag;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$return;
|
||||||
|
}egmxs;
|
||||||
|
|
||||||
|
my $fn_body;
|
||||||
|
($body, $fn_body) = do_footnotes($body);
|
||||||
|
$fn_body = "" unless defined $fn_body;
|
||||||
|
|
||||||
|
$metadata->{content} = make_fragment($body) . $fn_body;
|
||||||
}
|
}
|
||||||
|
|
||||||
# HACK: Stuffing the basename in the metadata because I don't want
|
# HACK: Stuffing the basename in the metadata because I don't want
|
||||||
|
|
37
ptouch.pl
37
ptouch.pl
|
@ -3,9 +3,10 @@
|
||||||
use strict;
|
use strict;
|
||||||
use utf8;
|
use utf8;
|
||||||
use Getopt::Std;
|
use Getopt::Std;
|
||||||
use Tie::File;
|
use File::Slurp;
|
||||||
|
use Text::FrontMatter::YAML;
|
||||||
|
|
||||||
$::VERSION = "1.0.0";
|
$::VERSION = "2.0.0";
|
||||||
|
|
||||||
getopts("ne", \my %opts);
|
getopts("ne", \my %opts);
|
||||||
|
|
||||||
|
@ -23,24 +24,20 @@ if (defined $opts{n}) {
|
||||||
my $timestamp = time;
|
my $timestamp = time;
|
||||||
|
|
||||||
my $fname = shift @ARGV;
|
my $fname = shift @ARGV;
|
||||||
tie my @fharr, 'Tie::File', $fname or die $!;
|
|
||||||
|
|
||||||
my $done = 0;
|
my $fdata = read_file($fname);
|
||||||
for (@fharr) {
|
my $mdfm = Text::FrontMatter::YAML->new(
|
||||||
if (s/^$metavar=.+/$metavar=$timestamp/) {
|
document_string => $fdata
|
||||||
print "$fname:$.: Overwriting existing \`$metavar\'\n";
|
);
|
||||||
$done++;
|
|
||||||
}
|
|
||||||
if (s/^(--.+?--)$/$metavar=$timestamp\n$1/) {
|
|
||||||
print "$fname:$.: Inserting new \`$metavar\'\n";
|
|
||||||
$done++;
|
|
||||||
}
|
|
||||||
last if ($done);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$done) {
|
my $metadata = $mdfm->frontmatter_hashref;
|
||||||
print "$fname: No content and no \`$metavar\', inserting at end\n";
|
$metadata->{$metavar} = $timestamp;
|
||||||
push @fharr, "$metavar=$timestamp";
|
|
||||||
}
|
|
||||||
|
|
||||||
untie @fharr;
|
my $wrfm = Text::FrontMatter::YAML->new(
|
||||||
|
frontmatter_hashref => $metadata,
|
||||||
|
data_text => $mdfm->data_text
|
||||||
|
);
|
||||||
|
|
||||||
|
write_file($fname, $wrfm->document_string);
|
||||||
|
|
||||||
|
1;
|
||||||
|
|
Loading…
Reference in a new issue