Skip to content
Browse files

initial checkin

  • Loading branch information...
0 parents commit 8fd2abddef9745530badce2295c24e7a366adc22 @gray committed Aug 15, 2010
Showing with 543 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +4 −0 Changes
  3. +16 −0 MANIFEST
  4. +62 −0 MANIFEST.SKIP
  5. +51 −0 Makefile.PL
  6. +31 −0 README
  7. +169 −0 lib/MojoX/Renderer/Xslate.pm
  8. +61 −0 lib/Mojolicious/Plugin/XslateRenderer.pm
  9. +8 −0 t/00_compile.t
  10. +64 −0 t/lite_app.t
  11. +1 −0 t/templates/foo.html.tx
  12. +7 −0 xt/kwalitee.t
  13. +10 −0 xt/perlcritic.t
  14. +23 −0 xt/perlcriticrc
  15. +10 −0 xt/pod.t
  16. +9 −0 xt/pod_coverage.t
  17. +11 −0 xt/vars.t
6 .gitignore
@@ -0,0 +1,6 @@
+MojoX-Renderer-Xslate-*
+Makefile
+Makefile.old
+blib/
+pm_to_blib
+t/tmp
4 Changes
@@ -0,0 +1,4 @@
+Revision history for MojoX-Renderer-Xslate
+
+0.01 Sun Aug 15 20:37:52 UTC 2010
+ - Initial distribution.
16 MANIFEST
@@ -0,0 +1,16 @@
+Changes
+lib/Mojolicious/Plugin/XslateRenderer.pm
+lib/MojoX/Renderer/Xslate.pm
+Makefile.PL
+MANIFEST This list of files
+MANIFEST.SKIP
+README
+t/00_compile.t
+t/lite_app.t
+t/templates/foo.html.tx
+xt/kwalitee.t
+xt/perlcritic.t
+xt/perlcriticrc
+xt/pod.t
+xt/pod_coverage.t
+xt/vars.t
62 MANIFEST.SKIP
@@ -0,0 +1,62 @@
+
+#!start included /opt/local/lib/perl5/5.12.1/ExtUtils/MANIFEST.SKIP
+# Avoid version control files.
+\bRCS\b
+\bCVS\b
+\bSCCS\b
+,v$
+\B\.svn\b
+\B\.git\b
+\B\.gitignore\b
+\b_darcs\b
+\B\.cvsignore$
+
+# Avoid VMS specific MakeMaker generated files
+\bDescrip.MMS$
+\bDESCRIP.MMS$
+\bdescrip.mms$
+
+# Avoid Makemaker generated and utility files.
+\bMANIFEST\.bak
+\bMakefile$
+\bblib/
+\bMakeMaker-\d
+\bpm_to_blib\.ts$
+\bpm_to_blib$
+\bblibdirs\.ts$ # 6.18 through 6.25 generated this
+
+# Avoid Module::Build generated and utility files.
+\bBuild$
+\b_build/
+\bBuild.bat$
+\bBuild.COM$
+\bBUILD.COM$
+\bbuild.com$
+
+# Avoid temp and backup files.
+~$
+\.old$
+\#$
+\b\.#
+\.bak$
+\.tmp$
+\.#
+\.rej$
+
+# Avoid OS-specific files/dirs
+# Mac OSX metadata
+\B\.DS_Store
+# Mac OSX SMB mount metadata files
+\B\._
+
+# Avoid Devel::Cover and Devel::CoverX::Covered files.
+\bcover_db\b
+\bcovered\b
+
+# Avoid MYMETA files
+^MYMETA\.
+#!end included /opt/local/lib/perl5/5.12.1/ExtUtils/MANIFEST.SKIP
+
+
+# Ignore the generated template cache.
+t/tmp/
51 Makefile.PL
@@ -0,0 +1,51 @@
+use strict;
+use warnings;
+use ExtUtils::MakeMaker;
+
+my %conf = (
+ NAME => 'MojoX::Renderer::Xslate',
+ AUTHOR => 'gray <gray@cpan.org>',
+ LICENSE => 'perl',
+ VERSION_FROM => 'lib/MojoX/Renderer/Xslate.pm',
+ ABSTRACT_FROM => 'lib/MojoX/Renderer/Xslate.pm',
+ PREREQ_PM => {
+ parent => 0,
+ Mojo => 0,
+ 'Text::Xslate' => 0,
+ 'Try::Tiny' => 0,
+ },
+ BUILD_REQUIRES => {
+ 'Test::Mojo' => 0,
+ 'Test::More' => 0.82,
+ },
+ META_MERGE => {
+ resources => {
+ repository => 'http://github.com/gray/mojox-renderer-xslate',
+ },
+ },
+ dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', },
+ clean => { FILES => 'MojoX-Renderer-Xslate-*' },
+);
+
+my $eumm_version=$ExtUtils::MakeMaker::VERSION;
+delete $conf{META_MERGE} if $eumm_version < 6.46;
+$conf{PREREQ_PM} = {
+ %{ $conf{PREREQ_PM} || {} }, %{ delete $conf{BUILD_REQUIRES} },
+} if ($conf{BUILD_REQUIRES} and $eumm_version < 6.5503);
+
+WriteMakefile(%conf);
+
+
+sub MY::postamble {
+ return <<" MAKE_FRAG";
+authortest:
+\t\$(MAKE) -e \$(TEST_TYPE) TEST_FILES="xt/*.t"
+ MAKE_FRAG
+}
+
+sub MY::dist_test {
+ my $self = shift;
+ return $self->MM::dist_test . <<" MAKE_FRAG";
+\tcd \$(DISTVNAME) && \$(MAKE) authortest \$(PASTHRU)
+ MAKE_FRAG
+}
31 README
@@ -0,0 +1,31 @@
+MojoX-Renderer-Xslate
+=====================
+
+This module provides a Text::Xslate renderer for Mojo.
+
+INSTALLATION
+
+To install this module type the following:
+
+ perl Makefile.PL
+ make
+ make test
+ make install
+
+DEPENDENCIES
+
+This module requires these other modules and libraries:
+
+ Mojo
+ parent
+ Text::Xslate
+ Test::Mojo
+ Test::More
+ Try::Tiny
+
+COPYRIGHT AND LICENCE
+
+Copyright (C) 2010 by gray <gray@cpan.org>
+
+This library is free software; you can redistribute it and/or modify it under
+the same terms as Perl itself.
169 lib/MojoX/Renderer/Xslate.pm
@@ -0,0 +1,169 @@
+package MojoX::Renderer::Xslate;
+
+use strict;
+use warnings;
+use parent qw(Mojo::Base);
+
+use File::Spec ();
+use Text::Xslate ();
+use Try::Tiny;
+
+our $VERSION = '0.01';
+$VERSION = eval $VERSION;
+
+__PACKAGE__->attr('xslate');
+
+sub build {
+ my $self = shift->SUPER::new(@_);
+ $self->_init(@_);
+ return sub { $self->_render(@_) };
+}
+
+sub _init {
+ my ($self, %args) = @_;
+
+ my $app = $args{mojo} || $args{app};
+ my %config = (
+ cache_dir => File::Spec->tmpdir,
+ $app ? (
+ cache_dir => $app->home->rel_dir('tmp/compiled_templates'),
+ path => [ $app->home->rel_dir('templates') ],
+ ) : (),
+ %{ $args{template_options} || {} },
+ );
+
+ $self->xslate(Text::Xslate->new(\%config));
+
+ return $self;
+}
+
+sub _render {
+ my ($self, $renderer, $c, $output, $options) = @_;
+
+ my $name = $c->stash->{'template_name'}
+ || $renderer->template_name($options);
+ my @params = (%{$c->stash}, c => $c);
+
+ try {
+ $$output = $self->xslate->render($name, {@params});
+ }
+ catch {
+ my $err = $_;
+ $c->app->log->error(qq(Template error in "$name": $err));
+ $c->render_exception($err);
+ $$output = '';
+ return 0;
+ };
+
+ return 1;
+}
+
+
+1;
+
+__END__
+
+=head1 NAME
+
+MojoX::Renderer::Xslate - Text::Xslate renderer for Mojo
+
+=head1 SYNOPSIS
+
+ sub startup {
+ ....
+
+ # Via mojolicious plugin
+ $self->plugin('xslate_renderer');
+
+ # or manually
+ use MojoX::Renderer::Xslate;
+ my $xslate = MojoX::Renderer::Xslate->build(
+ mojo => $self,
+ template_options => { },
+ );
+ $self->renderer->add_handler(tx => $xslate);
+ }
+
+=head1 DESCRIPTION
+
+The C<MojoX::Renderer::Xslate> module is called by C<MojoX::Renderer> for
+any matching template.
+
+=head1 METHODS
+
+=head2 build
+
+ $renderer = MojoX::Renderer::Xslate->build(...)
+
+This method returns a handler for the Mojo renderer.
+
+Supported parameters are:
+
+=over
+
+=item mojo
+
+C<build> currently uses a C<mojo> parameter pointing to the base class
+object (C<Mojo>).
+
+=item template_options
+
+A hash reference of options that are passed to Text::Xslate->new().
+
+=back
+
+=head1 SEE ALSO
+
+L<Text::Xslate>, L<MojoX::Renderer>
+
+=head1 REQUESTS AND BUGS
+
+Please report any bugs or feature requests to
+L<http://rt.cpan.org/Public/Bug/Report.html?Queue=MojoX-Renderer-Xslate>.
+I will be notified, and then you'll automatically be notified of progress on
+your bug as I make changes.
+
+=head1 SUPPORT
+
+You can find documentation for this module with the perldoc command.
+
+ perldoc MojoX::Renderer::Xslate
+
+You can also look for information at:
+
+=over
+
+=item * GitHub Source Repository
+
+L<http://github.com/gray/mojox-renderer-xslate>
+
+=item * AnnoCPAN: Annotated CPAN documentation
+
+L<http://annocpan.org/dist/MojoX-Renderer-Xslate>
+
+=item * CPAN Ratings
+
+L<http://cpanratings.perl.org/d/MojoX-Renderer-Xslate>
+
+=item * RT: CPAN's request tracker
+
+L<http://rt.cpan.org/Public/Dist/Display.html?Name=MojoX-Renderer-Xslate>
+
+=item * Search CPAN
+
+L<http://search.cpan.org/dist/MojoX-Renderer-Xslate>
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2010 gray <gray at cpan.org>, all rights reserved.
+
+This library is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=head1 AUTHOR
+
+gray, <gray at cpan.org>
+
+=cut
61 lib/Mojolicious/Plugin/XslateRenderer.pm
@@ -0,0 +1,61 @@
+package Mojolicious::Plugin::XslateRenderer;
+
+use strict;
+use warnings;
+use parent qw(Mojolicious::Plugin);
+
+use MojoX::Renderer::Xslate;
+
+sub register {
+ my ($self, $app, $args) = @_;
+
+ $args ||= {};
+
+ my $xslate = MojoX::Renderer::Xslate->build(app => $app, %$args);
+ $app->renderer->add_handler(tx => $xslate);
+}
+
+
+1;
+
+__END__
+
+=head1 NAME
+
+Mojolicious::Plugin::Xslate - Text::Xslate plugin
+
+=head1 SYNOPSIS
+
+ # Mojolicious
+ $self->plugin('xslate_renderer');
+ $self->plugin(xslate_renderer => {
+ template_options => { syntax => 'TTerse', ...}
+ });
+
+ # Mojolicious::Lite
+ plugin 'xslate_renderer';
+ plugin xslate_renderer => {
+ template_options => { syntax => 'TTerse', ...}
+ };
+
+=head1 DESCRIPTION
+
+L<Mojolicous::Plugin::Xslate> is a simple loader for
+L<MojoX::Renderer::Xslate>.
+
+=head1 METHODS
+
+L<Mojolicious::Plugin::Xslate> inherits all methods from
+L<Mojolicious::Plugin> and overrides the following ones:
+
+=head2 register
+
+ $plugin->register
+
+Registers renderer in L<Mojolicious> application.
+
+=head1 SEE ALSO
+
+L<MojoX::Renderer::Xslate>, L<Mojolicious>
+
+=cut
8 t/00_compile.t
@@ -0,0 +1,8 @@
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+BEGIN {
+ use_ok qw(MojoX::Renderer::Xslate);
+ use_ok qw(Mojolicious::Plugin::XslateRenderer);
+}
64 t/lite_app.t
@@ -0,0 +1,64 @@
+use strict;
+use warnings;
+
+use Test::More tests => 14;
+use Test::Mojo;
+
+use Mojolicious::Lite;
+use MojoX::Renderer::Xslate;
+
+# Silence
+app->log->level('fatal');
+
+plugin 'xslate_renderer';
+
+my $xslate = MojoX::Renderer::Xslate->build(
+ mojo => app,
+ template_options => {
+ syntax => 'TTerse',
+ path => [ Mojo::Command->new->get_all_data(__PACKAGE__) ],
+ },
+);
+app->renderer->add_handler(tt => $xslate);
+
+get '/exception' => 'error';
+get '/with_include' => 'include';
+get '/with_wrapper' => 'wrapper';
+get '/foo/:message' => 'index';
+get '/on-disk' => 'foo';
+
+my $t = Test::Mojo->new;
+
+$t->get_ok('/exception')->status_is(500)->content_like(qr/error/i);
+$t->get_ok('/foo/hello')->content_like(qr/^hello\s*$/);
+$t->get_ok('/with_include')->content_like(qr/^Hello\s*Include!Hallo\s*$/);
+$t->get_ok('/with_wrapper')->content_like(qr/^wrapped\s*$/);
+$t->get_ok('/on-disk')->content_is(4);
+$t->get_ok('/not_found')->status_is(404)->content_like(qr/not found/i);
+
+__DATA__
+
+@@ error.html.tt
+[% 1 + 1 %%]
+
+@@ index.html.tt
+[%- message -%]
+
+@@ include.inc
+Hello
+
+@@ includes/include.inc
+Hallo
+
+@@ include.html.tt
+[%- INCLUDE 'include.inc' -%]
+Include!
+[%- INCLUDE 'includes/include.inc' -%]
+
+@@ layouts/layout.html.tt
+w[%- content -%]d
+
+@@ wrapper.html.tt
+[%- WRAPPER 'layouts/layout.html.tt' -%]
+rappe
+[%- END -%]
1 t/templates/foo.html.tx
@@ -0,0 +1 @@
+<:- 2 + 2 -:>
7 xt/kwalitee.t
@@ -0,0 +1,7 @@
+use strict;
+use warnings;
+use Test::More;
+
+eval { require Test::Kwalitee; Test::Kwalitee->import(); 1 } or do {
+ plan skip_all => 'Test::Kwalitee not installed; skipping';
+};
10 xt/perlcritic.t
@@ -0,0 +1,10 @@
+use strict;
+use warnings;
+use Test::More;
+
+eval { require Test::Perl::Critic; 1 } or do {
+ plan skip_all => "Test::Perl::Critic is not installed.";
+};
+Test::Perl::Critic->import( -profile => 'xt/perlcriticrc' );
+
+all_critic_ok(qw( ex lib t xt ));
23 xt/perlcriticrc
@@ -0,0 +1,23 @@
+verbose = 8
+
+[CodeLayout::ProhibitHardTabs]
+allow_leading_tabs = 0
+severity = 5
+
+[CodeLayout::ProhibitTrailingWhitespace]
+severity = 5
+
+# requires Perl::Critic::More
+[CodeLayout::RequireASCII]
+severity = 5
+
+[TestingAndDebugging::RequireUseWarnings]
+severity = 5
+
+# requires Regexp::Parser
+[RegularExpressions::ProhibitUnusedCapture]
+severity = 5
+
+[-TestingAndDebugging::ProhibitNoStrict]
+[-BuiltinFunctions::ProhibitStringyEval]
+[-ValuesAndExpressions::ProhibitLeadingZeros]
10 xt/pod.t
@@ -0,0 +1,10 @@
+use strict;
+use warnings;
+use Test::More;
+
+eval "use Test::Pod 1.00";
+if ($@) {
+ plan skip_all => 'Test::Pod 1.00 required for testing POD';
+}
+
+all_pod_files_ok();
9 xt/pod_coverage.t
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+use Test::More;
+
+eval "use Test::Pod::Coverage 1.00; 1" or do {
+ plan skip_all => 'Test::Pod::Coverage 1.00 is not installed.';
+};
+
+all_pod_coverage_ok();
11 xt/vars.t
@@ -0,0 +1,11 @@
+use strict;
+use warnings;
+use Test::More;
+
+eval "use Test::Vars; 1" or do {
+ plan skip_all => 'Test::Vars is not installed.';
+};
+
+all_vars_ok();
+
+done_testing;

0 comments on commit 8fd2abd

Please sign in to comment.
Something went wrong with that request. Please try again.