-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
332 lines (286 loc) · 10.4 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="imgs/favicon.ico">
<title>Cheatsheet</title>
<link href="html/css/bootstrap.min.css" rel="stylesheet">
<link href="html/styles/github.css" rel="stylesheet">
<link href="html/css/cheatsheet.css" rel="stylesheet">
</head>
<body>
<!-- Placeholder store for transformed document -->
<div class="hide" id="csdoc"></div>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="https://github.com/billyquith/cheatsheet" target="_blank">Cheatsheet</a>
<ul class="nav navbar-nav navbar-left" id="viewchoice">
<li class="dropdown">
<a id="sheet_menu" href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
SHEET<span class="caret"></span></a>
<ul id="sheets_items" class="dropdown-menu">
</ul>
</li>
</ul>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right" id="viewchoice">
<li><a href="#">sections</a></li>
<li><a href="#">all</a></li>
</ul>
</div>
</div>
</nav>
<div class="container-fluid">
<!-- show sections -->
<div class="row" id="show_sections" style="display:none">
<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar" id="cheatsections">
</ul>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<div id="cs_section"></div>
</div>
</div>
<!-- show all -->
<div class="row" id="show_all" style="display:none">
<div class="col-sm-9 col-sm-offset-3 col-md-10 main main_show_all">
<div id="cs_all"></div>
</div>
</div>
</div>
<script src="html/jquery.min.js"></script>
<script src="html/bootstrap.min.js"></script>
<script src="html/marked.min.js"></script>
<script src="html/highlight.pack.js"></script>
<script src="sheets/config.js"></script>
<script type="text/javascript">
var g_viewchoice = null;
var g_sectchoice = null;
// get URL param by name
function getURLParam(name) {
var url = window.location.search.substring(1);
var urlVars = url.split('&');
for (var i=0; i < urlVars.length; i++) {
var kv = urlVars[i].split('=');
if (kv[0] == name) {
return kv[1];
}
}
return null;
}
function capitalise(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function transformDoc()
{
// don't need title in text as in title bar
$('#csdoc h1').remove();
// group all items between h2 sections
$('#csdoc h2').each(function(){
$(this).nextUntil('h2').wrapAll('<div></div>');
});
// table markup
$('#csdoc table').addClass('table table-striped table-bordered');
// tranform ul lists containing '::' splitters into list groups
$('#csdoc ul > li:contains( :: )')
.parent()
.addClass('vals-list')
.children('li')
.each(function() {
var txt = $(this).html();
var info = txt.split(' :: ');
$(this).replaceWith('<div class="row">'
+ '<div class="vals-value">'+info[0]+'</div>'
+ '<div class="vals-comment">'+ info[1]+'</div></div>');
});
// group all items between h2 sections
// $('#csdoc h2').each(function(){
// });
// pre-process the notes
$('#csdoc notes') // look for section containing <notes/>
.parent()
.addClass('csnotes')
.children('h4')
.each(function(){
var note = $(this).text();
$(this).nextUntil('h4').wrapAll('<div class="csnote" name="' + note + '"></div>');
});
// record the notes
var csnotes = new Object();
$('#csdoc div.csnote')
.each(function(){
var note = $(this).attr('name').toLowerCase();
var content = $(this).html();
csnotes[note] = content;
});
// make popovers for notes referenced
$('#csdoc note')
.replaceWith(function(){
var ref = $(this).attr('ref');
// console.log('ref ' + ref);
if (typeof ref == 'undefined') { // no ref, inline
return notePopOver(glyph('file'), "Note:", $(this).html());
}
var nref = ref.toLowerCase();
if (!(nref in csnotes)) { // bad/undefined ref name
return '<span class="note-unknown">e.g. ref undefined: "' + ref + '"</span>';
}
return notePopOver(glyph('file'), ref, csnotes[nref]);
});
// open external links in a new tab
$('#csdoc a')
.filter(function() {
return this.hostname != window.location.hostname;
}).attr('target', '_blank');
}
// reformat note as popover in HTML
// - this is necessary as can't use inline string (data-content)
function notePopOver(reftext, title, content)
{
var trigger = '<a href="#" class="potrigger">' + reftext + '</a>';
var pohead = '<div class="pohead hide">' + title + '</div>';
var pocont = '<div class="pocontent hide">' + content + '</div>';
return '<span class="popover-markup">' + trigger + pohead + pocont + '</span>';
}
function glyph(name) {
return '<span class="glyphicon glyphicon-' + name + '"></span>';
}
var entityMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'/': '/',
'`': '`',
'=': '='
};
function escapeHtml(string) {
return String(string).replace(/[&<>"'`=\/]/g, function (s) {
return entityMap[s];
});
}
function showSectionChoice() {
$('#csdoc h2').filter(function() {
if ($(this).text() == g_sectchoice) {
// show section
$('#cs_section').empty();
$(this).next().clone().appendTo('#cs_section');
}
});
}
function showAllSections() {
$('#cs_section').empty();
$('#cs_all').empty();
$('#csdoc').children().clone().appendTo('#cs_all');
}
function updateView() {
// swap
if (g_viewchoice == 'all') {
// show all
$('#show_sections').attr('style', 'display:none');
$('#show_all').attr('style', 'display:block');
showAllSections();
}
else {
// show section
$('#cs_all').empty();
$('#show_all').attr('style', 'display:none');
$('#show_sections').attr('style', 'display:block');
showSectionChoice();
}
// I don't think we can copy popovers so need to format here.
$('.popover-markup > .potrigger').popover({
html : true,
title: function() {
return $(this).parent().find('.pohead').html();
},
content: function() {
return $(this).parent().find('.pocontent').html();
},
container: 'body',
placement: 'bottom',
trigger:'hover'
});
}
function loadSheet(sheetName)
{
var cheatFileUrl = 'sheets/' + sheetName + '.md';
try {
// reset
$('#csdoc').empty();
$('#cheatsections').empty();
console.log('requesting: ' + cheatFileUrl);
$.get(cheatFileUrl, function(data) {
var doc = marked(data); // transform markdown
$('#csdoc').append(doc);
transformDoc();
// populate sheets available menu
$('#sheet_menu').text(capitalise(sheetName));
$('#sheet_menu').append(' <span class="caret"/>');
// populate sidebar
$('#csdoc h2').each(function() {
$('#cheatsections').append('<li><a href="#">' + $(this).text() + '</a></li>');
});
$('#cheatsections a').click(function(e) {
// select a section choice
g_sectchoice = $(this).text();
$(this).parent().siblings().removeClass('active');
$(this).parent().addClass('active');
updateView();
});
// default view
g_viewchoice = 'sections';
$("#viewchoice li").first().addClass('active');
// respond to view selection
$('#navbar a').click(function (){
$(this).parent().siblings().removeClass('active');
$(this).parent().addClass('active');
g_viewchoice = $(this).text();
updateView();
});
// select first section by default
g_sectchoice = $('#csdoc h2').first().text();
$('#cheatsections li').first().addClass('active');
updateView();
});
} catch (e) {
console.log(e);
}
}
$(document).ready(function()
{
marked.setOptions({
highlight: function (code) {
return hljs.highlightAuto(code).value;
}
});
var sheetName = getURLParam('sheet') || 'eg';
loadSheet(sheetName);
// sheet selection
var esheets = $('#sheets_items');
for (var i=0; i<sheets.length; ++i) {
var item = '<li><a href="#" sheet="'+sheets[i]+'">'+capitalise(sheets[i])+'</a></li>';
esheets.append(item);
}
esheets.append('<li role="separator" class="divider"></li>');
esheets.append('<li><a href="#" sheet="eg">Example</a></li>');
// sheet selection action
$('a[sheet]').each(function(){
var lnk = $(this).attr('sheet');
$(this).click(function(){
// console.log('chosen ' + lnk);
loadSheet(lnk);
});
});
});
</script>
</body>
</html>