mirror of
https://github.com/lucaspalomodevelop/JSSTE.git
synced 2026-03-12 23:17:22 +00:00
add examples | fix relativ-path import
This commit is contained in:
parent
112504233d
commit
f358a79a87
@ -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);
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
{
|
||||
"_STYLES_":["./style"],
|
||||
"js$Date": "return Date.now() + ' is the TimeStemp';"
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
{
|
||||
"_IMPORTS_":["data.jsste","base.jsste"],
|
||||
"_IMPORTS_":["data.jsste","lib/base.jsste"],
|
||||
"a":"h1 - test",
|
||||
"c":"p - test"
|
||||
}
|
||||
6
examples/pages/lib/base.jsste
Normal file
6
examples/pages/lib/base.jsste
Normal 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"
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
{
|
||||
"_STYLES_":["./style"],
|
||||
"_STYLES_":["base"],
|
||||
"_TEMPLATE_":"page1",
|
||||
"_TITLE_":"Page2.2",
|
||||
"root":"root",
|
||||
|
||||
5
examples/pages/page3/index.jsste
Normal file
5
examples/pages/page3/index.jsste
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"_TEMPLATE_":"page3",
|
||||
"Title":"Page3",
|
||||
"_IMPORTS_":["lib/base.jsste","./mybase.jsste"]
|
||||
}
|
||||
3
examples/pages/page3/mybase.jsste
Normal file
3
examples/pages/page3/mybase.jsste
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"js$Date": "return Date.now();"
|
||||
}
|
||||
4
examples/pages/test/a.jsste
Normal file
4
examples/pages/test/a.jsste
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"a":"Ameise",
|
||||
"_IMPORTS_":["./c.jsste","./b.jsste"]
|
||||
}
|
||||
4
examples/pages/test/b.jsste
Normal file
4
examples/pages/test/b.jsste
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"b":"braunbär",
|
||||
"_IMPORTS_":["./d.jsste","./e.jsste"]
|
||||
}
|
||||
4
examples/pages/test/c.jsste
Normal file
4
examples/pages/test/c.jsste
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"c":"cobra",
|
||||
"_IMPORTS_":["./f.jsste","./g.jsste"]
|
||||
}
|
||||
3
examples/pages/test/d.jsste
Normal file
3
examples/pages/test/d.jsste
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"d":"delfin"
|
||||
}
|
||||
3
examples/pages/test/e.jsste
Normal file
3
examples/pages/test/e.jsste
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"e":"esel"
|
||||
}
|
||||
3
examples/pages/test/f.jsste
Normal file
3
examples/pages/test/f.jsste
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"f":"fau"
|
||||
}
|
||||
3
examples/pages/test/g.jsste
Normal file
3
examples/pages/test/g.jsste
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"g":"giraffe"
|
||||
}
|
||||
4
examples/pages/test/index.jsste
Normal file
4
examples/pages/test/index.jsste
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"_TEMPLATE_": "example",
|
||||
"_IMPORTS_":["./a.jsste","lib/base.jsste"]
|
||||
}
|
||||
36
examples/templates/example.tjsste
Normal file
36
examples/templates/example.tjsste
Normal 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>
|
||||
@ -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>
|
||||
|
||||
9
examples/templates/page3.tjsste
Normal file
9
examples/templates/page3.tjsste
Normal file
@ -0,0 +1,9 @@
|
||||
|
||||
<html>
|
||||
<head>
|
||||
<title><[Title]></title>
|
||||
</head>
|
||||
<body>
|
||||
<[js$Date]>
|
||||
</body>
|
||||
</html>
|
||||
@ -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;
|
||||
|
||||
58
src/index.js
58
src/index.js
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user