Introducing the <include-html> tag

This commit is contained in:
Patrick Steele-Idem 2016-09-26 13:24:08 -06:00
parent 690c1d1433
commit da4c341c78
4 changed files with 67 additions and 1 deletions

View File

@ -0,0 +1,17 @@
'use strict';
module.exports = function codeGenerator(el, codegen) {
let argument = el.argument;
if (!argument) {
return;
}
let builder = codegen.builder;
let pathExpression = builder.parseExpression(argument);
if (pathExpression.type !== 'Literal' || typeof pathExpression.value !== 'string') {
codegen.addError('Argument to the <include-text> tag should be a string value: <include-text("./foo.txt")/>');
return;
}
var path = pathExpression.value;
return builder.text(builder.literal('<include-html> cannot be compiled in the browser (path="' + path + '")'));
};

View File

@ -0,0 +1,30 @@
'use strict';
var resolveFrom = require('resolve-from');
var fs = require('fs');
module.exports = function codeGenerator(el, codegen) {
let argument = el.argument;
if (!argument) {
return;
}
let builder = codegen.builder;
let pathExpression = builder.parseExpression(argument);
if (pathExpression.type !== 'Literal' || typeof pathExpression.value !== 'string') {
codegen.addError('Argument to the <include-html> tag should be a string value: <include-html("./foo.txt")/>');
return;
}
var path = pathExpression.value;
var dirname = codegen.context.dirname;
try {
path = resolveFrom(dirname, path);
} catch(e) {
codegen.addError('File not found: ' + path);
return;
}
var txt = fs.readFileSync(path, { encoding: 'utf8' });
return builder.text(builder.literal(txt), false /* do not escape since this is HTML*/);
};

View File

@ -88,9 +88,27 @@
}
]
},
"<include-html>": {
"code-generator": "./include-html-tag",
"autocomplete": [
{
"displayText": "include-html(<path>)",
"snippet": "include-html(${1:\"./foo.html\"})",
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
},
{
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
}
]
},
"<include-text>": {
"code-generator": "./include-text-tag",
"autocomplete": [
{
"displayText": "include-text(<path>)",
"snippet": "include-text(${1:\"./foo.txt\"})",
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
},
{
"descriptionMoreURL": "http://markojs.com/docs/marko/language-guide/#includes"
}

View File

@ -1,5 +1,6 @@
{
"browser": {
"./include-text-tag.js": "./include-text-tag-browser.js"
"./include-text-tag.js": "./include-text-tag-browser.js",
"./include-html-tag.js": "./include-html-tag-browser.js"
}
}