about summary refs log tree commit diff stats
path: root/js-merge-helper.pl
blob: 35a72edf2a11598af1e72610314af14245e23c30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/env perl -w
use strict;

sub escape_c_string {
	shift;
	s/\t|\r|\n/ /g;     # convert spacings to whitespaces
	s/[^\/]\/\*.*?\*\///g;   # remove comments (careful: hinttags look like comment!)
	s/ {2,}/ /g;        # strip whitespaces
	s/(^|\(|\)|;|,|:|\}|\{|=|\+|\-|\*|\&|\||\<|\>|!) +/$1/g;
	s/ +($|\(|\)|;|,|:|\}|\{|=|\+|\-|\*|\&|\||\<|\>|!)/$1/g;
	s/\\/\\\\/g;        # escape backslashes
	s/\"/\\\"/g;        # escape quotes
	return $_
}

if (scalar @ARGV < 1) {
	print "usage: js-merge-helper.pl jsfile ... \n";
	exit 1;
}

my ($jsfile, $define, $js);

while (@ARGV) {

	$jsfile = shift @ARGV;
	my @fn = split /\//, $jsfile;
	my $fn = pop @fn;
	$fn =~ /^(.*)\.js$/;
	
	$define = "JS_".uc($1);
	$define =~ s/\-/_/;

	open(JSFILE, $jsfile) or die "Failed to open file: $!";
	$_ = do { local $/; <JSFILE> };
	close(JSFILE);

	$js = escape_c_string($_);

	print "#define $define ";
	printf "\"%s\"\n", $js;

}

exit;