From: Siegel, D. <DS...@ai...> - 2000-11-27 10:09:32
|
Hy! >There are several ways to strip comments from source code. It >is easy to >select a JS compression tool as standard. Or, we can write our own >script/program to filter out comments of all files recursively under a >directory. If it is desired, I can contribute it in Perl/Java/C. I've an Code Stripping System running under PERL. It uses two directories: js js/stripped All files have to be in both directories. If your running codestripper.pl in js, it searches all writable files in js/stripped, read the coresponding file in js, strippe all comments and copy ist to js/stripped. The two directories are necessary, if your using a revision control system. The codestripper only touches files which are checked out. Here is the code (comments are in german :-( ). Bye Dirk #Header: #$Id: codestripper.pl 1.1 1999/07/23 09:36:32 e567 i.B. $ #$ProjectName: I:\Projekte\ProNet_2_1\source\mks\DEV_Web\ProNet_2_1_dev.pj $ #$ProjectRevision: 1.1 $ #!/usr/bin/perl # Codestripper für ProNet 2 # Progrmamiert von Dirk Siegel # Begonnen am 27.05.1998 # Liest alle JavaScript Files eines Verzeichnisses ein # und entfernt alle Kommentare, um Ladezeit zu sparen. # Dabei wird jedes File Zeilenweise eingelesen und # über die Suche mit Regulären Ausdrücken die Kommentar- # Blöcke von '/*' bid '*/' entfernt. Danach werden alle # Kommentare ab '//' entfernt. # Lege das Unterverzeichnis fest $directory = "./stripped/"; # Lese das aktuelle Verzeichnis ein opendir(DIR,"."); @javascriptfiles = grep(/\.js/,readdir(DIR)); # print "Folgende Dateien werden gekuerzt:\n"; # print "@javascriptfiles\n\n"; # Führe für jedes JavaScript-File des Verzeichnisses aus: foreach $arg (@javascriptfiles) { # Öffne das File # print "Oeffne das File $arg\n"; open(FILE,$arg) || die "FEHLER: Kann $! nicht oeffnen!\n"; # Erzeuge das Ausgabefile # Teste, ob das File ausgecheckt ist if (-w "$directory$arg") { open(AUS,">$directory$arg") || die "FEHLER: Kann $! nicht erzeugen!\n"; # print "$arg ist geoeffnet\n"; } else { next; } $zaehler = 0; $kommentar = 0; $anzahl_zeilen = 0; while (<FILE>) { # print "Lese: $_"; $anzahl_zeilen++; # Lösche bei Zeilen mit Kommentar alles nach dem '//' s/(.*)\/\/.*/$1/g; # Sind wir im Kommentarmode ? if ( $kommentar == 1 ) { # Wurde das Ende eines Kommentarblocks erreicht? if (/\*\//) { $kommentar = 0; } $zaehler++; } else { # Wurde der Anfang eines Kommentarblocks erreicht? if ((/\/\*/) && !(/\/\*\#Header\:/)) { $kommentar = 1; $zaehler++; } else { # Lösche überflüssige Leerzeichen s/\ \ */\ /g; # Lösche alle Tabulatoren s/\t*//g; # Schreibe die Zeilen in das temporäre File, wenn sie keine Leerzeilen sind if (!( (/^$/) || (/^ $/))) { print AUS "$_"; } else { $zaehler++; } } } } # Schließe die Files close AUS; close FILE; print "In der Datei $arg wurden von $anzahl_zeilen Zeilen Code $zaehler Zeilen entfernt\n"; # print "$arg wurde umgewandelt.\n"; } print "\n\nDas Kuerzen der JavaScript Dateien ist abgeschlossen\n"; |