forked from queryluke/fromSheetsToJekyll
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
385 lines (352 loc) · 17.3 KB
/
index.html
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>fromSheetsToJekyll</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<style>
.pageload-overlay {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
visibility: hidden;
background: rgba(0,0,0,.7);
}
.pageload-overlay.show {
visibility: visible;
}
.pageload-overlay .spin-container {
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
body {
margin-bottom: 4rem;
}
</style>
</head>
<body>
<div class="jumbotron">
<div class="container">
<h1>From Sheets to Jekyll</h1>
<p>Turn your Google Sheets into Jekyll files</p>
</div>
</div>
<div class="container">
<p class="alert alert-warning">Uploading a CSV but not seeing data in the menus? Try refreshing the page.</p>
<div class="row">
<div class="col-xs-12">
<h2>Step 1: <small>Download your Google Sheet as a CSV, upload here</small></h2>
<div class="form-group">
<label for="csvFile" class="control-label">CSV File</label>
<input type="file" id="csvFile" class="form-control">
<span class="glyphicon glyphicon-ok form-control-feedback hidden" aria-hidden="true" id="getUrlFeedback"></span>
<span id="getUrlStatus" class="sr-only">gtg!</span>
</div><!-- /input-group -->
<div id="getUrlAlert" class="hidden alert alert-success">
<h3>You're gtg, dude or dudette! <span class="glyphicon glyphicon-thumbs-up"></span></h3>
</div>
</div>
</div>
</div>
<div class="container hidden" id="apiOk">
<div class="row">
<div class="col-xs-12">
<h2>Step 2: <small>Choose what type of file you want to make</small></h2>
<table class="table">
<thead>
<tr><th>Type</th><th>Description</th></tr>
</thead>
<tbody>
<tr><td>Data File</td><td>Formatted as a jekyll data file that you can drop into your _data/ folder. Choose your key!</td></tr>
<tr><td>Template</td><td>Create a template and propogate front matter and download a zip of html files</td></tr>
<tr><td>Json</td><td>A simple json printout of your sheet. Pretty boring stuff...</td></tr>
</tbody>
</table>
<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#dataFile" aria-controls="dataFile" role="tab" data-toggle="tab">Jekyll Data File</a></li>
<li role="presentation"><a href="#template" aria-controls="templates" role="tab" data-toggle="tab">Templates</a></li>
<li role="presentation"><a href="#simpleJson" aria-controls="simpleJson" role="tab" data-toggle="tab">Json</a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="dataFile">
<h3>Make a Jekyll data file</h3>
<h4>Select A Key</h4>
<div id="dataFileKey"></div>
</div>
<div role="tabpanel" class="tab-pane" id="template">
<h3>Create html files for the _post directory</h3>
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#frontMatterModal">
Normalized Sheets Keys
</button>
<div class="form-group">
<label for="addFrontMatter">Add front matter</label>
<textarea id="addFrontMatter" class="form-control"></textarea>
<span class="help-block">
<strong>DO NOT INCLUDE THE <code>title:</code> front matter. This script will use the value from File Title below</strong><br>
To include sheets data as front matter, provide a name, colon, then the normalized lowercase sheets key wrapped in brackets, e.g. <code>location: [presentation-location]</code>
<br>Additionally, you can make a list of associated values using hyphen+space. e.g.
<br><br>
<code>
speakers:<br>
- [speaker-1]<br>
- [speaker-2]
</code><br>
<br>If the content is sensitive but you want to use it as a key to other content (e.g. email) you can add <code>|hash</code> to the end of the key.
<br>You can slugify the key by adding <code>|slug</code> to the end of the key <code>slugTitle: [title|slug]</code>.
<br>
<br>If the data is linked to a yaml data file, be sure to use the key of that entry (e.g. a speaker's email instead of the speaker's name).
</span>
</div>
<div class="form-group">
<label for="fileTitle">Title: Select the input you want to use as the file's title</label>
<select name="fileTitle" id="fileTitle" class="form-control"></select>
</div>
<div class="form-group">
<label for="jekyllContent">Content: Select the input you want to use as the content</label>
<select name="jekyllContent" id="jekyllContent" class="form-control"></select>
</div>
<div class="form-group">
<label for="fileDate">Date: Enter a date to prepend to the file titles</label>
<input type="text" class="form-control" id="fileDate" name="fileDate" value="2017-03-06">
<span class="help-block">yyyy-mm-dd</span>
</div>
<!-- Modal -->
<div class="modal fade" id="frontMatterModal" tabindex="-1" role="dialog" aria-labelledby="frontMatterModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="frontMatterModalLabel">Possible Front Matter Values</h4>
</div>
<div class="modal-body" id="frontMatterKeys">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
<div role="tabpanel" class="tab-pane" id="simpleJson"><h3>No options here</h3></div>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<h2>Step 3: <small>Boomshakalaka!</small></h2>
<button class="btn btn-default" id="boom">Boomshakalaka!</button>
</div>
</div>
<div class="row hidden" id="step5">
<div class="col-xs-12">
<h2>Step 4: <small>DATA!</small></h2>
<!-- Trigger -->
<button class="cp-btn btn" data-clipboard-target="#response">
Copy to clipboard
</button>
<pre id="response"></pre>
</div>
</div>
</div>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script src="scripts/jquery-csv.js"></script>
<script src="scripts/jszip.js"></script>
<script src="scripts/clipboard.js"></script>
<script>
$(function(){
$('#csvFile').on('change',function(){
parseFile(this.files[0]);
createForm();
});
String.prototype.hashCode = function() {
var hash = 0, i, chr, len;
if (this.length){
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
//hash |= 0; // Convert to 32bit integer
}
if(hash < 0){
hash = hash * -1;
}
}
return hash;
};
function parseFile(file){
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(event){
var csv = event.target.result;
var data = $.csv.toObjects(csv);
localStorage.setItem('table',JSON.stringify(data));
localStorage.setItem('columns',JSON.stringify(Object.keys(data[0])));
};
reader.onerror = function(){ alert('Unable to read ' + file.fileName); };
}
function normalizeKey(key){
if(key){
var normIndex = key.toLowerCase().trim();
normIndex = normIndex.replace(/[^\w\s\d]/gi,'');
normIndex = normIndex.replace(/[\s]/gi,'-');
return normIndex;
}
}
function createForm(){
var data = JSON.parse(localStorage.getItem('table'));
var columns = JSON.parse(localStorage.getItem('columns'));
if(typeof data != 'undefined'){
//unhide the rest of the form
$('#getUrlFeedback, #getUrlAlert, #apiOk').removeClass('hidden');
//Give user feedback
$('#csvFile').parents('.form-group').addClass('has-feedback has-success');
var theHTML = '<table class="table table-condensed"><tbody>';
var frontMatterKeys = '<ul class="list-unstyled">';
var contentSelect = '';
$.each(columns,function(i,v){
var normIndex = normalizeKey(v);
frontMatterKeys += '<li>'+normIndex+'</li>';
contentSelect += '<option value="'+normIndex+'">'+v+'</option>';
theHTML += '<tr><td><div class="radio"><label><input type="radio" name="dataFileKey" id="'+normIndex+'" value="'+normIndex+'">'+v+'</label></td>';
theHTML += '<td><div class="checkbox"><label for="'+normIndex+'-text"><input type="checkbox" id="'+normIndex+'-text">Text?</label></td>';
theHTML += '<td><div class="checkbox"><label for="'+normIndex+'-include"><input type="checkbox" id="'+normIndex+'-include" checked>Include?</label></td>';
theHTML += '<td><div class="checkbox"><label for="'+normIndex+'-slug"><input type="checkbox" id="'+normIndex+'-slug">Slugify?</label></td>';
theHTML += '<td><div class="checkbox"><label for="'+normIndex+'-hash"><input type="checkbox" id="'+normIndex+'-hash">Hash?</label></td></tr>';
});
theHTML += '</tbody></table>';
$('#dataFileKey').html(theHTML);
$('#frontMatterKeys').html(frontMatterKeys);
$('#jekyllContent, #fileTitle').html(contentSelect);
} else {
console.log('table is undefined')
}
}
function simpleJson(){
$('#response').html(localStorage.getItem('table'));
$('#step5').removeClass('hidden');
}
function dataFile() {
var data = JSON.parse(localStorage.getItem('table'));
var columns = JSON.parse(localStorage.getItem('columns'));
var key = normalizeKey($('[name="dataFileKey"]:checked').val());
var returnString = '';
$.each(data,function(index,value){
var dataKey = normalizeKey(value[findKey(columns,key)]);
var subspace = false;
if(dataKey){
if($('#'+key+ '-hash').prop('checked')){
returnString += dataKey.hashCode()+':\n';
} else {
returnString += dataKey+':\n';
}
} else {
returnString += '-';
subspace = true;
}
$.each(value,function(i,v){
var normIndex = normalizeKey(i);
if($('#'+normIndex+'-include').prop('checked')) {
if(subspace){
var space = ' ';
} else {
var space = ' ';
}
returnValue = v;
if($('#'+normIndex+ '-slug').prop('checked')) {
returnValue = normalizeKey(returnValue);
}
if($('#'+normIndex+ '-hash').prop('checked')) {
returnValue = returnValue.hashCode();
}
if ($('#' + normIndex + '-text').prop('checked')) {
returnValue = '"'+returnValue.replace(/\"/gm,'\\"')+'"';
}
returnString += space + normIndex + ': ' + returnValue + '\n';
subspace = false;
}
});
returnString += '\n';
});
$('#response').html(returnString);
}
function template() {
var zip = new JSZip();
var data = JSON.parse(localStorage.getItem('table'));
var contentKey = $('#jekyllContent').val();
var fileDate = $('#fileDate').val();
var fileTitleKey = $('#fileTitle').val();
$.each(data,function(index,value){
var returnString = '';
var template = $('#addFrontMatter').val();
var content = '';
var title = '';
var fileTitle = '';
$.each(value,function(i,v){
var normIndex = normalizeKey(i);
if(normIndex == fileTitleKey){
title = v;
fileTitle = v.replace(/[^\w\s\d]/gi,'');
fileTitle = fileTitle.replace(/[\s]/gi,'-')
}
if(normIndex == contentKey) {
content = v;
}
var re = new RegExp('\\['+normIndex+'\\]',"g");
template = template.replace(re,v);
var re = new RegExp('\\['+normIndex+'\\|slug\\]',"g");
var slugV = v.replace(/[^\w\d]/gi,'-');
slugV = slugV.replace(/-(-+)/g,'-').toLowerCase();
template = template.replace(re,slugV);
var re = new RegExp('\\['+normIndex+'\\|hash\\]',"g");
template = template.replace(re, v.hashCode());
});
title = title.replace(':',':');
returnString += '---\n'+template+'\n'+'title: '+title+'\n---\r\n'+content;
zip.folder('fromSheetsToJekyll').file(fileDate+'-'+fileTitle+'.md',returnString);
});
var a = document.createElement('a');
var odsFile = zip.generate({type:'blob'});
var url = window.URL.createObjectURL(odsFile);
a.href = url;
a.download = 'fromSheetsToJekyll.zip';
a.click();
}
$('#boom').click(function(){
var wtd = $('.tab-pane.active').attr('id');
switch(wtd){
case 'dataFile':
dataFile();
break;
case 'template':
template();
break;
case 'simpleJson':
simpleJson();
break;
}
$('#step5').removeClass('hidden');
});
function findKey(keys, normalized){
var correctKey = '';
$.each(keys, function(i,v){
if(normalizeKey(v) == normalized){
correctKey = v;
}
});
return correctKey;
}
var clipboard = new Clipboard('.cp-btn');
});
</script>
</html>