filename(./htgraph)
formtitle(Url Graph Drawing Tool)
proc(id)
desc(htgraphDesc)
livefeedback(htgraphLive)
annotate(nodecolor)
iform(text)
init(addURL)
quit(drawGraph)
annotate(colorNode)

##

=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

=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

=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 htgraphDesc {
    return "Utility to build graphs based on browsed pages.";
}

=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 id {
    my $item = shift;
    if ($item =~ m/__MARCA__/) {
        $item = _loadit($item);
    }
    return $item;
}

=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 htgraphLive {
    my $r = 'Browse pages to add them to graph.<br>Tip: use the form on the right to change the node color for this page.<br>Hit the quit button to finish browisng and check your graph.';
    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 addURL {
    my $count = keys %estado;
    $count eq '' and $count = 0;
    $U =~ s/\/$//g;
    $estado{++$count} = "white;$U";
}

sub drawGraph {
    use GraphViz;

    my $path = GraphViz->new(node => {fontsize => '10'});
    my $file = int(rand(10000));

    $path->add_node('START',style=>'filled',fillcolor=>'orange',shape=>'octagon');
    $last = 'START';
    foreach (sort keys %estado) {
        my ($left,$right) = split /;/, $estado{$_};
        $path->add_node($right,style=>'filled',fillcolor=>$left);
        $path->add_edge($last=>$right);
        $last = $right;
    }

    open FPNG, ">/var/www/html/nrc/navegante/$file.png";
    open FPS, ">/var/www/html/nrc/navegante/$file.ps";
    open FSVG, ">/var/www/html/nrc/navegante/$file.svg";
    print FPNG $path->as_png;
    print FPS $path->as_ps;
    print FSVG $path->as_svg;
    system "/usr/bin/ps2pdf /var/www/html/nrc/navegante/$file.ps /var/www/html/nrc/navegante/$file.pdf";
    unlink "/var/www/html/nrc/navegante/$file.ps";
    close FPNG;
    close FPS;
    close PSVG;

    print "<center><img border='1' src='http://nrc.homelinux.org/navegante/$file.png'>";

    print "<table width='50%'>";
    print "<tr bgcolor='orange'><td>Pos</td><td>URL</td></tr>";
    foreach (sort keys %estado) {
        my ($left,$right) = split /;/, $estado{$_};
        print "<tr><td>$_</td><td><a href='$right'>$right</a></td></tr>";
    }
    print "</table><hr>Your graph exported in other formats: <a href='http://nrc.homelinux.org/navegante/$file.pdf'>PDF</a> || <a href='http://nrc.homelinux.org/navegante/$file.svg'>SVG</a></center>";
}

sub colorNode {
    my $url = $U;
    my $color = param('user_data');

    foreach (sort keys %estado) {
print STDERR "I $_ $estado{$_}\n";
        my ($left, $right) = split /;/, $estado{$_};
        if ($right eq $url) {
            $left = $color;
            $estado{$_} = "$left;$right" and return 1;
        }
    }
}

