mirror of
				https://github.com/JuanCanham/HackMyResume.git
				synced 2025-10-31 13:17:26 +00:00 
			
		
		
		
	Finish HackMyCore reshaping.
Reintroduce HackMyCore, dropping the interim submodule, and reorganize and improve tests.
This commit is contained in:
		
							
								
								
									
										95
									
								
								src/renderers/handlebars-generator.coffee
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								src/renderers/handlebars-generator.coffee
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,95 @@ | ||||
| ###* | ||||
| Definition of the HandlebarsGenerator class. | ||||
| @license MIT. See LICENSE.md for details. | ||||
| @module renderers/handlebars-generator | ||||
| ### | ||||
|  | ||||
|  | ||||
| _ = require 'underscore' | ||||
| HANDLEBARS = require 'handlebars' | ||||
| FS = require 'fs' | ||||
| registerHelpers = require '../helpers/handlebars-helpers' | ||||
| PATH = require 'path' | ||||
| parsePath = require 'parse-filepath' | ||||
| READFILES = require 'recursive-readdir-sync' | ||||
| HMSTATUS = require '../core/status-codes' | ||||
| SLASH = require 'slash' | ||||
|  | ||||
|  | ||||
|  | ||||
| ###* | ||||
| Perform template-based resume generation using Handlebars.js. | ||||
| @class HandlebarsGenerator | ||||
| ### | ||||
| HandlebarsGenerator = module.exports = | ||||
|  | ||||
|  | ||||
|  | ||||
|   generateSimple: ( data, tpl ) -> | ||||
|  | ||||
|     try | ||||
|       # Compile and run the Handlebars template. | ||||
|       template = HANDLEBARS.compile tpl, {strict: false, assumeObjects: false} | ||||
|       return template data | ||||
|     catch | ||||
|       throw | ||||
|         fluenterror: if template then HMSTATUS.invokeTemplate else HMSTATUS.compileTemplate | ||||
|         inner: _error | ||||
|  | ||||
|  | ||||
|  | ||||
|   generate: ( json, jst, format, curFmt, opts, theme ) -> | ||||
|  | ||||
|     # Set up partials and helpers | ||||
|     registerPartials format, theme | ||||
|     registerHelpers theme, opts | ||||
|  | ||||
|     # Preprocess text | ||||
|     encData = json | ||||
|     if format == 'html' || format == 'pdf' | ||||
|       encData = json.markdownify() | ||||
|     if( format == 'doc' ) | ||||
|       encData = json.xmlify() | ||||
|  | ||||
|     # Set up the context | ||||
|     ctx = | ||||
|       r: encData | ||||
|       RAW: json | ||||
|       filt: opts.filters | ||||
|       format: format | ||||
|       opts: opts | ||||
|       engine: @ | ||||
|       results: curFmt.files | ||||
|       headFragment: opts.headFragment || '' | ||||
|  | ||||
|     # Render the template | ||||
|     return this.generateSimple ctx, jst | ||||
|  | ||||
|  | ||||
|  | ||||
| registerPartials = (format, theme) -> | ||||
|  | ||||
|   if _.contains( ['html','doc','md','txt','pdf'], format ) | ||||
|  | ||||
|     # Locate the global partials folder | ||||
|     partialsFolder = PATH.join( | ||||
|       parsePath( require.resolve('fresh-themes') ).dirname, | ||||
|       '/partials/', | ||||
|       if format == 'pdf' then 'html' else format | ||||
|     ) | ||||
|  | ||||
|     # Register global partials in the /partials/[format] folder | ||||
|     # TODO: Only do this once per HMR invocation. | ||||
|     _.each READFILES( partialsFolder, (error)->{ }), ( el ) -> | ||||
|       pathInfo = parsePath el | ||||
|       name = SLASH PATH.relative( partialsFolder, el ).replace(/\.(?:html|xml|hbs|md|txt)$/i, '') | ||||
|       tplData = FS.readFileSync el, 'utf8' | ||||
|       compiledTemplate = HANDLEBARS.compile tplData | ||||
|       HANDLEBARS.registerPartial name, compiledTemplate | ||||
|       theme.partialsInitialized = true | ||||
|  | ||||
|   # Register theme-specific partials | ||||
|   _.each theme.partials, ( el ) -> | ||||
|     tplData = FS.readFileSync el.path, 'utf8' | ||||
|     compiledTemplate = HANDLEBARS.compile tplData | ||||
|     HANDLEBARS.registerPartial el.name, compiledTemplate | ||||
							
								
								
									
										44
									
								
								src/renderers/jrs-generator.coffee
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								src/renderers/jrs-generator.coffee
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,44 @@ | ||||
| ###* | ||||
| Definition of the JRSGenerator class. | ||||
| @license MIT. See LICENSE.md for details. | ||||
| @module renderers/jrs-generator | ||||
| ### | ||||
|  | ||||
| _ = require('underscore') | ||||
| HANDLEBARS = require('handlebars') | ||||
| FS = require('fs') | ||||
| registerHelpers = require('../helpers/handlebars-helpers') | ||||
| PATH = require('path') | ||||
| parsePath = require('parse-filepath') | ||||
| READFILES = require('recursive-readdir-sync') | ||||
| SLASH = require('slash') | ||||
| MD = require('marked') | ||||
|  | ||||
| ###* | ||||
| Perform template-based resume generation for JSON Resume themes. | ||||
| @class JRSGenerator | ||||
| ### | ||||
|  | ||||
| JRSGenerator = module.exports = | ||||
|  | ||||
|   generate: ( json, jst, format, cssInfo, opts, theme ) -> | ||||
|  | ||||
|     # Disable JRS theme chatter (console.log, console.error, etc.) | ||||
|     turnoff = ['log', 'error', 'dir']; | ||||
|     org = turnoff.map(c) -> | ||||
|       ret = console[c] | ||||
|       console[c] = () -> | ||||
|  | ||||
|     # Freeze and render | ||||
|     rezHtml = theme.render json.harden() | ||||
|  | ||||
|     # Turn logging back on | ||||
|     turnoff.forEach (c, idx) -> console[c] = org[idx] | ||||
|  | ||||
|     # Unfreeze and apply Markdown | ||||
|     rezHtml = rezHtml.replace /@@@@~.*?~@@@@/gm, (val) -> | ||||
|       MDIN( val.replace( /~@@@@/gm,'' ).replace( /@@@@~/gm,'' ) ) | ||||
|  | ||||
|  | ||||
| MDIN = (txt) -> # TODO: Move this | ||||
|   MD(txt || '' ).replace(/^\s*<p>|<\/p>\s*$/gi, '') | ||||
							
								
								
									
										52
									
								
								src/renderers/underscore-generator.coffee
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								src/renderers/underscore-generator.coffee
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| ###* | ||||
| Definition of the UnderscoreGenerator class. | ||||
| @license MIT. See LICENSE.md for details. | ||||
| @module underscore-generator.js | ||||
| ### | ||||
|  | ||||
|  | ||||
|  | ||||
| _ = require 'underscore' | ||||
| registerHelpers = require '../helpers/underscore-helpers' | ||||
| HMSTATUS = require '../core/status-codes' | ||||
|  | ||||
|  | ||||
| ###* | ||||
| Perform template-based resume generation using Underscore.js. | ||||
| @class UnderscoreGenerator | ||||
| ### | ||||
| UnderscoreGenerator = module.exports = | ||||
|  | ||||
|   generateSimple: ( data, tpl ) -> | ||||
|     try | ||||
|       # Compile and run the Handlebars template. | ||||
|       template = _.template( tpl ); | ||||
|       return template( data ); | ||||
|     catch | ||||
|       throw | ||||
|         fluenterror: if template then HMSTATUS.invokeTemplate else HMSTATUS.compileTemplate, | ||||
|         inner: _error | ||||
|  | ||||
|   generate: ( json, jst, format, cssInfo, opts, theme ) -> | ||||
|  | ||||
|     # Tweak underscore's default template delimeters | ||||
|     delims = (opts.themeObj && opts.themeObj.delimeters) || opts.template; | ||||
|     if opts.themeObj && opts.themeObj.delimeters | ||||
|       delims = _.mapObject delims, (val,key) -> new RegExp( val, "ig") | ||||
|     _.templateSettings = delims; | ||||
|  | ||||
|     # Strip {# comments #} | ||||
|     jst = jst.replace delims.comment, '' | ||||
|  | ||||
|     ctx = | ||||
|       r: if format == 'html' || format == 'pdf' || format == 'png' then json.markdownify() else json | ||||
|       filt: opts.filters | ||||
|       XML: require 'xml-escape' | ||||
|       RAW: json | ||||
|       cssInfo: cssInfo | ||||
|       #engine: this | ||||
|       headFragment: opts.headFragment || '' | ||||
|       opts: opts | ||||
|  | ||||
|     registerHelpers theme, opts, cssInfo, ctx, this | ||||
|     @generateSimple ctx, jst | ||||
		Reference in New Issue
	
	Block a user