2
0
mirror of https://github.com/thumbsup/thumbsup synced 2024-11-15 18:12:46 +00:00

Big refactor / cleanup to support multiple pages on the website

This commit is contained in:
Romain Prieto 2014-12-01 20:49:37 +11:00
parent 5de02a543f
commit 82c560d96f
16 changed files with 154 additions and 69 deletions

View File

@ -23,6 +23,10 @@ header {
padding: 1em 0; padding: 1em 0;
} }
header a {
text-decoration: none;
}
h1 { h1 {
color: #33609c; color: #33609c;
display: inline-block; display: inline-block;

View File

@ -1,10 +1,10 @@
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require('path'); var path = require('path');
var async = require('async'); var async = require('async');
var metadata = require('./metadata'); var make = require('./utils/make');
var website = require('./website'); var metadata = require('./input/metadata');
var thumbs = require('./thumbs'); var thumbs = require('./output-media/thumbs');
var make = require('./make'); var website = require('./output-website/generator');
exports.build = function(opts) { exports.build = function(opts) {

View File

@ -4,8 +4,8 @@ var path = require('path');
var glob = require('glob'); var glob = require('glob');
var async = require('async'); var async = require('async');
var pad = require('pad'); var pad = require('pad');
var progress = require('../utils/progress');
var exif = require('./exif'); var exif = require('./exif');
var progress = require('./progress');
exports.update = function(opts, callback) { exports.update = function(opts, callback) {

View File

@ -1,32 +1,41 @@
var _ = require('lodash');
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require('path'); var path = require('path');
var async = require('async'); var async = require('async');
var pad = require('pad'); var pad = require('pad');
var viewModel = require('./view-model'); var files = require('../utils/files');
var render = require('./render'); var template = require('./template');
var files = require('./files'); var model = require('./model');
var pages = require('./pages');
exports.build = function(metadata, opts, callback) { 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) { 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; structure.forEach(function(folder, index) {
var gallery = pages.gallery(structure, index);
var rendered = render.gallery(galleries, galleries[0], opts.title, style, opts.googleAnalytics); var page = render(folder.name + '.html', 'gallery', gallery);
var outputPath = path.join(opts.output, 'index.html'); items.push(page);
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);
}); });
callback(); async.parallel(items, callback);
} }
function support(callback) { function support(callback) {
@ -50,7 +59,6 @@ exports.build = function(metadata, opts, callback) {
support, support,
customStyle customStyle
], function(err) { ], function(err) {
process.stdout
console.log('[===================] done'); console.log('[===================] done');
callback(err); callback(err);
}); });

View File

@ -3,7 +3,12 @@ var fs = require('fs');
var path = require('path'); var path = require('path');
var glob = require('glob'); 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) { function fileInfo(data, file) {
return { return {

View File

@ -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]
};
};

View File

@ -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);
};

View File

@ -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
});
};

12
templates/analytics.hbs Normal file
View File

@ -0,0 +1,12 @@
{{#if googleAnalytics}}
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{googleAnalytics}}', 'auto');
ga('send', 'pageview');
</script>
{{/if}}

View File

@ -18,8 +18,10 @@
<body> <body>
<header> <header>
<h1>{{title}}</h1> <a href="index.html">
<h2>{{subtitle}}</h2> <h1>{{title}}</h1>
<h2>{{subtitle}}</h2>
</a>
</header> </header>
<nav> <nav>
@ -90,16 +92,7 @@
}; };
</script> </script>
{{#if googleAnalytics}} {{> analytics}}
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{googleAnalytics}}', 'auto');
ga('send', 'pageview');
</script>
{{/if}}
</body> </body>

37
templates/homepage.hbs Normal file
View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<title>{{gallery.name}}</title>
<link rel="stylesheet" href="public/reset.css" />
<link rel="stylesheet" href="public/theme.css" />
{{#if css}}
<link rel="stylesheet" href="public/{{css}}" />
{{/if}}
</head>
<body>
<header>
<h1>{{title}}</h1>
<h2>{{subtitle}}</h2>
</header>
<nav>
<ul>
{{#each gallery}}
<li>
<a href="{{name}}.html">{{name}}</a>
<div>{{media.length}} items</div>
</li>
{{/each}}
</ul>
</nav>
{{> analytics}}
</body>
</html>