| 1 | #!/usr/bin/env perl | ||
| 2 | # Original: https://github.com/defunkt/mustache/blob/master/bin/mustache | ||
| 3 | |||
| 4 | # PODNAME: mustache.pl | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use autodie; | ||
| 9 | |||
| 10 | use Template::Mustache; | ||
| 11 | use YAML::XS qw/ Load /; | ||
| 12 | |||
| 13 | my ($self, $args) = @_; | ||
| 14 | my @args = $args ? @$args : @ARGV; | ||
| 15 | my $cli_opt = shift @args || '-h'; | ||
| 16 | |||
| 17 | if ($cli_opt eq "-h" and -t STDIN) { | ||
| 18 | print "Usage: mustache FILE ...\n"; | ||
| 19 | print "`perldoc mustache` for more usage info.\n"; | ||
| 20 | } | ||
| 21 | else { | ||
| 22 | my $doc = join('', <>); | ||
| 23 | if($doc =~ /^(\s*---(.+)---\s*)/isg) { | ||
| 24 | my $yml = $2; | ||
| 25 | $doc =~ s/\Q$1\E//g; # remove YAML data block, quote metachars | ||
| 26 | $yml =~ s/^\s+|\s+$//g; # trim whitespace | ||
| 27 | $yml = Load($yml); | ||
| 28 | print Template::Mustache->render($doc, $yml); | ||
| 29 | } | ||
| 30 | else { | ||
| 31 | print Template::Mustache->render($doc); | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | __END__ |