Permalink
Browse files

First pass at packaging for browser.

Manually confirmed that it works in WebKit.
Requires uglify-js as devDependency.
  • Loading branch information...
1 parent 0a30aa6 commit ded246b06a9735334eaf947c89acad23812b63d6 @paularmstrong committed Sep 30, 2011
Showing with 134 additions and 87 deletions.
  1. +1 −0 .gitignore
  2. +48 −1 Makefile
  3. +2 −2 index.js
  4. +77 −79 lib/dateformat.js
  5. +3 −3 lib/filters.js
  6. +2 −1 package.json
  7. +1 −1 scripts/config-lint.js
View
1 .gitignore
@@ -1,3 +1,4 @@
.DS_Store
npm-debug.log
node_modules/*
+browser/dist/*
View
49 Makefile
@@ -1,13 +1,60 @@
+BROWSER_FILE = "browser/dist/swig.js"
+TEMP_FILE = "browser/.swig.js"
+
all:
@npm install -d
@cp scripts/githooks/* .git/hooks/
@chmod -R +x .git/hooks/
+browser:
+ @rm -rf browser/dist
+ @mkdir -p browser/dist
+ @echo "swig = (function () {" >> $(BROWSER_FILE)
+ @echo "var swig = {}," >> $(BROWSER_FILE)
+ @echo " dateformat = {}," >> $(BROWSER_FILE)
+ @echo " filters = {}," >> $(BROWSER_FILE)
+ @echo " helpers = {}," >> $(BROWSER_FILE)
+ @echo " parser = {}," >> $(BROWSER_FILE)
+ @echo " tags = {};" >> $(BROWSER_FILE)
+
+ @echo "(function (exports) {" >> $(BROWSER_FILE)
+ @cat index.js >> $(BROWSER_FILE)
+ @echo "})(swig);" >> $(BROWSER_FILE)
+
+ @echo "(function (exports) {" >> $(BROWSER_FILE)
+ @cat lib/dateformat.js >> $(BROWSER_FILE)
+ @echo "})(dateformat);" >> $(BROWSER_FILE)
+
+ @echo "(function (exports) {" >> $(BROWSER_FILE)
+ @cat lib/filters.js >> $(BROWSER_FILE)
+ @echo "})(filters);" >> $(BROWSER_FILE)
+
+ @echo "(function (exports) {" >> $(BROWSER_FILE)
+ @cat lib/helpers.js >> $(BROWSER_FILE)
+ @echo "})(helpers);" >> $(BROWSER_FILE)
+
+ @echo "(function (exports) {" >> $(BROWSER_FILE)
+ @cat lib/parser.js >> $(BROWSER_FILE)
+ @echo "})(parser);" >> $(BROWSER_FILE)
+
+ @echo "(function (exports) {" >> $(BROWSER_FILE)
+ @cat lib/tags.js >> $(BROWSER_FILE)
+ @echo "})(tags);" >> $(BROWSER_FILE)
+
+ @echo "return swig;" >> $(BROWSER_FILE)
+ @echo "})();" >> $(BROWSER_FILE)
+
+ @cp $(BROWSER_FILE) $(TEMP_FILE)
+ @sed "/require/d" <$(TEMP_FILE) > $(BROWSER_FILE)
+ @rm $(TEMP_FILE)
+
+ @node_modules/uglify-js/bin/uglifyjs $(BROWSER_FILE) > browser/dist/swig.min.js
+
test:
@node tests/speed.js
@node scripts/runtests.js
lint:
@node scripts/runlint.js
-.PHONY: all test lint
+.PHONY: all browser test lint
View
4 index.js
@@ -6,9 +6,9 @@ var fs = require('fs'),
filters = require('./lib/filters'),
helpers = require('./lib/helpers'),
- _ = require('underscore'),
+ _ = require('underscore');
- config,
+var config,
CACHE = {};
// Call this before using the templates
View
156 lib/dateformat.js
@@ -1,110 +1,108 @@
-var _dateFormats = module.exports = {
- _months: {
+var _months = {
full: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
abbr: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
- _days: {
+ _days = {
full: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
abbr: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
alt: {'-1': 'Yesterday', 0: 'Today', 1: 'Tomorrow'}
- },
+ };
// Day
- d: function (input) {
- return (input.getDate() < 10 ? '0' : '') + input.getDate();
- },
- D: function (input) {
- return _dateFormats._days.abbr[input.getDay()];
- },
- j: function (input) {
- return input.getDate();
- },
- l: function (input) {
- return _dateFormats._days.full[input.getDay()];
- },
- N: function (input) {
- return input.getDay();
- },
- S: function (input) {
- return (input.getDate() % 10 === 1 && input.getDate() !== 11 ? 'st' : (input.getDate() % 10 === 2 && input.getDate() !== 12 ? 'nd' : (input.getDate() % 10 === 3 && input.getDate() !== 13 ? 'rd' : 'th')));
- },
- w: function (input) {
- return input.getDay() - 1;
- },
+exports.d = function (input) {
+ return (input.getDate() < 10 ? '0' : '') + input.getDate();
+};
+exports.D = function (input) {
+ return _days.abbr[input.getDay()];
+};
+exports.j = function (input) {
+ return input.getDate();
+};
+exports.l = function (input) {
+ return _days.full[input.getDay()];
+};
+exports.N = function (input) {
+ return input.getDay();
+};
+exports.S = function (input) {
+ return (input.getDate() % 10 === 1 && input.getDate() !== 11 ? 'st' : (input.getDate() % 10 === 2 && input.getDate() !== 12 ? 'nd' : (input.getDate() % 10 === 3 && input.getDate() !== 13 ? 'rd' : 'th')));
+};
+exports.w = function (input) {
+ return input.getDay() - 1;
+};
//z = function (input) { return ''; },
// Week
//W = function (input) { return ''; },
// Month
- F: function (input) {
- return _dateFormats._months.full[input.getMonth()];
- },
- m: function (input) {
- return (input.getMonth() < 9 ? '0' : '') + (input.getMonth() + 1);
- },
- M: function (input) {
- return _dateFormats._months.abbr[input.getMonth()];
- },
- n: function (input) {
- return input.getMonth() + 1;
- },
+exports.F = function (input) {
+ return _months.full[input.getMonth()];
+};
+exports.m = function (input) {
+ return (input.getMonth() < 9 ? '0' : '') + (input.getMonth() + 1);
+};
+exports.M = function (input) {
+ return _months.abbr[input.getMonth()];
+};
+exports.n = function (input) {
+ return input.getMonth() + 1;
+};
//t = function (input) { return ''; },
// Year
//L = function (input) { return ''; },
//o = function (input) { return ''; },
- Y: function (input) {
- return input.getFullYear();
- },
- y: function (input) {
- return (input.getFullYear().toString()).substr(2);
- },
+exports.Y = function (input) {
+ return input.getFullYear();
+};
+exports.y = function (input) {
+ return (input.getFullYear().toString()).substr(2);
+};
// Time
- a: function (input) {
- return input.getHours() < 12 ? 'am' : 'pm';
- },
- A: function (input) {
- return input.getHours() < 12 ? 'AM' : 'PM';
- },
+exports.a = function (input) {
+ return input.getHours() < 12 ? 'am' : 'pm';
+};
+exports.A = function (input) {
+ return input.getHours() < 12 ? 'AM' : 'PM';
+};
//B = function () { return ''; },
- g: function (input) {
- return input.getHours() === 0 ? 12 : (input.getHours() > 12 ? input.getHours() - 12 : input.getHours());
- },
- G: function (input) {
- return input.getHours();
- },
- h: function (input) {
- return (input.getHours() < 10 || (12 < input.getHours() < 22) ? '0' : '') + (input.getHours() < 10 ? input.getHours() : input.getHours() - 12);
- },
- H: function (input) {
- return (input.getHours() < 10 ? '0' : '') + input.getHours();
- },
- i: function (input) {
- return (input.getMinutes() < 10 ? '0' : '') + input.getMinutes();
- },
- s: function (input) {
- return (input.getSeconds() < 10 ? '0' : '') + input.getSeconds();
- },
+exports.g = function (input) {
+ return input.getHours() === 0 ? 12 : (input.getHours() > 12 ? input.getHours() - 12 : input.getHours());
+};
+exports.G = function (input) {
+ return input.getHours();
+};
+exports.h = function (input) {
+ return (input.getHours() < 10 || (12 < input.getHours() < 22) ? '0' : '') + (input.getHours() < 10 ? input.getHours() : input.getHours() - 12);
+};
+exports.H = function (input) {
+ return (input.getHours() < 10 ? '0' : '') + input.getHours();
+};
+exports.i = function (input) {
+ return (input.getMinutes() < 10 ? '0' : '') + input.getMinutes();
+};
+exports.s = function (input) {
+ return (input.getSeconds() < 10 ? '0' : '') + input.getSeconds();
+};
//u = function () { return ''; },
// Timezone
//e = function () { return ''; },
//I = function () { return ''; },
- O: function (input) {
- return (input.getTimezoneOffset() < 0 ? '-' : '+') + (input.getTimezoneOffset() / 60 < 10 ? '0' : '') + (input.getTimezoneOffset() / 60) + '00';
- },
+exports.O = function (input) {
+ return (input.getTimezoneOffset() < 0 ? '-' : '+') + (input.getTimezoneOffset() / 60 < 10 ? '0' : '') + (input.getTimezoneOffset() / 60) + '00';
+};
//T = function () { return ''; },
- Z: function (input) {
- return input.getTimezoneOffset() * 60;
- },
+exports.Z = function (input) {
+ return input.getTimezoneOffset() * 60;
+};
// Full Date/Time
//c = function () { return ''; },
- r: function (input) {
- return input.toString();
- },
- U: function (input) {
- return input.getTime() / 1000;
- }
+exports.r = function (input) {
+ return input.toString();
+};
+exports.U = function (input) {
+ return input.getTime() / 1000;
};
View
6 lib/filters.js
@@ -1,6 +1,6 @@
var helpers = require('./helpers'),
_ = require('underscore'),
- _dateFormats = require('./dateformat');
+ dateformat = require('./dateformat');
exports.add = function (input, addend) {
if (Array.isArray(input) && Array.isArray(addend)) {
@@ -38,8 +38,8 @@ exports.date = function (input, format) {
for (; i < l; i += 1) {
cur = format.charAt(i);
- if (_dateFormats.hasOwnProperty(cur)) {
- out += _dateFormats[cur](date);
+ if (dateformat.hasOwnProperty(cur)) {
+ out += dateformat[cur](date);
} else {
out += cur;
}
View
3 package.json
@@ -14,7 +14,8 @@
"devDependencies": {
"express": ">=2.4.0",
"nodelint": "0.5.0",
- "nodeunit": "0.5.3"
+ "nodeunit": "0.5.3",
+ "uglify-js": ">=1.1.0"
},
"main": "index",
"engines": {
View
2 scripts/config-lint.js
@@ -1,6 +1,6 @@
module.exports = {
root: __dirname + '/../',
- pathIgnore: ['*node_modules*']
+ pathIgnore: ['*node_modules*', '*browser/underscore*', '*browser/dist/*']
};
var options = {

0 comments on commit ded246b

Please sign in to comment.