[feat] new option to pass settings to a theme, from a JSON file

Fixes #168
pull/178/head
Romain 5 years ago
parent 26e7840985
commit a09ff03f2e

@ -119,6 +119,7 @@ Album options:
--sort-albums-direction Album sorting direction [choices: "asc", "desc"] [default: "asc"]
--sort-media-by How to sort photos and videos [choices: "filename", "date"] [default: "date"]
--sort-media-direction Media sorting direction [choices: "asc", "desc"] [default: "asc"]
--album-zip-files Create a ZIP file per album [boolean] [default: false]
Website options:
--index Filename of the home page [default: "index.html"]
@ -126,6 +127,7 @@ Website options:
--theme Name of a built-in gallery theme [choices: "classic", "cards", "mosaic", "flow"] [default: "classic"]
--theme-path Path to a custom theme [string]
--theme-style Path to a custom LESS/CSS file for additional styles [string]
--theme-settings Path to a JSON file with theme settings [string]
--title Website title [default: "Photo album"]
--footer Text or HTML footer [default: null]
--google-analytics Code for Google Analytics tracking [string]

@ -231,6 +231,11 @@ const OPTIONS = {
description: 'Path to a custom LESS/CSS file for additional styles',
normalize: true
},
'theme-settings': {
group: 'Website options:',
description: 'Path to a JSON file with theme settings',
normalize: true
},
'title': {
group: 'Website options:',
description: 'Website title',
@ -408,6 +413,7 @@ exports.get = (args) => {
theme: opts['theme'],
themePath: opts['theme-path'],
themeStyle: opts['theme-style'],
themeSettings: opts['theme-settings'],
css: opts['css'],
googleAnalytics: opts['google-analytics'],
index: opts['index'],

@ -57,12 +57,11 @@ class Theme {
}
}
// return a function that renders the given album HTML page
// this is used so that all pages can be created in parallel
render (album, data, next) {
const fullPath = path.join(this.dest, album.path)
debug(`Theme rendering ${album.path}`)
const contents = this.template(Object.assign({ album: album }, data))
// renders the given album HTML page
render (targetPath, data, next) {
const fullPath = path.join(this.dest, targetPath)
debug(`Theme rendering ${targetPath}`)
const contents = this.template(data)
fs.mkdirpSync(path.dirname(fullPath))
fs.writeFile(fullPath, contents, next)
}

@ -1,3 +1,4 @@
const fs = require('fs')
const path = require('path')
const async = require('async')
const resolvePkg = require('resolve-pkg')
@ -18,9 +19,12 @@ exports.build = function (rootAlbum, opts, callback) {
customStylesPath: opts.themeStyle
})
// custom theme settings
const settings = readThemeSettings(opts.themeSettings)
// and finally render each page
const gallery = galleryModel(rootAlbum, opts)
const tasks = createRenderingTasks(theme, rootAlbum, gallery, [])
const tasks = createRenderingTasks(theme, rootAlbum, gallery, settings, [])
// now build everything
async.series([
@ -41,18 +45,20 @@ function galleryModel (rootAlbum, opts) {
}
}
function createRenderingTasks (theme, album, gallery, breadcrumbs) {
function createRenderingTasks (theme, album, gallery, settings, breadcrumbs) {
// a function to render this album
const thisAlbumTask = next => {
theme.render(album, {
theme.render(album.path, {
settings: settings,
gallery: gallery,
breadcrumbs: breadcrumbs
breadcrumbs: breadcrumbs,
album: album
}, next)
}
const tasks = [thisAlbumTask]
// and all nested albums
album.albums.forEach(function (nested) {
const nestedTasks = createRenderingTasks(theme, nested, gallery, breadcrumbs.concat([album]))
const nestedTasks = createRenderingTasks(theme, nested, gallery, settings, breadcrumbs.concat([album]))
Array.prototype.push.apply(tasks, nestedTasks)
})
return tasks
@ -65,3 +71,15 @@ function localThemePath (themeName) {
}
return local
}
function readThemeSettings (filepath) {
if (!filepath) {
return {}
}
const content = fs.readFileSync(filepath).toString()
try {
return JSON.parse(content)
} catch (ex) {
throw new Error('Failed to parse JSON theme settings file: ' + filepath)
}
}

@ -233,7 +233,7 @@ function renderTheme (theme, album, next) {
theme.validateStructure()
theme.prepare(err => {
should(err).be.null()
theme.render(album, {}, err => {
theme.render(album.path, { album: album }, err => {
should(err).be.null()
next()
})

Loading…
Cancel
Save