1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
#!/usr/bin/perl
# FILE: "/home/evmik/src/my_src/GradeBook/banner2csv.pl"
# LAST MODIFICATION: "Wed, 21 Jan 2015 16:06:08 -0500 (evmik)"
# (C) 2012 by Eugeniy Mikhailov, <evgmik@gmail.com>
#use HTML::Entities;
use HTML::Parser;
use HTML::TableExtract;
use Encode;
#use HTML::TableExtract qw(tree);
use Data::Dumper;
#use strict;
if ( @ARGV < 1 )
{
print "Converts a WM banner summary list to csv file with students info\n";
print "\n";
print "Usage:\n";
print " " . $0 . " banner_html_file_to_parse\n";
print "\n";
print "Example:\n";
print " " . $0 . " banner_class_summary_list.html\n";
die;
}
my $html_file = shift;
open (F_HTML, "<", $html_file) or die "Failed to read file $html_file : $!";
my @fcontent = <F_HTML>;
$html_string=join('', @fcontent);
# lets find CRN, i.e. section number
my $flag_crn_located = 0;
foreach $ln (@fcontent) {
if ($flag_crn_located == 1) {
$crn_str = $ln;
last;
}
if ($ln =~ /CRN:/) {
$flag_crn_located = 1;
}
}
#print "crn string ".$crn_str;
my $section = "unknown";
if ( $crn_str =~ m{>(\d+)<} ) {
$section = $1;
} else {
$section = "unknown";
}
#print "section: ".$section."\n";
# this look for the table based on its headers
my $te = HTML::TableExtract->new(
headers => [qw(Record Student ID)],
keep_html => 1,
slice_columns => 0
);
# find table with students info
$te->parse($html_string);
my $parsed_text="";
my $href="";
$aparser = HTML::Parser->new(api_version => 3,
start_h => [\&a_start_handler, "self,tagname,attr"],
report_tags => [qw(a img)],
);
$spanparser = HTML::Parser->new(api_version => 3,
start_h => [\&span_start_handler, "self,tagname,attr"],
report_tags => [qw(span)],
);
sub span_start_handler
{
my($self, $tag, $attr) = @_;
return unless $tag eq "span";
$self->handler(text => [], '@{dtext}' );
$self->handler(start => \&span_start_handler);
$self->handler(end => \&spantag_end_handler, "self,tagname");
}
sub a_start_handler
{
my($self, $tag, $attr) = @_;
return unless $tag eq "a";
return unless exists $attr->{href};
$href=$attr->{href};
#print "A $attr->{href}\n";
$self->handler(text => [], '@{dtext}' );
$self->handler(start => \&img_handler);
$self->handler(end => \&atag_end_handler, "self,tagname");
}
sub img_handler
{
my($self, $tag, $attr) = @_;
return unless $tag eq "img";
push(@{$self->handler("text")}, $attr->{alt} || "[IMG]");
}
sub atag_end_handler
{
my($self, $tag) = @_;
my $text = encode('utf8',join("", @{$self->handler("text")}));
$text =~ s/^\s+//;
$text =~ s/\s+$//;
$text =~ s/\s+/ /g;
#print "T $text\n";
$parsed_text = $text;
$self->handler("text", undef);
$self->handler("start", \&a_start_handler);
$self->handler("end", undef);
}
sub spantag_end_handler
{
my($self, $tag) = @_;
my $text = encode('utf8',join("", @{$self->handler("text")}));
$text =~ s/^\s+//;
$text =~ s/\s+$//;
$text =~ s/\s+/ /g;
#print "T $text\n";
$parsed_text = $text;
$self->handler("text", undef);
$self->handler("start", \&span_start_handler);
$self->handler("end", undef);
}
# assumes that we were able to chose a proper table before
# and really get the proper one
foreach $row ($te->rows) {
#student name
$aparser->parse(@$row[1]);
$name=$parsed_text;
($lname, $fname)=split(',', $name);
$fname =~ s/^ //;
#student id
#print @$row[2];
$spanparser->parse(@$row[2]);
$sid=$parsed_text;
#student name
$aparser->parse(@$row[7]);
$email = $href;
$email =~ s/mailto://;
print join(',', $sid, $fname, $lname, $section, $email);
print "\n";
}
|