| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use IO::File; | ||
| 7 | use Getopt::Std; | ||
| 8 | |||
| 9 | use App::Followme::Web; | ||
| 10 | |||
| 11 | # False outside of body tags, true inside | ||
| 12 | our $in_body; | ||
| 13 | |||
| 14 | use constant PATTERNS => <<EOQ; | ||
| 15 | # Rework quote paragraphs | ||
| 16 | (<p><font><b>*</b></font></p>) -> <p>(<b>*</b><br/>)</p> | ||
| 17 | # Remove cruft from comment paragraphs | ||
| 18 | <p><font>*</font></p> -> <p>*</p> | ||
| 19 | # Remove paragraphs containing breaks | ||
| 20 | <p><br/>*</p> -> | ||
| 21 | # Title | ||
| 22 | <p><font><font size="6"><b>*</b></font></font></p> -> <h1>*</h1> | ||
| 23 | # Subtitle | ||
| 24 | <h1>*</h1> -> <h2>*</h2> | ||
| 25 | # Remove font tags | ||
| 26 | <font>*</font> -> * | ||
| 27 | # Remove other span tags | ||
| 28 | <span>*</span> -> * | ||
| 29 | # Remove document styling | ||
| 30 | <style>*</style> -> | ||
| 31 | # Remove metadata tags | ||
| 32 | <meta> -> | ||
| 33 | EOQ | ||
| 34 | |||
| 35 | use constant TEMPLATE => <<EOQ; | ||
| 36 | <html> | ||
| 37 | <head> | ||
| 38 | <!-- section meta --> | ||
| 39 | <title></title> | ||
| 40 | <!-- endsection meta --> | ||
| 41 | </head> | ||
| 42 | <body> | ||
| 43 | <!-- section primary --> | ||
| 44 | <!-- endsection primary--> | ||
| 45 | <!-- section secondary --> | ||
| 46 | <!-- endsection secondary--> | ||
| 47 | </body> | ||
| 48 | </html> | ||
| 49 | EOQ | ||
| 50 | |||
| 51 | use constant METADATA => <<EOQ; | ||
| 52 | <title></title> | ||
| 53 | <meta name="description" content=""> | ||
| 54 | <meta name="keywords" content=""> | ||
| 55 | <meta name="author" content=""> | ||
| 56 | EOQ | ||
| 57 | |||
| 58 | #---------------------------------------------------------------------- | ||
| 59 | # Main | ||
| 60 | |||
| 61 | my %opt; | ||
| 62 | getopts('r', \%opt); | ||
| 63 | |||
| 64 | if ($opt{r}) { | ||
| 65 | revert_files(@ARGV); | ||
| 66 | } else { | ||
| 67 | my $patterns = read_patterns(PATTERNS); | ||
| 68 | clean_files($patterns, @ARGV); | ||
| 69 | } | ||
| 70 | |||
| 71 | #---------------------------------------------------------------------- | ||
| 72 | # Add sections to cleaned file | ||
| 73 | |||
| 74 | sub add_sections { | ||
| 75 | my ($text, $template) = @_; | ||
| 76 | |||
| 77 | my @hold; | ||
| 78 | my @output; | ||
| 79 | my @input = web_split_at_tags($text); | ||
| 80 | |||
| 81 | foreach my $tag ($template =~ /<[^>]*>/g) { | ||
| 82 | if ($tag =~ /^<!--\s*end/) { | ||
| 83 | push(@hold, $tag); | ||
| 84 | |||
| 85 | } elsif ($tag =~ /^<!--/) { | ||
| 86 | if (@hold) { | ||
| 87 | push(@hold, $tag); | ||
| 88 | } else { | ||
| 89 | push(@output, shift @input); | ||
| 90 | push(@output, "$tag\n"); | ||
| 91 | } | ||
| 92 | |||
| 93 | } else { | ||
| 94 | my $extra; | ||
| 95 | if (@hold) { | ||
| 96 | $extra = join("\n", @hold) . "\n"; | ||
| 97 | @hold = (); | ||
| 98 | } | ||
| 99 | |||
| 100 | my $output = search_for_tag(\@input, $tag, $extra); | ||
| 101 | push(@output, $output); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | die "No matching tag for\n" . join("\n", @hold) . "\n" if @hold; | ||
| 106 | |||
| 107 | push(@output, @input); | ||
| 108 | return join('', @output) | ||
| 109 | } | ||
| 110 | |||
| 111 | #---------------------------------------------------------------------- | ||
| 112 | # Create a backup copy of the original file | ||
| 113 | |||
| 114 | sub backup_file { | ||
| 115 | my ($file, $text) = @_; | ||
| 116 | $file .= '~'; | ||
| 117 | |||
| 118 | write_file($file, $text); | ||
| 119 | } | ||
| 120 | |||
| 121 | #---------------------------------------------------------------------- | ||
| 122 | # Clean each file by substuting tokens with their patterns | ||
| 123 | |||
| 124 | sub clean_files { | ||
| 125 | my ($patterns, @files) = @_; | ||
| 126 | |||
| 127 | |||
| 128 | foreach my $file (@files) { | ||
| 129 | my $text = slurp_file($file); | ||
| 130 | backup_file($file, $text); | ||
| 131 | |||
| 132 | my @tags = web_split_at_tags($text); | ||
| 133 | my $tokens = {next => 0, data => \@tags}; | ||
| 134 | $text = replace_tokens($patterns, $tokens); | ||
| 135 | |||
| 136 | $text = add_sections($text, TEMPLATE); | ||
| 137 | $text = web_substitute_sections($text, {meta => METADATA}); | ||
| 138 | write_file($file, $text); | ||
| 139 | } | ||
| 140 | |||
| 141 | return; | ||
| 142 | } | ||
| 143 | |||
| 144 | #---------------------------------------------------------------------- | ||
| 145 | # Remove parameters from a token | ||
| 146 | |||
| 147 | sub clean_token { | ||
| 148 | my ($token) = @_; | ||
| 149 | |||
| 150 | if (web_is_tag($token)) { | ||
| 151 | $in_body = 1 if $in_body == 0 and $token =~ /^<body/; | ||
| 152 | $in_body = 0 if $in_body == 1 and $token =~ /^<\/body/; | ||
| 153 | $token =~ s/^<\s*([\/\w]+)[^>]*>/<$1>/ if $in_body; | ||
| 154 | } | ||
| 155 | |||
| 156 | return $token; | ||
| 157 | } | ||
| 158 | |||
| 159 | #---------------------------------------------------------------------- | ||
| 160 | # Fill text to fit 65+ characters per line | ||
| 161 | |||
| 162 | sub fill_text { | ||
| 163 | my ($text) = @_; | ||
| 164 | |||
| 165 | # Convert code to space | ||
| 166 | $text =~ s/ / /g; | ||
| 167 | # Convert all white space to a single space | ||
| 168 | $text =~ s/\s+/ /g; | ||
| 169 | # Convert first white space after 65th character to a newline | ||
| 170 | $text =~ s/(.{65}\S*)\s/$1\n/g; | ||
| 171 | |||
| 172 | return $text; | ||
| 173 | } | ||
| 174 | |||
| 175 | #---------------------------------------------------------------------- | ||
| 176 | # Find the location ($i) of the $iast-th asterisk in a pattern | ||
| 177 | |||
| 178 | sub find_asterisk { | ||
| 179 | my ($pattern, $iast) = @_; | ||
| 180 | |||
| 181 | for (my $i = 0; $i < @$pattern; $i++) { | ||
| 182 | $iast -- if $pattern->[$i] eq '*'; | ||
| 183 | return $i if $iast == 0; | ||
| 184 | } | ||
| 185 | |||
| 186 | die "No asterisk found for replace_match"; | ||
| 187 | } | ||
| 188 | |||
| 189 | #---------------------------------------------------------------------- | ||
| 190 | # Find the $j-th group in the array | ||
| 191 | |||
| 192 | sub find_group { | ||
| 193 | my ($array, $igroup) = @_; | ||
| 194 | |||
| 195 | for (my $i = 0; $i <= @$array; $i++) { | ||
| 196 | $igroup -- if ref($array->[$i]) eq 'ARRAY'; | ||
| 197 | return $i if $igroup == 0; | ||
| 198 | } | ||
| 199 | |||
| 200 | die "Group not found in array in replace_match"; | ||
| 201 | } | ||
| 202 | |||
| 203 | #---------------------------------------------------------------------- | ||
| 204 | # Find the match to a pattern, or fail | ||
| 205 | |||
| 206 | sub find_match { | ||
| 207 | my ($pattern, $patterns, $tokens, $repeat) = @_; | ||
| 208 | |||
| 209 | my @matches = (); | ||
| 210 | |||
| 211 | do { | ||
| 212 | my $next = 0; | ||
| 213 | my @submatch = (); | ||
| 214 | my $first = $tokens->{next}; | ||
| 215 | |||
| 216 | while ($next < @$pattern && | ||
| 217 | $tokens->{next} < @{$tokens->{data}}) { | ||
| 218 | |||
| 219 | my $token = $tokens->{data}[$tokens->{next}]; | ||
| 220 | if ($token !~ /\S/) { | ||
| 221 | $tokens->{next} ++; | ||
| 222 | next; | ||
| 223 | } | ||
| 224 | |||
| 225 | $token = web_parse_tag($token) if web_is_tag($token); | ||
| 226 | |||
| 227 | if ($pattern->[$next] eq '*') { | ||
| 228 | die "Invalid pattern" if $next+1 == @$pattern; | ||
| 229 | my $text = search_for_end($patterns, $tokens, | ||
| 230 | $pattern->[$next+1]); | ||
| 231 | last unless defined $text; | ||
| 232 | |||
| 233 | push(@submatch, $text); | ||
| 234 | |||
| 235 | } elsif (ref $pattern->[$next] eq 'ARRAY') { | ||
| 236 | my $match = find_match($pattern->[$next], $patterns, $tokens, 1); | ||
| 237 | last unless @$match; | ||
| 238 | |||
| 239 | push(@submatch, $match); | ||
| 240 | $tokens->{next} ++; | ||
| 241 | |||
| 242 | } elsif (ref $pattern->[$next] eq 'HASH') { | ||
| 243 | last unless ref $token; | ||
| 244 | last unless web_same_tag($pattern->[$next], $token); | ||
| 245 | push(@submatch, $token); | ||
| 246 | $tokens->{next} ++; | ||
| 247 | |||
| 248 | } else { | ||
| 249 | last if ref $token; | ||
| 250 | last unless $token =~ /$pattern->[$next]/; | ||
| 251 | push(@submatch, $token); | ||
| 252 | $tokens->{next} ++; | ||
| 253 | } | ||
| 254 | |||
| 255 | $next ++; | ||
| 256 | } | ||
| 257 | |||
| 258 | if ($next < @$pattern) { | ||
| 259 | $tokens->{next} = $first; | ||
| 260 | return \@matches; | ||
| 261 | |||
| 262 | } else { | ||
| 263 | push(@matches, \@submatch); | ||
| 264 | } | ||
| 265 | |||
| 266 | } while ($repeat); | ||
| 267 | |||
| 268 | return \@matches; | ||
| 269 | } | ||
| 270 | |||
| 271 | #---------------------------------------------------------------------- | ||
| 272 | # Get the replacement for the next matched tokens | ||
| 273 | |||
| 274 | sub get_match { | ||
| 275 | my ($patterns, $tokens) = @_; | ||
| 276 | |||
| 277 | my $i = 0; | ||
| 278 | my $first = $tokens->{next}; | ||
| 279 | |||
| 280 | while ($i < @$patterns) { | ||
| 281 | $tokens->{next} = $first; | ||
| 282 | |||
| 283 | my $matches = find_match($patterns->[$i], $patterns, $tokens); | ||
| 284 | if (@$matches) { | ||
| 285 | return replace_match($patterns->[$i], $patterns->[$i+1], $matches); | ||
| 286 | } else { | ||
| 287 | $i += 2; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | $tokens->{next} = $first; | ||
| 292 | return; | ||
| 293 | } | ||
| 294 | |||
| 295 | #---------------------------------------------------------------------- | ||
| 296 | # Parse pattern to match text | ||
| 297 | |||
| 298 | sub parse_pattern { | ||
| 299 | my ($pattern_parts, $stopper) = @_; | ||
| 300 | |||
| 301 | my $part; | ||
| 302 | my @pattern; | ||
| 303 | while (defined($part = shift @$pattern_parts)) { | ||
| 304 | if ($part eq '(') { | ||
| 305 | push(@pattern, parse_pattern($pattern_parts, ')')); | ||
| 306 | |||
| 307 | } elsif ($stopper && $part eq $stopper) { | ||
| 308 | last; | ||
| 309 | |||
| 310 | } else { | ||
| 311 | foreach (web_split_at_tags($part)) { | ||
| 312 | if (web_is_tag($_)) { | ||
| 313 | push(@pattern, web_parse_tag($_)); | ||
| 314 | } else { | ||
| 315 | push(@pattern, $_); | ||
| 316 | } | ||
| 317 | } | ||
| 318 | } | ||
| 319 | } | ||
| 320 | |||
| 321 | return \@pattern; | ||
| 322 | } | ||
| 323 | |||
| 324 | #---------------------------------------------------------------------- | ||
| 325 | # Read and parse pattern tokens in file | ||
| 326 | |||
| 327 | sub read_patterns { | ||
| 328 | my ($patterns) = @_; | ||
| 329 | |||
| 330 | my @patterns; | ||
| 331 | my @pattern_line = split(/\n/, $patterns); | ||
| 332 | |||
| 333 | foreach (@pattern_line) { | ||
| 334 | s/\#.*//; | ||
| 335 | next unless /\S/; | ||
| 336 | |||
| 337 | my @pattern_text = split(/->/, $_); | ||
| 338 | if (@pattern_text > 2 || $pattern_text[0] !~ /\S/) { | ||
| 339 | die "Bad patterns on line:\n$_\n"; | ||
| 340 | } | ||
| 341 | |||
| 342 | foreach (@pattern_text) { | ||
| 343 | s/^\s+//; | ||
| 344 | s/\s+$//; | ||
| 345 | my @pattern_parts = grep (/\S/, split(/([\(\)])/, $_)); | ||
| 346 | push(@patterns, parse_pattern(\@pattern_parts)) | ||
| 347 | } | ||
| 348 | |||
| 349 | push(@patterns, ["\n"]) if @pattern_text == 1; | ||
| 350 | } | ||
| 351 | |||
| 352 | return \@patterns; | ||
| 353 | } | ||
| 354 | |||
| 355 | #---------------------------------------------------------------------- | ||
| 356 | # Get the replacement for a match | ||
| 357 | |||
| 358 | sub replace_match { | ||
| 359 | my ($pattern, $replacement, $matches) = @_; | ||
| 360 | |||
| 361 | my @tokens; | ||
| 362 | for my $match (@$matches) { | ||
| 363 | my $iast = 1; | ||
| 364 | my $igroup = 1; | ||
| 365 | my @subtokens = (); | ||
| 366 | for (my $irep = 0; $irep < @$replacement; $irep++) { | ||
| 367 | my $subtoken; | ||
| 368 | if (ref $replacement->[$irep] eq 'HASH') { | ||
| 369 | my %subtoken = %{$replacement->[$irep]}; | ||
| 370 | foreach my $key (keys %subtoken) { | ||
| 371 | $subtoken{$key} = $match->[$irep]{$key} | ||
| 372 | if $subtoken{$key} eq '*'; | ||
| 373 | } | ||
| 374 | |||
| 375 | $subtoken = '<'; | ||
| 376 | $subtoken .= delete $subtoken{_}; | ||
| 377 | while (my ($key, $value) = each %subtoken) { | ||
| 378 | $subtoken .= " $key=\"$value\""; | ||
| 379 | } | ||
| 380 | $subtoken .= '>'; | ||
| 381 | |||
| 382 | } elsif (ref $replacement->[$irep] eq 'ARRAY') { | ||
| 383 | my $group = find_group($pattern, $igroup); | ||
| 384 | $igroup ++; | ||
| 385 | |||
| 386 | $subtoken = replace_match($pattern->[$group], | ||
| 387 | $replacement->[$irep], | ||
| 388 | $match->[$group]); | ||
| 389 | |||
| 390 | } elsif ($replacement->[$irep] eq '*') { | ||
| 391 | my $asterisk = find_asterisk($pattern, $iast); | ||
| 392 | $subtoken = fill_text($match->[$asterisk]); | ||
| 393 | $iast ++; | ||
| 394 | |||
| 395 | } else { | ||
| 396 | $subtoken = $replacement->[$irep]; | ||
| 397 | } | ||
| 398 | |||
| 399 | push(@subtokens, $subtoken); | ||
| 400 | } | ||
| 401 | |||
| 402 | push(@tokens, join('', @subtokens)) | ||
| 403 | } | ||
| 404 | |||
| 405 | return join("\n", @tokens); | ||
| 406 | } | ||
| 407 | |||
| 408 | #---------------------------------------------------------------------- | ||
| 409 | # Find the replacement for the next token or tokens | ||
| 410 | |||
| 411 | sub replace_a_token { | ||
| 412 | my ($patterns, $tokens) = @_; | ||
| 413 | |||
| 414 | my $replacement; | ||
| 415 | my $token = $tokens->{data}[$tokens->{next}]; | ||
| 416 | $replacement = get_match($patterns, $tokens) if web_is_tag($token); | ||
| 417 | |||
| 418 | if (! defined $replacement) { | ||
| 419 | $replacement = clean_token($token); | ||
| 420 | $tokens->{next} ++; | ||
| 421 | } | ||
| 422 | |||
| 423 | return $replacement | ||
| 424 | } | ||
| 425 | |||
| 426 | #---------------------------------------------------------------------- | ||
| 427 | # Replace tokens in text according to patterns | ||
| 428 | |||
| 429 | sub replace_tokens { | ||
| 430 | my ($patterns, $tokens) = @_; | ||
| 431 | |||
| 432 | $in_body = 0; | ||
| 433 | my @replaced_tokens; | ||
| 434 | while ($tokens->{next} < @{$tokens->{data}}) { | ||
| 435 | push(@replaced_tokens, replace_a_token($patterns, $tokens)); | ||
| 436 | } | ||
| 437 | |||
| 438 | my $text = join('', @replaced_tokens); | ||
| 439 | |||
| 440 | $text =~ s/[ \r\t]+\n/\n/g; | ||
| 441 | $text =~ s/\n{3,}/\n\n/g; | ||
| 442 | |||
| 443 | return $text; | ||
| 444 | } | ||
| 445 | |||
| 446 | #---------------------------------------------------------------------- | ||
| 447 | # Revert to original version of files | ||
| 448 | |||
| 449 | sub revert_files { | ||
| 450 | |||
| 451 | foreach my $file (@_) { | ||
| 452 | my $old_file = "$file~"; | ||
| 453 | rename($old_file, $file) if -e $old_file; | ||
| 454 | } | ||
| 455 | |||
| 456 | return; | ||
| 457 | } | ||
| 458 | |||
| 459 | #---------------------------------------------------------------------- | ||
| 460 | # Look for a stop pattern, accumulate tokens until then | ||
| 461 | |||
| 462 | sub search_for_end { | ||
| 463 | my ($patterns, $tokens, $next_pattern) = @_; | ||
| 464 | |||
| 465 | my $found; | ||
| 466 | my @replaced_tokens; | ||
| 467 | while ($tokens->{next} < @{$tokens->{data}}) { | ||
| 468 | my $token = $tokens->{data}[$tokens->{next}]; | ||
| 469 | |||
| 470 | if (web_is_tag($token)) { | ||
| 471 | $token = web_parse_tag($token); | ||
| 472 | if (web_same_tag($next_pattern, $token)) { | ||
| 473 | $found = 1; | ||
| 474 | last; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | |||
| 478 | push(@replaced_tokens, replace_a_token($patterns, $tokens)); | ||
| 479 | } | ||
| 480 | |||
| 481 | return $found ? join('', @replaced_tokens) : undef; | ||
| 482 | |||
| 483 | } | ||
| 484 | |||
| 485 | #---------------------------------------------------------------------- | ||
| 486 | # Look for a stop tag, accumulate tokens until then | ||
| 487 | |||
| 488 | sub search_for_tag { | ||
| 489 | my ($input, $next_tag, $extra) = @_; | ||
| 490 | |||
| 491 | my @output; | ||
| 492 | my $pattern = web_parse_tag($next_tag); | ||
| 493 | |||
| 494 | while (@$input) { | ||
| 495 | my $input_tag = shift(@$input); | ||
| 496 | |||
| 497 | if (web_is_tag($input_tag)) { | ||
| 498 | my $token = web_parse_tag($input_tag); | ||
| 499 | |||
| 500 | if (web_same_tag($pattern, $token)) { | ||
| 501 | push(@output, $extra) if $extra; | ||
| 502 | push(@output, $input_tag); | ||
| 503 | last; | ||
| 504 | } | ||
| 505 | } | ||
| 506 | |||
| 507 | push(@output, $input_tag); | ||
| 508 | } | ||
| 509 | |||
| 510 | return join('', @output); | ||
| 511 | } | ||
| 512 | |||
| 513 | #---------------------------------------------------------------------- | ||
| 514 | # Read the file into a single string | ||
| 515 | |||
| 516 | sub slurp_file { | ||
| 517 | my ($file) = @_; | ||
| 518 | |||
| 519 | local $/; | ||
| 520 | my $fd = IO::File->new($file, 'r'); | ||
| 521 | die "Couldn't read file ($file): $!\n" unless $fd; | ||
| 522 | |||
| 523 | my $text = <$fd>; | ||
| 524 | return $text; | ||
| 525 | } | ||
| 526 | |||
| 527 | #---------------------------------------------------------------------- | ||
| 528 | # Write | ||
| 529 | |||
| 530 | sub write_file { | ||
| 531 | my ($file, $text) = @_; | ||
| 532 | |||
| 533 | my $fd = IO::File->new($file, 'w'); | ||
| 534 | die "Couldn't write file ($file): $!\n" unless $fd; | ||
| 535 | |||
| 536 | print $fd $text; | ||
| 537 | close($fd); | ||
| 538 | } | ||
| 539 | |||
| 540 | __END__ |