| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # use perl -*- mode: Perl; -*- | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | my $COMPATIBLE_NEWS_CLIPPER_VERSION = '1.18'; | ||
| 8 | my $VERSION = 0.14; | ||
| 9 | my $handlerserver = 'handlers.newsclipper.com'; | ||
| 10 | #my $handlerserver = 'localhost'; | ||
| 11 | |||
| 12 | #------------------------------------------------------------------------------- | ||
| 13 | |||
| 14 | die <<EOF | ||
| 15 | $0 handler1.pm handler2.pm ... | ||
| 16 | |||
| 17 | Run this program to modernize handlers built for a version of News Clipper | ||
| 18 | prior to $COMPATIBLE_NEWS_CLIPPER_VERSION | ||
| 19 | EOF | ||
| 20 | if $#ARGV == -1 || $ARGV[0] =~ /^-/; | ||
| 21 | |||
| 22 | DisplayMessage(); | ||
| 23 | |||
| 24 | foreach my $handler (@ARGV) | ||
| 25 | { | ||
| 26 | print "----> Processing handler $handler...\n"; | ||
| 27 | |||
| 28 | open HANDLER, $handler or die "Can't open handler: $!\n"; | ||
| 29 | my $code = join '',<HANDLER>; | ||
| 30 | close HANDLER; | ||
| 31 | |||
| 32 | my $old_code = $code; | ||
| 33 | |||
| 34 | my $old_version = GetNewsClipperVersion($code); | ||
| 35 | |||
| 36 | if (defined $old_version && $old_version >= $COMPATIBLE_NEWS_CLIPPER_VERSION) | ||
| 37 | { | ||
| 38 | print <<" EOF"; | ||
| 39 | This handler is for News Clipper version $old_version, | ||
| 40 | and this ConvertHandler will update up to version $COMPATIBLE_NEWS_CLIPPER_VERSION. | ||
| 41 | No need to update. | ||
| 42 | EOF | ||
| 43 | |||
| 44 | next; | ||
| 45 | } | ||
| 46 | |||
| 47 | if ($old_version < 1.18) | ||
| 48 | { | ||
| 49 | $code = CleanTrailingSpacesAndLinefeeds($code); | ||
| 50 | print "================\n"; | ||
| 51 | $code = ConvertComments($code); | ||
| 52 | print "================\n"; | ||
| 53 | $code = ConvertVersion($code); | ||
| 54 | print "================\n"; | ||
| 55 | $code = StripUses($code); | ||
| 56 | print "================\n"; | ||
| 57 | $code = StripDprint($code); | ||
| 58 | print "================\n"; | ||
| 59 | $code = ConvertDefaultHandlers($code); | ||
| 60 | print "================\n"; | ||
| 61 | $code = ConvertOldPatternMatches($code); | ||
| 62 | print "================\n"; | ||
| 63 | CheckGetFunction($code); | ||
| 64 | print "================\n"; | ||
| 65 | CheckErrorMessage($code); | ||
| 66 | print "================\n"; | ||
| 67 | CheckIsa($code); | ||
| 68 | print "================\n"; | ||
| 69 | CheckRef($code); | ||
| 70 | print "================\n"; | ||
| 71 | $code = ConvertFilterTypeFunction($code); | ||
| 72 | print "================\n"; | ||
| 73 | $code = ConvertOutputTypeFunction($code); | ||
| 74 | print "================\n"; | ||
| 75 | $code = ConvertTypeNames($code); | ||
| 76 | print "================\n"; | ||
| 77 | $code = AddComputeURL($code); | ||
| 78 | print "================\n"; | ||
| 79 | $code = AddSetDefaultAttributes($code); | ||
| 80 | print "================\n"; | ||
| 81 | $code = CheckLeftOvers($code); | ||
| 82 | print "================\n"; | ||
| 83 | $code = CleanTrailingSpacesAndLinefeeds($code); | ||
| 84 | } | ||
| 85 | |||
| 86 | #print $code,"\n"; | ||
| 87 | #next; | ||
| 88 | if ($code eq $old_code) | ||
| 89 | { | ||
| 90 | print "No change to handler -- nothing written\n"; | ||
| 91 | } | ||
| 92 | else | ||
| 93 | { | ||
| 94 | WriteHandler($handler,$code); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | #------------------------------------------------------------------------------- | ||
| 99 | |||
| 100 | sub GetNewsClipperVersion | ||
| 101 | { | ||
| 102 | my $code = shift; | ||
| 103 | |||
| 104 | my ($for_news_clipper_version) = | ||
| 105 | $code =~ /'For_News_Clipper_Version'} *= *'(.*?)' *;/s; | ||
| 106 | |||
| 107 | # Ug. Pre "For_News_Clipper_Version" days... | ||
| 108 | if (!defined $for_news_clipper_version) | ||
| 109 | { | ||
| 110 | return 1.00; | ||
| 111 | } | ||
| 112 | else | ||
| 113 | { | ||
| 114 | return $for_news_clipper_version; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | #------------------------------------------------------------------------------- | ||
| 119 | |||
| 120 | sub Pager | ||
| 121 | { | ||
| 122 | my $message = shift; | ||
| 123 | |||
| 124 | my @lines = split /\n/,$message; | ||
| 125 | my $count = 0; | ||
| 126 | foreach my $line (@lines) | ||
| 127 | { | ||
| 128 | $count++; | ||
| 129 | print "$line\n"; | ||
| 130 | print "Press enter..." and <STDIN> if $count % 24 == 0; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | #------------------------------------------------------------------------------- | ||
| 135 | |||
| 136 | sub DisplayMessage | ||
| 137 | { | ||
| 138 | my $message =<<' EOF'; | ||
| 139 | Greetings! This program will convert handlers written for the prior version of | ||
| 140 | News Clipper. Here are the new things you should know about the new handler | ||
| 141 | format: | ||
| 142 | |||
| 143 | - The comment block has been replaced with a hash, which will help with the | ||
| 144 | automated processing of the handler. | ||
| 145 | - "MAINTAINER_NAME" and "MAINTAINER_EMAIL" fields have been added. | ||
| 146 | - A "CATEGORY" field says what category the handler belongs. Be careful to | ||
| 147 | choose a legal category. | ||
| 148 | - A "FOR_NEWSCLIPPER_VERSION" field tells what version of News Clipper the | ||
| 149 | handler was built for. This helps accidental download of incompatible | ||
| 150 | handlers. | ||
| 151 | - Version numbers have been updated so that you can indicate when a change | ||
| 152 | will break people's input files, when a change adds functionality, and when | ||
| 153 | a change fixes a bug. | ||
| 154 | - Types have changed substantially: | ||
| 155 | - FilterType and OutputType return a "type signature", like "@($ | %)", | ||
| 156 | which means "an array of either scalars or hashes". @($ & %Slashdot) means | ||
| 157 | an array of at least one scalar and at least one Slashdot hash. | ||
| 158 | - A hunk of data matches a type signature if the structure matches, and any | ||
| 159 | subtype relationships hold. For example, a type signature of @($ | %) | ||
| 160 | would be matched by a data strcuture whose type signature is @($URL), | ||
| 161 | @Myarray(%), and @Myarray($URL & %Slashdot), but not by @(@). | ||
| 162 | - Everything in the data structure is a reference -- even scalars. So you | ||
| 163 | can't do this: | ||
| 164 | push @grabbedData, {'name' => $name, 'url' => $url} | ||
| 165 | Instead, do this: | ||
| 166 | push @grabbedData, {'name' => $name, 'url' => $url} | ||
| 167 | And when you want to use it, you have to do this: | ||
| 168 | print ${$hashref->{'name'}} | ||
| 169 | - Bless each new data type, not the whole data structure. Instead of this: | ||
| 170 | while ($line =~ /(.*)::(.*)/g) | ||
| 171 | { | ||
| 172 | my %hash = ('name' => $1,'url' => $2); | ||
| 173 | push @data, \%hash; | ||
| 174 | } | ||
| 175 | MakeSubType('ArrayOfSlashdotHash','ArrayOfHash'); | ||
| 176 | bless \@data,'ArrayOfSlashdotHash'; | ||
| 177 | Do this: | ||
| 178 | MakeSubType('Slashdot','HASH'); | ||
| 179 | while ($line =~ /(.*)::(.*)/g) | ||
| 180 | { | ||
| 181 | my %hash = ('name' => \\$1,'url' => \\$2); | ||
| 182 | bless \%hash,'Slashdot'; | ||
| 183 | push @data, \%hash; | ||
| 184 | } | ||
| 185 | - Before, you had to bless things as "String", "Array", or "Hash". You don't | ||
| 186 | have to do that now. | ||
| 187 | - However, any new types you introduce should be related to the others with | ||
| 188 | the MakeSubtype function: | ||
| 189 | MakeSubtype('Slashdot','HASH'); | ||
| 190 | - There's a new API function called TypesMatch(), which takes a data reference | ||
| 191 | and a types signature, and returns 1 if the data matches the signature. | ||
| 192 | - dprint() can be used to send output as <!-- DEBUG: ... --> messages in debug | ||
| 193 | mode. | ||
| 194 | - error() can be used to output errors about invalid arguments, etc. | ||
| 195 | - Default handlers are now specified as real News Clipper commands, like this: | ||
| 196 | <filter name=map filter=highlight words=linux> | ||
| 197 | <output name=array> | ||
| 198 | - FilterType and OutputType now provide the $data and $attributes as | ||
| 199 | arguments, so you can return a different type signature depending on | ||
| 200 | parameters such as "style". | ||
| 201 | - ComputeURL should now be used to compute the URL from the attributes. You'll | ||
| 202 | probably need to do this by hand. | ||
| 203 | - SetDefaultAttributes should now be used to set the default attributes for | ||
| 204 | the handler. For example, the "date" handler has a style attribute which | ||
| 205 | defaults to "day": <input name="date" style="day"> | ||
| 206 | - You should never have to check the type of $grabbedData -- News Clipper will | ||
| 207 | do that for you. | ||
| 208 | - A RunHandler API function was added, which takes the handler name, type of | ||
| 209 | handler, data, and attributes. It will automatically check for correct | ||
| 210 | types. | ||
| 211 | - "use NewsClipper::Handler" automatically pulls in a lot of stuff, so you | ||
| 212 | don't have to "use" the Types, HTMLTools, AcquisitionFunctions, DEBUG, | ||
| 213 | dprint, etc. | ||
| 214 | |||
| 215 | Nearly all of these will be automatically handled by this program. If | ||
| 216 | something can not be handled, a message will appear. This program will move | ||
| 217 | old copies of your handlers to .bak files, but backup your handlers anyway. | ||
| 218 | EOF | ||
| 219 | |||
| 220 | Pager $message; | ||
| 221 | } | ||
| 222 | |||
| 223 | #------------------------------------------------------------------------------- | ||
| 224 | |||
| 225 | sub CleanTrailingSpacesAndLinefeeds($) | ||
| 226 | { | ||
| 227 | my $code = shift; | ||
| 228 | |||
| 229 | $code =~ s/ +\n/\n/gs; | ||
| 230 | $code =~ s/\r//gs; | ||
| 231 | |||
| 232 | return $code; | ||
| 233 | } | ||
| 234 | |||
| 235 | #------------------------------------------------------------------------------- | ||
| 236 | |||
| 237 | sub ConvertComments($) | ||
| 238 | { | ||
| 239 | my $code = shift; | ||
| 240 | |||
| 241 | print "- Converting Comments\n"; | ||
| 242 | |||
| 243 | $code =~ /\n(#\s*AUTH.*?)\npackage /s; | ||
| 244 | my ($commentBlock) = $1; | ||
| 245 | |||
| 246 | print "Couldn't find comment block. Skipping comment conversion...\n" and | ||
| 247 | return $code unless defined $commentBlock; | ||
| 248 | |||
| 249 | my ($author) = $code =~ /AUTHOR: *(.*?) *\n/s; | ||
| 250 | my ($email) = $code =~ /EMAIL: *(.*?) *\n/s; | ||
| 251 | my ($description) = $code =~ /DESCRIPTION: *(.*?) *\n/s; | ||
| 252 | my ($syntax) = $code =~ /SYNTAX: *(.*?)\n(# [A-Z][A-Z]+:|[^#])/s; | ||
| 253 | my ($notes) = $code =~ /NOTES: *(.*?)\n(# [A-Z][A-Z]+:|[^#])/s; | ||
| 254 | my ($url) = $code =~ /URL: *(.*?)\n/s; | ||
| 255 | my ($license) = $code =~ /LICENSE: *(.*?)\n/s; | ||
| 256 | |||
| 257 | print <<" EOF" | ||
| 258 | Comment block couldn't be parsed correctly. Skipping comment conversion... | ||
| 259 | EOF | ||
| 260 | and return $code unless defined $author && defined $email && | ||
| 261 | defined $description && defined $syntax && defined $notes && | ||
| 262 | defined $url && defined $license; | ||
| 263 | |||
| 264 | $author =~ s/[\n#]//gs; | ||
| 265 | $author =~ s/ */ /g; | ||
| 266 | $email =~ s/[\n#]//gs; | ||
| 267 | $email =~ s/ */ /g; | ||
| 268 | $description =~ s/[\n#]//gs; | ||
| 269 | $description =~ s/ */ /g; | ||
| 270 | $description =~ s/\n\s*$/\n/s; | ||
| 271 | $description = '' if $description eq "\n"; | ||
| 272 | $description .= "\n" if $description ne '' && $description !~ /\n$/s; | ||
| 273 | |||
| 274 | $syntax =~ s/^ *# *\n/\n/s while $syntax =~ /^ *#/s; | ||
| 275 | $syntax =~ s/\n *# *$/\n/s while $syntax =~ /\n *# *$/s; | ||
| 276 | $syntax =~ s/^\s+//s; | ||
| 277 | $syntax =~ s/(^|\n) *#/$1/sg; | ||
| 278 | $syntax = dequote ($syntax); | ||
| 279 | $syntax =~ s/\n +\n/\n\n/s; | ||
| 280 | $syntax =~ s/\n\s*$/\n/s; | ||
| 281 | $syntax = '' if $syntax eq "\n"; | ||
| 282 | $syntax .= "\n" if $syntax ne '' && $syntax !~ /\n$/s; | ||
| 283 | |||
| 284 | $notes =~ s/^ *# *\n//s while $notes =~ /^ *#/s; | ||
| 285 | $notes =~ s/\n *# *$/\n/s while $notes =~ /\n *# *$/s; | ||
| 286 | $notes =~ s/^\s+//s; | ||
| 287 | $notes =~ s/(^|\n) *#/$1/sg; | ||
| 288 | $notes = dequote ($notes); | ||
| 289 | $notes =~ s/\n +\n/\n\n/s; | ||
| 290 | $notes =~ s/\n\s*$/\n/s; | ||
| 291 | $notes = '' if $notes eq "\n"; | ||
| 292 | $notes .= "\n" if $notes ne '' && $notes !~ /\n$/s; | ||
| 293 | |||
| 294 | my $category; | ||
| 295 | |||
| 296 | { | ||
| 297 | my ($handler) = | ||
| 298 | $code =~ /package NewsClipper::Handler::Acquisition::([^;]+);/; | ||
| 299 | $category = GetCategory($handler); | ||
| 300 | } | ||
| 301 | |||
| 302 | my $newMetaInfo = <<" EOF"; | ||
| 303 | \$handlerInfo{'Author_Name'} = '$author'; | ||
| 304 | \$handlerInfo{'Author_Email'} = '$email'; | ||
| 305 | \$handlerInfo{'Maintainer_Name'} = '$author'; | ||
| 306 | \$handlerInfo{'Maintainer_Email'} = '$email'; | ||
| 307 | \$handlerInfo{'Description'} = <<'EOF'; | ||
| 308 | ${description}EOF | ||
| 309 | \$handlerInfo{'Category'} = '$category'; | ||
| 310 | \$handlerInfo{'URL'} = <<'EOF'; | ||
| 311 | $url | ||
| 312 | EOF | ||
| 313 | \$handlerInfo{'License'} = '$license'; | ||
| 314 | \$handlerInfo{'For_News_Clipper_Version'} = '$COMPATIBLE_NEWS_CLIPPER_VERSION'; | ||
| 315 | \$handlerInfo{'Language'} = 'English'; | ||
| 316 | \$handlerInfo{'Notes'} = <<'EOF'; | ||
| 317 | ${notes}EOF | ||
| 318 | \$handlerInfo{'Syntax'} = <<'EOF'; | ||
| 319 | ${syntax}EOF | ||
| 320 | EOF | ||
| 321 | |||
| 322 | $code =~ s/\n(#\s*AUTH.*?)\npackage /\n\npackage /s; | ||
| 323 | |||
| 324 | my $use_vars; | ||
| 325 | $code =~ s/\b(use vars[^\n]+)\n/$use_vars = $1;''/es; | ||
| 326 | $use_vars =~ s/ *\);/ %handlerInfo );/; | ||
| 327 | |||
| 328 | $code =~ s/\n+(package [^\n]+\n)/\n\n$1\n$use_vars\n\n$newMetaInfo/s; | ||
| 329 | |||
| 330 | return $code; | ||
| 331 | } | ||
| 332 | |||
| 333 | #------------------------------------------------------------------------------- | ||
| 334 | |||
| 335 | sub GetCategory($) | ||
| 336 | { | ||
| 337 | my $handler = shift; | ||
| 338 | |||
| 339 | print "- Getting handler category\n"; | ||
| 340 | |||
| 341 | use LWP::Simple; | ||
| 342 | |||
| 343 | my $info = get("http://$handlerserver/cgi-bin/getinfo?field=Name\&string=$handler\&print=Category"); | ||
| 344 | |||
| 345 | if ($info =~ /Category *: (.+)\n/) | ||
| 346 | { | ||
| 347 | return $1; | ||
| 348 | } | ||
| 349 | else | ||
| 350 | { | ||
| 351 | print<<' EOF'; | ||
| 352 | - Couldn't locate handler in the database. Please choose a category: | ||
| 353 | 1) News Clipper | ||
| 354 | 2) General | ||
| 355 | 3) Tech | ||
| 356 | 4) Business | ||
| 357 | 5) Sports | ||
| 358 | 6) Science | ||
| 359 | 7) Weather | ||
| 360 | 8) Music | ||
| 361 | 9) Local | ||
| 362 | 10) Humor | ||
| 363 | 11) Comics | ||
| 364 | 12) Linux | ||
| 365 | 13) Programming | ||
| 366 | 14) Personal Computers | ||
| 367 | 15) Miscellaneous | ||
| 368 | EOF | ||
| 369 | |||
| 370 | my $choice; | ||
| 371 | $choice = <STDIN> until $choice =~ /\d+/s && $choice < 16 && $choice > 0; | ||
| 372 | |||
| 373 | return "News Clipper" if $choice == 1; | ||
| 374 | return "General" if $choice == 2; | ||
| 375 | return "Tech" if $choice == 3; | ||
| 376 | return "Business" if $choice == 4; | ||
| 377 | return "Sports" if $choice == 5; | ||
| 378 | return "Science" if $choice == 6; | ||
| 379 | return "Weather" if $choice == 7; | ||
| 380 | return "Music" if $choice == 8; | ||
| 381 | return "Local" if $choice == 9; | ||
| 382 | return "Humor" if $choice == 10; | ||
| 383 | return "Comics" if $choice == 11; | ||
| 384 | return "Linux" if $choice == 12; | ||
| 385 | return "Programming" if $choice == 13; | ||
| 386 | return "Personal Computers" if $choice == 14; | ||
| 387 | return "Miscellaneous" if $choice == 15; | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | #------------------------------------------------------------------------------- | ||
| 392 | |||
| 393 | sub ConvertVersion($) | ||
| 394 | { | ||
| 395 | my $code = shift; | ||
| 396 | |||
| 397 | print "- Converting Version Number\n"; | ||
| 398 | |||
| 399 | my $commentBlock = <<' EOF'; | ||
| 400 | # - The first number should be incremented when a change is made to the | ||
| 401 | # handler that will break people's input files. | ||
| 402 | # - The second number should be incremented when a change is made that won't | ||
| 403 | # break people's input files, but changes the functionality. | ||
| 404 | # - The third number should be incremented when only a bugfix is applied. | ||
| 405 | EOF | ||
| 406 | |||
| 407 | $code =~ s|\n\$VERSION\s*=\s+((?!do).*?);| | ||
| 408 | $commentBlock | ||
| 409 | \$VERSION = do {my \@r=('$1.0'=~/\\d+/g);sprintf "\%d."."\%02d"x\$#r,\@r};|sx; | ||
| 410 | |||
| 411 | return $code; | ||
| 412 | } | ||
| 413 | |||
| 414 | #------------------------------------------------------------------------------- | ||
| 415 | |||
| 416 | sub StripUses($) | ||
| 417 | { | ||
| 418 | my $code = shift; | ||
| 419 | |||
| 420 | print "- Removing Extra \"use\" Statements\n"; | ||
| 421 | |||
| 422 | $code =~ s/use NewsClipper::Types.*?;\n\n?//s; | ||
| 423 | $code =~ s/use NewsClipper::HTMLTools.*?;\n\n?//s; | ||
| 424 | $code =~ s/use NewsClipper::AcquisitionFunctions.*?;\n\n?//s; | ||
| 425 | $code =~ s/use constant DEBUG.*?;\n\n?//s; | ||
| 426 | $code =~ s/# DEBUG for this package is the same as the main.\n\n?//s; | ||
| 427 | |||
| 428 | return $code; | ||
| 429 | } | ||
| 430 | |||
| 431 | #------------------------------------------------------------------------------- | ||
| 432 | |||
| 433 | sub StripDprint($) | ||
| 434 | { | ||
| 435 | my $code = shift; | ||
| 436 | |||
| 437 | print "- Removing dprint declaration\n"; | ||
| 438 | |||
| 439 | $code =~ s/\n*sub dprint;\n*/\n/s; | ||
| 440 | $code =~ s/\n*\*dprint.*?;\n*/\n/s; | ||
| 441 | |||
| 442 | return $code; | ||
| 443 | } | ||
| 444 | |||
| 445 | #------------------------------------------------------------------------------- | ||
| 446 | |||
| 447 | sub ConvertDefaultHandlers($) | ||
| 448 | { | ||
| 449 | my $code = shift; | ||
| 450 | |||
| 451 | print "- Converting Default Handlers\n"; | ||
| 452 | |||
| 453 | if ($code =~ /(sub GetDefaultHandlers\s*{.*?\n}\n)/s) | ||
| 454 | { | ||
| 455 | my $subCode = $1; | ||
| 456 | my $storedSubCode = $subCode; | ||
| 457 | |||
| 458 | $subCode =~ s/\n( *)my \@returnVal\s*=\s*\(/\n$1my \@returnVal =<<'$1EOF';/sg; | ||
| 459 | $subCode =~ s/(my \@returnVal[^\n]+EOF';.*?)\);/$1EOF/sg; | ||
| 460 | $subCode =~ s/my \@returnVal/my \$returnVal/sg; | ||
| 461 | |||
| 462 | $subCode =~ s/\n( *)\@returnVal\s*=\s*\(/\n$1\@returnVal =<<'$1EOF';/sg; | ||
| 463 | $subCode =~ s/(\@returnVal[^\n]+EOF';.*?)\);/$1EOF/sg; | ||
| 464 | $subCode =~ s/\@returnVal/\$returnVal/sg; | ||
| 465 | |||
| 466 | while ($subCode =~ /\n((\s*{'name'.*?},?\n)+)/s) | ||
| 467 | { | ||
| 468 | my $match = $1; | ||
| 469 | my $storedMatch = $match; | ||
| 470 | my ($prefix) = $match =~ /^(\s*)/; | ||
| 471 | |||
| 472 | $match =~ s/,\s*'(\w+)'\s*=>\s*/ $1=/sg; | ||
| 473 | $match =~ s/'(\w+)'\s*=>\s*/$1=/sg; | ||
| 474 | $match =~ s/{\s*name/name/sg; | ||
| 475 | $match =~ s/},? *\n/\n/sg; | ||
| 476 | # $match =~ s/, *'/ /sg; | ||
| 477 | $match =~ s/(^|\n)(\s*)(name)/$1$2<filter $3/sg; | ||
| 478 | $match =~ s/(\S)(\n\s*<filter|$)/$1>$2/sg; | ||
| 479 | $match =~ s/^(.*)<filter /$1<output /s; | ||
| 480 | |||
| 481 | $subCode =~ s/\Q$storedMatch\E/$match/s; | ||
| 482 | } | ||
| 483 | |||
| 484 | $code =~ s/\Q$storedSubCode\E/$subCode/s; | ||
| 485 | } | ||
| 486 | else | ||
| 487 | { | ||
| 488 | print <<" EOF" | ||
| 489 | GetDefaultHandlers subroutine could not be found. Skipping default handler | ||
| 490 | conversion... | ||
| 491 | EOF | ||
| 492 | and return $code; | ||
| 493 | } | ||
| 494 | |||
| 495 | return $code; | ||
| 496 | } | ||
| 497 | |||
| 498 | #------------------------------------------------------------------------------- | ||
| 499 | |||
| 500 | sub ConvertOldPatternMatches($) | ||
| 501 | { | ||
| 502 | my $code = shift; | ||
| 503 | |||
| 504 | print "- Converting old pattern matches to use references.\n"; | ||
| 505 | |||
| 506 | $code =~ s/(grep *{\s*)!\//$1\$\$_ !~ \//gs; | ||
| 507 | $code =~ s/(grep *{\s*)\//$1\$\$_ =~ \//gs; | ||
| 508 | |||
| 509 | return $code; | ||
| 510 | } | ||
| 511 | |||
| 512 | #------------------------------------------------------------------------------- | ||
| 513 | |||
| 514 | sub CheckGetFunction($) | ||
| 515 | { | ||
| 516 | my $code = shift; | ||
| 517 | |||
| 518 | print "- Checking Get Function\n"; | ||
| 519 | |||
| 520 | if ($code =~ /MakeSubtype/s) | ||
| 521 | { | ||
| 522 | print <<' EOF'; | ||
| 523 | This handler's Get function calls "MakeSubType", which means that you need to | ||
| 524 | edit the Get function to reflect the new type system. Instead of doing | ||
| 525 | something like this: | ||
| 526 | |||
| 527 | while ($line =~ /(.*)::(.*)/g) | ||
| 528 | { | ||
| 529 | push @data, {'name' => $1,'url' => $2}; | ||
| 530 | } | ||
| 531 | MakeSubType('ArrayOfSlashdotHash','ArrayOfHash'); | ||
| 532 | bless \@data,'ArrayOfSlashdotHash'; | ||
| 533 | |||
| 534 | you now bless the new data type, and let News Clipper worry about the | ||
| 535 | structure of the rest of the data: | ||
| 536 | |||
| 537 | MakeSubType('Slashdot','HASH'); | ||
| 538 | |||
| 539 | while ($line =~ /(.*)::(.*)/g) | ||
| 540 | { | ||
| 541 | my %hash = ('name' => $1,'url' => $2); | ||
| 542 | bless \%hash,'Slashdot'; | ||
| 543 | push @data, \%hash; | ||
| 544 | } | ||
| 545 | |||
| 546 | EOF | ||
| 547 | } | ||
| 548 | } | ||
| 549 | |||
| 550 | #------------------------------------------------------------------------------- | ||
| 551 | |||
| 552 | sub CheckErrorMessage($) | ||
| 553 | { | ||
| 554 | my $code = shift; | ||
| 555 | |||
| 556 | print "- Checking use of error messages\n"; | ||
| 557 | |||
| 558 | if ($code =~ /\bprint\b/s) | ||
| 559 | { | ||
| 560 | print <<' EOF'; | ||
| 561 | This handler calls "print", which means that you need to edit the handler to | ||
| 562 | use the new error (for errors) or dprint (for debugging) functions instead. | ||
| 563 | You don't have to check the input types, since News Clipper will check them | ||
| 564 | for you based on the FilterType and OutputType specifications. | ||
| 565 | |||
| 566 | If this is an output handler, be sure to edit your code to deal with | ||
| 567 | references to scalars instead of scalars. i.e.: | ||
| 568 | |||
| 569 | print $grabbedData->[0]; | ||
| 570 | |||
| 571 | becomes | ||
| 572 | |||
| 573 | print ${$grabbedData->[0]}; | ||
| 574 | |||
| 575 | since everything is a reference now. | ||
| 576 | EOF | ||
| 577 | } | ||
| 578 | } | ||
| 579 | |||
| 580 | #------------------------------------------------------------------------------- | ||
| 581 | |||
| 582 | sub CheckRef($) | ||
| 583 | { | ||
| 584 | my $code = shift; | ||
| 585 | |||
| 586 | print "- Checking use of ref\n"; | ||
| 587 | |||
| 588 | if ($code =~ /[^\$\%\@]\bref\b/s) | ||
| 589 | { | ||
| 590 | print <<' EOF'; | ||
| 591 | This handler calls "ref", which means that you might need to edit the handler. | ||
| 592 | You can be guaranteed that every item passed into any handler function, and | ||
| 593 | every item inside data structures is a reference. Conversely, you need to | ||
| 594 | dereference every reference to a scalar, which you didn't have to do before. | ||
| 595 | EOF | ||
| 596 | } | ||
| 597 | } | ||
| 598 | |||
| 599 | #------------------------------------------------------------------------------- | ||
| 600 | |||
| 601 | sub CheckIsa($) | ||
| 602 | { | ||
| 603 | my $code = shift; | ||
| 604 | |||
| 605 | print "- Checking use of isa\n"; | ||
| 606 | |||
| 607 | if ($code =~ /\bisa\b/s) | ||
| 608 | { | ||
| 609 | print <<' EOF'; | ||
| 610 | This handler calls "isa", which means that you need to edit the handler to | ||
| 611 | use the new TypesMatch function instead. For example, instead of doing this: | ||
| 612 | |||
| 613 | if (ref $grabbedData && $grabbedData->isa('HASH')) | ||
| 614 | |||
| 615 | do this: | ||
| 616 | |||
| 617 | if (TypesMatch($grabbedData,'%')) | ||
| 618 | EOF | ||
| 619 | } | ||
| 620 | } | ||
| 621 | |||
| 622 | #------------------------------------------------------------------------------- | ||
| 623 | |||
| 624 | sub ConvertFilterTypeFunction($) | ||
| 625 | { | ||
| 626 | my $code = shift; | ||
| 627 | |||
| 628 | print "- Converting FilterType Function\n"; | ||
| 629 | |||
| 630 | if ($code =~ /(sub FilterType\s*{\n(.*?return ["'].*?["'];\n)}\n)/s) | ||
| 631 | { | ||
| 632 | my $subCode = $1; | ||
| 633 | my $storedSubCode = $subCode; | ||
| 634 | my $body = $2; | ||
| 635 | my $storedBody = $2; | ||
| 636 | |||
| 637 | $body =~ s/"/'/sg; | ||
| 638 | $body =~ s/of([A-Z][a-z]*)/\($1\)/sgi; | ||
| 639 | $body =~ s/Array/\@/sg; | ||
| 640 | $body =~ s/String/\$/sg; | ||
| 641 | $body =~ s/Image/\$Image/sg; | ||
| 642 | $body =~ s/Link/\$Link/sg; | ||
| 643 | $body =~ s/Table/\@(\@(\$))/sg; | ||
| 644 | $body =~ s/Thread/\@(\@(\$\|\@))/sg; | ||
| 645 | $body =~ s/Hash/\%/sg; | ||
| 646 | $body =~ s/([a-z]+)([\@\$\%])/$2$1/sgi; | ||
| 647 | $body =~ s/,/ \| /sg; | ||
| 648 | |||
| 649 | my $preamble =<<' EOF'; | ||
| 650 | my $self = shift; | ||
| 651 | my $attributes = shift; | ||
| 652 | my $grabbedData = shift; | ||
| 653 | |||
| 654 | EOF | ||
| 655 | $subCode =~ s/\Q$storedBody\E/$preamble$body/s; | ||
| 656 | |||
| 657 | $code =~ s/\Q$storedSubCode\E/$subCode/s; | ||
| 658 | } | ||
| 659 | |||
| 660 | return $code; | ||
| 661 | } | ||
| 662 | |||
| 663 | #------------------------------------------------------------------------------- | ||
| 664 | |||
| 665 | sub ConvertOutputTypeFunction($) | ||
| 666 | { | ||
| 667 | my $code = shift; | ||
| 668 | |||
| 669 | print "- Converting OutputType Function\n"; | ||
| 670 | |||
| 671 | if ($code =~ /(sub OutputType\s*{\n(.*?return ["'].*?["'];\n)}\n)/s) | ||
| 672 | { | ||
| 673 | my $subCode = $1; | ||
| 674 | my $storedSubCode = $subCode; | ||
| 675 | my $body = $2; | ||
| 676 | my $storedBody = $2; | ||
| 677 | |||
| 678 | $body =~ s/"/'/sg; | ||
| 679 | $body =~ s/^(.*)[Oo]f([A-Z][^'"]*)/$1\($2\)/sg | ||
| 680 | while $body =~ /[Oo]f[A-Z]/; | ||
| 681 | $body =~ s/Array/\@/sg; | ||
| 682 | $body =~ s/String/\$/sg; | ||
| 683 | $body =~ s/Image/\$Image/sg; | ||
| 684 | $body =~ s/Link/\$Link/sg; | ||
| 685 | $body =~ s/Table/\@(\@(\$))/sg; | ||
| 686 | $body =~ s/Thread/\@(\@(\$\ & \@))/sg; | ||
| 687 | $body =~ s/Hash/\%/sg; | ||
| 688 | $body =~ s/([a-z]+)([\@\$\%])/$2$1/sgi; | ||
| 689 | $body =~ s/,/ \| /sg; | ||
| 690 | |||
| 691 | my $preamble =<<' EOF'; | ||
| 692 | my $self = shift; | ||
| 693 | my $attributes = shift; | ||
| 694 | my $grabbedData = shift; | ||
| 695 | |||
| 696 | EOF | ||
| 697 | |||
| 698 | $subCode =~ s/\Q$storedBody\E/$preamble$body/s; | ||
| 699 | |||
| 700 | $code =~ s/\Q$storedSubCode\E/$subCode/s; | ||
| 701 | } | ||
| 702 | |||
| 703 | return $code; | ||
| 704 | } | ||
| 705 | |||
| 706 | #------------------------------------------------------------------------------- | ||
| 707 | |||
| 708 | sub ConvertTypeNames() | ||
| 709 | { | ||
| 710 | my $code = shift; | ||
| 711 | |||
| 712 | print "- Converting Array to ARRAY, Hash to HASH, and String to SCALAR.\n"; | ||
| 713 | |||
| 714 | $code =~ s/\bString\b/SCALAR/sg; | ||
| 715 | $code =~ s/\bHash\b/HASH/sg; | ||
| 716 | $code =~ s/\bArray\b/ARRAY/sg; | ||
| 717 | |||
| 718 | return $code; | ||
| 719 | } | ||
| 720 | |||
| 721 | #------------------------------------------------------------------------------- | ||
| 722 | |||
| 723 | |||
| 724 | sub CheckLeftOvers($) | ||
| 725 | { | ||
| 726 | my $code = shift; | ||
| 727 | |||
| 728 | print "- Doing final check.\n"; | ||
| 729 | |||
| 730 | print<<" EOF" | ||
| 731 | Your handler uses "bless". You can remove it if you're blessing something as a | ||
| 732 | String, Hash, Array, ArrayOfString, or HashOfString. Also, instead of blessing | ||
| 733 | the returned data as something like "ArrayOfSlashdotHash", bless each hash | ||
| 734 | reference as "Slashdot" as you put them in the array, and delare Slashdot to | ||
| 735 | be a subtype of HASH by calling MakeSubtype('Slashdot','HASH') | ||
| 736 | EOF | ||
| 737 | if $code =~ /\bbless\b/; | ||
| 738 | |||
| 739 | if ($code =~ /\b((Array|Hash)Of\w+)\b/) | ||
| 740 | { | ||
| 741 | print<<" EOF"; | ||
| 742 | |||
| 743 | Your handler uses "$1" | ||
| 744 | You need to fix this for the new type system. If your data structure is | ||
| 745 | something like "HashOfSlashdot", you can just change this to "Slashdot". If | ||
| 746 | it's a complex structure like "ArrayOfHashOfSlashdot", just bless the hash | ||
| 747 | references as "Slashdot" as you insert them into the array. | ||
| 748 | EOF | ||
| 749 | } | ||
| 750 | |||
| 751 | print<<' EOF' | ||
| 752 | Your handler appears to use HandlerFactory to call another handler. While this | ||
| 753 | will work, you should replace it with the new RunHandler technique in order | ||
| 754 | to take advantage of type checking and other features. For example, replace: | ||
| 755 | |||
| 756 | # Put together some attributes to use when calling the cacheimages handler | ||
| 757 | my $newAttributes = { 'maxage' => 15 * 60 * 60 }; | ||
| 758 | |||
| 759 | my $handlerFactory = new NewsClipper::HandlerFactory; | ||
| 760 | |||
| 761 | # Ask the HandlerFactory to create a handler for us, based on the name. | ||
| 762 | my $handler = $handlerFactory->Create('cacheimages'); | ||
| 763 | |||
| 764 | my $cachedimage = $handler->Filter($newAttributes,$links[0]); | ||
| 765 | |||
| 766 | with: | ||
| 767 | |||
| 768 | # Put together some attributes to use when calling the cacheimages handler | ||
| 769 | my $newAttributes = { 'maxage' => 15 * 60 * 60 }; | ||
| 770 | |||
| 771 | my $cachedimage = RunHandler('cacheimages','filter',$links[0],$newAttributes); | ||
| 772 | EOF | ||
| 773 | if $code =~ /handlerFactory->Create/; | ||
| 774 | |||
| 775 | return $code; | ||
| 776 | } | ||
| 777 | |||
| 778 | #------------------------------------------------------------------------------- | ||
| 779 | |||
| 780 | sub WriteHandler($$) | ||
| 781 | { | ||
| 782 | my $handler = shift; | ||
| 783 | my $code = shift; | ||
| 784 | |||
| 785 | my $backup = $handler; | ||
| 786 | $backup =~ s/.pm$/.bak/i; | ||
| 787 | |||
| 788 | print "\"$backup\" already exists. Handler NOT saved.\n" and return | ||
| 789 | if -e $backup; | ||
| 790 | rename $handler,$backup or | ||
| 791 | warn "Couldn't not rename $handler\n to $backup. Skipping...\n" | ||
| 792 | and return; | ||
| 793 | open NEW, ">$handler" or die "Can't open $handler for writing: $!\n"; | ||
| 794 | print NEW $code; | ||
| 795 | close NEW; | ||
| 796 | |||
| 797 | print<<" EOF"; | ||
| 798 | - Handler $handler | ||
| 799 | has been converted and saved. Check it to make sure everything look reasonable. | ||
| 800 | (The old handler was renamed to $backup.) | ||
| 801 | EOF | ||
| 802 | } | ||
| 803 | |||
| 804 | #------------------------------------------------------------------------------- | ||
| 805 | |||
| 806 | sub AddComputeURL($) | ||
| 807 | { | ||
| 808 | my $code = shift; | ||
| 809 | |||
| 810 | return $code unless $code =~ /Handler::Acquisition/; | ||
| 811 | |||
| 812 | print "- Adding ComputeURL Function\n"; | ||
| 813 | |||
| 814 | if ($code =~ /(# *-{70,}[^-]+(?:# This fun[^\n]+\n)?sub Get\s*{.*?\n}\n)/s) | ||
| 815 | { | ||
| 816 | my $subCode = $1; | ||
| 817 | my $storedSubCode = $subCode; | ||
| 818 | |||
| 819 | my $addedCode =<<' EOF'; | ||
| 820 | # ------------------------------------------------------------------------------ | ||
| 821 | |||
| 822 | sub ComputeURL | ||
| 823 | { | ||
| 824 | my $self = shift; | ||
| 825 | my $attributes = shift; | ||
| 826 | |||
| 827 | my $url = 'INSERT URL HERE'; | ||
| 828 | |||
| 829 | return $url; | ||
| 830 | } | ||
| 831 | |||
| 832 | EOF | ||
| 833 | |||
| 834 | if ($subCode =~ /my \$url = (['"][^'"]+['"])\s*;/s) | ||
| 835 | { | ||
| 836 | my $url = $1; | ||
| 837 | $subCode =~ s/my\s+\$url\s*=\s*.*?;/my \$url = \$self->ComputeURL(\$attributes);/s; | ||
| 838 | $addedCode =~ s/'INSERT URL HERE'/$url/s; | ||
| 839 | } | ||
| 840 | elsif ($subCode =~ /( *)my \$data = [^\(]+Get[^\(]+\((['"][^'"]+['"])/s) | ||
| 841 | { | ||
| 842 | my $whitespace = $1; | ||
| 843 | my $url = $2; | ||
| 844 | my $new_line = "my \$url = \$self->ComputeURL(\$attributes);\n\n$whitespace"; | ||
| 845 | $subCode =~ s/(my \$data = [^\(]+Get[^\(]+\()['"][^'"]+['"]/$new_line$1\$url/s; | ||
| 846 | $addedCode =~ s/'INSERT URL HERE'/$url/s; | ||
| 847 | } | ||
| 848 | else | ||
| 849 | { | ||
| 850 | print <<' EOF'; | ||
| 851 | A Get subroutine was found, but your URL code was too complicated to convert | ||
| 852 | automatically. Move any URL calculation code to ComputeURL, and have your Get | ||
| 853 | subroutine call it like this: | ||
| 854 | |||
| 855 | my $url = $self->ComputeURL($attributes); | ||
| 856 | EOF | ||
| 857 | } | ||
| 858 | |||
| 859 | $code =~ s/\Q$storedSubCode\E/$addedCode$subCode/s; | ||
| 860 | } | ||
| 861 | else | ||
| 862 | { | ||
| 863 | print <<" EOF" | ||
| 864 | Get subroutine could not be found. Skipping addition of ComputeURL | ||
| 865 | subroutine... | ||
| 866 | EOF | ||
| 867 | and return $code; | ||
| 868 | } | ||
| 869 | |||
| 870 | return $code; | ||
| 871 | } | ||
| 872 | |||
| 873 | #------------------------------------------------------------------------------- | ||
| 874 | |||
| 875 | sub AddSetDefaultAttributes($) | ||
| 876 | { | ||
| 877 | my $code = shift; | ||
| 878 | |||
| 879 | return $code unless $code =~ /Handler::Acquisition/; | ||
| 880 | |||
| 881 | print "- Adding SetDefaultAttributes Function\n"; | ||
| 882 | |||
| 883 | if ($code =~ /(# *-{70,}[^-]+(?:# This fun[^\n]+\n)?sub Get\s*{.*?\n}\n)/s) | ||
| 884 | { | ||
| 885 | my $subCode = $1; | ||
| 886 | my $storedSubCode = $subCode; | ||
| 887 | |||
| 888 | my $addedCode =<<' EOF'; | ||
| 889 | # ------------------------------------------------------------------------------ | ||
| 890 | |||
| 891 | # This subroutine checks the handler's attributes to make sure they are valid, | ||
| 892 | # and sets any default attributes if necessary. | ||
| 893 | |||
| 894 | sub ProcessAttributes | ||
| 895 | { | ||
| 896 | my $self = shift; | ||
| 897 | my $attributes = shift; | ||
| 898 | my $handlerRole = shift; | ||
| 899 | |||
| 900 | # Set defaults here. You can safely delete this function if your handler has | ||
| 901 | # no attributes with default values. | ||
| 902 | |||
| 903 | # $attributes->{'some_attribute'} = 'default_value' | ||
| 904 | # unless defined $attributes->{'some_attribute'}; | ||
| 905 | |||
| 906 | # Verify any attributes you need to here. Output an error and return undef | ||
| 907 | # if something is wrong. | ||
| 908 | |||
| 909 | # unless ($attributes->{somevalue} > 0) | ||
| 910 | # { | ||
| 911 | # error "The \"somevalue\" attribute for handler \"HANDLERNAME\" " . | ||
| 912 | # "should be greater than 0.\n"; | ||
| 913 | # return undef; | ||
| 914 | # } | ||
| 915 | |||
| 916 | return $attributes; | ||
| 917 | } | ||
| 918 | |||
| 919 | EOF | ||
| 920 | |||
| 921 | while ($subCode =~ | ||
| 922 | /\n( *\$attributes->{[^}]+}\s=\s[^;]+\sif !defined[^;]+\$attributes[^;]+;\n)/s) | ||
| 923 | { | ||
| 924 | my $setdefault = $1; | ||
| 925 | $subCode =~ s/\s+\Q$setdefault\E/\n/s; | ||
| 926 | $addedCode =~ s/\n(\s+return \$attributes)/\n$setdefault$1/s; | ||
| 927 | } | ||
| 928 | |||
| 929 | while ($subCode =~ | ||
| 930 | /\n( *\$attributes->{[^}]+}\s=\s[^;]+\sunless\sdefined[^;]+\$attributes[^;]+;\n)/s) | ||
| 931 | { | ||
| 932 | my $setdefault = $1; | ||
| 933 | $subCode =~ s/\s+\Q$setdefault\E/\n/s; | ||
| 934 | $addedCode =~ s/\n(\s+return \$attributes)/\n$setdefault$1/s; | ||
| 935 | } | ||
| 936 | |||
| 937 | $code =~ s/\Q$storedSubCode\E/$addedCode$subCode/s; | ||
| 938 | } | ||
| 939 | else | ||
| 940 | { | ||
| 941 | print <<" EOF" | ||
| 942 | Get subroutine could not be found. Skipping addition of SetDefaultAttributes | ||
| 943 | subroutine... | ||
| 944 | EOF | ||
| 945 | and return $code; | ||
| 946 | } | ||
| 947 | |||
| 948 | return $code; | ||
| 949 | } | ||
| 950 | |||
| 951 | #------------------------------------------------------------------------------- | ||
| 952 | |||
| 953 | sub dequote | ||
| 954 | { | ||
| 955 | my $prefix; | ||
| 956 | $prefix = shift if $#_ == 1; | ||
| 957 | |||
| 958 | local $_ = shift; | ||
| 959 | |||
| 960 | my ($white, $leader); | ||
| 961 | |||
| 962 | if (/^\s*(?:([^\w\s]+)(\s*).*\n)(?:\s*\1\2?.*\n)+$/) | ||
| 963 | { | ||
| 964 | ($white, $leader) = ($2, quotemeta($1)); | ||
| 965 | } | ||
| 966 | else | ||
| 967 | { | ||
| 968 | ($white, $leader) = (/^(\s+)/,''); | ||
| 969 | } | ||
| 970 | |||
| 971 | s/^\n/$white\n/gm; | ||
| 972 | s/^\s*?$leader(?:$white)?//gm; | ||
| 973 | |||
| 974 | # Put the prefix on if one was specified | ||
| 975 | $_ =~ s/^/$prefix/gm if $prefix; | ||
| 976 | |||
| 977 | return $_; | ||
| 978 | } |