Skip to content

Commit

Permalink
initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
tokuhirom committed Sep 23, 2010
0 parents commit fd3f8a7
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
inc/
Makefile
12 changes: 12 additions & 0 deletions Makefile.PL
@@ -0,0 +1,12 @@
use inc::Module::Install;

all_from 'Tripel.pm';

requires 'Text::Xslate';
requires 'Text::Xslate::Bridge::TT2Like';
requires 'Router::Simple::Sinatraish';
requires 'Mouse';
requires 'HTML::FillInForm::Lite';
requires 'Plack';

WriteAll;
105 changes: 105 additions & 0 deletions Tripel.pm
@@ -0,0 +1,105 @@
use strict;
use warnings;

package Tripel;
use Text::Xslate;
use Router::Simple::Sinatraish ();
use File::Basename;
use File::Spec;
use 5.008001;

our $VERSION='0.02';

sub import {
my $caller = caller(0);
my $app_path = dirname([caller(0)]->[1]);
my %xslates;

Router::Simple::Sinatraish->export_to_level(1);

no strict 'refs';
*{"${caller}::xslate"} = sub { $xslates{$caller} };
*{"${caller}::res"} = sub { Tripel::Response->new(@_) };
*{"${caller}::to_app"} = sub {
# setup
$xslates{$caller} = Text::Xslate->new(
syntax => 'TTerse',
path => [ File::Spec->catfile($app_path, 'tmpl') ]
);

sub {
my $env = shift;
if ( my $route = $caller->router->match($env) ) {
my $c = Tripel::Context->new(env => $env, 'caller' => $caller, app_path => $app_path);
my $res = $route->{code}->($c, $route);
return $res->finalize();
}
else {
my $content = 'not found';
return [404, ['Content-Length' => length($content)], [$content]];
}
}
};
}

package Tripel::Context;
use Mouse;
use HTML::FillInForm::Lite;
use Encode qw/encode_utf8/;

has env => ( is => 'ro', isa => 'HashRef', required => 1 );
has req => (
is => 'ro',
isa => 'Tripel::Request',
default => sub { Tripel::Request->new( $_[0]->env ) }
);
has caller => (is => 'ro', isa => 'Str', required => 1);
has app_path => (is => 'ro', isa => 'Str', required => 1);
has fdat => (is => 'rw', isa => 'Any');

sub xslate { shift->caller->xslate }

sub render_with_fillin_form {
my ($self, $tmpl, $args, $fdat) = @_;
my $html = $self->xslate->render($tmpl, $args);
$html = HTML::FillInForm::Lite->fill( \$html, $fdat );
return $self->make_html_response($html);
}
sub render {
my $self = shift;
my $html = $self->xslate->render(@_);
return $self->make_html_response($html);
}

sub make_html_response {
my ($self, $html) = @_;
$html = encode_utf8($html);
return Tripel::Response->new(
200,
[
'Content-Type' => 'text/html; charset=utf-8',
'Content-Length' => length($html)
],
[$html]
);
}

no Mouse;
__PACKAGE__->meta->make_immutable;

package Tripel::Request;
use parent qw/Plack::Request/;

package Tripel::Response;
use parent qw/Plack::Response/;
1;
=head1 AUTHOR
Tokuhiro Matsuno(tokuhirom)
=head1 LICENSE
Copyright (c) 2010, Tokuhiro Matsuno(tokuhirom). All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

0 comments on commit fd3f8a7

Please sign in to comment.