Skip to content

Instantly share code, notes, and snippets.

@skaji
Created September 27, 2012 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skaji/3795366 to your computer and use it in GitHub Desktop.
Save skaji/3795366 to your computer and use it in GitHub Desktop.
print templates
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
if ($ARGV[0] =~ /-h/) {
my ($script) = $0 =~ m{([^/]+)\z}xsm;
print <<"HELP";
usage:
$script ls # display available templates
$script xxx # print xxx template
It may be useful to use this script in vim:
:r! $script xxx
HELP
exit;
}
my $data = get_data(<<'END');
**** getopt
use Getopt::Long qw(
:config
posix_default
no_ignore_case
gnu_compat
);
GetOptions(
"help|h" => \my $help,
"size|s=i" => \my $size,
);
#GetOptions(\my %opt, qw{string=s number=i});
**** mojo
use Mojolicious::Lite;
get '/' => sub {
my $self = shift;
$self->render('index');
};
app->start;
__DATA__
@@ index.html.ep
<!DOCTYPE html>
<meta charset="utf-8">
**** open
my $filename = '';
open my $fh, '<', $filename or die "$filename: $!";
**** while
while (my $line = <>) {
}
**** new
sub new {
my ($class, $arg) = @_;
bless $arg, $class;
}
**** exporter
use parent 'Exporter';
our @EXPORT = qw{
};
**** document
__END__
=encoding utf-8
=for stopwords
=head1 NAME
xxx -
=head1 SYNOPSIS
use xxx;
=head1 DESCRIPTION
xxx is
=cut
END
my $name = shift or exit;
$name = lc($name);
if ($name eq 'ls') {
print "$_\n" for sort keys %{$data};
}
elsif (exists $data->{$name}) {
print $data->{$name};
}
else {
for my $key (keys %{ $data }) {
if ($key =~ /$name/) {
print $data->{$key};
last;
}
}
}
sub get_data {
my $data = shift;
# cf. Data::Section::Simple
my $regexp = qr/^[*]{2,} [ ]* (\S+) [ ]* \n/xsm;
my @data = split $regexp, $data;
shift @data;
my $all = {};
while (@data) {
my ($name, $content) = splice @data, 0, 2;
$name = lc($name);
$content =~ s/\A \n+ //xsm;
$content =~ s/ \n+ \z/\n/xsm;
$all->{$name} = $content;
}
return $all;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment