From 82c560d96f726a30d39c446cbad3a5f3c1b95a77 Mon Sep 17 00:00:00 2001 From: Romain Prieto Date: Mon, 1 Dec 2014 20:49:37 +1100 Subject: [PATCH] Big refactor / cleanup to support multiple pages on the website --- public/theme.css | 4 ++ src/index.js | 8 ++-- src/{ => input}/exif.js | 0 src/{ => input}/metadata.js | 2 +- src/{ => output-media}/thumbs.js | 0 .../generator.js} | 44 +++++++++++-------- .../model.js} | 7 ++- src/output-website/pages.js | 40 +++++++++++++++++ src/output-website/template.js | 19 ++++++++ src/render.js | 33 -------------- src/{ => utils}/files.js | 0 src/{ => utils}/make.js | 0 src/{ => utils}/progress.js | 0 templates/analytics.hbs | 12 +++++ templates/gallery.hbs | 17 +++---- templates/homepage.hbs | 37 ++++++++++++++++ 16 files changed, 154 insertions(+), 69 deletions(-) rename src/{ => input}/exif.js (100%) rename src/{ => input}/metadata.js (98%) rename src/{ => output-media}/thumbs.js (100%) rename src/{website.js => output-website/generator.js} (53%) rename src/{view-model.js => output-website/model.js} (92%) create mode 100644 src/output-website/pages.js create mode 100644 src/output-website/template.js delete mode 100644 src/render.js rename src/{ => utils}/files.js (100%) rename src/{ => utils}/make.js (100%) rename src/{ => utils}/progress.js (100%) create mode 100644 templates/analytics.hbs create mode 100644 templates/homepage.hbs diff --git a/public/theme.css b/public/theme.css index 08c1228..71028d4 100755 --- a/public/theme.css +++ b/public/theme.css @@ -23,6 +23,10 @@ header { padding: 1em 0; } +header a { + text-decoration: none; +} + h1 { color: #33609c; display: inline-block; diff --git a/src/index.js b/src/index.js index bca75c7..5ba3e7a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,10 @@ var fs = require('fs-extra'); var path = require('path'); var async = require('async'); -var metadata = require('./metadata'); -var website = require('./website'); -var thumbs = require('./thumbs'); -var make = require('./make'); +var make = require('./utils/make'); +var metadata = require('./input/metadata'); +var thumbs = require('./output-media/thumbs'); +var website = require('./output-website/generator'); exports.build = function(opts) { diff --git a/src/exif.js b/src/input/exif.js similarity index 100% rename from src/exif.js rename to src/input/exif.js diff --git a/src/metadata.js b/src/input/metadata.js similarity index 98% rename from src/metadata.js rename to src/input/metadata.js index f6a3b2e..8c0f256 100644 --- a/src/metadata.js +++ b/src/input/metadata.js @@ -4,8 +4,8 @@ var path = require('path'); var glob = require('glob'); var async = require('async'); var pad = require('pad'); +var progress = require('../utils/progress'); var exif = require('./exif'); -var progress = require('./progress'); exports.update = function(opts, callback) { diff --git a/src/thumbs.js b/src/output-media/thumbs.js similarity index 100% rename from src/thumbs.js rename to src/output-media/thumbs.js diff --git a/src/website.js b/src/output-website/generator.js similarity index 53% rename from src/website.js rename to src/output-website/generator.js index d2a79e6..a25b077 100644 --- a/src/website.js +++ b/src/output-website/generator.js @@ -1,32 +1,41 @@ +var _ = require('lodash'); var fs = require('fs-extra'); var path = require('path'); var async = require('async'); var pad = require('pad'); -var viewModel = require('./view-model'); -var render = require('./render'); -var files = require('./files'); - +var files = require('../utils/files'); +var template = require('./template'); +var model = require('./model'); +var pages = require('./pages'); exports.build = function(metadata, opts, callback) { + var common = pages.common(opts); + + function render(filename, templateName, data) { + var fullPath = path.join(opts.output, filename); + var pageData = _.extend(data, common); + var contents = template.render(templateName, pageData); + return function(next) { + fs.writeFile(fullPath, contents, next); + }; + } + function website(callback) { + var structure = model.create(metadata, opts); + var homepage = pages.homepage(structure); - var galleries = viewModel.build(metadata, opts); + var items = [ + render('index.html', 'homepage', homepage) + ]; - var style = opts.css ? path.basename(opts.css) : null; - - var rendered = render.gallery(galleries, galleries[0], opts.title, style, opts.googleAnalytics); - var outputPath = path.join(opts.output, 'index.html'); - fs.writeFileSync(outputPath, rendered); - - galleries.forEach(function(folder) { - var rendered = render.gallery(galleries, folder, opts.title, style, opts.googleAnalytics); - var outputPath = path.join(opts.output, folder.url); - fs.writeFileSync(outputPath, rendered); + structure.forEach(function(folder, index) { + var gallery = pages.gallery(structure, index); + var page = render(folder.name + '.html', 'gallery', gallery); + items.push(page); }); - callback(); - + async.parallel(items, callback); } function support(callback) { @@ -50,7 +59,6 @@ exports.build = function(metadata, opts, callback) { support, customStyle ], function(err) { - process.stdout console.log('[===================] done'); callback(err); }); diff --git a/src/view-model.js b/src/output-website/model.js similarity index 92% rename from src/view-model.js rename to src/output-website/model.js index 13f988c..789bb6d 100644 --- a/src/view-model.js +++ b/src/output-website/model.js @@ -3,7 +3,12 @@ var fs = require('fs'); var path = require('path'); var glob = require('glob'); -exports.build = function(metadata, opts) { +/* + In-memory structure of the galleries organised in folders + with relative links to the media, thumbnails, etc... +*/ + +exports.create = function(metadata, opts) { function fileInfo(data, file) { return { diff --git a/src/output-website/pages.js b/src/output-website/pages.js new file mode 100644 index 0000000..dd178aa --- /dev/null +++ b/src/output-website/pages.js @@ -0,0 +1,40 @@ +var _ = require('lodash'); +var path = require('path'); + +/* + Common page data shared by all models +*/ +exports.common = function(opts) { + var titleParts = opts.title.split(' '); + return { + css: opts.css ? path.basename(opts.css) : null, + title: titleParts[0], + subtitle: titleParts.slice(1).join(' '), + googleAnalytics: opts.googleAnalytics + }; + +}; + +/* + Homepage data +*/ +exports.homepage = function(structure) { + return {}; +}; + +/* + Single gallery page +*/ +exports.gallery = function(structure, index) { + var links = structure.map(function(folder, i) { + return { + name: folder.name, + url: folder.name + '.html', + active: (i === index) + }; + }); + return { + links: links, + gallery: structure[index] + }; +}; diff --git a/src/output-website/template.js b/src/output-website/template.js new file mode 100644 index 0000000..7835335 --- /dev/null +++ b/src/output-website/template.js @@ -0,0 +1,19 @@ +var fs = require('fs'); +var path = require('path'); +var handlebars = require('handlebars'); + +function compileTemplate(hbsFile) { + var src = fs.readFileSync(path.join(__dirname, '..', '..', 'templates', hbsFile)); + return handlebars.compile(src.toString()); +} + +handlebars.registerPartial('analytics', compileTemplate('analytics.hbs')); + +var templates = { + 'homepage': compileTemplate('homepage.hbs'), + 'gallery': compileTemplate('gallery.hbs') +}; + +exports.render = function(template, data) { + return templates[template](data); +}; diff --git a/src/render.js b/src/render.js deleted file mode 100644 index 47fd8ff..0000000 --- a/src/render.js +++ /dev/null @@ -1,33 +0,0 @@ -var fs = require('fs'); -var path = require('path'); -var handlebars = require('handlebars'); - -function compileTemplate(hbsFile) { - var src = fs.readFileSync(path.join(__dirname, '..', 'templates', hbsFile)); - return handlebars.compile(src.toString()); -} - -var galleryTemplate = compileTemplate('gallery.hbs'); - -exports.gallery = function(list, active, title, css, googleAnalytics) { - - var links = list.map(function(item) { - return { - name: item.name, - url: item.name + '.html', - active: (item === active) - }; - }); - - var titleParts = title.split(' '); - - return galleryTemplate({ - css: css, - links: links, - gallery: active, - title: titleParts[0], - subtitle: titleParts.slice(1).join(' '), - googleAnalytics: googleAnalytics - }); - -}; diff --git a/src/files.js b/src/utils/files.js similarity index 100% rename from src/files.js rename to src/utils/files.js diff --git a/src/make.js b/src/utils/make.js similarity index 100% rename from src/make.js rename to src/utils/make.js diff --git a/src/progress.js b/src/utils/progress.js similarity index 100% rename from src/progress.js rename to src/utils/progress.js diff --git a/templates/analytics.hbs b/templates/analytics.hbs new file mode 100644 index 0000000..73bcdc5 --- /dev/null +++ b/templates/analytics.hbs @@ -0,0 +1,12 @@ +{{#if googleAnalytics}} + + + +{{/if}} diff --git a/templates/gallery.hbs b/templates/gallery.hbs index 4ee85a5..ccdadc2 100644 --- a/templates/gallery.hbs +++ b/templates/gallery.hbs @@ -18,8 +18,10 @@
-

{{title}}

-

{{subtitle}}

+ +

{{title}}

+

{{subtitle}}

+