Permalink
Browse files

massive refactoring

There is no longer  a single index page. Instead, the site-manager now
supports loading any index.html file kept in public directories.
  • Loading branch information...
1 parent 1d29d3b commit d740eb39b408fb542001c2e3e4328cbb571d7a31 Joachim Kainz committed May 12, 2012
View
@@ -5,4 +5,7 @@ sites/*
.idea
*~
*.log
-.idea
+.idea
+.project
+.buildpath
+.settings
@@ -1,11 +0,0 @@
-<div class="container"></div>
-<script id="layout-template" type="text/x-handlebars-template">
- <div class="header">
- <h1>{{header1}}</h1>
- <div class="main-menu"></div>
- </div>
- <div style="clear: both;"></div>
- <div class="page">Loading</div>
- <div style="clear: both;"></div>
- <div class="footer"></div>
-</script>
@@ -1,18 +0,0 @@
-<script id="some-template" type="text/x-handlebars-template">
- <table>
- <thead>
- <th>Username</th>
- <th>Real Name</th>
- <th>Email</th>
- </thead>
- <tbody>
- {{#users}}
- <tr>
- <td>{{username}}</td>
- <td>{{firstName}} {{lastName}}</td>
- <td>{{email}}</td>
- </tr>
- {{/users}}
- </tbody>
- </table>
-</script>
View
@@ -0,0 +1,81 @@
+/*jshint white: false, node: true, vars: true, indent: 4 */
+(function (module, __dirname) {
+ "use strict";
+
+ var path = require("path");
+
+ module.exports = {
+ /**
+ * The options to be passed to connect.static
+ */
+ "staticOptions": { /* maxAge: oneDay */ },
+ /**
+ * {{#each metas}}
+ * <meta name="{{this.name}}" content="{{this.values}}">
+ * {{/each}}
+ */
+ "metas": [
+ {
+ "viewport": "width=device-width"
+ }
+ ],
+ /**
+ * The directories to be searched for public artifacts.
+ */
+ "public": [
+ path.join(__dirname, "..", "public")
+ ],
+ /**
+ * {{#each stylesheets}}
+ * <link rel="stylesheet" href="{{this}}">
+ * {{/each}}
+ */
+ "stylesheets": ["css/style.css"],
+ /**
+ * Be default we are using require.js; not quite a mainstream tool, so please set this property to false
+ * unless you want to spend the time using it.
+ */
+ "useRequireJS": true,
+ /**
+ *
+ */
+ "htmlFiles": [],
+ /**
+ * Loading scripts at the end of the file. Unless there is a good reason not to do it,
+ * you should put all your scripts here.
+ *
+ * {{#each tailingScripts}}
+ * <script src="{{this}}"></script>
+ * {{/each}}
+ */
+ "tailingScripts": [
+ ],
+ /**
+ * Adding scripts in the head. Only modernizer should go here. The rest of the scripts
+ * should go to traillingScripts.
+ *
+ * {{#each scripts}}
+ * <script src="{{this}}"></script>
+ * {{/each}}
+ */
+ "scripts": [
+ "js/libs/modernizr-2.5.2.min.js"
+ ],
+ /**
+ * {{#each htmlFiles}}
+ * {{{this}}}
+ * {{/each}}
+ */
+ "templateFiles": [],
+ /**
+ * allow users to register server-side functions
+ */
+ "dispatcher": {
+ /* projects: {
+ "GET": function (cb, param1, param2, param3) {
+ return cb(undefined, { success: true });
+ }
+ }*/
+ }
+ };
+})(module, __dirname);
View
@@ -0,0 +1,148 @@
+/*jshint white: false, node: true, vars: true, indent: 4 */
+(function (module, __dirname) {
+ "use strict";
+
+ var HTTP_SUCCESS = 200,
+ path = require("path"),
+ Batch = require("batch"),
+ debug = require("./debug"),
+ hbars = require("handlebars"),
+ locate = require("./util").locate,
+ _ = require('underscore'),
+ url = require("url"),
+ fs = require("fs");
+
+ function readFile(cache, file, cb) {
+ var cached = cache.files[file];
+
+ if (cached) {
+ return cb(undefined, cached);
+ }
+
+ debug("reading", file);
+
+ fs.readFile(file, "utf-8", function (err, content) {
+ if (err) {
+ return cb(err);
+ }
+
+ cache.files[file] = content;
+
+ return cb(undefined, content);
+ });
+ }
+
+ function compileSite(cache, conf, url, cb) {
+ var batch = new Batch(),
+ htmlFiles = conf.htmlFiles,
+ rawHtml = [],
+ idx = 0;
+
+ htmlFiles.forEach(function (file) {
+ var pos = idx++;
+
+ batch.push(function (done) {
+ readFile(cache, file, function (err, content) {
+ if (err) {
+ return done(err);
+ }
+
+ rawHtml[pos] = content;
+ return done();
+ });
+ });
+ });
+
+ conf.htmlFiles = rawHtml;
+
+ var templateFiles = conf.templateFiles,
+ templateHtml = [];
+
+ templateFiles.forEach(function (file) {
+ var name = path.basename(file, '.html');
+
+ batch.push(function (done) {
+ readFile(cache, file, function (err, content) {
+ if (err) {
+ return done(err);
+ }
+
+ templateHtml.push({
+ name: name,
+ content: content
+ });
+ return done();
+ });
+ });
+ });
+
+ conf.templateFiles = templateHtml;
+
+ return batch.end(function (err) {
+ if (err) {
+ return cb(err);
+ }
+
+ var file = url.pathname,
+ cached = cache.templates[file];
+
+ if (cached) {
+ return cb(undefined, cached);
+ }
+
+ locate(conf["public"], file, function (err, qfile) {
+ return readFile(cache, qfile, function (err, content) {
+ if (err) {
+ return cb(err);
+ }
+
+ var template = hbars.compile(content);
+
+ cache.templates[file] = template;
+
+ return cb(undefined, template);
+ });
+ });
+ });
+
+ }
+
+ module.exports = function (module) {
+ var cache = {
+ templates: {},
+ files: {}
+ };
+
+ return function (req, res, next) {
+ var parsed = url.parse(req.url, true, true);
+
+ debug("url", parsed);
+
+ if (!parsed.pathname || parsed.pathname === '/') {
+ res.statusCode = 301;
+ res.setHeader('Location', 'index.html');
+
+ return res.end();
+ }
+
+ if (!parsed.pathname.match(/\.html$/)) {
+ return next();
+ }
+
+ var mod = _.clone(module);
+
+ return compileSite(cache, mod, parsed, function (err, compiled) {
+ if (err) {
+ return next(err);
+ }
+
+ res.setHeader('Content-Type', 'text/html; charset=UTF-8');
+ res.statusCode = HTTP_SUCCESS;
+
+ var result = compiled(mod);
+
+ return res.end(result);
+ });
+ };
+ };
+})(module, __dirname);
Oops, something went wrong.

0 comments on commit d740eb3

Please sign in to comment.