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

Improve XML encoding for Word docs.

Fix various encoding errors.
This commit is contained in:
hacksalot 2015-12-31 06:38:30 -05:00
parent 069c02ddcc
commit b85d40b1b3
4 changed files with 93 additions and 4 deletions

View File

@ -13,6 +13,7 @@ Definition of the FRESHResume class.
, __ = require('lodash')
, PATH = require('path')
, moment = require('moment')
, XML = require('xml-escape')
, MD = require('marked')
, CONVERTER = require('./convert')
, JRSResume = require('./jrs-resume');
@ -81,12 +82,90 @@ Definition of the FRESHResume class.
return JSON.stringify( obj, replacer, 2 );
};
/**
Create a copy of this resume in which all string fields have been run through
a transformation function (such as a Markdown filter or XML encoder).
*/
FreshResume.prototype.transformStrings = function( filters, transformer ) {
var that = this;
var ret = this.dupe();
// TODO: refactor recursion
function transformStringsInObject( obj ) {
if( !obj ) return;
if( moment.isMoment( obj ) ) return;
if( _.isArray( obj ) ) {
obj.forEach( function(elem, idx, ar) {
if( typeof elem === 'string' || elem instanceof String )
ar[idx] = transformer( null, elem );
else if (_.isObject(elem))
transformStringsInObject( elem );
});
}
else if (_.isObject( obj )) {
Object.keys( obj ).forEach(function(k) {
var sub = obj[k];
if( typeof sub === 'string' || sub instanceof String ) {
if( filters.length && _.contains(filters, k) )
return;
obj[k] = transformer( k, sub );
}
else if (_.isObject( sub ))
transformStringsInObject( sub );
});
}
}
Object.keys( ret ).forEach(function(member){
transformStringsInObject( ret[ member ] );
});
return ret;
};
/**
Create a copy of this resume in which all fields have been interpreted as
Markdown.
*/
FreshResume.prototype.markdownify = function() {
function MDIN( txt ){
return MD(txt || '' ).replace(/^\s*<p>|<\/p>\s*$/gi, '');
}
function trx(key, val) {
if( key === 'summary' ) {
return MD(val);
}
return MDIN(val);
}
return this.transformStrings( ['skills','url','start','end','date'], trx );
};
/**
Create a copy of this resume in which all fields have been interpreted as
Markdown.
*/
FreshResume.prototype.xmlify = function() {
function trx(key, val) {
return XML(val);
}
return this.transformStrings( [], trx );
};
/**
Create a copy of this resume in which all fields have been interpreted as
Markdown.
*/
FreshResume.prototype.markdownify2 = function() {
var that = this;
var ret = this.dupe();

View File

@ -9,6 +9,7 @@ Generic template helper definitions for HackMyResume / FluentCV.
var MD = require('marked')
, H2W = require('../utils/html-to-wpml')
, XML = require('xml-escape')
, moment = require('moment')
, _ = require('underscore');
@ -33,10 +34,12 @@ Generic template helper definitions for HackMyResume / FluentCV.
wpml: function( txt, inline ) {
if(!txt) return '';
inline = (inline && !inline.hash) || false;
txt = XML(txt.trim());
txt = inline ?
MD(txt.trim()).replace(/^\s*<p>|<\/p>\s*$/gi, '') :
MD(txt.trim());
txt = H2W( txt.trim() );
MD(txt).replace(/^\s*<p>|<\/p>\s*$/gi, '') :
MD(txt);
txt = H2W( txt );
console.log(txt);
return txt;
},

View File

@ -35,8 +35,13 @@ Definition of the HandlebarsGenerator class.
// Compile and run the Handlebars template.
var template = HANDLEBARS.compile(jst);
var encData = json;
( format === 'html' || format === 'pdf' ) && (encData = json.markdownify());
( format === 'doc' ) && (encData = json.xmlify());
return template({
r: format === 'html' || format === 'pdf' || format === 'png' ? json.markdownify() : json,
r: encData,
RAW: json,
filt: opts.filters,
cssInfo: cssInfo,

View File

@ -47,6 +47,7 @@ Definition of the Markdown to WordProcessingML conversion routine.
break;
case 'Chars':
if( tok.chars.trim().length ) {
var style = is_bold ? '<w:b/>' : '';
style += is_italic ? '<w:i/>': '';
style += is_link ? '<w:rStyle w:val="Hyperlink"/>' : '';
@ -54,6 +55,7 @@ Definition of the Markdown to WordProcessingML conversion routine.
(is_link ? ('<w:hlink w:dest="' + link_url + '">') : '') +
'<w:r><w:rPr>' + style + '</w:rPr><w:t>' + tok.chars +
'</w:t></w:r>' + (is_link ? '</w:hlink>' : '');
}
break;
}
});