1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00

Better dynamic font handling.

This commit is contained in:
hacksalot 2016-01-22 22:19:28 -05:00
parent ac9e4aa1a0
commit f073c79b8d
4 changed files with 102 additions and 21 deletions

View File

@ -18,6 +18,7 @@ Error-handling routines for HackMyResume.
, WRAP = require('word-wrap') , WRAP = require('word-wrap')
, M2C = require('../utils/md2chalk.js') , M2C = require('../utils/md2chalk.js')
, chalk = require('chalk') , chalk = require('chalk')
, extend = require('extend')
, YAML = require('yamljs') , YAML = require('yamljs')
, printf = require('printf') , printf = require('printf')
, SyntaxErrorEx = require('../utils/syntax-error-ex'); , SyntaxErrorEx = require('../utils/syntax-error-ex');
@ -195,10 +196,15 @@ Error-handling routines for HackMyResume.
}, this); }, this);
break; break;
case HMSTATUS.missingParam:
msg = printf( M2C( this.msgs.missingParam.msg ), ex.expected, ex.helper );
break;
case HMSTATUS.invalidHelperUse: case HMSTATUS.invalidHelperUse:
msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper ); msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper );
if( ex.error ) { if( ex.error ) {
msg += printf( '\n--> ' + M2C( this.msgs.invalidParamCount.msg ), ex.expected ); msg += '\n--> ' + assembleError.call( this, extend( true, {}, ex, {fluenterror: ex.error} )).msg;
//msg += printf( '\n--> ' + M2C( this.msgs.invalidParamCount.msg ), ex.expected );
} }
quit = false; quit = false;
etype = 'warning'; etype = 'warning';

View File

@ -94,3 +94,5 @@ errors:
msg: "Applying **%s** theme (? formats)" msg: "Applying **%s** theme (? formats)"
invalidParamCount: invalidParamCount:
msg: "Invalid number of parameters. Expected: **%s**." msg: "Invalid number of parameters. Expected: **%s**."
missingParam:
msg: The '**%s**' parameter was needed but not supplied.

View File

@ -30,7 +30,8 @@ Status codes for HackMyResume.
invokeTemplate: 20, invokeTemplate: 20,
compileTemplate: 21, compileTemplate: 21,
themeLoad: 22, themeLoad: 22,
invalidParamCount: 23 invalidParamCount: 23,
missingParam: 24
}; };
}()); }());

View File

