#!/usr/bin/perl # # De-moron-ise Text from Microsoft Applications # # by John Walker -- January 1998 # http://www.fourmilab.ch/ # # This program is in the public domain. # $quiet = 0; $lineWrap = 72; # Wrap lines at this column $lineBreak1 = '[<]'; # Line break first pass candidates $lineBreak2 = '[>]'; # Line break second pass candidates # Process command line options for ($i = 0; $i <= $#ARGV; $i++) { if ($ARGV[$i] =~ m/^-/) { $o = $ARGV[$i]; splice(@ARGV, $i, 1); $i--; if (length($o) == 1) { last; } $opt = substr($o, 1, 1); $arg = substr($o, 2); # -u -- Print how-to-call information if ($opt eq 'u' || $opt eq '?') { print("Usage: demoroniser [ options ] infile outfile\n"); print(" Options:\n"); print(" -u Print this message.\n"); print(" -wcols Wrap lines at cols columns, 0 = no wrap.\n"); print(" -q Be quiet\n"); exit(0); # -wcols -- Wrap lines at cols columns, 0 = no wrap } elsif ($opt eq 'w') { if ($arg =~ m/^\d+$/ && $arg >= 0) { $lineWrap = $arg; if ($lineWrap == 0) { $lineWrap = 1 << 31; } } else { die("Invalid wrap length '$arg' in -w option.\n"); } } elsif ($opt eq 'q') { $quiet = 1; } } } # Open input and output files $if = STDIN; $of = STDOUT; $ifname = "(stdin)"; if ($#ARGV >= 0) { $if = IF; open($if, "<$ARGV[0]") || die("Cannot open input file $ARGV[0]: $!\n"); $ifname = $ARGV[0]; } if ($#ARGV >= 1) { $of = OF; open($of, ">$ARGV[1]") || die("Cannot open output file $ARGV[1]: $!\n"); } $iline = 0; $oline = 0; while ($l = <$if>) { $iline++; $l1 = &demoronise($l); &printWrap($l1); } close($if); close($of); # demoronise -- Translate moronic Microsoft bit-drool into # vaguely readable and compatible HTML. sub demoronise { local($s) = @_; local($i, $c); # Eliminate idiot MS-DOS carriage returns from line terminator $s =~ s/\s+$//; $s .= "\n"; # Map strategically incompatible non-ISO characters in the # range 0x82 -- 0x9F into plausible substitutes where # possible. $s =~ s/\x82/,/g; $s =~ s-\x83-f-g; $s =~ s/\x84/,,/g; $s =~ s/\x85/.../g; $s =~ s/\x88/^/g; $s =~ s-\x89- °/°°-g; $s =~ s/\x8B/~-g; $s =~ s-\x99-TM-g; $s =~ s/\x9B/>/g; $s =~ s/\x9C/oe/g; # Now check for any remaining untranslated characters. if ($s =~ m/[\x00-\x08\x10-\x1F\x80-\x9F]/) { for ($i = 0; $i < length($s); $i++) { $c = substr($s, $i, 1); if ($c =~ m/[\x00-\x09\x10-\x1F\x80-\x9F]/) { printf(STDERR "$ifname: warning--untranslated character 0x%02X in input line %d, output line(s) %d(...).\n", unpack('C', $c), $iline, $oline + 1) unless $quiet; } } } # Supply missing semicolon at end of numeric entity if # Billy's bozos left it out. $s =~ s/(&#[0-2]\d\d)\s/$1; /g; # Fix dimbulb obscure numeric rendering of < > & $s =~ s/&/&/g; $s =~ s/</</g; $s =~ s/>/>/g; # Fix unquoted non-alphanumeric characters in table tags $s =~ s/(\s*.*)(\s*)-$1-gi; # Translate bonehead PowerPoint misuse of