-
-
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.
Merge pull request #6 from ulkajs/featureadd
async support in ulka and tests updated
- Loading branch information
Showing
11 changed files
with
133 additions
and
99 deletions.
There are no files selected for viewing
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 +1 @@ | ||
index | ||
about |
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,3 +1,3 @@ | ||
{% const page = "index" %} | ||
{% const page = "about" %} | ||
|
||
{% page %} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<html> | ||
<head> | ||
<title>Roshan Acharya</title> | ||
</head> | ||
|
||
<body> | ||
<div>Name: Roshan Acharya</div> | ||
<div>Age: 20</div> | ||
</body> | ||
</html> |
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
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
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,60 +1,66 @@ | ||
const vm = require('vm'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const { replaceString, hasEqualSign } = require('./utils'); | ||
const { replaceString, hasEqualSign, replaceAsync } = require('./utils'); | ||
|
||
const defaultOptions = { | ||
base: '', | ||
}; | ||
|
||
function parser(ulkaTemplate, values = {}, options = defaultOptions) { | ||
async function parser(ulkaTemplate, values = {}, options = defaultOptions) { | ||
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(reqPath); | ||
}, | ||
console, | ||
}; | ||
|
||
// If first index is equal sign then remove equal or minus sign | ||
const containsEqualsInFirstIndex = jsCode[0] === '='; | ||
const containsMinusInFirstIndex = jsCode[0] === '-'; | ||
|
||
if (containsEqualsInFirstIndex || containsMinusInFirstIndex) | ||
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) && | ||
!containsMinusInFirstIndex; | ||
|
||
return !shouldPrintResult ? '' : result || ''; | ||
}) | ||
.trim(); | ||
return await replaceAsync( | ||
ulkaTemplate, | ||
/\\?{%(.*?)%}/gs, | ||
parseCallback(ulkaTemplate, values, options), | ||
); | ||
} catch (e) { | ||
console.log(`Ulka Praser Error: `, e.message); | ||
console.log(e.message); | ||
throw e; | ||
} | ||
} | ||
|
||
const parseCallback = (ulkaTemplate, values, options) => async (...args) => { | ||
let jsCode = args[1]; | ||
|
||
values = { | ||
require: reqPath => { | ||
const rPath = path.join(options.base, reqPath); | ||
if (fs.existsSync(rPath)) return require(rPath); | ||
return require(reqPath); | ||
}, | ||
...values, | ||
console, | ||
}; | ||
|
||
// If first index is equal sign then remove equal or minus sign | ||
const containsEqualsInFirstIndex = jsCode[0] === '='; | ||
const containsMinusInFirstIndex = jsCode[0] === '-'; | ||
|
||
if (containsEqualsInFirstIndex || containsMinusInFirstIndex) | ||
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) && | ||
!containsMinusInFirstIndex; | ||
|
||
const dataToReturn = await result; | ||
|
||
return !shouldPrintResult ? '' : dataToReturn || ''; | ||
}; | ||
|
||
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
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