| 1 | # -*- mode: Perl; -*- | ||
| 2 | |||
| 3 | package NewsClipper::Parser; | ||
| 4 | |||
| 5 | # This package contains a parser for News Clipper "enabled" HTML files. It | ||
| 6 | # basically passes all tags except ones like <!--newsclipper ...-->, which are | ||
| 7 | # parsed for commands which are then executed. | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use HTML::Parser; | ||
| 11 | use NewsClipper::TagParser; | ||
| 12 | |||
| 13 | use vars qw( @ISA $VERSION ); | ||
| 14 | @ISA = qw(HTML::Parser); | ||
| 15 | |||
| 16 | BEGIN | ||
| 17 | { | ||
| 18 | # We do this in the BEGIN block to get the DEBUG constant before the rest of | ||
| 19 | # the code is compiled into bytecode. | ||
| 20 | require NewsClipper::Globals; | ||
| 21 | NewsClipper::Globals->import; | ||
| 22 | } | ||
| 23 | |||
| 24 | $VERSION = 0.70; | ||
| 25 | |||
| 26 | my $span_active = 0; | ||
| 27 | |||
| 28 | # ------------------------------------------------------------------------------ | ||
| 29 | |||
| 30 | # Basically pass everything through except the special tags. | ||
| 31 | sub text | ||
| 32 | { | ||
| 33 | return if $span_active; | ||
| 34 | print "$_[1]"; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub declaration | ||
| 38 | { | ||
| 39 | return if $span_active; | ||
| 40 | print "<!$_[1]>"; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub process | ||
| 44 | { | ||
| 45 | return if $span_active; | ||
| 46 | print pop @_; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub start | ||
| 50 | { | ||
| 51 | return if $span_active; | ||
| 52 | print pop @_; | ||
| 53 | } | ||
| 54 | |||
| 55 | # ------------------------------------------------------------------------------ | ||
| 56 | |||
| 57 | sub end | ||
| 58 | { | ||
| 59 | my $self = shift; | ||
| 60 | my $tagname = shift; | ||
| 61 | my $originalText = shift; | ||
| 62 | |||
| 63 | return if $span_active; | ||
| 64 | |||
| 65 | # Print a generator message so we can use the search engines to get a feel | ||
| 66 | # for how popular News Clipper is... | ||
| 67 | if ($tagname eq 'head') | ||
| 68 | { | ||
| 69 | print "<meta name=generator content=\"News Clipper ", | ||
| 70 | "$main::VERSION $config{product}\">\n"; | ||
| 71 | } | ||
| 72 | |||
| 73 | print $originalText; | ||
| 74 | } | ||
| 75 | |||
| 76 | # ------------------------------------------------------------------------------ | ||
| 77 | |||
| 78 | # We embed News Clipper commands in comments. | ||
| 79 | |||
| 80 | sub comment | ||
| 81 | { | ||
| 82 | my $self = shift; | ||
| 83 | my $originalText = pop @_; | ||
| 84 | |||
| 85 | if ($originalText =~ /^\s*$config{tag_text}\s+startcomment\b/is) | ||
| 86 | { | ||
| 87 | $span_active = 1; | ||
| 88 | } | ||
| 89 | elsif ($originalText =~ /^\s*$config{tag_text}\s+endcomment\b/is) | ||
| 90 | { | ||
| 91 | $span_active = 0; | ||
| 92 | } | ||
| 93 | elsif ($originalText =~ /^\s*$config{tag_text}\b/is) | ||
| 94 | { | ||
| 95 | return if $span_active; | ||
| 96 | |||
| 97 | dprint "Found $config{tag_text} tag:"; | ||
| 98 | lprint "Found $config{tag_text} tag:"; | ||
| 99 | |||
| 100 | # Clear out the %errors log | ||
| 101 | undef %errors; | ||
| 102 | |||
| 103 | dprint "<!--$originalText-->"; | ||
| 104 | lprint "<!--$originalText-->"; | ||
| 105 | |||
| 106 | # Take off the newsclipper stuff | ||
| 107 | my ($commandText) = $originalText =~ /^\s*$config{tag_text}\s*(.*)\s*$/is; | ||
| 108 | |||
| 109 | # Get the commands | ||
| 110 | my $parser = new NewsClipper::TagParser; | ||
| 111 | my @commands = $parser->parse($commandText); | ||
| 112 | |||
| 113 | if ($#commands == -1) | ||
| 114 | { | ||
| 115 | $errors{'parser'} .= | ||
| 116 | "A News Clipper comment was found, but no valid commands.\n"; | ||
| 117 | return; | ||
| 118 | } | ||
| 119 | else | ||
| 120 | { | ||
| 121 | # Now execute the commands | ||
| 122 | require NewsClipper::Interpreter; | ||
| 123 | my $interpreter = new NewsClipper::Interpreter; | ||
| 124 | |||
| 125 | # The trial version puts a nag in the output | ||
| 126 | if ($config{product} eq 'Trial') | ||
| 127 | { | ||
| 128 | print dequote <<" EOF"; | ||
| 129 | <table border=1> | ||
| 130 | <tr> | ||
| 131 | <td> | ||
| 132 | EOF | ||
| 133 | } | ||
| 134 | |||
| 135 | $interpreter->Execute(@commands); | ||
| 136 | |||
| 137 | # The trial version puts a nag in the output | ||
| 138 | if ($config{product} eq 'Trial') | ||
| 139 | { | ||
| 140 | print dequote <<" EOF"; | ||
| 141 | <p> | ||
| 142 | <a href="http://www.newsclipper.com/"> | ||
| 143 | <img src="http://www.newsclipper.com/images/ncnow.gif" align=left> | ||
| 144 | </a> | ||
| 145 | This dynamic content brought to you by | ||
| 146 | <a href="http://www.newsclipper.com/">News Clipper</a>. The registered | ||
| 147 | version does not have this message. | ||
| 148 | </p> | ||
| 149 | |||
| 150 | </td> | ||
| 151 | </tr> | ||
| 152 | </table> | ||
| 153 | EOF | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | LogErrorsAndPrintNotice($originalText); | ||
| 158 | undef %errors; | ||
| 159 | } | ||
| 160 | # If it's not a special tag, just print it out. | ||
| 161 | else | ||
| 162 | { | ||
| 163 | return if $span_active; | ||
| 164 | |||
| 165 | print "<!--$originalText-->"; | ||
| 166 | } | ||
| 167 | |||
| 168 | } | ||
| 169 | |||
| 170 | # ------------------------------------------------------------------------------ | ||
| 171 | |||
| 172 | # Print out any errors that occured while executing a sequence of News Clipper | ||
| 173 | # commands | ||
| 174 | |||
| 175 | sub LogErrorsAndPrintNotice | ||
| 176 | { | ||
| 177 | my $commands = shift; | ||
| 178 | |||
| 179 | my $expandedCommands = $errors{'expanded commands'} || undef; | ||
| 180 | delete $errors{'expanded commands'}; | ||
| 181 | |||
| 182 | return unless keys %errors; | ||
| 183 | |||
| 184 | # Localize %errors since we're going to change it. | ||
| 185 | local %errors = %errors; | ||
| 186 | |||
| 187 | my $wereErrors = 0; | ||
| 188 | $wereErrors = 1 if keys %errors; | ||
| 189 | |||
| 190 | if (exists $errors{'tagparser'}) | ||
| 191 | { | ||
| 192 | lprint reformat dequote <<" EOF"; | ||
| 193 | The following errors occurred while processing this | ||
| 194 | sequence of commands: $errors{'tagparser'} | ||
| 195 | EOF | ||
| 196 | } | ||
| 197 | |||
| 198 | if (exists $errors{'parser'}) | ||
| 199 | { | ||
| 200 | lprint reformat dequote <<" EOF"; | ||
| 201 | The following errors occurred while processing this | ||
| 202 | News Clipper comment: $errors{'parser'} | ||
| 203 | EOF | ||
| 204 | } | ||
| 205 | |||
| 206 | if (exists $errors{'acquisition'}) | ||
| 207 | { | ||
| 208 | lprint reformat dequote <<" EOF"; | ||
| 209 | The following errors occurred while attempting to | ||
| 210 | acquire the data from the remote server: $errors{'acquisition'} | ||
| 211 | EOF | ||
| 212 | } | ||
| 213 | |||
| 214 | if (exists $errors{'interpreter'} && !exists $errors{'acquisition'}) | ||
| 215 | { | ||
| 216 | lprint reformat dequote <<" EOF"; | ||
| 217 | The following errors occurred while attempting to execute the | ||
| 218 | News Clipper commands: $errors{'interpreter'} | ||
| 219 | EOF | ||
| 220 | } | ||
| 221 | |||
| 222 | foreach my $key (keys %errors) | ||
| 223 | { | ||
| 224 | if ($key =~ /^handler#(.*)/) | ||
| 225 | { | ||
| 226 | lprint reformat dequote <<" EOF"; | ||
| 227 | The following errors occurred while executing the handler "$1" | ||
| 228 | with this sequence of commands: $errors{$key} | ||
| 229 | EOF | ||
| 230 | delete $errors{$key}; | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | # Delete the error types we know of, since we're now done processing them. | ||
| 235 | delete $errors{'tagparser'}; | ||
| 236 | delete $errors{'acquisition'}; | ||
| 237 | delete $errors{'interpreter'}; | ||
| 238 | |||
| 239 | foreach my $key (keys %errors) | ||
| 240 | { | ||
| 241 | delete $errors{$key} if ($key =~ /^handler#(.*)/); | ||
| 242 | } | ||
| 243 | |||
| 244 | # Now print any remaining, unknown, errors. | ||
| 245 | foreach my $key (keys %errors) | ||
| 246 | { | ||
| 247 | lprint reformat dequote <<" EOF"; | ||
| 248 | Unrecognized error: $errors{$key} | ||
| 249 | |||
| 250 | EOF | ||
| 251 | delete $errors{$key}; | ||
| 252 | } | ||
| 253 | |||
| 254 | if ($wereErrors) | ||
| 255 | { | ||
| 256 | $commands =~ s/^.*?\n*(\s*<)/$1/s; | ||
| 257 | $commands =~ s/\s*$//s; | ||
| 258 | lprint "\nThe sequence of commands was:\n$commands\n"; | ||
| 259 | |||
| 260 | if (($commands !~ /<\s*output/i) && (defined $expandedCommands)) | ||
| 261 | { | ||
| 262 | $expandedCommands =~ s/\s*$//s; | ||
| 263 | lprint <<" EOF"; | ||
| 264 | This input command was expanded using the default filter and output commands | ||
| 265 | for the handler, which resulted in: | ||
| 266 | $commands | ||
| 267 | $expandedCommands | ||
| 268 | EOF | ||
| 269 | } | ||
| 270 | } | ||
| 271 | |||
| 272 | print<<" EOF"; | ||
| 273 | <!-- News Clipper error message: An error occured while processing this | ||
| 274 | sequence of News Clipper commands. See the run log file entry with timestamp | ||
| 275 | "$config{start_time}" for more information. --> | ||
| 276 | EOF | ||
| 277 | } | ||
| 278 | |||
| 279 | 1; |