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

{ {
"title": "Spus",
"base_url": "http://example.com",
"webroot": "web", "webroot": "web",
"port": 8081, "port": 8081,
"db": { "db": {
"key": "", "key": "",
"cert": "" "cert": ""
}, },
"web": {
"title": "Spus",
"base_url": "http://example.com"
},
"scrypt": { "scrypt": {
"maxtime": 1 "maxtime": 1
}, },

+ 1
- 1
lib/includeHtml.js View File

var html = fs.readFileSync(path, "utf8"); var html = fs.readFileSync(path, "utf8");


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

+ 36
- 17
lib/preprocess.js View File

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


var localRegex = new RegExp(regexStr); var localRegex = new RegExp(regexStr);
var globalRegex = new RegExp(regexStr, "g"); 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) { module.exports = function(str, env) {
var s = parts[0]; var s = parts[0];


//Ternary //Ternary
if (parts[6]) {
if (parts[3]) {
try { 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) { } catch (err) {
return; return;
} }
//Direct value //Direct value
else { else {
try { try {
var val = getVal(parts[1], parts[2], env);
var val = getVal(parts[1], env);
} catch (err) { } catch (err) {
return; return;
} }

+ 1
- 1
server.js View File

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


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

+ 4
- 4
templates/head.html View File

<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width"> <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="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://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="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