mirror of
https://github.com/marko-js/marko.git
synced 2025-12-08 19:26:05 +00:00
Marko v3: Introduced <include-text('./foo.txt')/>
This commit is contained in:
parent
a3d76a3678
commit
9a43cc7663
30
taglibs/core/include-text-tag.js
Normal file
30
taglibs/core/include-text-tag.js
Normal 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-text> tag should be a string value: <include-text("./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));
|
||||
};
|
||||
@ -17,6 +17,9 @@
|
||||
"<include>": {
|
||||
"code-generator": "./include-tag"
|
||||
},
|
||||
"<include-text>": {
|
||||
"code-generator": "./include-text-tag"
|
||||
},
|
||||
"<invoke>": {
|
||||
"code-generator": "./invoke-tag"
|
||||
},
|
||||
|
||||
@ -1,3 +0,0 @@
|
||||
BEGIN
|
||||
<include resource="include-resource-target.txt" static="true"/>
|
||||
END
|
||||
5
test/fixtures/render/autotest/include-text/template.marko
vendored
Normal file
5
test/fixtures/render/autotest/include-text/template.marko
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
BEGIN
|
||||
<include-text('./include-resource-target.txt')/>
|
||||
END
|
||||
---
|
||||
Loading…
x
Reference in New Issue
Block a user