@ -84,21 +84,51 @@ Generic template helper definitions for HackMyResume / FluentCV.
*/ */
fontFace: function( key, defFont ) { fontFace: function( key, defFont ) {
var ret = ''; var ret = ''
if( arguments.length !== 3 ) { // key, defFont, and the HandleBars options , hasDef = defFont && String.is( defFont )
, fontSpec;
// Key must be specified
if( !( key && key.trim()) ) {
_reportError( HMSTATUS.invalidHelperUse, { _reportError( HMSTATUS.invalidHelperUse, {
helper: 'fontFace', error: HMSTATUS.invalidParamCount, expected: 2 helper: 'fontFace', error: HMSTATUS.missingParam, expected: 'key'
}); });
} }
// If the theme doesn't have a "fonts" section, use defFont.
else if( !GenericHelpers.theme.fonts ) { else if( !GenericHelpers.theme.fonts ) {
ret = defFont; ret = defFont;
if( !hasDef ) {
_reportError( HMSTATUS.invalidHelperUse, {
helper: 'fontFace', error: HMSTATUS.missingParam,
expected: 'defFont'});
}
} }
// If the theme has a "fonts" section, lookup the font face.
else if (fontSpec = GenericHelpers.theme.fonts[ key ]) { // jshint ignore:line
ret = String.is( fontSpec ) ? fontSpec : // [1]
(_.isArray( fontSpec ) && fontSpec[0]);
if( !(ret && ret.trim()) ) {
ret = defFont; // [2]
if( !hasDef ) {
_reportError( HMSTATUS.invalidHelperUse, {
helper: 'fontFace', error: HMSTATUS.missingParam,
expected: 'defFont'});
}
}
}
// The key wasn't found in the "fonts" section. Default to defFont.
else { else {
var fontSpec = GenericHelpers.theme.fonts[ key ]; if( !hasDef ) {
if( !fontSpec ) _reportError( HMSTATUS.invalidHelperUse, {
_reportError( HMSTATUS.invalidHelperUse, { helper: 'fontFace' } ); helper: 'fontFace', error: HMSTATUS.missingParam,
else expected: 'defFont'});
ret = String.is( fontSpec ) ? fontSpec : fontSpec[0]; }
ret = defFont;
} }
return ret; return ret;
@ -109,25 +139,38 @@ Generic template helper definitions for HackMyResume / FluentCV.
provided key. provided key.
@param key {String} A named style from the "fonts" section of the theme's @param key {String} A named style from the "fonts" section of the theme's
theme.json file. For example: 'default' or 'heading1'. theme.json file. For example: 'default' or 'heading1'.
@param defFont {String} The font to use if the specified key isn't present. @param defFontList {Array} The font list to use if the specified key isn't
Can be any valid font-face name such as 'Helvetica Neue' or 'Calibri'. present. Can be an array of valid font-face name such as 'Helvetica Neue'
or 'Calibri'.
@param sep {String} The default separator to use in the rendered output.
Defaults to ", " (comma with a space).
*/ */
fontList: function( key, defFontList, sep ) { fontList: function( key, defFontList, sep ) {
var ret = '';
if( !_.contains( [3,4], arguments.length ) ) { var ret = ''
, hasDef = defFontList && String.is( defFontList )
, fontSpec;
// Key must be specified
if( !( key && key.trim()) ) {
_reportError( HMSTATUS.invalidHelperUse, { _reportError( HMSTATUS.invalidHelperUse, {
helper: 'fontList', error: HMSTATUS.invalidParamCount, expected: [2,3] helper: 'fontList', error: HMSTATUS.missingParam, expected: 'key'
}); });
} }
// If the theme doesn't have a "fonts" section, use defFont
else if( !GenericHelpers.theme.fonts ) { else if( !GenericHelpers.theme.fonts ) {
ret = defFontList; ret = defFontList;
} if( !hasDef ) {
else { _reportError( HMSTATUS.invalidHelperUse, {
var fontSpec = GenericHelpers.theme.fonts[ key ]; helper: 'fontList', error: HMSTATUS.missingParam,
if( !fontSpec ) { expected: 'defFontList'});
_reportError( HMSTATUS.invalidHelperUse, { helper: 'fontFace' } );
} }
else if( _.isArray( fontSpec ) ) { }
// If the theme has a "fonts" section, lookup the font list.
else if( fontSpec = GenericHelpers.theme.fonts[ key ] ) { // jshint ignore:line
if( _.isArray( fontSpec ) ) {
fontSpec = fontSpec.map( function(ff) { fontSpec = fontSpec.map( function(ff) {
return "'" + ff + "'"; return "'" + ff + "'";
}); });
@ -138,6 +181,16 @@ Generic template helper definitions for HackMyResume / FluentCV.
} }
} }
// The key wasn't found in the "fonts" section. Default to defFont.
else {
if( !hasDef ) {
_reportError( HMSTATUS.invalidHelperUse, {
helper: 'fontList', error: HMSTATUS.missingParam,
expected: 'defFontList'});
}
ret = defFontList;
}
return ret; return ret;
}, },
@ -447,3 +500,22 @@ Generic template helper definitions for HackMyResume / FluentCV.
} }
}()); }());
// Note [1] --------------------------------------------------------------------
// Make sure it's precisely a string or array since some template engines jam
// their options/context object into the last parameter and we are allowing the
// defFont parameter to be omitted in certain cases. This is a little kludgy,
// but works fine for this case. If we start doing this regularly, we should
// rebind these parameters.
// Note [2]: -------------------------------------------------------------------
// If execution reaches here, some sort of cosmic ray or sunspot has landed on
// HackMyResume, or a theme author is deliberately messing with us by doing
// something like:
//
// "fonts": {
// "default": "",
// "heading1": null
// }
//
// Rather than sort it out, we'll just fall back to defFont.