From 2d595350c64b9cf7b48bb9b511e519f0e4a7622c Mon Sep 17 00:00:00 2001 From: hacksalot Date: Sat, 13 Feb 2016 20:40:17 -0500 Subject: [PATCH] Escape LaTeX during generation. --- dist/renderers/underscore-generator.js | 34 ++++++++++++++++++++--- package.json | 1 + src/renderers/underscore-generator.coffee | 25 +++++++++++++---- 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/dist/renderers/underscore-generator.js b/dist/renderers/underscore-generator.js index 2a1894b..91796d0 100644 --- a/dist/renderers/underscore-generator.js +++ b/dist/renderers/underscore-generator.js @@ -6,12 +6,16 @@ Definition of the UnderscoreGenerator class. */ (function() { - var UnderscoreGenerator, _, registerHelpers; + var UnderscoreGenerator, _, escapeLaTeX, registerHelpers; _ = require('underscore'); registerHelpers = require('../helpers/underscore-helpers'); + require('../utils/string'); + + escapeLaTeX = require('escape-latex'); + /** Perform template-based resume generation using Underscore.js. @@ -33,7 +37,7 @@ Definition of the UnderscoreGenerator class. } }, generate: function(json, jst, format, cssInfo, opts, theme) { - var ctx, delims; + var ctx, delims, r, traverse; delims = (opts.themeObj && opts.themeObj.delimeters) || opts.template; if (opts.themeObj && opts.themeObj.delimeters) { delims = _.mapObject(delims, function(val, key) { @@ -41,9 +45,31 @@ Definition of the UnderscoreGenerator class. }); } _.templateSettings = delims; - jst = jst.replace(delims.comment, ''); + r = null; + switch (format) { + case 'html': + r = json.markdownify(); + break; + case 'pdf': + r = json.markdownify(); + break; + case 'png': + r = json.markdownify(); + break; + case 'latex': + traverse = require('traverse'); + r = traverse(json).map(function(x) { + if (this.isLeaf && String.is(this.node)) { + return escapeLaTeX(this.node); + } + return this.node; + }); + break; + default: + r = json; + } ctx = { - r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json, + r: r, filt: opts.filters, XML: require('xml-escape'), RAW: json, diff --git a/package.json b/package.json index 873068c..3dd4684 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "chalk": "^1.1.1", "commander": "^2.9.0", "copy": "^0.1.3", + "escape-latex": "^0.1.2", "extend": "^3.0.0", "fresca": "~0.6.0", "fresh-jrs-converter": "^0.2.2", diff --git a/src/renderers/underscore-generator.coffee b/src/renderers/underscore-generator.coffee index 45ef731..015251f 100644 --- a/src/renderers/underscore-generator.coffee +++ b/src/renderers/underscore-generator.coffee @@ -8,8 +8,8 @@ Definition of the UnderscoreGenerator class. _ = require 'underscore' registerHelpers = require '../helpers/underscore-helpers' - - +require '../utils/string' +escapeLaTeX = require 'escape-latex' ###* Perform template-based resume generation using Underscore.js. @@ -41,11 +41,23 @@ UnderscoreGenerator = module.exports = delims = _.mapObject delims, (val,key) -> new RegExp val, "ig" _.templateSettings = delims; - # Strip {# comments #} - jst = jst.replace delims.comment, '' + # Massage resume strings / text + r = null + switch format + when 'html' then r = json.markdownify() + when 'pdf' then r = json.markdownify() + when 'png' then r = json.markdownify() + when 'latex' + traverse = require 'traverse' + r = traverse(json).map (x) -> + if @isLeaf && String.is @node + return escapeLaTeX @node + @node + else r = json + # Set up the context ctx = - r: if format == 'html' || format == 'pdf' || format == 'png' then json.markdownify() else json + r: r filt: opts.filters XML: require 'xml-escape' RAW: json @@ -54,5 +66,8 @@ UnderscoreGenerator = module.exports = headFragment: opts.headFragment || '' opts: opts + # Link to our helpers registerHelpers theme, opts, cssInfo, ctx, @ + + # Generate! @generateSimple ctx, jst