Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 6f3fa8d

Browse files
first pass at an autoloader script, run_prettify.js which will be hosted, with compiled JS and CSS under /loader.
1 parent 0a5391d commit 6f3fa8d

File tree

7 files changed

+2298
-66
lines changed

7 files changed

+2298
-66
lines changed

Makefile

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,26 @@ YUI_COMPRESSOR=java -jar yui-compressor/yuicompressor-2.4.4.jar \
1414

1515
TAR_ROOT=distrib/google-code-prettify
1616

17-
all: src/prettify.js distrib
17+
all: distrib
1818

1919
clean:
20-
rm -rf distrib.tstamp distrib src/prettify.js
20+
rm -rf distrib.tstamp distrib src/prettify.js src/run_prettify.js
2121

2222
src/prettify.js: js-modules/*.js js-modules/*.pl
23-
@if [ -e $@ ]; then chmod +w $@; fi
24-
@perl -e '\
25-
sub readInclude($$$$) {\
26-
my $$prefix = $$_[0];\
27-
my $$name = "js-modules/" . $$_[1];\
28-
my $$buf = "";\
29-
if ($$name =~ /\.pl$$/) {\
30-
open(IN, "|perl $$name") or die "$$name: $$!";\
31-
} else {\
32-
open(IN, "<$$name") or die "$$name: $$!";\
33-
}\
34-
while (<IN>) {\
35-
$$buf .= "$$prefix$$_";\
36-
}\
37-
return $$buf;\
38-
}' \
39-
-pe 's/^(\s*)include\("([^"]+)"\);/readInclude($$1, $$2)/ge' \
40-
js-modules/prettify.js \
41-
> src/prettify.js \
42-
|| rm src/prettify.js
43-
@if [ -e $@ ]; then chmod -w $@; fi
23+
@if [ -e "$@" ]; then chmod +w "$@"; fi
24+
@perl js-modules/js_include.pl "$$(basename $@)" > "$@"
25+
@if [ -e "$@" ]; then chmod -w "$@"; fi
26+
27+
src/run_prettify.js: js-modules/*.js js-modules/*.pl
28+
@if [ -e "$@" ]; then chmod +w "$@"; fi
29+
@perl js-modules/js_include.pl "$$(basename $@)" > "$@"
30+
@if [ -e "$@" ]; then chmod -w "$@"; fi
4431

4532
distrib: distrib.tstamp distrib/prettify-small.tgz distrib/prettify-small.zip distrib/prettify-small.tar.bz2
4633
@wc -c distrib/prettify-small.{tar.bz2,tgz,zip} \
4734
| grep -v total
4835

49-
distrib.tstamp: src/*.js src/*.css
36+
distrib.tstamp: src/prettify.js src/run_prettify.js src/*.js src/*.css
5037
@echo Compiling
5138
@mkdir -p $(TAR_ROOT)
5239
@for f in src/*.css; do \
@@ -57,10 +44,20 @@ distrib.tstamp: src/*.js src/*.css
5744
done
5845
@$(CLOSURE_COMPILER) --js src/prettify.js \
5946
--externs closure-compiler/console-externs.js \
60-
--externs closure-compiler/amd-externs.js \
47+
--externs closure-compiler/amd-externs.js \
48+
--define IN_GLOBAL_SCOPE=true \
49+
--output_wrapper='!function(){%output%}()' \
6150
> $(TAR_ROOT)/prettify.js
6251
@wc -c src/prettify.js $(TAR_ROOT)/prettify.js \
6352
| grep -v total
53+
@$(CLOSURE_COMPILER) --js src/run_prettify.js \
54+
--externs closure-compiler/console-externs.js \
55+
--externs closure-compiler/amd-externs.js \
56+
--define IN_GLOBAL_SCOPE=false \
57+
--output_wrapper='!function(){%output%}()' \
58+
> $(TAR_ROOT)/run_prettify.js
59+
@wc -c src/run_prettify.js $(TAR_ROOT)/run_prettify.js \
60+
| grep -v total
6461
@for f in src/lang*.js; do \
6562
if [ $$f -nt $(TAR_ROOT)/$$(basename $$f) ]; then \
6663
$(CLOSURE_COMPILER) --js $$f --externs js-modules/externs.js \

js-modules/js_include.pl

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/perl
2+
3+
# Given a JS file looks for lines like
4+
# include("path/to/file/to/include");
5+
# and replaces them with the quoted file relative to the js-modules directory.
6+
# If the included file ends with ".pl" then it is treated as a perl file to
7+
# execute and the stdout is used as the JS to include.
8+
9+
use strict;
10+
11+
# Closure Compiler @define annotations that need to be pulled out of included
12+
# files because @defines need to be top-level vars.
13+
my $global_defs = "";
14+
15+
# Find @defines at the top of a JS file by pulling off comments and looking for
16+
# comments containing @define followed by a var declaration.
17+
sub extractGlobalDefs($) {
18+
my @headerComments;
19+
my $s = shift;
20+
while ($s) {
21+
last unless $s =~ m#^\s*(?://[^\r\n]*|/\*.*?\*/[ \t]*)[\r\n]*#s;
22+
my $comment = $&;
23+
$s = $';
24+
if ($comment =~ /[\@]define/ && $s =~ /^\s*var\s+[^;]+;[ \t]*[\r\n]*/) {
25+
my $global = $&;
26+
$s = $';
27+
$global =~ s/(var\s*IN_GLOBAL_SCOPE\s*=\s*)true\b/$1false/;
28+
$global_defs .= "$comment$global";
29+
} else {
30+
push(@headerComments, $comment);
31+
}
32+
}
33+
return (join "", @headerComments) . $s;
34+
}
35+
36+
# readInclude(whiteSpacePrefix, path) returns the JS content at path
37+
# (with the ".pl" adjustment above) and prepends each line with the
38+
# whitespace in whiteSpacePrefix to produce a chunk of JS that matches the
39+
# indentation of the including file.
40+
# @defines are extracted so that they can all appear globally at the top of
41+
# the file.
42+
sub readInclude($$) {
43+
my $prefix = shift;
44+
my $name = "js-modules/" . (shift);
45+
my $in;
46+
if ($name =~ /\.pl$/) {
47+
open($in, "perl $name|") or die "$name: $!";
48+
} else {
49+
open($in, "<$name") or die "$name: $!";
50+
}
51+
my $buf = "";
52+
while (<$in>) {
53+
if (m/(\s*)include\("([^"]+)"\);\s*$/) {
54+
my $inc = extractGlobalDefs(readInclude("$prefix$1", $2));
55+
$buf .= $inc;
56+
} else {
57+
$buf .= "$prefix$_";
58+
}
59+
}
60+
close($in);
61+
return $buf;
62+
}
63+
64+
my $target = shift;
65+
my $inc = readInclude("", $target);
66+
my $header = "";
67+
# Put descriptive top level comments above the grouped @defines.
68+
if ($inc =~ s#^(?://[^\r\n]*|/\*.*?\*/|\s)+##s) {
69+
$header = $&;
70+
}
71+
my $globals = $global_defs;
72+
# Un-indent @defines.
73+
$globals =~ s#^[ \t]*##gm;
74+
$globals .= "\n" unless $globals eq "";
75+
76+
print "$header$globals$inc";

