package JavaScript::Bookmarklet; use strict; use warnings; use base 'Exporter'; our @EXPORT_OK = qw(make_bookmarklet); our $VERSION = '0.02'; use URI::Escape qw(uri_escape_utf8); sub make_bookmarklet { my $src = shift; my $no_src = shift; $src =~ s{^// ?javascript:.+\n}{} ; # Zap the first line if there's already a bookmarklet comment: my $bookmarklet = $src; $bookmarklet =~ s{^\s*//.+\n}{}gm; # Kill comments. $bookmarklet =~ s{\t}{ }gm; # Tabs to spaces $bookmarklet =~ s{ +}{ }gm; # Space runs to one space $bookmarklet =~ s{^\s+}{}gm; # Kill line-leading whitespace $bookmarklet =~ s{\s+$}{}gm; # Kill line-ending whitespace $bookmarklet =~ s{\n}{}gm; # Kill newlines $bookmarklet = "javascript:" . uri_escape_utf8($bookmarklet, qq('" \x00-\x1f\x7f-\xff)) ; # Escape single- and double-quotes, spaces, control chars, unicode: return $no_src ? $bookmarklet : "// $bookmarklet\n" . $src; } 1; __END__ =head1 NAME JavaScript::Bookmarklet - utility library and command-line script for converting human-readable JavaScript code into bookmarklet form. =head1 SYNOPSIS use JavaScript::Bookmarklet qw(make_bookmarklet); my $src = < For other issues, contact the author. =head1 AUTHOR Timothy Appnel =head1 SEE ALSO http://daringfireball.net/2007/03/javascript_bookmarklet_builder, L =head1 COPYRIGHT AND LICENCE The software is released under the Artistic License. The terms of the Artistic License are described at http://www.perl.com/language/misc/Artistic.html. Except where otherwise noted, JavaScript::Bookmarklet is Copyright 2008, Timothy Appnel, tima@cpan.org. All rights reserved.