forked from carboneio/carbone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocessor.js
162 lines (153 loc) · 5.78 KB
/
preprocessor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
var path = require('path');
var helper = require('./helper');
var preprocessor = {
/**
* Execute preprocessor on main, and embedded document
* @param {Object} template
* @param {Function} callback
*/
execute : function (template, callback) {
if (template === null || template.files === undefined) {
return callback(null, template);
}
for (var i = -1; i < template.embeddings.length; i++) {
var _mainOrEmbeddedTemplate = template.filename;
var _parentFilter = '';
if (i > -1) {
_mainOrEmbeddedTemplate = _parentFilter = template.embeddings[i];
}
var _fileType = path.extname(_mainOrEmbeddedTemplate);
switch (_fileType) {
case '.xlsx':
preprocessor.convertSharedStringToInlineString(template, _parentFilter);
break;
default:
break;
}
}
return callback(null, template);
},
/**
* [XLSX] Convert shared string to inline string in Excel format in order to be compatible with Carbone algorithm
* @param {Object} template (modified)
* @param {String} parentFilter apply the transformation on a specific embedded file
* @return {Object} the modified template
*/
convertSharedStringToInlineString : function (template, parentFilter) {
var _sharedStrings = [];
var _filesToConvert = [];
var _sharedStringIndex = -1;
// parse all files and find shared strings first
for (var i = 0; i < template.files.length; i++) {
var _file = template.files[i];
if (_file.parent === parentFilter) {
if (/sharedStrings\.xml$/.test(_file.name) === true) {
_sharedStringIndex = i;
_sharedStrings = preprocessor.readSharedString(_file.data);
}
else if (/\.xml$/.test(_file.name) === true) {
if (_file.name.indexOf('sheet') !== -1) {
_filesToConvert.push(_file);
}
}
}
}
preprocessor.removeOneFile(template, _sharedStringIndex, parentFilter);
// once shared string is found, convert files
for (var f = 0; f < _filesToConvert.length; f++) {
var _modifiedFile = _filesToConvert[f];
_modifiedFile.data = preprocessor.removeRowCounterInWorksheet(
preprocessor.convertToInlineString(_modifiedFile.data, _sharedStrings)
);
}
return template;
},
/**
* [XLSX] Remove one file in the template and its relations
* @param {Object} template
* @param {Integer} indexOfFileToRemove index of the file to remove
* @param {String} parentFilter filter to modify only an embedded document
* @return {} it modifies the template directly
*/
removeOneFile : function (template, indexOfFileToRemove, parentFilter) {
if (indexOfFileToRemove < 0 || indexOfFileToRemove >= template.files.length) {
return;
}
var _fileToRemove = template.files[indexOfFileToRemove];
var _dirname = path.dirname(_fileToRemove.name);
var _basename = path.basename(_fileToRemove.name);
template.files.splice(indexOfFileToRemove, 1);
for (var i = 0; i < template.files.length; i++) {
var _file = template.files[i];
if (_file.parent === parentFilter) {
// remove relations
if (_dirname + '/_rels/workbook.xml.rels' === _file.name) {
var _regExp = new RegExp('<Relationship [^>]*Target="' + helper.regexEscape(_basename) + '"[^>]*\/>');
_file.data = _file.data.replace(_regExp, '');
}
}
}
},
/**
* [XLSX] Parse and generate an array of shared string
* @param {String} sharedStringXml shared string content
* @return {Array} array
*/
readSharedString : function (sharedStringXml) {
var _sharedStrings = [];
if (sharedStringXml === null || sharedStringXml === undefined) {
return _sharedStrings;
}
var _tagRegex = new RegExp('<si>(.+?)<\/si>','g');
var _tag = _tagRegex.exec(sharedStringXml);
while (_tag !== null) {
_sharedStrings.push(_tag[1]);
_tag = _tagRegex.exec(sharedStringXml);
}
return _sharedStrings;
},
/**
* [XLSX] Inject shared string in sheets
* @param {String} xml sheets where to insert shared strings
* @param {Array} sharedStrings shared string
* @return {String} updated xml
*/
convertToInlineString : function (xml, sharedStrings) {
if (typeof(xml) !== 'string') {
return xml;
}
// find all tags which have attribute t="s" (type = shared string)
var _inlinedXml = xml.replace(/(<(\w)[^>]*t="s"[^>]*>)(.*?)(<\/\2>)/g, function (m, openTag, tagName, content, closeTag) {
// change type of tag to "inline string"
var _newXml = openTag.replace('t="s"', 't="inlineStr"');
// get the index of shared string
var _tab = /<v>(\d+?)<\/v>/.exec(content);
if (_tab instanceof Array && _tab.length > 0) {
// replace the index by the string
var _sharedStringIndex = parseInt(_tab[1], 10);
_newXml += '<is>' + sharedStrings[_sharedStringIndex] + '</is>' + closeTag;
return _newXml;
}
// if something goes wrong, do nothing
return m;
});
return _inlinedXml;
},
/**
* [XLSX] Remove row and column counter (r=1, c=A1) in sheet (should be added in post-processing)
* Carbone Engine cannot update these counter itself
* @param {String} xml sheet
* @return {String} sheet updated
*/
removeRowCounterInWorksheet : function (xml) {
if (typeof(xml) !== 'string') {
return xml;
}
return xml.replace(/<(?:c|row)[^>]*(r="\S+")[^>]*>/g, function (m, rowValue) {
return m.replace(rowValue, '');
}).replace(/<(?:c|row)[^>]*(spans="\S+")[^>]*>/g, function (m, rowValue) {
return m.replace(rowValue, '');
});
}
};
module.exports = preprocessor;