Text::Markdown::ApacheHandler - Processes files with Markdown syntax for Apache


Text-Markdown-ApacheHandler documentation Contained in the Text-Markdown-ApacheHandler distribution.

Index


Code Index:

NAME

Top

Text::Markdown::ApacheHandler - Processes files with Markdown syntax for Apache

VERSION

Top

Version 0.04

SYNOPSIS

Top

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 .htaccess or 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>

METHODS

Top

handler

Standard Apache module entry point

AUTHOR

Top

Darren Kulp, <darren at kulp.ch>

BUGS

Top

Please report any bugs or feature requests to bug-text-markdown-apachehandler at rt.cpan.org, or through the web interface at 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.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc Text::Markdown::ApacheHandler

You can also look for information at:

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/Text-Markdown-ApacheHandler

* CPAN Ratings

http://cpanratings.perl.org/d/Text-Markdown-ApacheHandler

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-Markdown-ApacheHandler

* Search CPAN

http://search.cpan.org/dist/Text-Markdown-ApacheHandler

TODO

Top

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.

ACKNOWLEDGEMENTS

Top

The excellent Text::Markdown module and its author, and "Writing Apache Modules with Perl and C" by Lincoln Stein and Doug MacEachern.

COPYRIGHT & LICENSE

Top


Text-Markdown-ApacheHandler documentation Contained in the Text-Markdown-ApacheHandler distribution.
package Text::Markdown::ApacheHandler;

use strict;
use warnings;

our $VERSION = '0.04';

use Apache::Constants qw(:common);
use Apache::File ();
use CGI qw(:standard);
use Text::Markdown 'markdown';

sub handler {
	my $r = shift;
	return DECLINED unless $r->content_type() eq 'text/markdown';
	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 (-e $r->finfo) {
		$r->log_error("File does not exist: $file");    
		return NOT_FOUND;
	}
	unless (-r _) {
		$r->log_error("File permissions deny access: $file");
		return FORBIDDEN;
	}

	my $modtime = localtime((stat _)[9]);

	my $fh;
	unless ($fh = Apache::File->new($file)) {
		$r->log_error("Couldn't open $file for reading: $!");
		return 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 OK;
}

1; # End of Text::Markdown::ApacheHandler

__END__