add examples | fix relativ-path import

This commit is contained in:
lucaspalomodevelop 2021-11-15 19:26:33 +01:00
parent 112504233d
commit f358a79a87
21 changed files with 171 additions and 45 deletions

View File

@ -22,9 +22,28 @@ let fs = require("fs");
// // res.send("File Not found");
// });
let folders = {
jsste: "pages",
css: "styles",
};
function getFolderFromFileEnding(filename) {
let regex_isAnDotfile = /\w+\.[a-z]*[A-Z]*/;
if (regex_isAnDotfile.test(filename)) {
let ending = filename.split(".").pop();
return folders[ending];
}
return folders.jsste;
}
function defaultUse(req, res, next) {
let regex_isAnDotfile = /\w+\.[a-z]*[A-Z]*/;
let filePath = path.join(__dirname, "pages", req.url);
let filePath = path.join(
__dirname,
getFolderFromFileEnding(req.url),
req.url
);
if (regex_isAnDotfile.test(req.url) && !filePath.endsWith(".jsste")) {
res.sendFile(filePath);

View File

@ -1,4 +0,0 @@
{
"_STYLES_":["./style"],
"js$Date": "return Date.now() + ' is the TimeStemp';"
}

View File

@ -1,5 +1,5 @@
{
"_IMPORTS_":["data.jsste","base.jsste"],
"_IMPORTS_":["data.jsste","lib/base.jsste"],
"a":"h1 - test",
"c":"p - test"
}

View File

@ -0,0 +1,6 @@
{
"_STYLES_":["base"],
"js$Date": "return Date.now() + ' is the TimeStemp';",
"js$uxTime":"Date.Now();",
"js$TimeStemp":"const heute = new Date(); return heute"
}

View File

@ -1,5 +1,5 @@
{
"_STYLES_":["./style"],
"_STYLES_":["base"],
"_TEMPLATE_":"page1",
"_TITLE_":"Page2.2",
"root":"root",

View File

@ -0,0 +1,5 @@
{
"_TEMPLATE_":"page3",
"Title":"Page3",
"_IMPORTS_":["lib/base.jsste","./mybase.jsste"]
}

View File

@ -0,0 +1,3 @@
{
"js$Date": "return Date.now();"
}

View File

@ -0,0 +1,4 @@
{
"a":"Ameise",
"_IMPORTS_":["./c.jsste","./b.jsste"]
}

View File

@ -0,0 +1,4 @@
{
"b":"braunbär",
"_IMPORTS_":["./d.jsste","./e.jsste"]
}

View File

@ -0,0 +1,4 @@
{
"c":"cobra",
"_IMPORTS_":["./f.jsste","./g.jsste"]
}

View File

@ -0,0 +1,3 @@
{
"d":"delfin"
}

View File

@ -0,0 +1,3 @@
{
"e":"esel"
}

View File

@ -0,0 +1,3 @@
{
"f":"fau"
}

View File

@ -0,0 +1,3 @@
{
"g":"giraffe"
}

View File

@ -0,0 +1,4 @@
{
"_TEMPLATE_": "example",
"_IMPORTS_":["./a.jsste","lib/base.jsste"]
}

View File

@ -0,0 +1,36 @@
<html>
<head>
<title>Template Example</title>
</head>
<body>
<ul>
<li>
<p>timestemp: <[js$Date]></p>
</li>
<li>
<p>path: <[_SELFPATH_]></p>
</li>
<li>
<p>a: <[a]></p>
</li>
<li>
<p>b: <[b]></p>
</li>
<li>
<p>c: <[c]></p>
</li>
<li>
<p>d: <[d]></p>
</li>
<li>
<p>e: <[e]></p>
</li>
<li>
<p>f: <[f]></p>
</li>
<li>
<p>g: <[g]></p>
</li>
</ul>
</body>
</html>

View File

@ -11,6 +11,7 @@
<p><[c]></p>
<p><[b]></p>
<p><[js$Date]></p>
<p><[js$TimeStemp]></p>
<p> root: <[root]></p>
<p><[Base_Date]></p>
<script>

View File

@ -0,0 +1,9 @@
<html>
<head>
<title><[Title]></title>
</head>
<body>
<[js$Date]>
</body>
</html>

View File

@ -37,10 +37,14 @@ app.render = function (pagecode, templatecode) {
//TODO
if (!templatecode) {
templatecode = fs.readFileSync(
path.join(app.config.templatePath, pagecode["_TEMPLATE_"] + ".tjsste"),
"utf-8"
);
try {
templatecode = fs.readFileSync(
path.join(app.config.templatePath, pagecode["_TEMPLATE_"] + ".tjsste"),
"utf-8"
);
} catch (error) {
return 404;
}
}
let DissolveImports = function (_pagecode, imports) {
@ -48,10 +52,17 @@ app.render = function (pagecode, templatecode) {
let recursive = function (importNames) {
importNames.forEach(function (importName) {
let importCodeString = "";
let importPath = importName.startsWith(".")
? path.join(importName)
? path.join(_pagecode["_SELFPATH_"].toString(), importName.toString())
: path.join(app.config.pagePath, importName);
let importCodeString = fs.readFileSync(importPath, "utf-8");
console.log(importPath);
console.log(_pagecode);
try {
importCodeString = fs.readFileSync(importPath, "utf-8");
} catch (error) {
return "Ups... Import File Failed";
}
let importCode = JSON.parse(importCodeString);
if (importCode["_IMPORTS_"] !== undefined) {
@ -63,22 +74,22 @@ app.render = function (pagecode, templatecode) {
recursive(imports);
console.log(ImportSet);
//console.log(ImportSet);
let currentPagecode = _pagecode;
ImportSet.forEach(function (importPath) {
console.log(importPath);
let importCodeString = fs.readFileSync(importPath, "utf-8");
let importCode = JSON.parse(importCodeString);
currentPagecode = jsonmerger.mergeJson(currentPagecode, importCode);
});
pagecode = currentPagecode;
};
//TODO Killed Root Import
app.CONST(pagecode, "_IMPORTS_", DissolveImports);
console.log(pagecode);
//console.log(pagecode);
app.CONST(pagecode, "_STYLES_", (pagecode, value) => {
let rex = /<head>(.|\n|\t|\r)*?<\/head>/;
let header = templatecode.match(rex);
@ -104,7 +115,17 @@ app.render = function (pagecode, templatecode) {
value = pagecode[i].toString();
templatecode = replaceAll(templatecode, "<[" + i + "]>", value);
}
return templatecode.replace(new RegExp(/<\[([A-Z]*|[a-z]*)\w*\]>/g), "");
templatecode = templatecode.replace(
new RegExp(/<\[([A-Z]*|[a-z]*)\w*\]>/g),
""
);
templatecode = templatecode.replace(
new RegExp(/<\[([A-Z]*|[a-z]*)\$([A-Z]*|[a-z]*)\w*\]>/g),
""
);
return templatecode;
};
module.exports = app;

View File

@ -8,6 +8,8 @@ app.render = engine.render;
app.CONST = engine.CONST;
app.renderFile = (filePath) => {
let file = fs.readFileSync(filePath, "utf8");
file = JSON.parse(file);
file["_SELFPATH_"] = path.dirname(filePath);
return engine.render(file);
};
// engine.config = app.config;
@ -15,34 +17,34 @@ app.__config = require("./config");
app.config = app.__config.getConfig();
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);
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);
// });
// };
// };
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;