Compare commits

..

No commits in common. "main" and "1.2.0" have entirely different histories.
main ... 1.2.0

16 changed files with 12969 additions and 1222 deletions

View File

@ -1,37 +0,0 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
name: Node.js Package
on:
# push:
# tags:
# - "*"
release:
types: [created]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
- run: npm ci
- run: npm install
- run: npm test
publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
testfiles
planning
.vscode
built

View File

@ -72,58 +72,6 @@ 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 |
## Commandline
## Installation
```sh
npm install jsste -g
```
or
```sh
yarn install jsste -g
```
## excuting
```sh
jsste <argument>
```
| arguemnt | description | Example |
| :--------- | :--------------------------------------: | :-----: |
| -log | show jsste log | |
| -Jsconfig= | set jsste config as json | |
| -pageFile= | set pageFile path | |
| -page= | set pageFile as json | |
| -tempFile= | set tempFile path | |
| -temp= | set pageFile as code | |
| -info | show jsste.info | |
| -out | write rendered code into the commandling | |
#### Example
```javascript
@ -157,5 +105,3 @@ let result = Jsste.render(pagecode, templatecode);
// </html>
```
#### Examples on [https://github.com/lucaspalomodevelop/JSSTE-Examples](https://github.com/lucaspalomodevelop/JSSTE-Examples)

