Skip to content

Commit 3b29abe

Browse files
committed
Use JavaScript Standard Style.
1 parent 42c8c31 commit 3b29abe

File tree

7 files changed

+508
-541
lines changed

7 files changed

+508
-541
lines changed

.jshintignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

js/compile.js

Lines changed: 67 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,75 +10,74 @@
1010
* http://www.opensource.org/licenses/MIT
1111
*/
1212

13-
/*jslint nomen: true, stupid: true */
1413
/*global require, __dirname, process, console */
1514

16-
(function () {
17-
"use strict";
18-
var tmpl = require("./tmpl.js").tmpl,
19-
fs = require("fs"),
20-
path = require("path"),
21-
uglifyJS = require("uglify-js"),
22-
// Retrieve the content of the minimal runtime:
23-
runtime = fs.readFileSync(__dirname + "/runtime.js", "utf8"),
24-
// A regular expression to parse templates from script tags in a HTML page:
25-
regexp = /<script( id="([\w\-]+)")? type="text\/x-tmpl"( id="([\w\-]+)")?>([\s\S]+?)<\/script>/gi,
26-
// A regular expression to match the helper function names:
27-
helperRegexp = new RegExp(
28-
tmpl.helper.match(/\w+(?=\s*=\s*function\s*\()/g).join("\\s*\\(|") + "\\s*\\("
29-
),
30-
// A list to store the function bodies:
31-
list = [],
32-
code;
33-
// Extend the Templating engine with a print method for the generated functions:
34-
tmpl.print = function (str) {
35-
// Only add helper functions if they are used inside of the template:
36-
var helper = helperRegexp.test(str) ? tmpl.helper : "",
37-
body = str.replace(tmpl.regexp, tmpl.func);
38-
if (helper || (/_e\s*\(/.test(body))) {
39-
helper = "_e=tmpl.encode" + helper + ",";
40-
}
41-
return "function(" + tmpl.arg + ",tmpl){" +
42-
("var " + helper + "_s='" + body + "';return _s;")
43-
.split("_s+='';").join("") + "}";
44-
};
45-
// Loop through the command line arguments:
46-
process.argv.forEach(function (file, index) {
47-
var listLength = list.length,
48-
stats,
49-
content,
50-
result,
51-
id;
52-
// Skipt the first two arguments, which are "node" and the script:
53-
if (index > 1) {
54-
stats = fs.statSync(file);
55-
if (!stats.isFile()) {
56-
console.error(file + " is not a file.");
57-
return;
58-
}
59-
content = fs.readFileSync(file, "utf8");
60-
while (true) {
61-
// Find templates in script tags:
62-
result = regexp.exec(content);
63-
if (!result) {
64-
break;
65-
}
66-
id = result[2] || result[4];
67-
list.push("'" + id + "':" + tmpl.print(result[5]));
68-
}
69-
if (listLength === list.length) {
70-
// No template script tags found, use the complete content:
71-
id = path.basename(file, path.extname(file));
72-
list.push("'" + id + "':" + tmpl.print(content));
73-
}
15+
;(function () {
16+
'use strict'
17+
var path = require('path')
18+
var tmpl = require(path.join(__dirname, 'tmpl.js'))
19+
var fs = require('fs')
20+
var uglifyJS = require('uglify-js')
21+
// Retrieve the content of the minimal runtime:
22+
var runtime = fs.readFileSync(path.join(__dirname, 'runtime.js'), 'utf8')
23+
// A regular expression to parse templates from script tags in a HTML page:
24+
var regexp = /<script( id="([\w\-]+)")? type="text\/x-tmpl"( id="([\w\-]+)")?>([\s\S]+?)<\/script>/gi
25+
// A regular expression to match the helper function names:
26+
var helperRegexp = new RegExp(
27+
tmpl.helper.match(/\w+(?=\s*=\s*function\s*\()/g).join('\\s*\\(|') + '\\s*\\('
28+
)
29+
// A list to store the function bodies:
30+
var list = []
31+
var code
32+
// Extend the Templating engine with a print method for the generated functions:
33+
tmpl.print = function (str) {
34+
// Only add helper functions if they are used inside of the template:
35+
var helper = helperRegexp.test(str) ? tmpl.helper : ''
36+
var body = str.replace(tmpl.regexp, tmpl.func)
37+
if (helper || (/_e\s*\(/.test(body))) {
38+
helper = '_e=tmpl.encode' + helper + ','
39+
}
40+
return 'function(' + tmpl.arg + ',tmpl){' +
41+
('var ' + helper + "_s='" + body + "';return _s;")
42+
.split("_s+='';").join('') + '}'
43+
}
44+
// Loop through the command line arguments:
45+
process.argv.forEach(function (file, index) {
46+
var listLength = list.length
47+
var stats
48+
var content
49+
var result
50+
var id
51+
// Skipt the first two arguments, which are "node" and the script:
52+
if (index > 1) {
53+
stats = fs.statSync(file)
54+
if (!stats.isFile()) {
55+
console.error(file + ' is not a file.')
56+
return
57+
}
58+
content = fs.readFileSync(file, 'utf8')
59+
while (true) {
60+
// Find templates in script tags:
61+
result = regexp.exec(content)
62+
if (!result) {
63+
break
7464
}
75-
});
76-
if (!list.length) {
77-
console.error("Missing input file.");
78-
return;
65+
id = result[2] || result[4]
66+
list.push("'" + id + "':" + tmpl.print(result[5]))
67+
}
68+
if (listLength === list.length) {
69+
// No template script tags found, use the complete content:
70+
id = path.basename(file, path.extname(file))
71+
list.push("'" + id + "':" + tmpl.print(content))
72+
}
7973
}
80-
// Combine the generated functions as cache of the minimal runtime:
81-
code = runtime.replace("{}", "{" + list.join(",") + "}");
82-
// Generate the minified code and print it to the console output:
83-
console.log(uglifyJS.minify(code, {fromString: true}).code);
84-
}());
74+
})
75+
if (!list.length) {
76+
console.error('Missing input file.')
77+
return
78+
}
79+
// Combine the generated functions as cache of the minimal runtime:
80+
code = runtime.replace('{}', '{' + list.join(',') + '}')
81+
// Generate the minified code and print it to the console output:
82+
console.log(uglifyJS.minify(code, {fromString: true}).code)
83+
}())

js/demo.js

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,59 +11,58 @@
1111

1212
/* global document, tmpl */
1313

14-
(function () {
15-
'use strict';
14+
;(function () {
15+
'use strict'
1616

17-
var templateInput = document.getElementById('template');
18-
var dataInput = document.getElementById('data');
19-
var resultNode = document.getElementById('result');
20-
var templateDemoNode = document.getElementById('tmpl-demo');
21-
var templateDataNode = document.getElementById('tmpl-data');
17+
var templateInput = document.getElementById('template')
18+
var dataInput = document.getElementById('data')
19+
var resultNode = document.getElementById('result')
20+
var templateDemoNode = document.getElementById('tmpl-demo')
21+
var templateDataNode = document.getElementById('tmpl-data')
2222

23-
function renderError(title, error) {
24-
resultNode.innerHTML = tmpl(
25-
'tmpl-error',
26-
{title: title, error: error}
27-
);
28-
}
23+
function renderError (title, error) {
24+
resultNode.innerHTML = tmpl(
25+
'tmpl-error',
26+
{title: title, error: error}
27+
)
28+
}
2929

30-
function render(event) {
31-
event.preventDefault();
32-
var data;
33-
try {
34-
data = JSON.parse(dataInput.value);
35-
} catch (e) {
36-
renderError('JSON parsing failed', e);
37-
return;
38-
}
39-
try {
40-
resultNode.innerHTML = tmpl(
41-
templateInput.value,
42-
data
43-
);
44-
} catch (e) {
45-
renderError('Template rendering failed', e);
46-
}
30+
function render (event) {
31+
event.preventDefault()
32+
var data
33+
try {
34+
data = JSON.parse(dataInput.value)
35+
} catch (e) {
36+
renderError('JSON parsing failed', e)
37+
return
4738
}
48-
49-
function empty(node) {
50-
while (node.lastChild) {
51-
node.removeChild(node.lastChild);
52-
}
39+
try {
40+
resultNode.innerHTML = tmpl(
41+
templateInput.value,
42+
data
43+
)
44+
} catch (e) {
45+
renderError('Template rendering failed', e)
5346
}
47+
}
5448

55-
function init(event) {
56-
if (event) {
57-
event.preventDefault();
58-
}
59-
templateInput.value = templateDemoNode.innerHTML.trim();
60-
dataInput.value = templateDataNode.innerHTML.trim();
61-
empty(resultNode);
49+
function empty (node) {
50+
while (node.lastChild) {
51+
node.removeChild(node.lastChild)
6252
}
53+
}
6354

64-
document.getElementById('render').addEventListener('click', render);
65-
document.getElementById('reset').addEventListener('click', init);
55+
function init (event) {
56+
if (event) {
57+
event.preventDefault()
58+
}
59+
templateInput.value = templateDemoNode.innerHTML.trim()
60+
dataInput.value = templateDataNode.innerHTML.trim()
61+
empty(resultNode)
62+
}
6663

67-
init();
64+
document.getElementById('render').addEventListener('click', render)
65+
document.getElementById('reset').addEventListener('click', init)
6866

69-
}());
67+
init()
68+
}())

js/runtime.js

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,39 +11,38 @@
1111

1212
/*global define, module */
1313

14-
(function ($) {
15-
"use strict";
16-
var tmpl = function (id, data) {
17-
var f = tmpl.cache[id];
18-
return data ? f(data, tmpl) : function (data) {
19-
return f(data, tmpl);
20-
};
21-
};
22-
tmpl.cache = {};
23-
tmpl.encReg = /[<>&"'\x00]/g;
24-
tmpl.encMap = {
25-
"<" : "&lt;",
26-
">" : "&gt;",
27-
"&" : "&amp;",
28-
"\"" : "&quot;",
29-
"'" : "&#39;"
30-
};
31-
tmpl.encode = function (s) {
32-
/*jshint eqnull:true */
33-
return (s == null ? "" : "" + s).replace(
34-
tmpl.encReg,
35-
function (c) {
36-
return tmpl.encMap[c] || "";
37-
}
38-
);
39-
};
40-
if (typeof define === "function" && define.amd) {
41-
define(function () {
42-
return tmpl;
43-
});
44-
} else if (typeof module === "object" && module.exports) {
45-
module.exports = tmpl;
46-
} else {
47-
$.tmpl = tmpl;
14+
;(function ($) {
15+
'use strict'
16+
var tmpl = function (id, data) {
17+
var f = tmpl.cache[id]
18+
return data ? f(data, tmpl) : function (data) {
19+
return f(data, tmpl)
4820
}
49-
}(this));
21+
}
22+
tmpl.cache = {}
23+
tmpl.encReg = /[<>&"'\x00]/g
24+
tmpl.encMap = {
25+
'<': '&lt;',
26+
'>': '&gt;',
27+
'&': '&amp;',
28+
'"': '&quot;',
29+
"'": '&#39;'
30+
}
31+
tmpl.encode = function (s) {
32+
return (s == null ? '' : '' + s).replace(
33+
tmpl.encReg,
34+
function (c) {
35+
return tmpl.encMap[c] || ''
36+
}
37+
)
38+
}
39+
if (typeof define === 'function' && define.amd) {
40+
define(function () {
41+
return tmpl
42+
})
43+
} else if (typeof module === 'object' && module.exports) {
44+
module.exports = tmpl
45+
} else {
46+
$.tmpl = tmpl
47+
}
48+
}(this))

0 commit comments

Comments
 (0)