[ic] Multiple selection form and Mainstreet Communications

Mike Heins interchange-users@icdevgroup.org
Thu Sep 19 11:35:01 2002


Quoting Philip S. Hempel (pshempel@linuxhardcore.com):
> > But it's not terribly hard to build that form.  All you are doing
> > is building your javascript on the fly - once again - we do it
> > all in perl.  The hardest part is getting quoting to work
> > and keeping your code legible at the same time.
> > 
> 
> Ok real dumb question. What is "quoting"?
> 

	string += 'this is some stuff\n';
	string += 'this is some more stuff\n';
	string += 'this is some stuff with a' + variable + ' in it\n';

Instead of in Perl:

	$string = <<EOF;
this is some stuff
this is some more stuff
this is some stuff with a $variable in it
EOF

I found that so annoying I created a [jsq] tag. Here is actual output:


string = [jsq]this is some stuff
this is some more stuff
this is some stuff with a $variable in it
[/jsq]

transformed to:

string = 'this is some stuff' +
'this is some more stuff' +
'this is some stuff with a ' + variable + ' in it'

The tag is easy:

UserTag jsquote Alias jsq
UserTag jsq hasEndTag
UserTag jsq NoReparse
UserTag jsq PosNumber 0
UserTag jsq Routine <<EOR
sub {
	my $text = shift;
	$text =~ s/^[ \t\r]*\n//;
	my @lines = split /\r?\n/, $text;

	for(@lines) {
		( $_ !~ /'/ and s/\r/\\r/g, s/(^|[^\\])\$\{?(\w+)\}?/$1' + $2 + '/g, $_ = qq{'$_'} )
		  or
		( $_ !~ /"/ and s/\r/\\r/g, s/(^|[^\\])\$\{?(\w+)\}?/$1" + $2 + "/g, $_ = qq{"$_"} )
		  or 
		( s/'/\\'/g,  s/\r/\\r/g, s/(^|[^\\])\$\{?(\w+)\}?/$1' + $2 + '/g, $_ = qq{'$_'} );
	}
	return join (" +\n", @lines);
}
EOR

UserTag jsq Documentation <<EOD
=head1 jsqn

Returns a string quoted for JavaScript, with variable substitution.

  <script>
  	var astring = 'just an insert';
  	var somevar = [jsq] Big long string you don't
		want to have to quote for JS, and you want to
		insert the variable $astring.[/jsq];
  </script>

Generates:

	<script>
		var astring = 'just an insert';
		var somevar = " Big long string you don't" +
	'       want to have to quote for JS, and you want to' +
	'       insert the variable ' + astring + '.';
	</script>

Used in the UI dynamic HTML.

EOD

-- 
Mike Heins
Perusion -- Expert Interchange Consulting    http://www.perusion.com/
phone +1.513.523.7621      <mike@perusion.com>

For a successful technology, reality must take precedence over public
relations, for Nature cannot be fooled. -- Dick Feynman