#!/usr/bin/perl

####################################################################
# Load modules
use HTTP::Request::Common;
use LWP::UserAgent;
use Term::ANSIColor;

system ('clear');
###############################################################################################################
print   color("red"),   "################################################################",color("reset"),"\n",
        color("white"), "Presenting:                                                     ",color("reset"),"\n",
        color("white"), "       dictionary lookup from webster.com                       ",color("reset"),"\n",
        color("red"),   "################################################################",color("reset"),"\n";
print   "Part 1:",color("magenta"),"\tRun this program with or without 1 argument         ",color("reset"),"\n",
                  color("magenta"),"\t\tIf you do not supply a work, you will be prompted.",color("reset"),"\n",
	          color("magenta"),"\tThe program goes to the webster web site,           ",color("reset"),"\n",
		  color("magenta"),"\tand parses the responce.                            ",color("reset"),"\n";

###############################################################################################################

####################################################################
# Check to see if we were run interactively 
#
if ( @ARGV ){
  chomp ( $word = shift ) ;
} else { 
  #
  # Since @ARGV was empty, we need to ask them for a url
  #
  print color("green"),"Please enter a word:\t",color("reset");
  chomp ( $word=<STDIN> );
}    

####################################################################
# Make a new object from the LWP::UserAgent class
my $ua = LWP::UserAgent->new;

####################################################################
# Make a request object.  
my $res=$ua->request( POST 'http://www.webster.com/cgi-bin/dictionary',
       Content_Type => 'application/x-www-form-urlencoded', #'multipart/form-data', # 'text/plain'
       Content      => [ 'book' => 'Dictionary', 'va' => "$word" ] 
       );
       
####################################################################
# if we get an error, die
if ( $res->is_error() ){ die "Error: " , $res->status_line , "\n"; }

####################################################################
# Assign the result to a string
my $page = $res->content();

####################################################################
# Turn the html into text
use HTML::TreeBuilder;
use HTML::FormatText;
$html = HTML::TreeBuilder->new();
$html->parse($page);
$formatter=HTML::FormatText->new(leftmargin => 0,rightmargin => 60);
$ascii = $formatter->format( $html );
####################################################################
#  Sample Data
####################################################################
#Main Entry: 1dog [IMAGE] [IMAGE]
#Pronunciation: 'dog, 'däg
#Function: noun
#Usage: often attributive
#Etymology: Middle English, from Old English docga
#Date: before 12th century
#1 a : CANID; especially : a highly variable domestic mammal (Canis
#familiaris) closely related to the common wolf (Canis lupus)
#b : a male dog; also : a male usually carnivorous mammal
#2 a : a worthless person b : FELLOW, CHAP <a lazy dog> <you
#lucky dog>
#3 a : any of various usually simple mechanical devices for
#holding, gripping, or fastening that consist of a spike, bar,
#or hook b : ANDIRON
#4 : uncharacteristic or affected stylishness or dignity <put
#on the dog>
#5 capitalized : either of the constellations Canis Major or
#Canis Minor
#6 plural : FEET
#7 plural : RUIN <going to the dogs>
#8 : one inferior of its kind: as a : an investment not worth
#its price b : an undesirable piece of merchandise
#9 : an unattractive person and especially a girl or woman
#10 : HOT DOG 1
#- dog·like [IMAGE] /'dog-"lIk/ adjective
#[dog illustration]
####################################################################
#  ## (optional word) : <scentence>
####################################################################
# Try to grab every line from Main to the last defenition
$ascii =~ m{
		\nMain\ Entry:\s((?:.*)(?:\n.*)?)   #capture a possible multiline entery, ?: is non-capturing
		\nPronunciation:\s((?:.*)(?:\n.*)?) # ...
		\nFunction:\s((?:.*)(?:\n.*)?)      # ...
		\nUsage:\s((?:.*)(?:\n.*)?)         # ...
		\nEtymology:\s((?:.*)(?:\n.*)?)     # ...
		\nDate:\s((?:.*)(?:\n.*)?)\n	    # ...
		(                     #----This Section Matches The Defenitions----
		  \d+                 # one or more digits
		  \s+                 # followed by a space
		  (?:\w\s)?           # followed by an optional word and a space(like pleral or concat)
		  :\s+                # followed by a colon and a space
		  \w+(?:\s|\w+)+      # word followed by one or more spaces or words
		  #(?:\w)+                           # Followed by a bunch of words
		  #(\.$|\n\w\b                      # Line ending in a perioud, or  
		  #(?:\w+(?:\s\w+)+)(?:\.)?    # Try 4: word, follwed by an optional word and space, optional perioud
		  #(?:\n\w+(?:\s\w+)+)?        # Try 4: Optionally, a perioud, or new line and more data.
		  #(?:\w+\n(\w+\n)?\.)     # Try 2: followed by words, and possibly a new line and more words, to a perioud.
		  #(?:.*\n(?:.*\n)?)       # Try 3: A scentence and new line, and optional multiline, ?: noncaptures
                  #(?s).*(?m)         # Try 5:
		  #(?:\n)             # ending in a new line
		)+                    # grab one or more defenition lines
}xig ; 

print "1\t$1\n
       2\t$2\n
       3\t$3\n
       4\t$4\n
       5\t$5\n
       6\t$6\n
       7\t$7\n
       8\t$8\n
       9\t$9\n";

#print "\n&\t$&\n";
#print "\n'\t$'";
#print "\n.\t$.";

####################################################################
# Assign the values to anonymous hash
$dictionary = {
	main          => "$1",
	pronunciation => "$2",
	function      => "$3",
	usage         => "$4",
	etymology     => "$5",
	date          => "$6",
	definition    => "$7"	
};	
####################################################################

####################################################################
# Print it out
#print "Main Entry:\t$dictionary->{main}\n",
#      "Pronunciation:\t$dictionary->{pronunciation}\n",
#      "Function:\t$dictionary->{function}\n", 
#      "Usage:\t$dictionary->{usage}\n", 
#      "Etymology:\t$dictionary->{etymology}\n", 
#      "Date:\t$dictionary->{date}\n";

####################################################################
# format the data using format and write.
print color("green");

format STDOUT =
Main Entry:      ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                 $dictionary->{main},    

Pronunciation:   ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<     
                 $dictionary->{pronunciation},    

Function:        ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                 $dictionary->{function},	

Usage:           ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                 $dictionary->{usage}, 
 
Date:            ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                 $dictionary->{date}

Etymology:       ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 
                 $dictionary->{etymology},       

Definition: 
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$dictionary->{definition}
.
write();
print color ("reset");
#print "\n$ascii\n";
