Browse Source

nice changes to preprocessor

master
mort 8 years ago
parent
commit
48df0e12bb
5 changed files with 44 additions and 27 deletions
  1. 2
    4
      conf.json.example
  2. 1
    1
      lib/includeHtml.js
  3. 36
    17
      lib/preprocess.js
  4. 1
    1
      server.js
  5. 4
    4
      templates/head.html

+ 2
- 4
conf.json.example View File

@@ -1,4 +1,6 @@
{
"title": "Spus",
"base_url": "http://example.com",
"webroot": "web",
"port": 8081,
"db": {
@@ -12,10 +14,6 @@
"key": "",
"cert": ""
},
"web": {
"title": "Spus",
"base_url": "http://example.com"
},
"scrypt": {
"maxtime": 1
},

+ 1
- 1
lib/includeHtml.js View File

@@ -8,7 +8,7 @@ module.exports = function load(path, conf) {
var html = fs.readFileSync(path, "utf8");

var env = {
conf: conf.web,
conf: conf,
template: function(key) {
var str = load("templates/"+key+".html", conf);
return str;

+ 36
- 17
lib/preprocess.js View File

@@ -1,28 +1,47 @@
var valueRegex = "([a-zA-Z0-9_\\-]+)#([a-zA-Z0-9_\\-]+)";
var vals = {
env: "[^\\s}]+#[^\\s}]+",
string: "\"[^\"}]+\""
}

//Get regex for all values
var valueRegex =
Object.keys(vals)
.map(function(i) { return vals[i]; })
.join("|");

//Turn value regex strings into actual regexes
Object.keys(vals).forEach(function(i) {
vals[i] = new RegExp(vals[i]);
});

var regexStr =
"{{"+ //{{
valueRegex+ //foo#bar - $1#$2
"("+valueRegex+")"+ //value - $1
"(?:"+ //<optional>
" \\? "+ //?
valueRegex+ //foo#bar - $3#$4
"("+valueRegex+")"+ //value - $2
" : "+ //:
valueRegex+ //foo#bar - $5#$6
"("+valueRegex+")"+ //value - $3
")?"+ //</optional>
"}}"; //}}

var localRegex = new RegExp(regexStr);
var globalRegex = new RegExp(regexStr, "g");

function getVal(ns, key, env) {
var n = env[ns];
function getVal(str, env) {
if (vals.env.test(str)) {
var parts = str.split("#");
var ns = env[parts[0]];

if (typeof n === "function")
return n(key);
else if (n)
return n[key];
else
throw new Error("Namespace "+ns+" doesn't exist.");
if (typeof ns === "function")
return ns(parts[1]);
else if (ns !== undefined)
return ns[parts[1]];
else
throw new Error("No: "+str);
} else if (vals.string.test(str)) {
return str.substring(1, str.length - 1);
}
}

module.exports = function(str, env) {
@@ -36,11 +55,11 @@ module.exports = function(str, env) {
var s = parts[0];

//Ternary
if (parts[6]) {
if (parts[3]) {
try {
var cond = getVal(parts[1], parts[2], env);
var val1 = getVal(parts[3], parts[4], env);
var val2 = getVal(parts[5], parts[6], env);
var cond = getVal(parts[1], env);
var val1 = getVal(parts[2], env);
var val2 = getVal(parts[3], env);
} catch (err) {
return;
}
@@ -54,7 +73,7 @@ module.exports = function(str, env) {
//Direct value
else {
try {
var val = getVal(parts[1], parts[2], env);
var val = getVal(parts[1], env);
} catch (err) {
return;
}

+ 1
- 1
server.js View File

@@ -70,7 +70,7 @@ try {
}
currentRun = (currentRun >= conf.max_runs ? 0 : currentRun);
currentRun = (currentRun || 0) + 1;
conf.web.currentRun = currentRun.toString();
conf.current_run = currentRun.toString();
fs.writeFileSync(".currentRun", currentRun, "utf8");

var loaded = loader.load(endpoints, conf);

+ 4
- 4
templates/head.html View File

@@ -2,11 +2,11 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">

<link rel="stylesheet" href="/global.css?{{conf#currentRun}}">
<link rel="stylesheet" href="/global.css?{{conf#current_run}}">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="/{{env#view}}/style.css?{{conf#currentRun}}">
<link rel="stylesheet" href="/{{env#view}}/style.css?{{conf#current_run}}">

<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script src="/global.js?{{conf#currentRun}}"></script>
<script src="/{{env#view}}/script.js?{{conf#currentRun}}"></script>
<script src="/global.js?{{conf#current_run}}"></script>
<script src="/{{env#view}}/script.js?{{conf#current_run}}"></script>

Loading…
Cancel
Save