#!/usr/local/bin/perl 

use strict;
use HTML::TokeParser;
use IO::File;

{
  my @content_of_style;
  my $content_style_string;
 
  my $target = 'web';

# read style file and put the content in string 
  {
      my $filesty=IO::File->new('/home/sasha/final/attempt2.sty')
	  || die "Couldn't open style file for reading: $!\n";
      @content_of_style = <$filesty>;   
  }
  $content_style_string = join(' ',@content_of_style);

# Call styleparser to parser syle string and create hash

  my %style_for_target = &styleparser($target,$content_style_string);
     
}

# style subroutine 
# create a table for defined target
# from the information from Style File

sub styleparser {

  my ($target,$content_style_string) = @_;
  my @target_list = ('target','web','tex','edit','modified','rat','answer','metadis');
  my @value_style = ();
  my $current_key = '';
  my $current_value = '';
  my $stoken;
  my $flag;                  
  my $iele;
  my $flag_target;

  my $pstyle = HTML::TokeParser->new(\$content_style_string);

  while ($stoken = $pstyle->get_token) {
# start for tag definition
      if ($stoken->[0] eq 'S' and $stoken->[1] eq 'definetag') {
# new key in hash
	  $current_key = $stoken->[2]{name};
	  $flag = 0;
# metadata output
	  if ($target eq 'meta') {
	      while ($stoken = $pstyle->get_token and $stoken->[1] ne 'definetag') { 
		  if ($stoken->[0] eq 'S' and $stoken->[1] eq 'meta') {
		      while ($stoken = $pstyle->get_token and $stoken->[1] ne 'meta') {
			  $current_value .= $stoken->[1];
		      }
		  }
	      }
	  } else {
# render output
	      while ($stoken = $pstyle->get_token and $stoken->[1] ne 'render') {
		  if ($stoken->[1] eq 'definetag') {
		      $flag = 1;
		      last;
		  }
	      }
	      if ($flag == 0) { 
		  while ($stoken = $pstyle->get_token and $stoken->[1] ne 'definetag'
			 and $stoken->[1] ne 'render') {
# if token not equal to target $flag_target=0 
		      $flag_target = 0;
		      for (my $i=0; $i<$#target_list; $i++) {
			  if ($stoken->[1] eq $target_list[$i]) {
			      $flag_target = 1;
			  }
		      }
		      if ($flag_target == 0) {
# target not found
			  if ($stoken->[0] eq 'T') {
			      $current_value .= $stoken->[1];
			  } elsif ($stoken->[0] eq 'S') {
			      my $flagelem = 0;    
			      for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
				  if ($stoken->[1] eq $value_style[$i]) {
				      $flagelem = 1;
				      $iele = $i + 1;
				      last;
				  }
			      }
			      if ($flagelem == 0) {
				  $current_value .= $stoken->[4]; 
			      } else {
				  $current_value .= $value_style[$iele];
			      }
			  } elsif ($stoken->[0] eq 'E') {
			      my $flagelem = 0;    
			      for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
				  if ($stoken->[1] eq $value_style[$i]) {
				      $flagelem = 1;
				      $iele = $i + 1;
				      last;
				  }
			      }
			      if ($flagelem == 0) {
				  $current_value .= $stoken->[2];
			      } else {
				  $current_value .= $value_style[$iele];
			      }                 
			  } 
		      } else {
# target found
			  if ($stoken->[0] eq 'S' and $stoken->[1] eq 'target') {
# target defined via <target> tag
			    if (defined $stoken->[2]{dest}) {
			      if (index($stoken->[2]{dest},$target) == -1) {
				  while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
				  }			
			      } elsif (index($stoken->[2]{dest},$target) != -1) {
				  while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
				      if ($stoken->[0] eq 'T') {
					  $current_value .= $stoken->[1];
				      } elsif ($stoken->[0] eq 'S') {
					  my $flagelem = 0;    
					  for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
					      if ($stoken->[1] eq $value_style[$i]) {
						  $flagelem = 1;
						  $iele = $i + 1;
						  last;
					      }
					  }
					  if ($flagelem == 0) {
					      $current_value .= $stoken->[4]; 
					  } else {
					      $current_value .= $value_style[$iele];
					  }
				      } elsif ($stoken->[0] eq 'E') {
					  my $flagelem = 0;    
					  for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
					      if ($stoken->[1] eq $value_style[$i]) {
						  $flagelem = 1;
						  $iele = $i + 1;
						  last;
					      }
					  }
					  if ($flagelem == 0) {
					      $current_value .= $stoken->[2];
					  } else {
					      $current_value .= $value_style[$iele];
					  }        
				      }
				  } 
			      }
			    } else {
			      if (index($stoken->[2]{excl},$target) != -1) {
				  while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
				  }			
			      } elsif (index($stoken->[2]{excl},$target) == -1) {
				  while ($stoken = $pstyle->get_token and $stoken->[1] ne 'target') {
				      if ($stoken->[0] eq 'T') {
					  $current_value .= $stoken->[1];
				      } elsif ($stoken->[0] eq 'S') {
					  my $flagelem = 0;    
					  for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
					      if ($stoken->[1] eq $value_style[$i]) {
						  $flagelem = 1;
						  $iele = $i + 1;
						  last;
					      }
					  }
					  if ($flagelem == 0) {
					      $current_value .= $stoken->[4]; 
					  } else {
					      $current_value .= $value_style[$iele];
					  }
				      } elsif ($stoken->[0] eq 'E') {
					  my $flagelem = 0;    
					  for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
					      if ($stoken->[1] eq $value_style[$i]) {
						  $flagelem = 1;
						  $iele = $i + 1;
						  last;
					      }
					  }
					  if ($flagelem == 0) {
					      $current_value .= $stoken->[2];
					  } else {
					      $current_value .= $value_style[$iele];
					  }        
				      }
				  } 
			      }


			    }
			  } elsif ($stoken->[1] ne $target) {
#target defined via short-form tag
			      my $tempo_token = $stoken->[1];
			      while ($stoken = $pstyle->get_token and $stoken->[1] ne $tempo_token) {
			      }
			  } else {
			      my $tempo_token = $stoken->[1];
			      while ($stoken = $pstyle->get_token and $stoken->[1] ne $tempo_token) {
				  if ($stoken->[0] eq 'T') {
				      $current_value .= $stoken->[1];
				  } elsif ($stoken->[0] eq 'S') {
				      my $flagelem = 0;    
				      for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
					  if ($stoken->[1] eq $value_style[$i]) {
					      $flagelem = 1;
					      $iele = $i + 1;
					      last;
					  }
				      }
				      if ($flagelem == 0) {
					  $current_value .= $stoken->[4]; 
				      } else {
					  $current_value .= $value_style[$iele];
				      }
				  } elsif ($stoken->[0] eq 'E') {
				      my $flagelem = 0;    
				      for (my $i=$#value_style-1;$i>=0;$i=$i-2) {
					  if ($stoken->[1] eq $value_style[$i]) {
					      $flagelem = 1;
					      $iele = $i + 1;
					      last;
					  }
				      }
				      if ($flagelem == 0) {
					  $current_value .= $stoken->[2];
				      } else {
					  $current_value .= $value_style[$iele];
				      }        
				  }                                                                                    
			      }           
			  }
		      }
		  }

	      }
	  }    
      }    
      $current_value =~ s/(\s)+/$1/g;
      if ($current_value ne ' ' and $current_value ne '' ) {  
	  push (@value_style,lc $current_key,$current_value);
      }
      $current_key = '';
      $current_value = '';         
  }  
  my %style_for_target = @value_style;   
# check printing
  while (($current_key,$current_value) = each %style_for_target) {
      print "$current_key => $current_value\n";
  }
# return result
  return %style_for_target; 
}

