#! /usr/bin/env perl BEGIN{$^W++} # Turn on debugging info # Allows bookmarklets to be stored and edited in an expanded form, # and this scripts converts it into an html page which documents the bookmarklet, # as well as makes a link of the compressed form of the bookmarklet. # # I spent approximately 5 hours on this. # # Example invocation: # conv_bkmklt.pl expanded.js > report.html # Copyright 2002, David Newcum. # The author hereby grants permission to use, copy, modify, distribute, and license this software and its documentation for any purpose. # Comment out the following line if you don't have or don't want to use enscript to produce syntax-colored output. my $ENSCRIPT = "/usr/bin/enscript"; use IPC::Open2; ######################################### Read all input ############################################## my $acc = ''; while (<>) { $acc .= $_; my $b = $acc; $b =~ s#//.*##; if ($b =~ m/"/) { die "Error. Bookmarklets must not use double quotes.\nUse \\' or unescape(%22) instead.\n"; } } ######################################### Create Compressed Form ############################################## ## This is much like the javascript program found here: ## http://www.brainjar.com/js/crunch/ my $compressed = $acc; ## remove comments #$compressed =~ s#//.*##mg; #### Remove uneeded spaces my $out = ''; $_ = $compressed; my $left_parens = 0; # num(left_parens) - num(right_parens) my $left_braces = 0; # num(left_braces) - num(right_braces) while (1) { # Borrowed from perlfaq "What good is \G in a regular expression?" question PARSER: { #print STDERR "Another pass\n"; ## outside of strings or comments m/ \G( [^'"\/]+ )/gcsx && do { #print STDERR "BETWEEN matched\n"; my $one = $1; # Remove extraneous spaces $one =~ s/\s+/ /sg; # Remove all spaces expect for ones that are surrounded on boht sides by a letter $one =~ s/(?<=[a-zA-Z]) +(?=[a-zA-Z])/\n/g; $one =~ s/[ \t]+//g; $one =~ s/\n/ /g; $out .= $one; # Remove semicolons that occur right before braces $out =~ s/;}/}/gs; # Count parens and braces $left_parens++ while ($one =~ m/\G.*?\(/gs); $left_parens-- while ($one =~ m/\G.*?\)/gs); $left_braces++ while ($one =~ m/\G.*?\{/gs); $left_braces-- while ($one =~ m/\G.*?\}/gs); redo PARSER; }; ## single quote strings m/ \G( ' (?:[^'\\]|\\.)* ' )/gcsx && do { #print STDERR "SINGLEQUOTE matched\n"; $out .= $1; redo PARSER; }; ## division operator m/ \G( \/(?=[^\/]) )/gcsx && do { $out .= '/'; redo PARSER; }; ## comments m/ \G( \/\/[^\n]*\n )/gcsx && do { redo PARSER; }; } #print STDERR "Done\n"; last; } $compressed = $out; if ($left_parens < 0) { print STDERR "-->Too many right parens.\n"; exit(1); } if ($left_parens > 0) { print STDERR "-->Too many right parens.\n"; exit(1); } if ($left_braces < 0) { print STDERR "-->Too many right braces.\n"; exit(1); } if ($left_braces > 0) { print STDERR "-->Too many right braces.\n"; exit(1); } ######################################### Create HTMLized Form ############################################## my $html; if ($ENSCRIPT && ! -f $ENSCRIPT) { print STDERR "Enscript not found at '$ENSCRIPT', displaying code in black and white\n"; print STDERR "instead.\n"; print STDERR "\n"; print STDERR "If you don't want to see these messages any more, edit\n"; print STDERR "$0 and comment out the \$ENSCRIPT line at the top.\n"; print STDERR "\n"; print STDERR "If you have enscript already (type: which enscript) or are able to\n"; print STDERR "download it (http://people.ssh.fi/mtr/genscript/), edit this script\n"; print STDERR "($0) and change the path on the \$ENSCRIPT line.\n"; # Don't use enscript $ENSCRIPT = ''; } if (! $ENSCRIPT) { $html = $acc; ## Convert & => &, etc $html =~ s#\&#&#sg; $html =~ s#<#<#sg; $html =~ s#>#>#sg; $html = "
$html
"; } else { my $pid = open2(\*RDR, \*WTR, "$ENSCRIPT --language=html --output=- --color --pretty-print=javascript --quiet -"); print WTR $acc; close WTR; $html = join('', ); close RDR; # Strip off header and footer junk. There's probly some command line or config way to do this, but I couldn't find it. $html =~ s/^.*?
/
/si;
	$html =~ s/
.*/<\/PRE>/si; } ######################################### Print Result ############################################## $compressed = "javascript:$compressed"; print "drag_and_rename (" . length($compressed) . " characters)
\n"; print "



$html";