1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
#!/usr/bin/perl
#####################################################################
#
# Offline HTMLHelp.com Validator
# by Liam Quinn <liam@htmlhelp.com>
#
# This is a simplified version of the online WDG HTML Validator
# found at <http://www.htmlhelp.com/tools/validator/>.
#
# Copyright (c) 1998-2001 by Liam Quinn
# This program is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
#
#####################################################################
#####################################################################
# Required libraries #
######################
# These are all standard Perl modules
use strict;
use Getopt::Long;
use Text::Wrap;
use POSIX;
#####################################################################
#####################################################################
# Variables to define #
#######################
# Version and identifier of this program
my $VERSION = '1.0.3';
my $progname = "Offline HTMLHelp.com Validator, Version $VERSION
by Liam Quinn <liam\@htmlhelp.com>";
my $usage = "Usage: validate [OPTION] [FILE...]";
my $tryHelp = qq{Try "validate --help" for more information.};
# SGML directory (catalog, DTDs, SGML declarations)
my $sgmlDir = '/usr/share/sgml/html/dtd';
# Location of lq-nsgmls executable
my $nsgmlsLocation = '/usr/bin/nsgmls';
# lq-nsgmls command line
# The SGML declaration and HTML document's filename will be appended
# to this string
my $nsgmls = "$nsgmlsLocation -E0 -s";
# Catalog files for HTML/SGML and XHTML/XML
my $htmlCatalog = "/etc/sgml/catalog";
my $xhtmlCatalog = "$sgmlDir/xml/1.0/xhtml.soc";
# Where to direct errors (typically *STDOUT or *STDERR)
my $errout = *STDOUT;
# Versions of HTML associated with a given FPI
my %HTMLversion = (
'PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"' => 'XHTML Basic',
'PUBLIC "ISO/IEC 15445:2000//DTD HyperText Markup Language//EN"' => 'ISO/IEC 15445:2000',
'PUBLIC "ISO/IEC 15445:2000//DTD HTML//EN"' => 'ISO/IEC 15445:2000',
'PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"' => 'XHTML 1.0 Strict',
'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"' => 'XHTML 1.0 Transitional',
'PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN"' => 'XHTML 1.0 Frameset',
'PUBLIC "-//W3C//DTD HTML 4.01//EN"' => 'HTML 4.01 Strict',
'PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"' => 'HTML 4.01 Transitional',
'PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"' => 'HTML 4.01 Frameset',
'PUBLIC "-//W3C//DTD HTML 4.0//EN"' => 'HTML 4.0 Strict',
'PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"' => 'HTML 4.0 Transitional',
'PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN"' => 'HTML 4.0 Frameset',
'PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"' => 'HTML 3.2',
'PUBLIC "-//W3C//DTD HTML 3.2 Draft//EN"' => 'HTML 3.2',
'PUBLIC "-//W3C//DTD HTML 3.2//EN"' => 'HTML 3.2',
'PUBLIC "-//W3C//DTD HTML Experimental 970421//EN"' => 'HTML 3.2 + Style',
'PUBLIC "-//W3O//DTD W3 HTML 3.0//EN"' => 'HTML 3.0 Draft',
'PUBLIC "-//IETF//DTD HTML 3.0//EN//"' => 'HTML 3.0 Draft',
'PUBLIC "-//IETF//DTD HTML 3.0//EN"' => 'HTML 3.0 Draft',
'PUBLIC "-//IETF//DTD HTML i18n//EN"' => 'HTML 2.0 + i18n',
'PUBLIC "-//IETF//DTD HTML//EN"' => 'HTML 2.0',
'PUBLIC "-//IETF//DTD HTML 2.0//EN"' => 'HTML 2.0',
'PUBLIC "-//IETF//DTD HTML Level 2//EN"' => 'HTML 2.0',
'PUBLIC "-//IETF//DTD HTML 2.0 Level 2//EN"' => 'HTML 2.0',
'PUBLIC "-//IETF//DTD HTML Level 1//EN"' => 'HTML 2.0 Level 1',
'PUBLIC "-//IETF//DTD HTML 2.0 Level 1//EN"' => 'HTML 2.0 Level 1',
'PUBLIC "-//IETF//DTD HTML Strict//EN"' => 'HTML 2.0 Strict',
'PUBLIC "-//IETF//DTD HTML 2.0 Strict//EN"' => 'HTML 2.0 Strict',
'PUBLIC "-//IETF//DTD HTML Strict Level 2//EN"' => 'HTML 2.0 Strict',
'PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 2//EN"' => 'HTML 2.0 Strict',
'PUBLIC "-//IETF//DTD HTML Strict Level 1//EN"' => 'HTML 2.0 Strict Level 1',
'PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 1//EN"' => 'HTML 2.0 Strict Level 1'
);
# SGML declarations for a given level of HTML
my %sgmlDecl = (
'XHTML Basic' => "$sgmlDir/xml/1.0/xml1.dcl",
'ISO/IEC 15445:2000' => "$sgmlDir/iso-15445/15445.dcl",
'XHTML 1.0 Strict' => "$sgmlDir/xml/1.0/xhtml1.dcl",
'XHTML 1.0 Transitional' => "$sgmlDir/xml/1.0/xhtml1.dcl",
'XHTML 1.0 Frameset' => "$sgmlDir/xml/1.0/xhtml1.dcl",
'HTML 4.01 Strict' => "$sgmlDir/4.01/HTML4.decl",
'HTML 4.01 Transitional' => "$sgmlDir/4.01/HTML4.decl",
'HTML 4.01 Frameset' => "$sgmlDir/4.01/HTML4.decl",
'HTML 4.0 Strict' => "$sgmlDir/4.0/HTML4.decl",
'HTML 4.0 Transitional' => "$sgmlDir/4.0/HTML4.decl",
'HTML 4.0 Frameset' => "$sgmlDir/4.0/HTML4.decl",
'HTML 3.2' => "$sgmlDir/html-32.decl",
'HTML 3.2 + Style' => "$sgmlDir/html-970421.decl",
'HTML 3.0 Draft' => "$sgmlDir/html-3.decl",
'HTML 2.0 + i18n' => "$sgmlDir/html-2-i18n.decl",
'HTML 2.0' => "$sgmlDir/html.dcl",
'HTML 2.0 Strict' => "$sgmlDir/html-2.decl",
'HTML 2.0 Level 1' => "$sgmlDir/html-2.decl",
'HTML 2.0 Strict Level 1' => "$sgmlDir/html-2.decl",
'Unknown' => "/usr/share/wdg-html-validator/custom.dcl",
# For generic XML validation (using the --xml option)
'XML' => "$sgmlDir/xml/1.0/xhtml1.dcl"
);
# XHTML DTDs
my %xhtml = (
'XHTML Basic' => 1,
'XHTML 1.0 Strict' => 1,
'XHTML 1.0 Transitional' => 1,
'XHTML 1.0 Frameset' => 1,
'XML' => 1
);
# Default DOCTYPE if the document is missing a DOCTYPE
my $defaultDoctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">';
# Default DOCTYPE if the document contains frames
my $defaultFramesetDoctype = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">';
# Error for missing DOCTYPE
my $noDoctype = "missing document type declaration; assuming HTML 4.01 Transitional";
# Error for missing DOCTYPE in a Frameset document
my $noFramesetDoctype = "missing document type declaration; assuming HTML 4.01 Frameset";
#####################################################################
#####################################################################
#
# The rest of the script...
#
#####################################################################
# Flush output buffer
$| = 1;
### Get user input ###
# Character encoding to use (optional)
my $charset;
# Verbose output (optional)
my $verbose;
# Emacs-friendly output
my $emacs;
# XML mode
my $xml;
# Help and version info
my $help;
my $versionInfo;
GetOptions("xml" => \$xml, "charset=s" => \$charset, "verbose" => \$verbose,
"help|h" => \$help, "version|v" => \$versionInfo, "emacs" => \$emacs);
# Files to validate
my @files = @ARGV;
######################
my $errors = 0;
if ($versionInfo || $help) {
if ($versionInfo) {
print "$progname\n";
}
if ($help) {
&helpText;
}
exit $errors;
}
if ($#files == -1) {
push(@files, '-');
}
# Check that nsgmls is available before we get too far
unless (-e $nsgmlsLocation) {
&error("$nsgmlsLocation is not installed");
exit $errors;
}
unless (-x $nsgmlsLocation) {
&error("$nsgmlsLocation is not executable");
exit $errors;
}
my $file;
foreach $file (@files) {
my $tempname = undef;
my $tempfh = undef;
# Read in document
my $document = "";
if ($file ne '-') {
unless (-e $file) {
&error("File $file does not exist.");
next;
}
unless (-r $file) {
&error("File $file is not readable.");
next;
}
open(IN, $file) || die "Unexpected error reading $file: $!\n";
while (<IN>) {
$document .= $_;
}
close(IN);
} else {
while (<>) {
$document .= $_;
}
}
unless ($charset) {
# Check for a META element specifying the character encoding
if ($document =~ m#<META(\s[^>]*http\-equiv\s*=\s*["']?Content\-Type["']?[^>]*)>#iso) {
my $metaAttributes = $1;
if ($metaAttributes =~ m#\scontent\s*=\s*["']?.*[\s;]charset\s*=\s*['"]?([^"']+)#iso) {
$charset = $1;
}
}
}
my @errors; # queue of errors
my @externalErrors; # queue of errors in an external DTD
my $lineAdjust = 0; # account for line number changes if we add a DOCTYPE
# Determine the level of HTML
my $htmlLevel;
my $fileToValidate = $file;
if ($xml) {
$htmlLevel = 'XML';
} else {
$htmlLevel = 'Unknown';
}
if ($document =~ /<!DOCTYPE([^>]*)>/iso) {
my $doctypeMeat = $1;
if ($doctypeMeat =~ /PUBLIC\s+("[^"]*")/iso) {
$htmlLevel = $HTMLversion{"PUBLIC $1"} || $htmlLevel;
}
if ($file eq '-') {
($tempname, $tempfh) = getTempFile();
print $tempfh "$document";
close($tempfh);
$fileToValidate = $tempname;
}
} else { # Missing DOCTYPE
# Add a default DOCTYPE
my ($insertedDoctype, $doctypeError);
if ($document =~ /<FRAMESET/io) {
$insertedDoctype = $defaultFramesetDoctype;
$doctypeError = $noFramesetDoctype;
} else {
$insertedDoctype = $defaultDoctype;
$doctypeError = $noDoctype;
}
($tempname, $tempfh) = getTempFile();
print $tempfh "$insertedDoctype\n$document";
close($tempfh);
$fileToValidate = $tempname;
$lineAdjust = 2;
push(@errors, "::" . (1 + $lineAdjust) . ":0:E: $doctypeError");
}
# Determine whether we're dealing with HTML or XHTML and set the SP
# environment accordingly.
if ($xhtml{$htmlLevel}) {
$ENV{'SGML_CATALOG_FILES'} = $xhtmlCatalog;
$ENV{'SP_ENCODING'} = 'xml';
} else {
$ENV{'SGML_CATALOG_FILES'} = $htmlCatalog;
if (defined $charset) {
$ENV{'SP_ENCODING'} = $charset;
}
}
$ENV{'SP_CHARSET_FIXED'} = 1;
if ($verbose) {
if ($file eq '-') {
print wrap("", "\t",
"Checking with $htmlLevel document type...\n");
} else {
print wrap("", "\t",
"Checking $file with $htmlLevel document type...\n");
}
}
# Run the validator
open(NSGMLS, "$nsgmls $sgmlDecl{$htmlLevel} $fileToValidate 2>&1 |")
|| die("Unable to execute $nsgmls: $!\n");
# Create a queue of errors
while (<NSGMLS>) {
my @error = split(/:/, $_, 6);
if ($#error < 5) {
next;
} elsif ($error[4] eq 'E' || $error[4] eq 'X') {
push(@errors, $_);
# If the DOCTYPE is bad, bail out
last if ($error[5] eq " unrecognized {{DOCTYPE}}; unable to check document\n");
} elsif ($error[1] =~ /^<URL>/o) { # error from external DTD
push(@externalErrors, $_);
} elsif (length($error[4]) > 1) { # Allow secondary messages about preceding error
push(@errors, $_);
}
}
close(NSGMLS);
# If we created a tempfile, unlink it
if (defined $tempname) {
unlink($tempname);
}
# Report errors
if ($#errors > -1 || $#externalErrors > -1) {
&startErrors($file);
foreach (@externalErrors) {
my @error = split(/:/, $_, 7);
# Determine URL containing the error
my $errorURL;
if ($error[1] =~ /<URL>(.+)/o) {
$errorURL = "$1:$error[2]";
}
my $lineNumber = $error[3];
my $character = $error[4] + 1;
my $errorMsg;
if ($emacs) {
$errorMsg = "$errorURL:$lineNumber:$character:";
} else {
$errorMsg = "$errorURL, line $lineNumber, character $character: ";
}
if ($error[6]) {
$errorMsg .= superChomp($error[6]);
} else {
$errorMsg .= superChomp($error[5]);
}
&htmlError($errorMsg);
}
foreach (@errors) {
my @error = split(/:/, $_, 6);
# I don't think this should happen, but I'm not sure
next if $#error < 4;
# Determine line number and character of error
my $lineNumber = $error[2] - $lineAdjust;
next unless $lineNumber > 0;
my $character = $error[3] + 1;
# Prepare error message
my $errorMsg;
if ($emacs) {
$errorMsg = "$file:$lineNumber:$character:";
} else {
$errorMsg = "Line $lineNumber, character $character: ";
}
if ($error[5]) {
$errorMsg .= superChomp($error[5]);
} else {
$errorMsg .= superChomp($error[4]);
}
while ($errorMsg =~ m#\{\{"?(.+?)"?\}\}#gos) {
my $linkText = $1;
$errorMsg =~ s#\{\{(")?$linkText(")?\}\}#$1$linkText$2#;
}
&htmlError($errorMsg);
}
} else {
if ($verbose) {
print "No errors!\n";
}
}
}
exit $errors;
# Return an error message
# The error message must be given as the first argument
sub error {
my $error_message = shift;
print $errout wrap("", "\t", "ERROR: \t$error_message\n");
++$errors;
}
# Heading to start HTML errors
# The file being validated should be given as the first argument
sub startErrors {
my $file = shift;
if (length $file) {
if ($file eq '-') {
print $errout "*** Errors: ***\n";
} else {
if ($emacs) {
print $errout "*** Errors validating $file: ***\n";
} else {
print $errout wrap("", "\t",
"*** Errors validating $file: ***\n");
}
}
}
}
# Report an HTML error
# The error message must be given as the first argument
sub htmlError {
my $error_message = shift;
if ($emacs) {
print $errout "$error_message\n";
} else {
print $errout wrap("", "\t", "$error_message\n");
}
++$errors;
}
# Remove any newline characters (\r or \n) at the end of a string
# First argument is the string
# Returns the new string
sub superChomp {
my $str = shift || return;
$str =~ s/[\r\n]+$//o;
return $str;
}
# Create temporary file securely
# Returns the name and file handle of the created file
sub getTempFile {
my $filename;
do {
$filename = POSIX::tmpnam();
} until sysopen(FH, $filename, O_RDWR|O_CREAT|O_EXCL, 0666);
return ($filename, \*FH);
}
sub helpText {
print<<"EndOfHelp";
"validate", the Offline HTMLHelp.com Validator, checks the syntax of HTML
documents using an SGML parser and reports any errors. XHTML documents
may also be validated using an XML parser.
$usage
The program's options are as follows:
--xml indicate that the documents to be validated are XML
documents. Known document types, such as HTML 4.01 and
XHTML 1.0, are automatically handled by "validate".
For unknown document types, "validate" will assume
XHTML/XML if this option is specified and HTML/SGML
otherwise.
--charset=ENCODING force ENCODING to be used as the character encoding
when validating HTML/SGML documents. This option is
ignored when validating XHTML/XML documents, which
are assumed to use XML rules for specifying the
character encoding. The following encodings
(case-insensitive) are supported: "utf-8",
"iso-10646-ucs-2", "euc-jp", "euc-kr", "gb2312",
"shift_jis", "big5", and "iso-8859-n" where n is
between 1 and 9 inclusive.
--verbose turn on verbose output messages
--emacs use an output format intended for parsing by Emacs
-h, --help display this help and exit
-v, --version output version information and exit
Any number of files may be specified after the options. With no FILE,
standard input is read.
"validate" is written by Liam Quinn <liam\@htmlhelp.com> and is based on the
WDG HTML Validator, an online validation service available at
<http://www.htmlhelp.com/tools/validator/>.
EndOfHelp
}
|