1
0
mirror of https://github.com/JuanCanham/HackMyResume.git synced 2024-11-05 09:56:22 +00:00
HackMyResume/dist/utils/html-to-wpml.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-01-27 10:29:26 +00:00
(function() {
2018-02-12 05:05:29 +00:00
/**
Definition of the Markdown to WordProcessingML conversion routine.
@license MIT. Copyright (c) 2015 James Devlin / FluentDesk.
@module utils/html-to-wpml
*/
2018-02-06 13:22:31 +00:00
var HTML5Tokenizer, XML, _;
XML = require('xml-escape');
2016-01-27 10:29:26 +00:00
_ = require('underscore');
HTML5Tokenizer = require('simple-html-tokenizer');
module.exports = function(html) {
var final, is_bold, is_italic, is_link, link_url, tokens;
2018-02-12 05:05:29 +00:00
// Tokenize the HTML stream.
2016-01-27 10:29:26 +00:00
tokens = HTML5Tokenizer.tokenize(html);
final = is_bold = is_italic = is_link = link_url = '';
2018-02-12 05:05:29 +00:00
// Process <em>, <strong>, and <a> elements in the HTML stream, producing
// equivalent WordProcessingML that can be dumped into a <w:p> or other
// text container element.
2016-01-27 10:29:26 +00:00
_.each(tokens, function(tok) {
var style;
switch (tok.type) {
case 'StartTag':
switch (tok.tagName) {
case 'p':
return final += '<w:p>';
case 'strong':
return is_bold = true;
case 'em':
return is_italic = true;
case 'a':
is_link = true;
return link_url = tok.attributes.filter(function(attr) {
return attr[0] === 'href';
})[0][1];
}
break;
case 'EndTag':
switch (tok.tagName) {
case 'p':
return final += '</w:p>';
case 'strong':
return is_bold = false;
case 'em':
return is_italic = false;
case 'a':
return is_link = false;
}
break;
case 'Chars':
if ((tok.chars.trim().length)) {
style = is_bold ? '<w:b/>' : '';
style += is_italic ? '<w:i/>' : '';
style += is_link ? '<w:rStyle w:val="Hyperlink"/>' : '';
2018-02-06 13:22:31 +00:00
return final += (is_link ? '<w:hlink w:dest="' + link_url + '">' : '') + '<w:r><w:rPr>' + style + '</w:rPr><w:t>' + XML(tok.chars) + '</w:t></w:r>' + (is_link ? '</w:hlink>' : '');
2016-01-27 10:29:26 +00:00
}
}
});
return final;
};
}).call(this);
2016-02-02 02:14:36 +00:00
//# sourceMappingURL=html-to-wpml.js.map