From 712b5041685c94e0f09840a82c34322adcd42be4 Mon Sep 17 00:00:00 2001 From: hacksalot Date: Wed, 6 Jan 2016 10:48:51 -0500 Subject: [PATCH] Support global theme partials (interim). --- package.json | 3 +- src/eng/handlebars-generator.js | 56 +++++++++++++++++++++++++++++++-- src/gen/template-generator.js | 1 + src/verbs/generate.js | 1 + 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 0426de2..5112104 100644 --- a/package.json +++ b/package.json @@ -50,7 +50,7 @@ "copy": "^0.1.3", "fresca": "~0.3.0", "fresh-resume-empty": "^0.1.0", - "fresh-themes": "~0.11.0-beta", + "fresh-themes": "~0.12.0-beta", "fs-extra": "^0.24.0", "handlebars": "^4.0.5", "html": "0.0.10", @@ -66,6 +66,7 @@ "phantom": "^0.8.4", "recursive-readdir-sync": "^1.0.6", "simple-html-tokenizer": "^0.2.0", + "slash": "^1.0.0", "string-padding": "^1.0.2", "string.prototype.startswith": "^0.2.0", "underscore": "^1.8.3", diff --git a/src/eng/handlebars-generator.js b/src/eng/handlebars-generator.js index 16c7e30..935c2c5 100644 --- a/src/eng/handlebars-generator.js +++ b/src/eng/handlebars-generator.js @@ -1,9 +1,11 @@ /** Definition of the HandlebarsGenerator class. -@license MIT. Copyright (c) 2015 James Devlin / FluentDesk. +@license MIT. See LICENSE.md for details. @module handlebars-generator.js */ + + (function() { @@ -11,7 +13,11 @@ Definition of the HandlebarsGenerator class. var _ = require('underscore') , HANDLEBARS = require('handlebars') , FS = require('fs') - , registerHelpers = require('./handlebars-helpers'); + , registerHelpers = require('./handlebars-helpers') + , PATH = require('path') + , parsePath = require('parse-filepath') + , READFILES = require('recursive-readdir-sync') + , SLASH = require('slash'); @@ -21,8 +27,51 @@ Definition of the HandlebarsGenerator class. */ var HandlebarsGenerator = module.exports = { + + initialized: false, + + init: function( format, theme ) { + // TODO: Move .partialsInitialized to application state; shouldn't be on theme + if( !theme.partialsInitialized ) { + + if( format !== 'html' && format != 'doc' ) + return; + + // Precompile global partials in the /partials folder + var partialsFolder = PATH.join( + parsePath( require.resolve('fresh-themes') ).dirname, + '/partials/', + format + ); + + _.each( READFILES( partialsFolder, function(error){ }), function( el ) { + + var pathInfo = parsePath( el ); + var name = SLASH( PATH.relative( partialsFolder, el ) + .replace(/\.html$|\.xml$/, '') ); + + // section-employment, section-education, etc + if( pathInfo.dirname.endsWith('section') ) { + name = SLASH(name.replace(/\.html$|\.xml$/, '')); + } + else { + + } + + var tplData = FS.readFileSync( el, 'utf8' ); + var compiledTemplate = HANDLEBARS.compile( tplData ); + HANDLEBARS.registerPartial( name, compiledTemplate ); + theme.partialsInitialized = true; + }); + } + }, + + + generate: function( json, jst, format, cssInfo, opts, theme ) { + this.init( format, theme ); + // Pre-compile any partials present in the theme. _.each( theme.partials, function( el ) { var tplData = FS.readFileSync( el.path, 'utf8' ); @@ -52,6 +101,7 @@ Definition of the HandlebarsGenerator class. } - }; + + }; }()); diff --git a/src/gen/template-generator.js b/src/gen/template-generator.js index a61046c..3a58b99 100644 --- a/src/gen/template-generator.js +++ b/src/gen/template-generator.js @@ -235,6 +235,7 @@ Definition of the TemplateGenerator class. // Verify the specified theme name/path var tFolder = PATH.join( parsePath( require.resolve('fresh-themes') ).dirname, + '/themes/', this.opts.theme ); diff --git a/src/verbs/generate.js b/src/verbs/generate.js index bd061ef..7b5c012 100644 --- a/src/verbs/generate.js +++ b/src/verbs/generate.js @@ -307,6 +307,7 @@ Implementation of the 'generate' verb for HackMyResume. function verify_theme( themeNameOrPath ) { var tFolder = PATH.join( parsePath ( require.resolve('fresh-themes') ).dirname, + '/themes/', themeNameOrPath ); var exists = require('path-exists').sync;