
# DropCap.pl
# Movable Type plugin tags for putting a drop cap at the beginning of some text
# by Kevin Shay
# http://www.staggernation.com/mtplugins/
# last modified July 12, 2004

package MT::Plugin::DropCap;
use strict;
use vars qw( $VERSION );
$VERSION = '1.1';

use MT;
use MT::Template::Context;

eval{ require MT::Plugin };
unless ($@) {
    my $plugin = {
        name => "DropCap $VERSION",
        description => 'Spruce up your entries by changing their first letters into eye-catching drop caps.',
        doc_link => 'http://www.staggernation.com/mtplugins/DropCap'
    }; 
    MT->add_plugin(new MT::Plugin($plugin));
}

MT::Template::Context->add_container_tag('DropCap' => sub{&_hdlr_drop_cap;});
MT::Template::Context->add_container_tag('DropCapFormat' => sub{&_hdlr_drop_cap_format;});
MT::Template::Context->add_tag('DropCapLetter' => sub{&_hdlr_drop_cap_letter;});

sub _hdlr_drop_cap {
# handler for MT container tag that replaces the first (non-tag, non-whitespace)
# character of its contents with a drop cap
	my ($ctx, $args, $cond) = @_;
	defined(my $text = $ctx->stash('builder')->build($ctx, $ctx->stash('tokens'), $cond))
			|| return $ctx->error($ctx->errstr);
		# format will have been stashed by MTDropCapFormat, with placeholder for
		# letter inserted by MTDropCapLetter
	my $format = $ctx->stash('dc_format');
		# skip any initial tags and/or whitespace
	$text =~ s/^(((<[^>]*>)|\s)*)(\w)/$1$format/;
	(my $letter = $4) || return $text;
	if (defined($args->{'case'})) {
		if ($args->{'case'} eq 'lower') {
			$letter = lc($letter);
		} elsif ($args->{'case'} eq 'upper') {
			$letter = uc($letter);
		}
	}
	$text =~ s/___DROPCAPLETTER___/$letter/g;
	return $text;
}

sub _hdlr_drop_cap_format {
# handler for MT container tag that delimits the HTML formatting for a 
# drop cap; doesn't output anything, just stashes its contents
	my ($ctx, $args, $cond) = @_;
	defined(my $text = $ctx->stash('builder')->build($ctx, $ctx->stash('tokens'), $cond))
			|| return $ctx->error($ctx->errstr);
	$ctx->stash('dc_format', $text);
	return '';
}

sub _hdlr_drop_cap_letter {
# handler for MT tag that indicates where, within the MTDropCapFormat
# container, the drop cap letter should be printed
	return '___DROPCAPLETTER___';
}

1;
