-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcss2js
More file actions
executable file
·89 lines (80 loc) · 2.09 KB
/
css2js
File metadata and controls
executable file
·89 lines (80 loc) · 2.09 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/perl
# This script converts a css file into a JavaScript file that can be
# loaded in an HTML page instead of the css file; the JavaScript file
# uses jQuery to inject a '<style>' tag into the page <head>.
#
# The generated JavaScript file requires jQuery; it assumes that
# jQuery has been loaded before it, and that jQuery is available in
# the global variable 'jQuery'.
#
# The main idea here is that if you have a JavaScript file that needs
# to define some css rules, you can maintain the css rules in a
# regular css file in the development copy of your project, but use
# this script to convert the css file to JavaScript for inclusion in a
# "production" version of your JavaScript file, so that users of your
# JavaScript file don't have to also include a separate css file.
#
# Usage:
#
# css2js [-o OUTPUTFILE.js] INPUTFILE.css
#
# The output file name defaults to INPUTFILE.css.js if the -o option
# is not specified.
#
# mbp Fri Nov 16 18:05:56 2012
while ( ($arg=shift) ne "" ) {
if ($arg eq "-o") {
$outfile = shift;
if (! $outfile) {
die "usage: css2js [-o OUTPUTFILE] INPUTFILE\n";
}
} else {
$infile = $arg;
last;
}
}
if (! -r $infile) {
die "can't read input file: $infile\n";
}
if (! $outfile) {
$outfile = "${infile}.js";
}
#
# suck in the css file, removing leading and trailing whitespace from each line, and
# concatenating all the lines together, with a single space between them
#
open(IN, "<$infile");
@lines = ();
while (<IN>) {
chomp;
s/^\s*//;
s/\s*$//;
push(@lines, $_);
}
close(IN);
$content = join(" ", @lines);
#
# remove /* comments */
#
while ( ($beg=index($content, "/*")) >= 0 ) {
if (($end=index($content, "*/")) >= 0) {
substr($content, $beg, $end-$beg+2) = "";
} else {
last;
}
}
#
# escape any single-quotes
#
$content =~ s/'/\\'/g;
#
# collapse whitespace
#
$content =~ s/\s+/ /g;
#
# output the final javascript
#
open(OUT, ">$outfile");
print OUT "jQuery('head').append(jQuery('<style type=\"text/css\">$content</style>'));\n";
close(OUT);
printf("wrote $outfile\n");