-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
option to return value on having equal sign
- Loading branch information
Showing
4 changed files
with
56 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,57 @@ | ||
const vm = require('vm'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { replaceString, hasEqualSign } = require('./utils'); | ||
|
||
const defaultOptions = { | ||
base: '', | ||
}; | ||
|
||
function parser(ulkaTemplate, values = {}, options = defaultOptions) { | ||
return ulkaTemplate | ||
.replace(/\\?{%(.*?)%}/gs, (...args) => { | ||
let jsCode = args[1]; | ||
|
||
values = { | ||
...values, | ||
// require, | ||
console, | ||
}; | ||
|
||
if (args[0][0] === '\\' && ulkaTemplate[args[2] - 1] !== '\\') | ||
return args[0].slice(1); | ||
|
||
jsCode = jsCode.replace(/(var |let |const )/gs, ''); | ||
|
||
const result = vm.runInNewContext(jsCode, values); | ||
|
||
const codeWithoutString = replaceString(jsCode, ''); | ||
const containsEqual = hasEqualSign(codeWithoutString); | ||
|
||
return containsEqual ? '' : result || ''; | ||
}) | ||
.trim(); | ||
try { | ||
return ulkaTemplate | ||
.replace(/\\?{%(.*?)%}/gs, (...args) => { | ||
let jsCode = args[1]; | ||
|
||
values = { | ||
...values, | ||
require: reqPath => { | ||
const rPath = path.join(options.base, reqPath); | ||
if (fs.existsSync(rPath)) return require(rPath); | ||
return require(rPath); | ||
}, | ||
console, | ||
}; | ||
|
||
/* | ||
If first index is equal sign then remove the equal sign | ||
*/ | ||
const containsEqualsInFirstIndex = jsCode[0] === '='; | ||
if (containsEqualsInFirstIndex) jsCode = jsCode.substr(1); | ||
|
||
/* | ||
- {% sth = "roshan" %} | ||
- \{% sth %} => {% sth %} | ||
- \\{% sth %} => \{% "roshan" %} | ||
*/ | ||
if (args[0][0] === '\\' && ulkaTemplate[args[2] - 1] !== '\\') | ||
return args[0].slice(1); | ||
|
||
jsCode = jsCode.replace(/(var |let |const )/gs, ''); | ||
|
||
const result = vm.runInNewContext(jsCode, values); | ||
|
||
const codeWithoutString = replaceString(jsCode, ''); | ||
const containsEqual = hasEqualSign(codeWithoutString); | ||
const shouldPrintResult = !containsEqual || containsEqualsInFirstIndex; | ||
|
||
return !shouldPrintResult ? '' : result || ''; | ||
}) | ||
.trim(); | ||
} catch (e) { | ||
console.log(`Ulka Praser Error: `, e.message); | ||
throw e; | ||
} | ||
} | ||
|
||
module.exports = parser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters