feat(core): load an external theme from a given path using --theme-path

pull/107/head
Romain 6 years ago
parent 03a9c9a319
commit 9612ca0048

@ -97,7 +97,8 @@ Album options:
Website options:
--index Filename of the home page [default: "index.html"]
--albums-output-folder Output subfolder for HTML albums (default: website root) [default: "."]
--theme Name of the gallery theme to apply [choices: "classic", "cards", "mosaic"] [default: "classic"]
--theme Name of a built-in gallery theme [choices: "classic", "cards", "mosaic"] [default: "classic"]
--theme-path Path to a custom theme [string]
--theme-style Path to a custom LESS/CSS file for additional styles [string]
--title Website title [default: "Photo album"]
--footer Text or HTML footer [default: null]

@ -143,10 +143,15 @@ const OPTIONS = {
},
'theme': {
group: 'Website options:',
description: 'Name of the gallery theme to apply',
description: 'Name of a built-in gallery theme',
choices: ['classic', 'cards', 'mosaic'],
'default': 'classic'
},
'theme-path': {
group: 'Website options:',
description: 'Path to a custom theme',
normalize: true
},
'theme-style': {
group: 'Website options:',
description: 'Path to a custom LESS/CSS file for additional styles',
@ -281,6 +286,7 @@ exports.get = (args) => {
sortMediaBy: opts['sort-media-by'],
sortMediaDirection: opts['sort-media-direction'],
theme: opts['theme'],
themePath: opts['theme-path'],
themeStyle: opts['theme-style'],
css: opts['css'],
googleAnalytics: opts['google-analytics'],

1
package-lock.json generated

@ -2495,7 +2495,6 @@
},
"listr-work-queue": {
"version": "git+https://github.com/thumbsup/listr-work-queue.git#867d499747af33a36b2c7c722239e1cbbee21986",
"integrity": "sha1-ak5b4doDE9hnh8SIVw7novOr4Tc=",
"requires": {
"async": "https://registry.npmjs.org/async/-/async-2.6.1.tgz",
"zen-observable": "https://registry.npmjs.org/zen-observable/-/zen-observable-0.6.1.tgz"

@ -20,6 +20,7 @@ class Theme {
// load all theme helpers
// and copy assets into the output folder (static files, CSS...)
prepare (done) {
this.validateStructure()
// compiled template
this.template = compileTemplate(path.join(this.dir, 'album.hbs'))
this.loadPartials()
@ -30,6 +31,15 @@ class Theme {
], done)
}
// make sure the given folder is a valid theme
validateStructure () {
const template = fs.existsSync(path.join(this.dir, 'album.hbs'))
const style = fs.existsSync(path.join(this.dir, 'theme.less'))
if (!template || !style) {
throw new Error(`Invalid theme structure in ${this.dir}`)
}
}
// 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) {
@ -48,6 +58,10 @@ class Theme {
// ------------------------
loadPartials () {
if (!isDirectory(path.join(this.dir, 'partials'))) {
return
}
// load all files in the <partials> folder
const partials = fs.readdirSync(path.join(this.dir, 'partials'))
const isTemplate = filepath => path.extname(filepath) === '.hbs'
partials.filter(isTemplate).forEach(filename => {
@ -58,6 +72,10 @@ class Theme {
}
loadHelpers () {
if (!isDirectory(path.join(this.dir, 'helpers'))) {
return
}
// load all files in the <helpers> folder
const helpers = fs.readdirSync(path.join(this.dir, 'helpers'))
const isHelper = filepath => path.extname(filepath) === '.js'
helpers.filter(isHelper).forEach(filename => {
@ -104,6 +122,10 @@ class Theme {
}
copyPublic (done) {
if (!isDirectory(path.join(this.dir, 'public'))) {
return done()
}
// copy all files in the <public> folder
const src = path.join(this.dir, 'public')
const dest = path.join(this.dest, 'public')
fs.copy(src, dest, done)
@ -115,4 +137,12 @@ function compileTemplate (hbsFile) {
return handlebars.compile(src.toString())
}
function isDirectory (fullPath) {
try {
return fs.statSync(fullPath).isDirectory()
} catch (ex) {
return false
}
}
module.exports = Theme

@ -13,7 +13,7 @@ exports.build = function (rootAlbum, opts, callback) {
})
// then create the actual theme assets
const themeDir = path.join(THEMES_DIR, opts.theme)
const themeDir = opts.themePath || path.join(THEMES_DIR, opts.theme)
const theme = new Theme(themeDir, opts.output, {
stylesheetName: 'theme.css',
customStylesPath: opts.themeStyle

Loading…
Cancel
Save