9889
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,11 @@
{
"name": "jsste",
"version": "1.3.3",
"description": "A Javascript Static Template Engine",
"version": "1.1.3",
"description": "A Javascript Static Template Emgine",
"main": "src/index.js",
"directories": {
"test": "test"
},
"bin": {
"jsste": "./src/cli.js"
},
"files": [
"src",
"package.json",
@ -18,7 +15,7 @@
"scripts": {
"test": "mocha ./test/test.test.js",
"publish": "npm publish --access publish",
"cmd": "node ./src/cli.js"
"compile": "tsc"
},
"repository": {
"type": "git",
@ -26,21 +23,30 @@
},
"keywords": [
"engine",
"templateengine",
"jsste"
"templateengine"
],
"author": "Lucas Palomo Lauterbach",
"author": "lucas palomo",
"license": "GPL",
"bugs": {
"url": "https://github.com/lucaspalomodevelop/JSSTE/issues"
},
"homepage": "https://github.com/lucaspalomodevelop/JSSTE#readme",
"dependencies": {},
"dependencies": {
"typechecker": "^7.18.0",
"yarn": "^1.22.19"
},
"engines": {
"node": ">= 0.10.0"
},
"devDependencies": {
"assert": "^2.0.0",
"chai": "^4.3.4",
"mocha": "^10.1.0"
"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"
}
}

View File

@ -1,99 +0,0 @@
#!/usr/bin/env node
let jsste = require("./index");
let myargs = process.argv.slice(2);
let output = "";
let pagefile, tempfile;
let path = require("path");
let fs = require("fs");
var currentPath = process.cwd();
jsste.__config.set("templatePath",currentPath)
jsste.__config.set("pagePath",currentPath)
/**
* this function filters the prefix and call the callback with the param
* @param {prefix, args } settings
* @param {*} callback
*/
let addCommand = ({ prefix, args = myargs }, callback) => {
myargs.forEach((elem) => {
if (elem.startsWith(prefix)) {
elem = elem.replace(prefix, "");
callback(elem);
}
});
};
/**
* -log | show jsste log
*/
addCommand({ prefix: "-log" }, (arg) => {
jsste.setStateFunction((state) => {
console.log(state);
});
});
/**
* -Jsconfig | set jsste config as json
*/
addCommand({ prefix: "-Jsconfig=" }, (arg) => {
jsste.__config.setConfig(arg);
});
/**
* -pageFile | set pageFile path
*/
addCommand({ prefix: "-pageFile=" }, (arg) => {
jsste.setState({ status: 4, statusMSG: currentPath })
pagefile = JSON.parse(fs.readFileSync(path.join(currentPath, arg), "utf8"));
pagefile["_SELFPATH_"] = path.dirname(arg);
});
/**
* -pageFile | set pageFile as json
*/
addCommand({ prefix: "-page=" }, (arg) => {
pagefile = JSON.parse(arg);
pagefile["_SELFPATH_"] = path.dirname(arg);
});
/**
* -tempFile | set tempFile path
*/
addCommand({ prefix: "-tempFile=" }, (arg) => {
tempfile = fs.readFileSync(arg, "utf8");
});
/**
* -pageFile | set pageFile as code
*/
addCommand({ prefix: "-temp=" }, (arg) => {
tempfile = arg;
});
/**
* rendering
*/
output = jsste.render(pagefile || undefined, tempfile || undefined);
/**
* -info | show jsste.info
*/
addCommand({ prefix: "-info" }, (arg) => {
if (arg == "?json") {
console.log(JSON.stringify(jsste.info));
} else {
Object.keys(jsste.info).forEach((key) => {
console.log(key + " : " + jsste.info[key]);
});
}
});
/**
* -out | write rendered code into the commandling
*/
addCommand({ prefix: "-out" }, (arg) => {
console.log(output);
});

View File

@ -6,27 +6,14 @@ 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,
@ -39,11 +26,6 @@ config.setConfig = function ({
configvar.stylesheets = stylesheets;
};
/**
* set config via key
* @param {*} key
* @param {*} value
*/
config.set = function (key, value) {
configvar[key] = value;
};

View File

@ -1,38 +1,22 @@
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
* @param {*} string
* @returns
*/
//function to escape regex
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]);
@ -41,55 +25,30 @@ app.CONST = function (pagecode, constant, callback) {
}
};
/**
* renders Pagecode in Templatecode
* @param {*} pagecode
* @param {*} templatecode
* @returns
*/
app.render = function (pagecode, templatecode) {
app.setState({ status: 0, statusMSG: "Render Page" });
//let result = "";
//if (!pagecode == JSON) pagecode = JSON.parse(pagecode);
if (
(pagecode != null || pagecode != undefined) &&
typeof pagecode === "string"
checker.isString(pagecode)
) {
pagecode = JSON.parse(pagecode);
app.setState({ status: 0, statusMSG: "Parse Pagecode" });
} else {
if (typeof pagecode === "object") {
app.setState({ status: 0, statusMSG: "Pagecode is an object" });
} else {
app.setState({ status: 1, statusMSG: "Pagecode is undefined" });
}
}
//TODO
if (!templatecode) {
try {
app.setState({ status: 0, statusMSG: "Load Templatecode" });
app.setState({
status: 4,
statusMSG: path.join(
app.config.templatePath,
pagecode["_TEMPLATE_"] + ".tjsste"
),
});
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) {
@ -98,10 +57,11 @@ 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";
}
@ -115,11 +75,12 @@ app.render = function (pagecode, templatecode) {
recursive(imports);
//console.log(ImportSet);
let currentPagecode = _pagecode;
ImportSet.forEach(function (importPath) {
app.setState({ status: 0, statusMSG: "Import Importfiles" });
console.log(importPath);
let importCodeString = fs.readFileSync(importPath, "utf-8");
let importCode = JSON.parse(importCodeString);
currentPagecode = jsonmerger.mergeJson(currentPagecode, importCode);
@ -127,12 +88,10 @@ app.render = function (pagecode, templatecode) {
pagecode = currentPagecode;
};
// Handle _IMPORTS_ const
//TODO Killed Root Import
app.CONST(pagecode, "_IMPORTS_", DissolveImports);
// Handle _STYLES_ const
//console.log(pagecode);
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>", "");
@ -141,28 +100,23 @@ 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),
""
@ -175,7 +129,6 @@ app.render = function (pagecode, templatecode) {
templatecode = templatecode.replace(new RegExp(/<\[\/\/]\>/g), "");
app.setState({ status: 0, statusMSG: "Return HTML" });
return templatecode;
};

View File

@ -6,57 +6,45 @@ var app = {};
app.render = engine.render;
app.CONST = engine.CONST;
let pj = require("../package.json");
app.info = {};
app.info["version"] = pj.version;
app.info["license"] = pj.license;
/**
* Render File
* @param {*} filePath
* @returns
*/
app.renderFile = (filePath, templatePath) => {
app.setState({ status: 0, statusMSG: "read file" });
app.renderFile = (filePath) => {
let file = fs.readFileSync(filePath, "utf8");
app.setState({ status: 0, statusMSG: "parse file" });
file = JSON.parse(file);
app.setState({ status: 0, statusMSG: "set Selfpath" });
file["_SELFPATH_"] = path.dirname(filePath);
if (!(templatePath == undefined)) {
app.setState({ status: 0, statusMSG: "read template" });
let temp = fs.readFileSync(templatePath, "utf8");
return engine.render(file, temp);
} else {
return engine.render(file);
}
return engine.render(file);
};
/**
* 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;
app.setState({ status: 0, statusMSG: "set Statefunction" });
app.setState({ status: 0, statusMSG: "JSSTE is ready" });
};
// engine.config = app.config;
app.__config = require("./config");
app.config = app.__config.getConfig();
engine.log = app.log;
engine.setState = app.setState;
engine.config = app.config;
// 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;

View File

@ -1,15 +1,12 @@
/**
* merge two JSONs
* @param {*} org
* @param {*} ext
* @returns
*/
function mergeJson(org, ext) {
const checker = require("typechecker");
if (!(typeof org === 'object' && org !== null)) {
function mergeJson(org, ext) {
// let placeholder = undefined;
if (!checker.isObject(org)) {
org = JSON.parse(org);
}
if (!(typeof ext === 'object' && ext !== null)) {
if (!checker.isObject(ext)) {
ext = JSON.parse(ext);
}
@ -33,12 +30,6 @@ 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);

View File

@ -1,18 +1,5 @@
/**
* that functions defines functions to use it in JSSTE JS var (js$varname)
*/
function Added() {
let outString = "";
//[jsste_info]
function jsste_info()
{
let jssteinfo = jsste_input_file
result = `\tINFOS\n\t${Object.keys(jssteinfo).map((key) => key + " : " + jssteinfo[key] ).join("\n\t") }`
return result;
}
function out(arg) {
outString += arg;
@ -21,13 +8,11 @@ function Added() {
function outLine(arg) {
outString += "\n" + arg;
}
}
/**
* exectue JSSTE JS var (js$varname)
* @param {*} script
* @returns
*/
function exec(script) {
let result = undefined;
@ -36,8 +21,6 @@ function exec(script) {
script
);
script = script.replace("//[jsste_info]",`let jsste_input_file = ${JSON.stringify(require("./index").info)}`),
script += "\n return outString;";
try {

View File

@ -1,11 +1,9 @@
let core_render = require("./core.render.test");
let core_scriptexec = require("./scriptexec.jsste.test");
let core_includeJSSTE = require("./includeJSSTE.test");
let core_state = require("./core.state.test");
function test() {
describe("JSSTE CORE", function () {
core_state();
core_render();
core_scriptexec();
core_includeJSSTE();

View File

@ -38,27 +38,27 @@ function test() {
result.should.equal("EXAMPLEEXAMPLE");
});
// 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.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("should not rendern _VAR_", function () {
let template = "<[_VAR_]>";

View File

@ -1,17 +0,0 @@
let libfolder = "../src/";
let JSSTE_Engine = require(libfolder);
const { assert } = require("chai");
function test() {
describe("test State", function () {
it(" set stateFunction", function () {
JSSTE_Engine.setStateFunction(({ status, statusMSG }) => {});
assert.typeOf(JSSTE_Engine.setStateFunction, "function");
});
it(" is setState a function", function () {
assert.typeOf(JSSTE_Engine.setState,"function")
});
});
}
module.exports = test;

View File

@ -1,5 +1,4 @@
require("chai").should();
const {assert} = require("chai");
let test_jsonMerger = require("./jsonMerger.test");
let test_core = require("./core.jsste.test");

3798
yarn.lock

File diff suppressed because it is too large Load Diff