Permalink
Browse files

first

  • Loading branch information...
0 parents commit 30b686773375ebaa216d93e0263bc20e54309978 @ingydotnet committed Jun 7, 2011
Showing with 160 additions and 0 deletions.
  1. +5 −0 Changes
  2. +1 −0 Makefile.PL
  3. +32 −0 README
  4. +59 −0 lib/Template/Plugin/YAMLVal.pm
  5. +60 −0 t/test.t
  6. +3 −0 t/use_ok.t
@@ -0,0 +1,5 @@
+---
+version: 0.10
+date: Wed Jun 8 08:02:17 EST 2011
+changes:
+- First Release
@@ -0,0 +1 @@
+use inc::Module::Package 'Ingy:modern 0.09';
32 README
@@ -0,0 +1,32 @@
+NAME
+ Template::Plugin::YAMLVal - yamlval vmethod for Template Toolkit
+
+SYNOPSIS
+ [% USE YAMLVal %]
+ foo: [% foo.yamlval %]
+ bar: [% bar.yamlval %]
+
+DESCRIPTION
+ This module lets you use Template Toolkit to write out YAML mapping
+ files in a certain top level key order, while making sure that the keys
+ are properly YAML encoded.
+
+ There may be other use cases, but that's the one that drove me to write
+ this.
+
+SEE ALSO
+ * Template
+
+ * YAML
+
+AUTHOR
+ Ingy döt Net <ingy@ingy.net>
+
+COPYRIGHT AND LICENSE
+ Copyright (c) 2011. Ingy döt Net.
+
+ This program is free software; you can redistribute it and/or modify it
+ under the same terms as Perl itself.
+
+ See http://www.perl.com/perl/misc/Artistic.html
+
@@ -0,0 +1,59 @@
+##
+# name: Template::Plugin::YAMLVal
+# abstract: yamlval vmethod for Template Toolkit
+# author: Ingy döt Net <ingy@ingy.net>
+# license: perl
+# copyright: 2011
+# see:
+# - Template
+# - YAML
+
+package Template::Plugin::YAMLVal;
+use 5.008003;
+use strict;
+use base 'Template::Plugin';
+
+our $VERSION = '0.10';
+
+use Template::Toolkit::Simple 0.13;
+use YAML 0.72 ();
+use YAML::XS 0.35 ();
+
+sub new {
+ my ($class, $context) = @_;
+ my $self = bless {}, $class;
+ $context->define_vmethod(
+ $_ => yamlval => sub {
+ $self->yamlval(@_)
+ }
+ ) for qw[hash list scalar];
+ return $self;
+}
+
+sub yamlval {
+ my ($self, $value) = @_;
+ my $dumper = ref($value)
+ ? \&YAML::XS::Dump
+ : \&YAML::Dump;
+ my $dump = &$dumper({ fakekey => $value });
+ $dump =~ s/^.*?fakekey://s;
+ $dump =~ s/^ //s;
+ chomp $dump;
+ return $dump;
+}
+
+1;
+
+=head1 SYNOPSIS
+
+ [% USE YAMLVal %]
+ foo: [% foo.yamlval %]
+ bar: [% bar.yamlval %]
+
+=head1 DESCRIPTION
+
+This module lets you use Template Toolkit to write out YAML mapping files in a
+certain top level key order, while making sure that the keys are properly YAML
+encoded.
+
+There may be other use cases, but that's the one that drove me to write this.
@@ -0,0 +1,60 @@
+use Test::More tests => 1;
+
+use Template::Toolkit::Simple;
+
+my $template = <<'...';
+[% USE YAMLVal -%]
+quoted: [% quoted.yamlval %]
+double: [% double.yamlval %]
+blank: [% blank.yamlval %]
+literal: [% literal.yamlval %]
+hash: [% hash.yamlval %]
+array: [% array.yamlval %]
+empty: [% empty.yamlval %]
+nested: [% nested.yamlval %]
+spaces: [% spaces.yamlval %]
+...
+
+my $data = {
+ quoted => 'x: y',
+ double => "C'Dent",
+ blank => '',
+ literal => <<'...',
+A flea and a fly in a flue
+Were caught, so what could they do?
+ Said the fly, "Let us flee."
+ "Let us fly," said the flea.
+So they flew through a flaw in the flue.
+...
+ hash => { qw[ I like pie ! ] },
+ empty => {},
+ nested => {foo => {bar => [1, 2]}},
+ array => [2, 4, 42],
+ spaces => ' ',
+};
+
+is tt->render(\$template, $data), <<'...', 'It just works';
+quoted: 'x: y'
+double: C'Dent
+blank: ''
+literal: |
+ A flea and a fly in a flue
+ Were caught, so what could they do?
+ Said the fly, "Let us flee."
+ "Let us fly," said the flea.
+ So they flew through a flaw in the flue.
+hash:
+ I: like
+ pie: '!'
+array:
+- 2
+- 4
+- 42
+empty: {}
+nested:
+ foo:
+ bar:
+ - 1
+ - 2
+spaces: ' '
+...
@@ -0,0 +1,3 @@
+use Test::More tests => 1;
+
+use_ok 'Template::Plugin::YAMLVal';

0 comments on commit 30b6867

Please sign in to comment.