From 483207e5a084d41a9f3074edc7712c3c842855ea Mon Sep 17 00:00:00 2001 From: hacksalot Date: Tue, 29 Dec 2015 10:01:45 -0500 Subject: [PATCH] Improve Markdown support for JSON Resume themes. --- src/core/jrs-resume.js | 65 ++++++++++++++++++++++++++++++++++++++++++ src/verbs/generate.js | 25 +++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/core/jrs-resume.js b/src/core/jrs-resume.js index 619e83a..49a8d86 100644 --- a/src/core/jrs-resume.js +++ b/src/core/jrs-resume.js @@ -11,6 +11,7 @@ Definition of the JRSResume class. , validator = require('is-my-json-valid') , _ = require('underscore') , PATH = require('path') + , MD = require('marked') , moment = require('moment'); /** @@ -238,6 +239,70 @@ Definition of the JRSResume class. }; + JRSResume.prototype.dupe = function() { + var rnew = new JRSResume(); + rnew.parse( this.stringify(), { } ); + return rnew; + }; + + /** + Create a copy of this resume in which all fields have been interpreted as + Markdown. + */ + JRSResume.prototype.harden = function() { + + var that = this; + var ret = this.dupe(); + + function HD(txt) { + return '@@@@~' + txt + '~@@@@'; + } + + function HDIN(txt){ + //return MD(txt || '' ).replace(/^\s*

|<\/p>\s*$/gi, ''); + return HD(txt); + } + + // TODO: refactor recursion + function hardenStringsInObject( obj, inline ) { + + if( !obj ) return; + inline = inline === undefined || inline; + + + if( Object.prototype.toString.call( obj ) === '[object Array]' ) { + obj.forEach(function(elem, idx, ar){ + if( typeof elem === 'string' || elem instanceof String ) + ar[idx] = inline ? HDIN(elem) : HD( elem ); + else + hardenStringsInObject( elem ); + }); + } + else if (typeof obj === 'object') { + Object.keys( obj ).forEach(function(key) { + var sub = obj[key]; + if( typeof sub === 'string' || sub instanceof String ) { + if( _.contains(['skills','url','website','startDate','endDate','releaseDate','date','phone','email','address','postalCode','city','country','region'], key) ) + return; + if( key === 'summary' ) + obj[key] = HD( obj[key] ); + else + obj[key] = inline ? HDIN( obj[key] ) : HD( obj[key] ); + } + else + hardenStringsInObject( sub ); + }); + } + + } + + Object.keys( ret ).forEach(function(member){ + hardenStringsInObject( ret[ member ] ); + }); + + return ret; + }; + /** Convert human-friendly dates into formal Moment.js dates for all collections. We don't want to lose the raw textual date as entered by the user, so we store diff --git a/src/verbs/generate.js b/src/verbs/generate.js index 5c07ee9..2291f53 100644 --- a/src/verbs/generate.js +++ b/src/verbs/generate.js @@ -3,6 +3,7 @@ var PATH = require('path') , FS = require('fs') , parsePath = require('parse-filepath') + , MD = require('marked') , MKDIRP = require('mkdirp') , _opts = require('../core/default-options') , FluentTheme = require('../core/theme') @@ -102,6 +103,11 @@ "/foo/bar/resume.pdf" or "c:\foo\bar\resume.txt". */ function single( targInfo, theme ) { + + function MDIN(txt) { + return MD(txt || '' ).replace(/^\s*

|<\/p>\s*$/gi, ''); + } + try { var f = targInfo.file , fType = targInfo.fmt.outFormat @@ -144,7 +150,24 @@ // return PATH.join(p2, p1); // } }); - var rezHtml = theme.render( rez ); + + // Prevent JSON Resume theme .js from chattering + var consoleLog = console.log; + console.log = function() { }; + + // Call the theme's render method + var rezDupe = rez.harden(); + var rezHtml = theme.render( rezDupe ); + + // Turn logging back on + console.log = consoleLog; + + // Unharden + rezHtml = rezHtml.replace( /@@@@~.+?~@@@@/g, function(val){ + return MDIN( val.replace( /~@@@@/gm,'' ).replace( /@@@@~/gm,'' ) ); + }); + + // Save the file FS.writeFileSync( f, rezHtml ); } else {