diff options
author | Aleš Smodiš <aless@guru.si> | 2015-10-01 20:01:00 +0200 |
---|---|---|
committer | Aleš Smodiš <aless@guru.si> | 2015-10-01 20:01:00 +0200 |
commit | f93e29372bb9cb5f71b320a1b3fd1263d7c6c045 (patch) | |
tree | 30e1d1d18ce58a717425404e496f55059c25bf28 /js | |
parent | 35f081e1d2131ae4b6831b28fb837af2effc1e7e (diff) |
Add support for @img in templates.
Diffstat (limited to 'js')
-rw-r--r-- | js/codeq/core.js | 73 |
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'); } }; |