Workin' in a code mine
November 13, 2003 Computers and Software
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 $_;
}
.:Posted by Michael on November 13, 2003 10:59 AM:.
Rambling On
I don’t suppose this has any practical value, at least none that immediately springs to mind, but I came across a script for totalling the word count of a MovableType blog (which I’ve incorporated into the title bar of both......
.:Posted to diachrony on Nov 20, 2003 3:03 AM:.

Can this be modified to count words in comments?

.:Posted by Mariann ( total) on January 11, 2004 1:58 PM:.
blog word count?
I'm looking at CODES for my blogsite's word count sana. Since I saw darkbeloved's entry re: the frequency of the words that she uses in her live journal site, I have been searching for 'em. Too bad it's exclusive for live journal bloggers. I found one ......
.:Posted to dominattyrix on May 22, 2004 8:22 AM:.
How many words? THIS many words!
As you might notice, I have installed a word count plugin from Adam Kalsey. This allows me to display the number of words, or "words" that are blogged here. Michael Croft has modified it so that I can only display......
.:Posted to The Scrawl at the End of the Hall on Sep 8, 2004 4:03 PM:.
Archives
Archives
Category Archives
Recent Entries
Comment Leaders
Links


Blogcritics: news and reviews
 
Syndicate this site (XML)
Powered by

powered by Movable Type
Movable Type 3.33
Apple Computer