Browse Source

factored away template engine, added basic skins

pull/2/head
asiekierka 6 years ago
parent
commit
066e9d23ce
5 changed files with 48 additions and 21 deletions
  1. 6
    12
      app.js
  2. 7
    0
      skins/default/skin.css
  3. 2
    0
      skins/default/skin.json
  4. 31
    0
      template.js
  5. 2
    9
      templates/main.html

+ 6
- 12
app.js View File

@@ -17,7 +17,8 @@ var express = require('express')
, imageHandler = require('./image.js')
, tempurl = require('./tempurl.js')
, util = require('./util.js')
, tagCloud = require('./cloudgen.js');
, tagCloud = require('./cloudgen.js')
, template = require('./template.js');

_.str = require('underscore.string');
_.mixin(_.str.exports());
@@ -45,8 +46,7 @@ function error(req,res,text,codeO) {
function parse(req,res,next) { if(req.params[0]) req.params = req.params[0].split("/"); else req.params = [""]; next(); }

// Configure
var config = fs.existsSync("./config.json") ? require('./config.json') : defaultConfig
, templates = {};
var config = fs.existsSync("./config.json") ? require('./config.json') : defaultConfig;

config = _.defaults(config,defaultConfig);
if(!_(config).contains("htmlTitle")) {
@@ -54,12 +54,6 @@ if(!_(config).contains("htmlTitle")) {
else config.htmlTitle = config.title;
}

_.each(fs.readdirSync("templates/"),function(filename) {
var name = filename.split(".")[0];
console.log("[Template] Loading template "+name);
templates[name] = fs.readFileSync("./templates/"+filename,"utf8");
});

imageHandler.express(express,app);

// Image Adding
@@ -92,16 +86,16 @@ function addImage(rawdata,format,info,callback,thumbnailsrc,grav) {
function makeRawTemplate(name,conf,noHeader) {
try {
var conf2 = _.defaults(conf,config,defaultSiteConfig,defaultConfig,templateFunctions);
var body = _.template(templates[name],conf2);
var body = template.execute(name, conf2);
if(!noHeader)
body = _.template(templates["header"],conf2) + body;
body = template.execute("header", conf2) + body;
return body;
} catch(e) { var s = "RawTemplate error: " + e.message + " ["+name+"]"; console.trace(s); return s; }
}
function makeTemplate(name,conf2,raw,noHeader) {
if(raw=="raw") return makeRawTemplate(name,conf2,noHeader);
var conf = _.defaults(conf2,config,defaultSiteConfig,defaultConfig,templateFunctions);
return _.template(templates["main"],_.defaults(conf,{page: makeRawTemplate(name,conf,noHeader)}));
return template.execute("main", _.defaults(conf,{page: makeRawTemplate(name,conf,noHeader)}));
}

// ** LOGIN HANDLER **

+ 7
- 0
skins/default/skin.css View File

@@ -0,0 +1,7 @@
a.normal {
color: #000;
text-decoration: none;
}
body {
background-color: #FEF5E9;
}

+ 2
- 0
skins/default/skin.json View File

@@ -0,0 +1,2 @@
{
}

+ 31
- 0
template.js View File

@@ -0,0 +1,31 @@
var _ = require("underscore")
, fs = require("fs")
, templates = {}
, skins = {}
, _skin = "default";

_.each(fs.readdirSync("./skins/"), function(name) {
var prefix = "./skins/"+name+"/";
console.log("[Templater] Loading skin "+name);
skins[name] = JSON.parse(fs.readFileSync(prefix+"skin.json","utf8"));
skins[name].css = fs.readFileSync(prefix+"skin.css","utf8");
});

_.each(fs.readdirSync("./templates/"),function(filename) {
var name = filename.split(".")[0];
console.log("[Templater] Loading template "+name);
templates[name] = _.template(fs.readFileSync("./templates/"+filename,"utf8"));
});

var skin = function(name) {
if(_.isString(name))
return skins[name];
else _skin = name;
}
exports.skin = skin;
exports.get = function(name) {
return templates[name];
}
exports.execute = function(name, config) {
return templates[name](_.defaults(config, {skin: skin(_skin)}));
}

+ 2
- 9
templates/main.html View File

@@ -1,4 +1,3 @@

<!DOCTYPE html>
<html>
<head>
@@ -6,22 +5,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/static/css/bootstrap.min.css" rel="stylesheet" type="text/css">
<link href="/static/css/mobile.css" rel="stylesheet" type="text/css" <% if(!mobile) print('media="handheld"'); %>>
<style>
<style type="text/css">
a.social:hover { opacity: 0.7; }
a.normal {
color: #000;
text-decoration: none;
}
.padded-row { margin-top: 20px; }
body {
background-color: <%= style.bgcolor %>;
}
.thumbnail { background-color: #FFF; }
.thumbnail-300 { width: 300px; height: 300px !important; }
h1 { color: #222; }
<% if(useLazyLoad) {
print(".lazy { display: none; }");
} %>
<%= skin.css %>
</style>
</head>
<body>

Loading…
Cancel
Save