summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/codeq/core.js73
1 files changed, 49 insertions, 24 deletions
diff --git a/js/codeq/core.js b/js/codeq/core.js
index 6b0719b..f1bd697 100644
--- a/js/codeq/core.js
+++ b/js/codeq/core.js
@@ -279,34 +279,59 @@
return result;
};
+ var resolveResource = function (resourceName, resourceBranches) {
+ var traversedPath = ['data'], // top-level directory
+ branch = resources,
+ candidate = null,
+ i, fragment;
+ if (!resourceName) {
+ codeq.log.error('No resource name provided; path: "' + resourceBranches.join('/') + '"');
+ return null;
+ }
+ if (branch[resourceName]) candidate = 'data/' + resourceName; // top-level match
+ for (i = 0; i < resourceBranches.length; i++) {
+ fragment = resourceBranches[i];
+ branch = branch[fragment];
+ if (!branch) {
+ codeq.log.error('Resource sub-branch ' + fragment + ' does not exist; resource: "' + resourceName + '", path: "' + resourceBranches.join('/') + '"');
+ break;
+ }
+ traversedPath.push(fragment);
+ if (branch[resourceName]) candidate = traversedPath.join('/') + '/' + resourceName;
+ }
+ if (candidate) return candidate;
+ codeq.log.error('Resource ' + resourceName + ' was not found; path: "' + resourceBranches.join('/') + '"');
+ return null;
+ };
+
var directiveHandlers = {
'resource': function (code, tokens, templatePath) {
- var resourceName = tokens[1],
- traversedPath = ['data'], // top-level directory
- branch = resources,
- candidate = null,
- i, fragment;
- if (!resourceName) {
- codeq.log.error('No resource name provided; path: "' + templatePath.join('/') + '"');
- code.push('_result.push("data/broken.png");\n');
- return;
- }
- if (branch[resourceName]) candidate = 'data/' + resourceName; // top-level match
- for (i = 0; i < templatePath.length; i++) {
- fragment = templatePath[i];
- branch = branch[fragment];
- if (!branch) {
- codeq.log.error('Resource sub-branch ' + fragment + ' does not exist; resource: "' + resourceName + '", path: "' + templatePath.join('/') + '"');
- break;
+ code.push('_result.push("', resolveResource(tokens[1], templatePath) || 'data/broken.png', '");\n');
+ },
+
+ 'img': function (code, tokens, templatePath) {
+ var N = tokens.length,
+ attrs = [],
+ token, i;
+ for (i = 1; i < N; i++) {
+ token = tokens[i];
+ if (!token || typeof token !== 'object') {
+ codeq.log.error('Invalid token at position ' + i + ' in @img');
+ continue;
+ }
+ switch (token.key) {
+ case 'src':
+ attrs.push('src="' + resolveResource(token.value, templatePath) + '"');
+ break;
+ case 'alt':
+ attrs.push('alt="' + codeq.escapeHtml(token.value) + '"');
+ break;
+ case 'class':
+ attrs.push('class="' + token.value + '"');
+ break;
}
- traversedPath.push(fragment);
- if (branch[resourceName]) candidate = traversedPath.join('/') + '/' + resourceName;
- }
- if (candidate) code.push('_result.push("', candidate, '");\n');
- else {
- codeq.log.error('Resource ' + resourceName + ' was not found; path: "' + templatePath.join('/') + '"');
- code.push('_result.push("data/broken.png");\n');
}
+ code.push('_result.push("<img ', attrs.join(' ').replace(regexpQuote, '\\"'), '>");\n');
}
};