|
From: Guangyi Wu <gua...@al...> - 2000-11-27 16:15:42
|
I modified the perl file and translate/rewrite the comments in English.
Several things changed:
1. The library structure of DynAPI is not flat, a strip_dir() is added to
travel into all sub-directories.
2. The original version can only handle simple cases, and fails when
different comment tokens nest or comment tokens are in string. (e.g.
document.write('// This is not comment')). The following version should be
much better. If there are still things missing, let me know.
cheers
George
##############
# CodeFilter.pl
##############
#!/usr/bin/perl
# Comment filter for ProNet 2
# Author Dirk Siegel
# Begin on 27.05.1998
# Modified by George on 17.11.2000
# read all JavaScript files under a directory recursively
# and remove all comments.
# set sub-directory
$#ARGV >= 2 && die "[Usage] $0 [src_path] [dest_path]\n";
if ($#ARGV > 0) {
$destdir = $ARGV[1];
} else {
$destdir = "js_prod";
}
if ($#ARGV > -1) {
$srcdir = $ARGV[0];
} else {
$srcdir = "js";
}
sub strip_dir {
my($srcdir, $destdir) = @_;
# get js files in source directory
opendir(SRCDIR, $srcdir);
my(@javascriptfiles) = grep(/\.(js|JS)/,readdir(SRCDIR));
closedir(SRCDIR);
# get sub-directories in source directory
opendir(SRCDIR, $srcdir);
my(@subdirs) = grep(/^[^.]/ && -d "$srcdir/$_", readdir(SRCDIR));
closedir(SRCDIR);
# create destination directory
unless (-e $destdir) {
mkdir($destdir, "0755") || die "Cannot make directory $destdir";
}
-d $destdir || die "$destdir is not a directory";
# strip all js files
foreach $jsfile (@javascriptfiles) {
strip_file("$srcdir/$jsfile", "$destdir/$jsfile");
}
# strip all sub-directories
foreach my $subdir (@subdirs) {
strip_dir("$srcdir/$subdir", "$destdir/$subdir");
}
}
sub strip_file {
my($srcfile, $destfile) = @_;
# open input js file
open(INJS,$srcfile) || die "Cannot open $srcfile!\n";
# open the output file if it is writable
if (-e $destfile && !(-w $destfile)) {
print "The file $destfile is not writable";
close(INJS);
return;
}
open(OUTJS, ">$destfile") || die "Cannot open $destfile!";
# see perl faq6 - remove C comment
# '//' is added for JS
$/ = undef;
$_ = <INJS>;
s#/\*[^*]*\*+([^/*][^*]*\*+)*/|//.*(?=\n)|("(\\.|[^"\\])*"|'(\\.|[^'\\])*'|\
n+|.[^/"'\\]*)#$2#g;
s/\n\s*\n/\n/g;
print OUTJS $_;
# close the files
close OUTJS;
close INJS;
}
strip_dir("$srcdir", "$destdir");
# strip_file("test.js", "output.js");
===============================
Test file: test.js,
which is actually not a JS file
===============================
line 1; // This is a comment
// This is a comment
// comments with indents
line 2; // This is a comment /*
line 3; /* This is a comment
and // a commment*/
/* short comment */line 4; /* and other comment */
/* short // comment */line 5; /* another comment */
line 6:"not a comment /*";
line 7:"not a comment */"; /* but it is a comment */
line 8:'not a comment //'; // but it is a comment
|