filename(./htspell)
formtitle(Spell Checking Tool)
proc(htspellFunction)
desc(htspellDesc)
init(htspellInit)
feedback(htspellFeedback)
livefeedback(htspellLive)

##

=head1 NAME

htspell - an application built using Navegante

=head1 SYNOPSIS

Build the application:

  $ navegante examples/htspell

Use the newly created file I<htspell> as a CGI.

=head1 DESCRIPTION

This application behaves as a CGI and it checks in a dictionary if
the word exists. The application keeps track of words not found. On
every request we also use the banner live feedback section to present
some information about the number of not found words. The specific DSL 
section is:

  cginame(./htspell)
  formtitle(Spell Checking Tool)
  proc(htspellFunction)
  desc(htspellDesc)
  init(htspellInit)
  feedback(htspellFeedback)
  livefeedback(htspellLive)

=head1 MODULES

=head2 C<Text::Aspell>

Module C<Text::Aspell> was used to check for words in a dictionary.

=cut

use Text::Aspell;

=head1 VARIABLES

Some variables were used so that we can present information about the
number of words processed with every request.

=head2 C<$processed>

This variable keeps tracks of the total of processed words in every
request.

=head2 C<$found>

This variable keeps track of the number of found words in the dictionary.

=head2 C<$not_found>

This variable keeps track of the number of found not words in the 
dictionary.

=cut

my $processed;
my $found;
my $not_found;

=head1 FUNCTIONS

=head2 C<htspellDesc>

This function prints the description of the application. This
is defined in the DSL by the C<desc> statement.

=cut

sub htspellDesc {
    return "Spell cheking utility.";
}

=head2 C<htspellFunction>

This function is used to process the page content. We are checking for
words in the dictionary, we are pushing words not found to the special
variable I<estado> in order to keep track of not found words over
sequential requests. We are also updating counters, like the number of
processed or not found words.

XXX _loadit

=cut

sub htspellFunction {
    my $item = shift;
    if ($item =~ m/__MARCA__/) {
        $item = _loadit($item);
    }
    else {
        @new = ();
        @words = split(/\s+/, $item);
        foreach (@words) {
            if ($_ =~ m/\w+/) {
                $processed++;
                if ($speller->check($_)) {
                    $found++;
                    push @new, $_;
                }
                else {
                    $not_found++;
                    $estado{$_}++;
                    push @new, "<u>$_</u>";

                }
            }
        }
    }
    return $item;
}

=head2 C<htspellFeedback>

This is the fuction called when the application's feedback link is 
followed. This is defined in the DSL by the C<livefeedback>. In this
case we are printing the list of words not found since the application
started, and for each one the number of occurrences.

=cut

sub htspellFeedback {
    h3("Words not found in the dictionary:").
    small(ul(li([map{"$_ - $estado{$_}"}
        sort {$estado{$b} <=> $estado{$a}} keys %estado])));
}

=head2 C<htspellLive>

This is the function that prints the HTML that feeds the banner section
for reporting information. This is defined in the DSL by the
C<livefeedback> statement. Here we print information about the number 
of not found words for every page viewed.

=cut

sub htspellLive {
    my $p = int($not_found/$processed*100);
    my $r = "I checked $processed words, $p% were not found in the dictionary.";
    ($p > 50) and $r .= "<br>Tip: Maybe this page isn't written in english.";
    return $r;
}

=head2 C<htspellInit>

This function runs on the beginig of every request and it's used here
to reset word counters and create an Aspell object for word checking. 
This is defined in the DSL by the C<init> statement.

=cut

sub htspellInit {
    $speller = Text::Aspell->new;
    $speller->set_option('lang','en_US');

    $processed = 0;
    $found = 0;
    $not_found = 0;

}
