From d82418ed45488c296910bd20cfe510a63512b75c Mon Sep 17 00:00:00 2001 From: ccd0 Date: Wed, 13 Apr 2016 16:54:39 -0700 Subject: [PATCH] Make globals.js more like Coffeescript output; move $$ to its own file. --- Makefile | 10 ++++++++-- src/General/$$.coffee | 2 ++ src/General/globals.js | 36 +++++++++++++++++++----------------- tools/declare.js | 2 +- tools/globalize.js | 6 +++--- 5 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 src/General/$$.coffee diff --git a/Makefile b/Makefile index 87682d56f..09a4c00ec 100644 --- a/Makefile +++ b/Makefile @@ -6,12 +6,14 @@ ifeq "$(OS)" "Windows_NT" RM := -del CP = copy /y $(subst /,\,$<) $(subst /,\,$@) MKDIR = -mkdir $(subst /,\,$@) + ESC_DOLLAR = $$ else BIN := node_modules/.bin/ RMDIR := rm -rf RM := rm -rf CP = cp $< $@ MKDIR = mkdir -p $@ + ESC_DOLLAR = \$$ endif coffee := $(BIN)coffee -c --no-header @@ -34,6 +36,7 @@ sources_Config := \ sources_API := \ src/General/$$.coffee \ + src/General/$$$$.coffee \ src/General/CrossOrigin.coffee \ src/Images/ImageCommon.coffee @@ -148,14 +151,17 @@ tmp/$1.js : tmp/$1.coffee $(coffee_deps) tools/globalize.js endef -$(foreach i,$(parts),$(eval $(call rules_part,$(i)))) +$(foreach i,$(filter-out API,$(parts)),$(eval $(call rules_part,$(i)))) + +tmp/API.jst : $(sources_API) $(cat_deps) | tmp + $(cat) $(subst $$,$(ESC_DOLLAR),$(sources_API)) $@ tmp/API_%.coffee : tmp/API.jst $(template_deps) $(template) $< $@ type=$* tmp/API_%.js : tmp/API_%.coffee $(coffee_deps) $(coffee) $< - node tools/globalize.js $@ $(sources_API) + node tools/globalize.js $@ $(subst $$,$(ESC_DOLLAR),$(sources_API)) tmp/eventPage.js : src/General/eventPage.coffee $(coffee_deps) | tmp $(coffee) -o tmp src/General/eventPage.coffee diff --git a/src/General/$$.coffee b/src/General/$$.coffee new file mode 100644 index 000000000..4c54223ab --- /dev/null +++ b/src/General/$$.coffee @@ -0,0 +1,2 @@ +$$ = (selector, root=d.body) -> + [root.querySelectorAll(selector)...] diff --git a/src/General/globals.js b/src/General/globals.js index 4c5514464..3dba35211 100644 --- a/src/General/globals.js +++ b/src/General/globals.js @@ -1,20 +1,28 @@ -var Conf, c, d, doc, g, E, $$; +var Conf, E, c, d, doc, g; Conf = {}; c = console; d = document; doc = d.documentElement; -g = { + +g = { VERSION: '<%= readJSON('version.json').version %>', NAMESPACE: '<%= meta.name %>.', boards: {} }; E = (function() { - var str = {'&': '&', "'": ''', '"': '"', '<': '<', '>': '>'}; - var r = String.prototype.replace; - var regex = /[&"'<>]/g; - var fn = function (x) { + var fn, r, regex, str; + str = { + '&': '&', + "'": ''', + '"': '"', + '<': '<', + '>': '>' + }; + r = String.prototype.replace; + regex = /[&"'<>]/g; + fn = function(x) { return str[x]; }; return function(text) { @@ -23,20 +31,14 @@ E = (function() { })(); E.cat = function(templates) { - var html = ''; - for (var i = 0, len = templates.length; i < len; i++) { + var html, i, len; + html = ''; + for (i = 0, len = templates.length; i < len; i++) { html += templates[i].innerHTML; } return html; }; -E.url = function (content) { - return 'data:text/html;charset=utf-8,' + encodeURIComponent(content.innerHTML); -}; - -$$ = function(selector, root) { - if (root == null) { - root = d.body; - } - return [].slice.call(root.querySelectorAll(selector)); +E.url = function(content) { + return "data:text/html;charset=utf-8," + encodeURIComponent(content.innerHTML); }; diff --git a/tools/declare.js b/tools/declare.js index d946b970d..e919adc1d 100644 --- a/tools/declare.js +++ b/tools/declare.js @@ -3,7 +3,7 @@ var fs = require('fs'); var names = []; for (var d of fs.readdirSync('src')) { for (var f of fs.readdirSync(`src/${d}`)) { - var m = f.match(/^([$A-Z]\w*)\.coffee$/); + var m = f.match(/^([$A-Z][$\w]*)\.coffee$/); if (m) names.push(m[1]); } } diff --git a/tools/globalize.js b/tools/globalize.js index 4b66d6e22..d7f99705f 100644 --- a/tools/globalize.js +++ b/tools/globalize.js @@ -9,7 +9,7 @@ var sources = process.argv.slice(3); var names = []; for (var f of sources) { f = f.match(/[^/]*$/)[0]; - var m = f.match(/^([$A-Z]\w*)\.coffee$/); + var m = f.match(/^([$A-Z][$\w]*)\.coffee$/); if (m) names.push(m[1]); } @@ -20,10 +20,10 @@ var replaced = 0; script = script.replace( // matches declaration at the start of the function, not including helper function assignments - / *\bvar\s+[\w$]+(,\s*[\w$]+)*(,\s*|;\n)/, + / *\bvar\s+[$\w]+(,\s*[$\w]+)*(,\s*|;\n)/, function(declaration) { - var parts = declaration.split(/([\w$]+)(?=[,;])/); + var parts = declaration.split(/([$\w]+)(?=[,;])/); for (var name of names) { var i = parts.indexOf(name);