This commit is contained in:
Tutur33
2023-11-24 22:35:41 +01:00
parent 3c0b507a93
commit 7644b2a0f7
45165 changed files with 4803356 additions and 3 deletions
+23
View File
@@ -0,0 +1,23 @@
The MIT License
===============
Copyright 2017 Artem Sapegin (http://sapegin.me), contributors
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+41
View File
@@ -0,0 +1,41 @@
# smpltmpl
[![Build Status](https://travis-ci.org/sapegin/smpltmpl.svg)](https://travis-ci.org/sapegin/smpltmpl)
Simple templates for Node.js based on ECMAScript [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) syntax.
## Installation
```bash
npm install --save smpltmpl
```
## Usage
Using a string:
```js
const { template } = require('smpltmpl');
console.log(template('Hello ${who}!', { who: 'templates' }));
```
Using a template file:
```js
const { templateFromFile } = require('smpltmpl');
console.log(templateFromFile('template.txt', { who: 'templates' }));
```
## Change log
The change log can be found on the [Releases page](https://github.com/sapegin/smpltmpl/releases).
## Contributing
Everyone is welcome to contribute. Please take a moment to review the [contributing guidelines](Contributing.md).
## Authors and license
[Artem Sapegin](http://sapegin.me) and [contributors](https://github.com/sapegin/smpltmpl/graphs/contributors).
MIT License, see the included [License.md](License.md) file.
+87
View File
@@ -0,0 +1,87 @@
// @ts-check
'use strict';
const fs = require('fs');
const path = require('path');
const vm = require('vm');
const codeFrame = require('babel-code-frame');
const STACK_REGEXP = /evalmachine\.<anonymous>:(\d+)(?::(\d+))?\n/;
const STACK_REGEXP_ALL = new RegExp(STACK_REGEXP.source, 'g');
/**
* @param {string} filename
* @return {string}
*/
function read(filename) {
try {
return fs.readFileSync(filename, 'utf8');
} catch (err) {
if (err.code === 'ENOENT') {
throw Error(`Template file not found: ${filename}`);
} else {
throw err;
}
}
}
/**
* @param {string} source
* @param {Error} exception
* @param {string} filename
* @return {string}
*/
function getErrorMessage(source, exception, filename) {
// Take line and column from the last mentioned position which would look like this:
// evalmachine.<anonymous>:1:11
const positions = exception.stack.match(STACK_REGEXP_ALL);
const position = [filename];
let line;
let col;
if (positions) {
const m = positions.pop().match(STACK_REGEXP);
if (m) {
line = m[1];
position.push(line);
col = m[2];
if (col) {
position.push(col);
}
}
}
const code = line ? codeFrame(source, Number(line), Number(col || 1)) : source;
return `Error in template ${position.join(':')}\n${exception.message}\n\n${code}`;
}
/**
* @param {string} tmpl
* @param {object} context
* @param {string} [filename]
* @return {string}
*/
function template(tmpl, context, filename) {
filename = filename || 'untitled';
tmpl = tmpl.replace(/`/g, '\\`');
try {
return vm.runInNewContext('`' + tmpl + '`', context);
} catch (exception) {
throw new Error(getErrorMessage(tmpl, exception, filename));
}
}
/**
* @param {string} filename
* @param {string} context
* @return {string}
*/
function templateFromFile(filename, context) {
const tmpl = read(filename);
return template(tmpl, context, path.basename(filename));
}
module.exports = {
template,
templateFromFile,
};
+54
View File
@@ -0,0 +1,54 @@
{
"name": "smpltmpl",
"version": "1.0.2",
"description": "Simple templates for Node.js based on ECMAScript template literals syntax",
"author": {
"name": "Artem Sapegin",
"url": "http://sapegin.me"
},
"homepage": "https://github.com/sapegin/smpltmpl",
"repository": "sapegin/smpltmpl",
"license": "MIT",
"engines": {
"node": ">=4"
},
"main": "index.js",
"files": [
"index.js"
],
"scripts": {
"lint": "eslint . --cache --fix",
"pretest": "npm run lint",
"precommit": "lint-staged",
"test:jest": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test": "npm run test:jest"
},
"keywords": [
"template",
"engine",
"ecmascript",
"es6",
"es2015",
"literal"
],
"devDependencies": {
"eslint": "^4.11.0",
"eslint-config-tamia": "^4.2.3",
"eslint-plugin-prettier": "^2.3.1",
"husky": "^0.14.3",
"jest": "^21.2.1",
"lint-staged": "^5.0.0",
"prettier": "^1.8.2"
},
"lint-staged": {
"*.js": [
"eslint --fix",
"git add"
]
},
"dependencies": {
"babel-code-frame": "^6.26.0"
}
}