# # # MTEntryContents 1.3 # Mark Paschal # 1 October 2003 # # based on MTWordCount 1.4 by Adam Kalsey # < http://kalsey.com/blog/2002/06/word_count_plugin_for_movable_type.stm > # # # Copyright 2003 Mark Paschal # # Permission is hereby granted, free of charge, to any person obtaining a # copy of this software and associated documentation files (the "Software"), # to deal in the Software without restriction, including without limitation # the rights to use, copy, modify, merge, publish, distribute, sublicense, # and/or sell copies of the Software, and to permit persons to whom the # Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. # # # Version history # 1.0, 21 March 2003 # 1.1, 25 March 2003: don't allow negative word counts; don't say '0 word' # 1.2, 3 May 2003: get item text in EntryContents and apply text formatting # filters # 1.3, 1 October 2003: don't implicitly split to @_ # # package MT::Plugin::EntryContents; use MT; use MT::Template::Context; MT::Template::Context->add_tag(EntryContents => \&EntryContents); use vars qw( $VERSION ); $VERSION = 1.2; sub WordCount { my $text = shift; return scalar(my @foo = split /(?:\s|<[^>]*>)+/, $text); } sub ParagraphCount { my $text = shift; my @pghs = $text =~ m{<\s*p[>\s]}ig; return scalar(@pghs) if @pghs; return scalar(my @foo = split /(?:\r\n|\r|\n){2}/, $text); } sub LinkCount { my $text = shift; my @links = $text =~ m{<\s*a\s}ig; return scalar(@links); } sub ImageCount { my $text = shift; my @images = $text =~ m{<\s*img}ig; return scalar(@images); } sub EntryContents { my ($ctx, $args) = @_; my $entry = $ctx->stash('entry'); return $ctx->error("MTEntryContents must be used in an entry context") unless $entry; my $text = $entry->text || ''; $text .= "\n\n"; $text .= $entry->text_more || ''; $text = MT->apply_text_filters($text, $entry->text_filters); my @ret; # Do words or more words. if($args->{'word'}) { my $wc = WordCount($text); my $words; if($args->{'no_generate'} || $entry->excerpt) { $words = ' word'; } else { $wc -= $ctx->stash('blog')->words_in_excerpt || 20; $wc = 0 if $wc < 0; $words = ' more word'; }; push @ret, $wc . $words . ($wc == 1 ? '' : 's'); }; # Do the other counts my %subs = ( 'paragraph' => \&ParagraphCount, 'link' => \&LinkCount, 'image' => \&ImageCount, ); foreach (keys %subs) { if($args->{$_}) { my $val = $subs{$_}->($text); push @ret, "$val $_" . ($val == 1 ? '' : 's') if $val; }; }; my $glue = $args->{'glue'} || ', '; return join $glue, @ret; }