File indexing completed on 2024-05-12 15:39:11

0001 #!/usr/bin/perl
0002 #
0003 # Copyright (C) 2009 Germain Garand <germain@ebooksfrance.org>
0004 #
0005 # This library is free software; you can redistribute it and/or
0006 # modify it under the terms of the GNU Library General Public
0007 # License as published by the Free Software Foundation; either
0008 # version 2 of the License, or (at your option) any later version.
0009 #
0010 # This library is distributed in the hope that it will be useful,
0011 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013 # Library General Public License for more details.
0014 #
0015 # You should have received a copy of the GNU Library General Public License
0016 # along with this library; see the file COPYING.LIB.  If not, write to
0017 # the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018 # Boston, MA 02110-1301, USA.
0019 
0020 use Getopt::Std;
0021 use File::Copy;
0022 use File::Temp qw|tempfile|;
0023 
0024 getopts('xit:', \%o) or die;
0025 
0026 $ARGV[0] or die "doctype [-t type] [-xi] filename
0027 \tAdd or remove or replace doctype declaration from filename.
0028 \tOptions:
0029 \t\t-t type: with type in trans(itional)|loo(se), frame(set), str(ict). Default is 'strict'.
0030 \t\t-x: use xhtml doctypes. Default uses HTML.
0031 \t\t-i: replace input file. Default prints to stdout.
0032 ";
0033 
0034 open (IN, $ARGV[0]) or die $!;
0035 
0036 if (exists  $o{'i'} ) {
0037     ($fh, $f) = tempfile() if exists $o{'i'};
0038 } else {
0039     $fh = \*STDOUT;
0040 }
0041 
0042 while (<IN>) {
0043 
0044 if (1..1) {
0045     while (/^\s*$/) { $_ = <IN> };
0046     
0047     if (/<!DOCTYPE/i) {
0048          chomp;
0049          while (!s/<!DOCTYPE[^>]*>//i) {
0050               $_ .= <IN>;
0051               chomp;
0052          }
0053          $found++
0054     }
0055     if ( not $found or exists $o{'t'} ) {
0056         if ($o{'t'} =~ /^(tran|loo)/i) {
0057             $_ = (exists $o {'x'} ? 
0058                '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
0059                  :
0060                '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">' ) . "\n" . $_;
0061         } elsif ($o{'t'} =~ /^frame/i) {      
0062             $_ = (exists $o {'x'} ?
0063                '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">'
0064                  :
0065                '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">' ) . "\n" . $_;
0066         } elsif ($o{'t'} =~ /^str/i or not $found) {
0067             $_ = (exists $o {'x'} ?
0068                '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
0069                  :                 
0070                '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">' ) . "\n" . $_;
0071         }
0072     } 
0073 }
0074 print $fh $_
0075 
0076 }
0077 
0078 close;
0079 do { move($f, $ARGV[0]) ; chmod 0644, $ARGV[0] } if exists  $o{'i'}