-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathconverter.js
226 lines (182 loc) · 6.43 KB
/
converter.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
!(function($) {
"use strict";
// Global variable with information on current workbooks. Don't judge me.
var workbooks = {};
// Another global to store LaTeX output. Don't judge me...again...
var latexOutput = {};
var excelParser = {
//latexEnvironment: 'tabular',
latexEnvironment: 'array',
latexEscape: function(text) {
if(!$('#escape').is(':checked')) return text;
var escapeRegExpr = function(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
};
var specials = ['\\', '&', '%', '$', '#', '_', '{', '}', '~', '^'];
$.each(specials, function() {
var regexp = new RegExp(escapeRegExpr(this), 'g');
text = text.replace(regexp, '\\' + this, 'g');
});
return text;
},
parseStringTable: function(data) {
var doc = $(data);
var stringTags = doc.find('si');
var strings = $.map(stringTags, function(s,i) {
return $(s).find('t').text();
});
return strings;
},
toLatex: function(table) {
var max = 0;
for(var i=0; i < table.length; i++) {
if(table[i] && table[i].length > max) { max = table[i].length; }
}
var numCols = max;
var args = [];
for(i=0; i < numCols; i++) {
args[i] = 'l';
}
args = ' | ' + args.join(' | ') + ' | ';
var latex = "\\begin{" + excelParser.latexEnvironment + "}{" + args + "}\n\\hline\n";
for(i=0; i < table.length; i++) {
var cols = table[i];
// TODO: replace "&" with "\&"
if(cols === undefined) { cols = []; }
if(cols.length < numCols) {
for(var x=cols.length; x < max; x++) {
cols[x] = '\\ ';
}
}
latex += "\t" + cols.join(' & ');
latex += " \\\\ \\hline\n";
}
latex += "\\end{" + excelParser.latexEnvironment + "}\n";
return latex;
},
processSheet: function(data, stringTable) {
// get jQuery object out of the data for accessing stuffs
var doc = $(data);
var table = [];
var rows = doc.find('sheetdata row');
$.each(rows, function(i,row) {
var rowNum = parseInt($(row).attr('r'), 10);
// get columns
var cols = $(row).find('c');
var colVals = $.map(cols, function(col,j) {
col = $(col);
var val = excelParser.latexEscape(col.find('v').text());
if(col.attr('t') == 's') {
return excelParser.latexEscape(stringTable[parseInt(val, 10)]);
} else {
return val;
}
});
table[rowNum-1] = colVals;
});
return table;
},
handleSheets: function(entries, stringTable) {
// get the workbook.xml file, which contains the names of all workbooks
var workbookMeta = entries.filter(function(entry) {
return entry.filename === 'xl/workbook.xml';
})[0];
// filter out all files that aren't worksheets
var sheets = $.grep(entries, function(n,i) {
var filter1 = /^xl\/worksheets\/.*\.xml$/;
var filter2 = /^xl\/worksheets\/_rels/;
return (filter1.test(n.filename)) && (!filter2.test(n.filename));
});
// read the workbook meta data to get the names and crap
workbookMeta.getData(new zip.TextWriter(), function(text) {
var doc = $(text);
// extract the names of the workbooks and their IDs for use later on...
$.each(doc.find('sheets sheet'), function(i, tag) {
tag = $(tag);
var id = tag.attr('sheetId');
var name = tag.attr('name');
workbooks[id] = name;
});
excelParser.updateSelect();
// iterate over all sheets and convert them to LaTeX
$.each(sheets, function(_, sheet) {
// the ID of the spreadsheet can only be found in the filename apparently :P
var id = sheet.filename.match(/(\d)\.xml/)[1];
sheet.getData(new zip.TextWriter(), function(text) {
var table = excelParser.processSheet(text, stringTable);
var latex = excelParser.toLatex(table);
latexOutput[id] = latex;
// I apologize for the hack :(
if(id === '1') {
excelParser.showOutput(1);
}
});
});
});
},
showOutput: function(id) {
var latex = latexOutput[id];
$('#latex-output').val(latex);
$('#preview').html('$$\n' + latex + '\n$$');
MathJax.Hub.Typeset("preview");
},
handleFiles: function(event) {
// prevent default browser behavior
event.stopPropagation();
event.preventDefault();
// get file information
var files = event.dataTransfer.files;
if(!files.length) {
// no files were given somehow...
$('#latex-output').val('No files were given...try again?');
return false;
}
var blob = files[0];
// unzip and process files
zip.createReader(new zip.BlobReader(blob), function(reader) {
reader.getEntries(function(entries) {
if(!entries.length) { return false; }
// get the string table
var stFile = $.grep(entries, function(n,i) {
var regexp = /^xl\/sharedStrings\.xml$/;
return regexp.test(n.filename);
})[0];
stFile.getData(new zip.TextWriter(), function(text) {
var stringTable = excelParser.parseStringTable(text);
excelParser.handleSheets(entries, stringTable);
});
return undefined;
});
});
return undefined;
},
updateSelect: function() {
var select = $('#workbook');
// clear existing option tags
select.empty();
for(var id in workbooks) {
var tag = $('<option value="' + id + '">' + workbooks[id] + '</option>');
tag.appendTo(select);
}
},
init: function() {
// hack because of jQuery shenanigans
jQuery.event.props.push('dataTransfer');
$('body').bind('dragover', function(event) {
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
});
$('body').bind('drop', excelParser.handleFiles);
// when a new workbook is selected, do stuff!
$('#workbook').change(function(event) {
var select = $(event.target);
excelParser.showOutput(select.val());
});
}
};
$(function() {
zip.workerScriptsPath = 'zip/';
excelParser.init();
});
})(window.jQuery);