diff --git a/wikiserve b/wikiserve index 91fdf64..f1d3c92 100755 --- a/wikiserve +++ b/wikiserve @@ -1,15 +1,38 @@ -#!/bin/bash -root=$1 -shift -plackup --port 4242 -MPlack::App::Directory -e ' - enable_if { $_[0]->{REQUEST_URI} =~ /\.(md|mkdwn|markdown)$/ } +#!/usr/bin/env perl +use strict; +use warnings; + +use Plack::Builder; +use Plack::Runner; +use Plack::App::Directory; + +my $root = @ARGV ? shift : '.'; + +# Default differently than plackup's port 5000 so we don't conflict. +unshift @ARGV, '--port' => 4242; + +my $app = builder { + my $is_markdown = qr/\.(md|mkdwn|markdown)$/i; + + enable_if { $_[0]->{REQUEST_URI} =~ /$is_markdown/ } SimpleContentFilter => filter => sub { - use Text::MultiMarkdown qw(markdown); - $_ = markdown($_, {use_wikilinks=>1}) + eval { + require Text::MultiMarkdown; + $_ = Text::MultiMarkdown::markdown($_, { use_wikilinks => 1 }); + }; + return $_; }; - enable_if { $_[0]->{REQUEST_URI} =~ /\.(md|mkdwn|markdown)$/ } - Header => set => [qw(Content-Type text/html)]; + + enable_if { $_[0]->{REQUEST_URI} =~ /$is_markdown/ } + Header => set => [ 'Content-Type' => 'text/html' ]; + enable Expires => content_type => qr/./, expires => "now plus 1 second"; - Plack::App::Directory->new({ root => q['"$root"'] })->to_app' "$@" + + Plack::App::Directory->new({ root => $root })->to_app; +}; + +my $runner = Plack::Runner->new; +$runner->parse_options(@ARGV); +$runner->run($app);