Adam Kalsey's Word count plugin for Movable Type seemed to be just the thing I needed to put a (2540 more words...) link in my index page to describe how much more text there was in the entry.
However, MTWordCount only shows the total count of the words in both the entry and the extended entry. Not what I needed. So, since my new mantra is 'let's see if I can code it...', I look into the source and found that I could.
I created a modified version of MTWordCount that has the property 'exclude' that allows selection of just the Body or just the Extended Entry. I am releasing it under the same MIT License that Adam used.
<MTWordCount>
Returns total word count.
<MTWordCount exclude="BodyText"> Returns word count from the extended entry only.
<MTWordCount exclude="ExtendedText"> Returns word count from the entry body only.
<MTImageCount> Returns total image count.
<MTImageCount exclude="BodyText"> Returns image count from the extended entry only.
<MTImageCount exclude="ExtendedText"> Returns image count from the entry body only.
Source of WordCount.pl
# MTWordCount 1.4.1
# Word count plugin for Movable Type
#
# Copyright 2002 Kalsey Consulting Group
# http://kalsey.com/
# Using this software signifies your acceptance of the license
# file that accompanies this software.
#
# Installation and usage instructions can be found at
# http://kalsey.com/blog/2002/06/word_count_plugin_for_movable_type.stm
#
# Modified 11/13/2003 by Michael Croft
# added parameter "exclude", with values "BodyText" or "ExtendedText"
# Exclude="BodyText" in either MTWordCount or MTImageCount will count the Extended Entry only
# Exclude="ExtendedText" in either MTWordCount or MTImageCount will count the Body Entry only
#
use strict;
package MT::Plugin::WordCount;
use MT::Template::Context;
use vars qw( $VERSION );
$VERSION = 1.4.1;
MT::Template::Context->add_tag(WordCount => sub { &WordCount; });
MT::Template::Context->add_tag(ImageCount => sub { &ImageCount; });
sub WordCount {
my($ctx) = shift;
my($args) = @_;
my $exclude = '';
if ($args->{exclude})
{
$exclude = $args->{exclude};
}
defined(my $entry = $ctx->stash('entry'))
or return $ctx->error(" must be used in an entry.");
my $strText = '';
$strText .= $entry->text ? strip($entry->text) : '' unless ($exclude eq 'BodyText' );
$strText .= $entry->text_more ? strip($entry->text_more) : '' unless ($exclude eq 'ExtendedText' );
my $count = $strText =~ s/((^|\s|$)\S)/$1/ig;
$count = $count ? $count : 0;
}
sub ImageCount {
my($ctx) = shift;
my($args) = @_;
my $exclude = '';
if ($args->{exclude})
{
$exclude = $args->{exclude};
}
defined(my $entry = $ctx->stash('entry'))
or return $ctx->error(" must be used in an entry.");
my $strText = '';
$strText .= $entry->text ? strip($entry->text) : '' unless ($exclude eq 'BodyText' );
$strText .= $entry->text_more ? strip($entry->text_more) : '' unless ($exclude eq 'ExtendedText' );
my $imgCount = $strText =~ s/( ]*>)/$1/ig;
$imgCount = $imgCount ? $imgCount : 0;
}
sub strip () {
$_ = shift;
s(<[^>]*>)( )g;
return $_;
}
|