The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Plack::App::File::PYX - Plack PYX directory application.

SYNOPSIS

 use Plack::App::File::PYX;

 my $obj = Plack::App::File::PYX->new(%parameters);
 my $psgi_ar = $obj->serve_path($env, $file);
 my $app = $obj->to_app;

METHODS

new

 my $obj = Plack::App::File::PYX->new(%parameters);

Constructor.

Returns instance of object.

  • content_type

    Content-Type of serialized output. There is possibility of callback (reference to code) with $file argument which return content type string.

    Default value is 'text/html'.

  • encoding

    Set the file encoding for text files. Defaults to 'utf-8'.

  • file

    The file path to create responses from. Optional.

    If it's set the application would ALWAYS create a response out of the file and there will be no security check etc. (hence fast). If it's not set, the application uses "root" to find the matching file.

  • indent

    Set Tags::Output::* class for output serialization.

    Default value is Tags::Output::Raw.

  • root

    Document root directory. Defaults to "." (current directory)

serve_path

 my $psgi_ar = $obj->serve_path($env, $file);

Process file on disk and serve it to application.

Returns reference to array (PSGI structure).

to_app

 my $app = $obj->to_app;

Creates Plack application.

Returns Plack::Component object.

EXAMPLE1

 use strict;
 use warnings;

 use File::Temp;
 use IO::Barf;
 use Plack::App::File::PYX;
 use Plack::Runner;

 # Temporary file with PYX.
 my $temp_pyx_file = File::Temp->new->filename;

 # PYX file.
 my $pyx = <<'END';
 (html
 (head
 (title
 -Title
 )title
 )head
 (body
 (div
 -Hello world
 )div
 )body
 )html
 END
 barf($temp_pyx_file, $pyx);

 # Run application with one PYX file.
 my $app = Plack::App::File::PYX->new('file' => $temp_pyx_file)->to_app;
 Plack::Runner->new->run($app);

 # Output:
 # HTTP::Server::PSGI: Accepting connections at http://0:5000/

 # > curl http://localhost:5000/
 # <html><head><title>Title</title></head><body><div>Hello world</div></body></html>

EXAMPLE2

 use strict;
 use warnings;

 use File::Temp;
 use IO::Barf;
 use Plack::App::File::PYX;
 use Plack::Runner;

 # Temporary file with PYX.
 my $temp_pyx_file = File::Temp->new->filename;

 # PYX file.
 my $pyx = <<'END';
 (html
 (head
 (title
 -Title
 )title
 )head
 (body
 (div
 -Hello world
 )div
 )body
 )html
 END
 barf($temp_pyx_file, $pyx);

 # Run application with one PYX file.
 my $app = Plack::App::File::PYX->new(
         'file' => $temp_pyx_file,
         'indent' => 'Tags::Output::Indent',
 )->to_app;
 Plack::Runner->new->run($app);

 # Output:
 # HTTP::Server::PSGI: Accepting connections at http://0:5000/

 # > curl http://localhost:5000/
 # <html>
 #   <head>
 #     <title>
 #       Title
 #     </title>
 #   </head>
 #   <body>
 #     <div>
 #       Hello world
 #     </div>
 #   </body>
 # </html>

EXAMPLE3

 use strict;
 use warnings;

 use File::Temp;
 use IO::Barf;
 use Plack::App::File::PYX;
 use Plack::Runner;

 # Temporary file with PYX.
 my $temp_pyx_file = File::Temp->new->filename;

 # PYX file.
 my $pyx = <<'END';
 ?xml version="1.0"
 (svg
 Axmlns http://www.w3.org/2000/svg
 (rect
 Ax 80
 Ay 60
 Awidth 250
 Aheight 250
 Arx 20
 Astyle fill:#ff0000; stroke:#000000; stroke-width:2px;
 )rect
 (rect
 Ax 140
 Ay 120
 Awidth 250
 Aheight 250
 Arx 40
 Astyle fill:#0000ff; stroke:#000000; stroke-width:2px; fill-opacity:0.7;
 )rect
 )svg
 END
 barf($temp_pyx_file, $pyx);

 # Run application with one PYX file.
 my $app = Plack::App::File::PYX->new(
         'content_type' => 'image/svg+xml',
         'file' => $temp_pyx_file,
         'indent' => 'Tags::Output::Indent',
 )->to_app;
 Plack::Runner->new->run($app);

 # Output:
 # HTTP::Server::PSGI: Accepting connections at http://0:5000/

 # > curl http://localhost:5000/
 # <?xml version="1.0"?>
 # <svg xmlns="http://www.w3.org/2000/svg">
 #   <rect x="80" y="60" width="250" height="250" rx="20" style=
 #     "fill:#ff0000; stroke:#000000; stroke-width:2px;">
 #   </rect>
 #   <rect x="140" y="120" width="250" height="250" rx="40" style=
 #     "fill:#0000ff; stroke:#000000; stroke-width:2px; fill-opacity:0.7;">
 #   </rect>
 # </svg>

EXAMPLE4

 use strict;
 use warnings;

 use File::Temp;
 use IO::Barf;
 use Plack::App::File::PYX;
 use Plack::Runner;

 # Temporary file with PYX.
 my $temp_pyx_file = File::Temp->new->filename;

 # PYX file in UTF8, prepared for iso-8859-2 output.
 my $pyx = <<'END';
 (html
 (head
 (title
 -žščřďťň
 )title
 (meta
 Acharset iso-8859-2
 )meta
 )head
 (body
 (div
 -Hello in iso-8859-2 encoding - Ahoj světe!
 )div
 )body
 )html
 END
 barf($temp_pyx_file, $pyx);

 # Run application with one PYX file.
 my $app = Plack::App::File::PYX->new(
         'encoding' => 'iso-8859-2',
         'file' => $temp_pyx_file,
         'indent' => 'Tags::Output::Indent',
 )->to_app;
 Plack::Runner->new->run($app);

 # Output:
 # HTTP::Server::PSGI: Accepting connections at http://0:5000/

 # > curl http://localhost:5000/
 # XXX in ISO-8859-2 encoding
 # <html>
 #   <head>
 #     <title>
 #       žščřďťň
 #     </title>
 #     <meta charset="iso-8859-2">
 #     </meta>
 #   </head>
 #   <body>
 #     <div>
 #       Hello in iso-8859-2 encoding - Ahoj světe!
 #     </div>
 #   </body>
 # </html>

DEPENDENCIES

Encode, English, Error::Pure, HTTP::Date, Plack::App::File, Plack::Util::Accessor, PYX::SGML::Tags, Tags::Output::Raw, Unicode::UTF8.

REPOSITORY

https://github.com/michal-josef-spacek/Plack-App-File-PYX

AUTHOR

Michal Josef Špaček mailto:skim@cpan.org

http://skim.cz

LICENSE AND COPYRIGHT

© 2020 Michal Josef Špaček

BSD 2-Clause License

VERSION

0.01