From 4fe74057f9b2d15e8e69bf4062bd9a3273bcf21a Mon Sep 17 00:00:00 2001 From: hacksalot Date: Fri, 22 Jan 2016 08:33:01 -0500 Subject: [PATCH] Improve font helpers. Log a warning on incorrect use. --- src/cli/error.js | 3 ++ src/cli/msg.yml | 6 ++-- src/core/status-codes.js | 3 +- src/helpers/generic-helpers.js | 60 +++++++++++++++++++++++++--------- 4 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/cli/error.js b/src/cli/error.js index e592c42..d6dba3e 100644 --- a/src/cli/error.js +++ b/src/cli/error.js @@ -197,6 +197,9 @@ Error-handling routines for HackMyResume. case HMSTATUS.invalidHelperUse: msg = printf( M2C( this.msgs.invalidHelperUse.msg ), ex.helper ); + if( ex.error ) { + msg += printf( '\n--> ' + M2C( this.msgs.invalidParamCount.msg ), ex.expected ); + } quit = false; etype = 'warning'; break; diff --git a/src/cli/msg.yml b/src/cli/msg.yml index 029b668..8639697 100644 --- a/src/cli/msg.yml +++ b/src/cli/msg.yml @@ -14,7 +14,7 @@ events: - Merging **%s** - " onto **%s**" applyTheme: - msg: Applying **%s** theme (%s format%s) + msg: Applying **%s** theme (**%s** format%s) afterBuild: msg: - "The **%s** theme says:" @@ -81,7 +81,7 @@ errors: parseError: msg: Invalid or corrupt JSON on line %s column %s. invalidHelperUse: - msg: Warning: Invalid use of the **%s** theme helper. + msg: "**Warning**: Incorrect use of the **%s** theme helper." fileSaveError: msg: An error occurred while writing %s to disk: %s. mixedMerge: @@ -92,3 +92,5 @@ errors: msg: "An error occurred during template compilation." themeLoad: msg: "Applying **%s** theme (? formats)" + invalidParamCount: + msg: "Invalid number of parameters. Expected: **%s**." diff --git a/src/core/status-codes.js b/src/core/status-codes.js index f398dcb..58fb135 100644 --- a/src/core/status-codes.js +++ b/src/core/status-codes.js @@ -29,7 +29,8 @@ Status codes for HackMyResume. mixedMerge: 19, invokeTemplate: 20, compileTemplate: 21, - themeLoad: 22 + themeLoad: 22, + invalidParamCount: 23 }; }()); diff --git a/src/helpers/generic-helpers.js b/src/helpers/generic-helpers.js index 57d6bf1..ee46246 100644 --- a/src/helpers/generic-helpers.js +++ b/src/helpers/generic-helpers.js @@ -79,15 +79,29 @@ Generic template helper definitions for HackMyResume / FluentCV. provided key. @param key {String} A named style from the "fonts" section of the theme's theme.json file. For example: 'default' or 'heading1'. + @param defFont {String} The font to use if the specified key isn't present. + Can be any valid font-face name such as 'Helvetica Neue' or 'Calibri'. */ - fontFace: function( key ) { - if( key && key.trim() && GenericHelpers.theme.fonts ) { - var fontSpec = GenericHelpers.theme.fonts[ key ]; - if( fontSpec ) - return String.is( fontSpec ) ? fontSpec : fontSpec[0]; + fontFace: function( key, defFont ) { + + var ret = ''; + if( arguments.length !== 3 ) { // key, defFont, and the HandleBars options + _reportError( HMSTATUS.invalidHelperUse, { + helper: 'fontFace', error: HMSTATUS.invalidParamCount, expected: 2 + }); } - _reportError( HMSTATUS.invalidHelperUse, { helper: 'fontFace' } ); - return ''; + else if( !GenericHelpers.theme.fonts ) { + ret = defFont; + } + else { + var fontSpec = GenericHelpers.theme.fonts[ key ]; + if( !fontSpec ) + _reportError( HMSTATUS.invalidHelperUse, { helper: 'fontFace' } ); + else + ret = String.is( fontSpec ) ? fontSpec : fontSpec[0]; + } + + return ret; }, /** @@ -95,19 +109,35 @@ Generic template helper definitions for HackMyResume / FluentCV. provided key. @param key {String} A named style from the "fonts" section of the theme's theme.json file. For example: 'default' or 'heading1'. + @param defFont {String} The font to use if the specified key isn't present. + Can be any valid font-face name such as 'Helvetica Neue' or 'Calibri'. */ - fontList: function( key ) { + fontList: function( key, defFontList, sep ) { var ret = ''; - var fontSpec = GenericHelpers.theme.fonts[ key ]; - if( _.isArray( fontSpec ) ) { - fontSpec = fontSpec.map( function(ff) { - return "'" + ff + "'"; + if( !_.contains( [3,4], arguments.length ) ) { + _reportError( HMSTATUS.invalidHelperUse, { + helper: 'fontList', error: HMSTATUS.invalidParamCount, expected: [2,3] }); - ret = fontSpec.join(', '); } - else if( _.isString( fontSpec )) { - ret = fontSpec; + else if( !GenericHelpers.theme.fonts ) { + ret = defFontList; } + else { + var fontSpec = GenericHelpers.theme.fonts[ key ]; + if( !fontSpec ) { + _reportError( HMSTATUS.invalidHelperUse, { helper: 'fontFace' } ); + } + else if( _.isArray( fontSpec ) ) { + fontSpec = fontSpec.map( function(ff) { + return "'" + ff + "'"; + }); + ret = fontSpec.join( sep === undefined ? ', ' : (sep || '') ); + } + else if( _.isString( fontSpec )) { + ret = fontSpec; + } + } + return ret; },