js-modules/prettify.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
// JSLint declarations
5656
/*global console, document, navigator, setTimeout, window, define */
5757

58+
/** @define {boolean} */
59+
var IN_GLOBAL_SCOPE = true;
60+
5861
/**
5962
* Split {@code prettyPrint} into multiple timeouts so as not to interfere with
6063
* UI events.
@@ -783,7 +786,7 @@ var prettyPrint;
783786
* @param opt_numberLines {number|boolean} True to number lines,
784787
* or the 1-indexed number of the first line in sourceCodeHtml.
785788
*/
786-
function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
789+
function $prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
787790
var container = document.createElement('div');
788791
// This could cause images to load and onload listeners to fire.
789792
// E.g. <img onerror="alert(1337)" src="nosuchimage.png">.
@@ -817,7 +820,7 @@ var prettyPrint;
817820
* containing all the elements to pretty print.
818821
* Defaults to {@code document.body}.
819822
*/
820-
function prettyPrint(opt_whenDone, opt_root) {
823+
function $prettyPrint(opt_whenDone, opt_root) {
821824
var root = opt_root || document.body;
822825
var doc = root.ownerDocument || document;
823826
function byTagName(tn) { return root.getElementsByTagName(tn); }
@@ -963,8 +966,14 @@ var prettyPrint;
963966
'PR_STRING': PR_STRING,
964967
'PR_TAG': PR_TAG,
965968
'PR_TYPE': PR_TYPE,
966-
'prettyPrintOne': win['prettyPrintOne'] = prettyPrintOne,
967-
'prettyPrint': win['prettyPrint'] = prettyPrint
969+
'prettyPrintOne':
970+
IN_GLOBAL_SCOPE
971+
? (win['prettyPrintOne'] = $prettyPrintOne)
972+
: (prettyPrintOne = $prettyPrintOne),
973+
'prettyPrint': prettyPrint =
974+
IN_GLOBAL_SCOPE
975+
? (win['prettyPrint'] = $prettyPrint)
976+
: (prettyPrint = $prettyPrint)
968977
};
969978

970979
// Make PR available via the Asynchronous Module Definition (AMD) API.

0 commit comments

Comments
 (0)