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')
17 17
   , imageHandler = require('./image.js')
18 18
   , tempurl = require('./tempurl.js')
19 19
   , util = require('./util.js')
20
-  , tagCloud = require('./cloudgen.js');
20
+  , tagCloud = require('./cloudgen.js')
21
+  , template = require('./template.js');
21 22
 
22 23
 _.str = require('underscore.string');
23 24
 _.mixin(_.str.exports());
@@ -45,8 +46,7 @@ function error(req,res,text,codeO) {
45 46
 function parse(req,res,next) { if(req.params[0]) req.params = req.params[0].split("/"); else req.params = [""]; next(); }
46 47
 
47 48
 // Configure
48
-var config = fs.existsSync("./config.json") ? require('./config.json') : defaultConfig
49
-  , templates = {};
49
+var config = fs.existsSync("./config.json") ? require('./config.json') : defaultConfig;
50 50
 
51 51
 config = _.defaults(config,defaultConfig);
52 52
 if(!_(config).contains("htmlTitle")) {
@@ -54,12 +54,6 @@ if(!_(config).contains("htmlTitle")) {
54 54
   else config.htmlTitle = config.title;
55 55
 }
56 56
 
57
-_.each(fs.readdirSync("templates/"),function(filename) {
58
-  var name = filename.split(".")[0];
59
-  console.log("[Template] Loading template "+name);
60
-  templates[name] = fs.readFileSync("./templates/"+filename,"utf8");    
61
-});
62
-
63 57
 imageHandler.express(express,app);
64 58
 
65 59
 // Image Adding
@@ -92,16 +86,16 @@ function addImage(rawdata,format,info,callback,thumbnailsrc,grav) {
92 86
 function makeRawTemplate(name,conf,noHeader) {
93 87
   try {
94 88
     var conf2 = _.defaults(conf,config,defaultSiteConfig,defaultConfig,templateFunctions);
95
-    var body = _.template(templates[name],conf2);
89
+    var body = template.execute(name, conf2);
96 90
     if(!noHeader)
97
-      body = _.template(templates["header"],conf2) + body;
91
+      body = template.execute("header", conf2) + body;
98 92
     return body;
99 93
   } catch(e) { var s = "RawTemplate error: " + e.message + " ["+name+"]"; console.trace(s); return s; }
100 94
 }
101 95
 function makeTemplate(name,conf2,raw,noHeader) {
102 96
   if(raw=="raw") return makeRawTemplate(name,conf2,noHeader);
103 97
   var conf = _.defaults(conf2,config,defaultSiteConfig,defaultConfig,templateFunctions);
104
-  return _.template(templates["main"],_.defaults(conf,{page: makeRawTemplate(name,conf,noHeader)}));
98
+  return template.execute("main", _.defaults(conf,{page: makeRawTemplate(name,conf,noHeader)}));
105 99
 }
106 100
 
107 101
 // ** LOGIN HANDLER **

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

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

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

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

+ 31
- 0
template.js View File

@@ -0,0 +1,31 @@
1
+var _ = require("underscore")
2
+  , fs = require("fs")
3
+  , templates = {}
4
+  , skins = {}
5
+  , _skin = "default";
6
+
7
+_.each(fs.readdirSync("./skins/"), function(name) {
8
+  var prefix = "./skins/"+name+"/";
9
+  console.log("[Templater] Loading skin "+name);
10
+  skins[name] = JSON.parse(fs.readFileSync(prefix+"skin.json","utf8"));
11
+  skins[name].css = fs.readFileSync(prefix+"skin.css","utf8");
12
+});
13
+
14
+_.each(fs.readdirSync("./templates/"),function(filename) {
15
+  var name = filename.split(".")[0];
16
+  console.log("[Templater] Loading template "+name);
17
+  templates[name] = _.template(fs.readFileSync("./templates/"+filename,"utf8"));
18
+});
19
+
20
+var skin = function(name) {
21
+  if(_.isString(name))
22
+    return skins[name];
23
+  else _skin = name;
24
+}
25
+exports.skin = skin;
26
+exports.get = function(name) {
27
+  return templates[name];
28
+}
29
+exports.execute = function(name, config) {
30
+  return templates[name](_.defaults(config, {skin: skin(_skin)}));
31
+}

+ 2
- 9
templates/main.html View File

@@ -1,4 +1,3 @@
1
-
2 1
 <!DOCTYPE html>
3 2
 <html>
4 3
 <head>
@@ -6,22 +5,16 @@
6 5
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
7 6
 <link href="/static/css/bootstrap.min.css" rel="stylesheet" type="text/css">
8 7
 <link href="/static/css/mobile.css" rel="stylesheet" type="text/css" <% if(!mobile) print('media="handheld"'); %>>
9
-<style>
8
+<style type="text/css">
10 9
 a.social:hover { opacity: 0.7; }
11
-a.normal {
12
-  color: #000;
13
-  text-decoration: none;
14
-}
15 10
 .padded-row { margin-top: 20px; }
16
-body {
17
-  background-color: <%= style.bgcolor %>;
18
-}
19 11
 .thumbnail { background-color: #FFF; }
20 12
 .thumbnail-300 { width: 300px; height: 300px !important; }
21 13
 h1 { color: #222; }
22 14
 <% if(useLazyLoad) {
23 15
   print(".lazy { display: none; }");
24 16
 } %>
17
+<%= skin.css %>
25 18
 </style>
26 19
 </head>
27 20
 <body>

Loading…
Cancel
Save