Subject: | Support for Apache2 |
The attached file is updated to work with Apache2. It would probably be
a good idea to detect which version of apache is running using a BEGIN
block, then use the appropriate implementation of either
Apache::Constants or Apache2::Const.
Subject: | ApacheHandler.pm |
package Text::Markdown::ApacheHandler;
use strict;
use warnings;
=head1 NAME
Text::Markdown::ApacheHandler - Processes files with Markdown syntax for Apache2
=head1 VERSION
Version 0.04_1
=cut
our $VERSION = '0.04';
use Apache2::Const -compile => qw(:common);
use IO::File ();
use CGI qw(:standard);
use Text::Markdown 'markdown';
=head1 SYNOPSIS
Processes files containing Markdown syntax into HTML files and serves them,
optionally applying CSS styles according to rules in your httpd.conf or (more
likely) .htaccess files.
You might put some lines like this in your C<.htaccess> or C<httpd.conf> file:
AddType text/markdown .markdown .mkd .mhtml
<Files ~ "\.(markdown|mkd|mhtml)$">
SetHandler perl-script
PerlHandler Text::Markdown::ApacheHandler
PerlAddVar mkd_stylesheet "style/mkd.css"
</Files>
<Directory /www/html/fancy/>
PerlAddVar mkd_stylesheet "fancy.css"
</Directory>
=head1 METHODS
=over 4
=item handler
Standard Apache2 module entry point
=cut
sub handler {
my $r = shift;
return Apache2::Const::DECLINED unless ($r->content_type() eq 'text/markdown' || $r->content_type() eq 'text/plain');
my $file = $r->filename;
my @head;
push @head, map {
Link({
-rel => 'stylesheet',
-href => $_,
# TODO: Extract stylesheet variable name
-type => 'text/css'}
)
} grep { $_ } $r->dir_config('mkd_stylesheet');
unless ($r->finfo) {
$r->log_error("File does not exist: $file");
return Apache2::Const::NOT_FOUND;
}
unless (-r _) {
$r->log_error("File permissions deny access: $file");
return Apache2::Const::FORBIDDEN;
}
my $modtime = localtime((stat _)[9]);
my $fh;
unless ($fh = IO::File->new($file,"r")) {
$r->log_error("Couldn't open $file for reading: $!");
return Apache2::Const::SERVER_ERROR;
}
my $content = do { local $/; <$fh> };
my ($title) = $file =~ m#/([^/]+?)(?:\.[^./]+)?$#;
$r->send_http_header('text/html');
$r->print(
start_html(-title => $title, -head => [ @head ]),
markdown($content),
end_html
);
return Apache2::Const::OK;
}
=back
=head1 AUTHOR
Darren Kulp, C<< <darren at kulp.ch> >>
=head1 BUGS
Please report any bugs or feature requests to
C<bug-text-markdown-apachehandler at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-Markdown-ApacheHandler>.
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 Text::Markdown::ApacheHandler
You can also look for information at:
=over 4
=item * AnnoCPAN: Annotated CPAN documentation
L<http://annocpan.org/dist/Text-Markdown-ApacheHandler>
=item * CPAN Ratings
L<http://cpanratings.perl.org/d/Text-Markdown-ApacheHandler>
=item * RT: CPAN's request tracker
L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-Markdown-ApacheHandler>
=item * Search CPAN
L<http://search.cpan.org/dist/Text-Markdown-ApacheHandler>
=back
=head1 TODO
Tests. I haven't yet looked into how to do tests for an Apache module like
this; I want to do them more for the experience than the necessity, since there
is practically nothing in this module.
=head1 ACKNOWLEDGEMENTS
The excellent L<Text::Markdown> module and its author, and "Writing Apache
Modules with Perl and C" by Lincoln Stein and Doug MacEachern.
=head1 COPYRIGHT & LICENSE
Copyright 2006 Darren Kulp, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
1; # End of Text::Markdown::ApacheHandler
__END__