mirror of
https://github.com/lucaspalomodevelop/JSSTE.git
synced 2026-03-12 23:17:22 +00:00
commit
6cdef37326
21
README.md
21
README.md
@ -72,6 +72,27 @@ let result = Jsste.render(pagecode, templatecode);
|
||||
- `_TEMPLATE_` -> Defines the path to the temp file
|
||||
- `_STYLES_` -> Defines a list of CSS files that will be implemented
|
||||
|
||||
### Include States
|
||||
|
||||
You can include an Callbackfunction that will be called when the state is change
|
||||
|
||||
```javascript
|
||||
jsste.setStateFunction(({ status, statusMSG }) => {
|
||||
yourstatus = status;
|
||||
yourstatusMSG = statusMSG;
|
||||
});
|
||||
```
|
||||
|
||||
### States-Codes
|
||||
|
||||
| Code | Meaning |
|
||||
| :--- | :-----: |
|
||||
| 0 | OK |
|
||||
| 1 | ERROR |
|
||||
| 2 | WARNING |
|
||||
| 3 | INFO |
|
||||
| 4 | DEBUG |
|
||||
|
||||
#### Example
|
||||
|
||||
```javascript
|
||||
|
||||
21
package.json
21
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "jsste",
|
||||
"version": "1.2.0",
|
||||
"description": "A Javascript Static Template Emgine",
|
||||
"version": "1.2.1",
|
||||
"description": "A Javascript Static Template Engine",
|
||||
"main": "src/index.js",
|
||||
"directories": {
|
||||
"test": "test"
|
||||
@ -14,8 +14,7 @@
|
||||
],
|
||||
"scripts": {
|
||||
"test": "mocha ./test/test.test.js",
|
||||
"publish": "npm publish --access publish",
|
||||
"compile": "tsc"
|
||||
"publish": "npm publish --access publish"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -23,16 +22,16 @@
|
||||
},
|
||||
"keywords": [
|
||||
"engine",
|
||||
"templateengine"
|
||||
"templateengine",
|
||||
"jsste"
|
||||
],
|
||||
"author": "lucas palomo",
|
||||
"author": "Lucas Palomo Lauterbach",
|
||||
"license": "GPL",
|
||||
"bugs": {
|
||||
"url": "https://github.com/lucaspalomodevelop/JSSTE/issues"
|
||||
},
|
||||
"homepage": "https://github.com/lucaspalomodevelop/JSSTE#readme",
|
||||
"dependencies": {
|
||||
"typechecker": "^7.18.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.10.0"
|
||||
@ -40,12 +39,6 @@
|
||||
"devDependencies": {
|
||||
"assert": "^2.0.0",
|
||||
"chai": "^4.3.4",
|
||||
"express": "^4.17.1",
|
||||
"gulp": "^4.0.2",
|
||||
"gulp-minify": "^3.1.0",
|
||||
"gulp-typescript": "^6.0.0-alpha.1",
|
||||
"mocha": "^8.4.0",
|
||||
"typescript": "^4.5.4",
|
||||
"urldecode": "^1.0.1"
|
||||
"mocha": "^8.4.0"
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,14 +6,27 @@ let configvar = {
|
||||
stylesheets: __dirname + "\\src\\styles",
|
||||
};
|
||||
|
||||
/**
|
||||
* get config var by key
|
||||
* @param {*} key
|
||||
* @returns
|
||||
*/
|
||||
config.get = function (key) {
|
||||
return configvar[key];
|
||||
};
|
||||
|
||||
/**
|
||||
* get full config
|
||||
* @returns
|
||||
*/
|
||||
config.getConfig = function () {
|
||||
return configvar;
|
||||
};
|
||||
|
||||
/**
|
||||
* set full config
|
||||
* @param {*} param0
|
||||
*/
|
||||
config.setConfig = function ({
|
||||
templatePath,
|
||||
pagePath,
|
||||
@ -26,6 +39,11 @@ config.setConfig = function ({
|
||||
configvar.stylesheets = stylesheets;
|
||||
};
|
||||
|
||||
/**
|
||||
* set config via key
|
||||
* @param {*} key
|
||||
* @param {*} value
|
||||
*/
|
||||
config.set = function (key, value) {
|
||||
configvar[key] = value;
|
||||
};
|
||||
|
||||
@ -1,22 +1,38 @@
|
||||
const fs = require("fs");
|
||||
const checker = require("typechecker");
|
||||
const path = require("path");
|
||||
const jsonmerger = require("./jsonMerger");
|
||||
//let appdir = path.join(__dirname, "..");
|
||||
let app = {};
|
||||
|
||||
app.__config = require("./config");
|
||||
app.config = app.__config.getConfig();
|
||||
|
||||
//function to escape regex
|
||||
/**
|
||||
* function to escape regex
|
||||
* @param {*} string
|
||||
* @returns
|
||||
*/
|
||||
function escapeRegExp(string) {
|
||||
return string.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
/**
|
||||
* replace all placeholder in a string
|
||||
* @param {*} str
|
||||
* @param {*} find
|
||||
* @param {*} replace
|
||||
* @returns
|
||||
*/
|
||||
function replaceAll(str, find, replace) {
|
||||
return str.replace(new RegExp(escapeRegExp(find), "g"), replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* function to handle consts
|
||||
* @param {*} pagecode
|
||||
* @param {*} constant
|
||||
* @param {*} callback
|
||||
* @returns
|
||||
*/
|
||||
app.CONST = function (pagecode, constant, callback) {
|
||||
if (pagecode[constant] !== undefined) {
|
||||
callback(pagecode, pagecode[constant]);
|
||||
@ -25,30 +41,44 @@ app.CONST = function (pagecode, constant, callback) {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* renders Pagecode in Templatecode
|
||||
* @param {*} pagecode
|
||||
* @param {*} templatecode
|
||||
* @returns
|
||||
*/
|
||||
app.render = function (pagecode, templatecode) {
|
||||
//let result = "";
|
||||
|
||||
//if (!pagecode == JSON) pagecode = JSON.parse(pagecode);
|
||||
app.setState({ status: 0, statusMSG: "Render Page" });
|
||||
if (
|
||||
(pagecode != null || pagecode != undefined) &&
|
||||
checker.isString(pagecode)
|
||||
typeof pagecode === "string"
|
||||
) {
|
||||
pagecode = JSON.parse(pagecode);
|
||||
app.setState({ status: 0, statusMSG: "Parse Pagecode" });
|
||||
} else {
|
||||
app.setState({ status: 1, statusMSG: "Pagecode is undefined" });
|
||||
}
|
||||
|
||||
//TODO
|
||||
if (!templatecode) {
|
||||
try {
|
||||
app.setState({ status: 0, statusMSG: "Load Templatecode" });
|
||||
templatecode = fs.readFileSync(
|
||||
path.join(app.config.templatePath, pagecode["_TEMPLATE_"] + ".tjsste"),
|
||||
"utf-8"
|
||||
);
|
||||
} catch (error) {
|
||||
app.setState({ status: 1, statusMSG: "Cant load Templatecode" });
|
||||
return 404;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dissolve Fileimports
|
||||
* @param {*} _pagecode
|
||||
* @param {*} imports
|
||||
*/
|
||||
let DissolveImports = function (_pagecode, imports) {
|
||||
app.setState({ status: 0, statusMSG: "Dissolve Imports" });
|
||||
let ImportSet = new Set();
|
||||
|
||||
let recursive = function (importNames) {
|
||||
@ -57,11 +87,10 @@ app.render = function (pagecode, templatecode) {
|
||||
let importPath = importName.startsWith(".")
|
||||
? path.join(_pagecode["_SELFPATH_"].toString(), importName.toString())
|
||||
: path.join(app.config.pagePath, importName);
|
||||
console.log(importPath);
|
||||
console.log(_pagecode);
|
||||
try {
|
||||
importCodeString = fs.readFileSync(importPath, "utf-8");
|
||||
} catch (error) {
|
||||
app.setState({ status: 1, statusMSG: "Import File Failed" });
|
||||
return "Ups... Import File Failed";
|
||||
}
|
||||
|
||||
@ -75,12 +104,11 @@ app.render = function (pagecode, templatecode) {
|
||||
|
||||
recursive(imports);
|
||||
|
||||
//console.log(ImportSet);
|
||||
|
||||
let currentPagecode = _pagecode;
|
||||
|
||||
ImportSet.forEach(function (importPath) {
|
||||
console.log(importPath);
|
||||
app.setState({ status: 0, statusMSG: "Import Importfiles" });
|
||||
|
||||
let importCodeString = fs.readFileSync(importPath, "utf-8");
|
||||
let importCode = JSON.parse(importCodeString);
|
||||
currentPagecode = jsonmerger.mergeJson(currentPagecode, importCode);
|
||||
@ -88,10 +116,12 @@ app.render = function (pagecode, templatecode) {
|
||||
pagecode = currentPagecode;
|
||||
};
|
||||
|
||||
//TODO Killed Root Import
|
||||
// Handle _IMPORTS_ const
|
||||
app.CONST(pagecode, "_IMPORTS_", DissolveImports);
|
||||
//console.log(pagecode);
|
||||
|
||||
// Handle _STYLES_ const
|
||||
app.CONST(pagecode, "_STYLES_", (pagecode, value) => {
|
||||
app.setState({ status: 0, statusMSG: "Import Styles" });
|
||||
let rex = /<head>(.|\n|\t|\r)*?<\/head>/;
|
||||
let header = templatecode.match(rex);
|
||||
header = header[0].replace("</head>", "");
|
||||
@ -100,23 +130,28 @@ app.render = function (pagecode, templatecode) {
|
||||
});
|
||||
|
||||
header += "\n</head>";
|
||||
// console.log(header);
|
||||
|
||||
templatecode = templatecode.replace(/<head>(.|\n|\t|\r)*?<\/head>/, header);
|
||||
// replaceAll(templatecode,rex,value)
|
||||
});
|
||||
|
||||
app.setState({ status: 0, statusMSG: "Set vars" });
|
||||
for (let i in pagecode) {
|
||||
app.setState({ status: 0, statusMSG: "Set " + pagecode[i] });
|
||||
let value = undefined;
|
||||
|
||||
if (new RegExp(/\d*_([A-Z]*|[a-z])\w*_/g).test(i)) continue;
|
||||
if (new RegExp(/\/\//g).test(i)) continue;
|
||||
if (new RegExp(/js\$([A-Z]*|[a-z]*)\w+/g).test(i)) {
|
||||
app.setState({ status: 0, statusMSG: "Execute Serverside Script" });
|
||||
let SE = require("./scriptExecuter");
|
||||
pagecode[i] = SE(pagecode[i]);
|
||||
}
|
||||
value = pagecode[i].toString();
|
||||
templatecode = replaceAll(templatecode, "<[" + i + "]>", value);
|
||||
}
|
||||
|
||||
app.setState({ status: 0, statusMSG: "Delete unused Placeholder" });
|
||||
|
||||
templatecode = templatecode.replace(
|
||||
new RegExp(/<\[([A-Z]*|[a-z]*)\w*\]>/g),
|
||||
""
|
||||
@ -129,6 +164,7 @@ app.render = function (pagecode, templatecode) {
|
||||
|
||||
templatecode = templatecode.replace(new RegExp(/<\[\/\/]\>/g), "");
|
||||
|
||||
app.setState({ status: 0, statusMSG: "Return HTML" });
|
||||
return templatecode;
|
||||
};
|
||||
|
||||
|
||||
62
src/index.js
62
src/index.js
@ -6,45 +6,45 @@ var app = {};
|
||||
|
||||
app.render = engine.render;
|
||||
app.CONST = engine.CONST;
|
||||
/**
|
||||
* Render File
|
||||
* @param {*} filePath
|
||||
* @returns
|
||||
*/
|
||||
app.renderFile = (filePath) => {
|
||||
this.setState({ status: 0, statusMSG: "read file" });
|
||||
let file = fs.readFileSync(filePath, "utf8");
|
||||
this.setState({ status: 0, statusMSG: "parse file" });
|
||||
file = JSON.parse(file);
|
||||
this.setState({ status: 0, statusMSG: "set Selfpath" });
|
||||
file["_SELFPATH_"] = path.dirname(filePath);
|
||||
return engine.render(file);
|
||||
};
|
||||
// engine.config = app.config;
|
||||
|
||||
/**
|
||||
* set State
|
||||
* @param {*} param0
|
||||
*/
|
||||
app.setState = ({ status, statusMSG }) => {
|
||||
if (app.stateCallback != undefined) {
|
||||
app.stateCallback({ status, statusMSG });
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Set function that would called by new state
|
||||
* @param {*} callback
|
||||
*/
|
||||
app.setStateFunction = function (callback) {
|
||||
app.stateCallback = callback;
|
||||
this.setState({ status: 0, statusMSG: "set Statefunction" });
|
||||
this.setState({ status: 0, statusMSG: "JSSTE is ready" });
|
||||
};
|
||||
|
||||
app.__config = require("./config");
|
||||
app.config = app.__config.getConfig();
|
||||
engine.config = app.config;
|
||||
engine.log = app.log;
|
||||
engine.setState = app.setState;
|
||||
|
||||
// app.expressEngine = (
|
||||
// config = {
|
||||
// templatePath: "templates",
|
||||
// pagePath: "pages",
|
||||
// }
|
||||
// ) => {
|
||||
// config = mergeJson.mergeJson(app.config, config);
|
||||
// let hasrendered = false;
|
||||
// return (filePath, options, callback) => {
|
||||
// if (!hasrendered) {
|
||||
// app.config.templatePath = path.join(
|
||||
// options.settings.views,
|
||||
// config.templatePath
|
||||
// );
|
||||
// app.config.pagePath = path.join(options.settings.views, config.pagePath);
|
||||
// hasrendered = true;
|
||||
// }
|
||||
// // define the template engine
|
||||
// fs.readFile(filePath, function (err, content) {
|
||||
// content = content.toString();
|
||||
// // content = mergeJson.mergeJson(JSON.parse(content), options);
|
||||
|
||||
// if (err) return callback(new Error(err));
|
||||
// // this is an extremely simple template engine
|
||||
// var rendered = app.render(content);
|
||||
// return callback(null, rendered);
|
||||
// });
|
||||
// };
|
||||
// };
|
||||
|
||||
module.exports = app;
|
||||
|
||||
@ -1,12 +1,15 @@
|
||||
const checker = require("typechecker");
|
||||
|
||||
/**
|
||||
* merge two JSONs
|
||||
* @param {*} org
|
||||
* @param {*} ext
|
||||
* @returns
|
||||
*/
|
||||
function mergeJson(org, ext) {
|
||||
// let placeholder = undefined;
|
||||
|
||||
if (!checker.isObject(org)) {
|
||||
if (!(typeof org === 'object' && org !== null)) {
|
||||
org = JSON.parse(org);
|
||||
}
|
||||
if (!checker.isObject(ext)) {
|
||||
if (!(typeof ext === 'object' && ext !== null)) {
|
||||
ext = JSON.parse(ext);
|
||||
}
|
||||
|
||||
@ -30,6 +33,12 @@ function mergeJson(org, ext) {
|
||||
return org;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merge multiple JSONs
|
||||
* @param {*} org
|
||||
* @param {...any} ext
|
||||
* @returns
|
||||
*/
|
||||
function mergeJsons(org, ...ext) {
|
||||
ext.forEach((element) => {
|
||||
org = mergeJson(org, element);
|
||||
|
||||
@ -1,3 +1,6 @@
|
||||
/**
|
||||
* that functions defines functions to use it in JSSTE JS var (js$varname)
|
||||
*/
|
||||
function Added() {
|
||||
let outString = "";
|
||||
|
||||
@ -8,11 +11,13 @@ function Added() {
|
||||
function outLine(arg) {
|
||||
outString += "\n" + arg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* exectue JSSTE JS var (js$varname)
|
||||
* @param {*} script
|
||||
* @returns
|
||||
*/
|
||||
function exec(script) {
|
||||
let result = undefined;
|
||||
|
||||
|
||||
@ -38,27 +38,27 @@ function test() {
|
||||
result.should.equal("EXAMPLEEXAMPLE");
|
||||
});
|
||||
|
||||
it.skip("blub", function () {
|
||||
let result = JSSTE_Engine.render(
|
||||
{
|
||||
js$test: `out('<h1>'+ Date.now().toString() + '</h1>')`,
|
||||
test: "hallo",
|
||||
js$test2: `test2`,
|
||||
_STYLES_: ["./test/style"],
|
||||
},
|
||||
`<html>
|
||||
<head>
|
||||
<title><[test]></title>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
</head>
|
||||
<body>
|
||||
<[js$test2]>
|
||||
</body>
|
||||
</html>`
|
||||
);
|
||||
console.log(result);
|
||||
});
|
||||
// it.only("blub", function () {
|
||||
// let result = JSSTE_Engine.render(
|
||||
// {
|
||||
// js$test: `out('<h1>'+ Date.now().toString() + '</h1>')`,
|
||||
// test: "hallo",
|
||||
// js$test2: ` retrun "test2"`,
|
||||
// _STYLES_: ["./test/style"],
|
||||
// },
|
||||
// `<html>
|
||||
// <head>
|
||||
// <title><[test]></title>
|
||||
// <meta charset="UTF-8">
|
||||
// <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
// </head>
|
||||
// <body>
|
||||
// <[js$test2]>
|
||||
// </body>
|
||||
// </html>`
|
||||
// );
|
||||
// console.log(result);
|
||||
// });
|
||||
|
||||
it("should not rendern _VAR_", function () {
|
||||
let template = "<[_VAR_]>";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user