-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocs.js
362 lines (323 loc) · 12.8 KB
/
docs.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
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Ubiquity.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Atul Varma <[email protected]>
* Sander Dijkhuis <[email protected]>
* Alberto Santini <[email protected]>
* Simone Deponti <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
(function($) {
// = App =
//
// This is the application that processes the code and lets the user
// navigate through and read the documentation.
//
// It lives in {{{window.code_illuminated}}} where it can be fetched
// to apply customizations.
if((typeof window.code_illuminated) === "undefined")
window.code_illuminated = {};
var App = window.code_illuminated;
// == App.trim() ==
//
// Returns {{{str}}} without whitespace at the beginning and the end.
App.trim = function trim(str) {
return str.replace(/^\s+|\s+$/g,"");
};
// == App.processors ==
//
// An array of user-defined processor functions. They should take one
// argument, the DOM node containing the documentation. User-defined
// processor functions are called after standard processing is done.
App.processors = [];
App.menuItems = {}; // Has a {label, urlOrCallback} dict for each keyword.
// == App.CREOLE ==
//
// The configurations bit of the creole markup,
// mostly used to add [[http://www.wikicreole.org/wiki/Creole1.0#section-Creole1.0-LinksInternalExternalAndInterwiki|interwiki]] links.
//
// As of now, the linked interwikis are:
// * {{{WikiCreole}}}, http://www.wikicreole.org/wiki/
// * {{{Wikipedia}}}, http://en.wikipedia.org/wiki/
// * {{{MDNJavascript}}}, https://developer.mozilla.org/en/JavaScript/Reference/
// * {{{MDNCSS}}}, https://developer.mozilla.org/en-US/docs/CSS/
// * {{{MDN}}}, https://developer.mozilla.org/en/
//
// Further interwikis can be added by adding the following javascript
// at the very bottom of the documentation index page (HTML):
// {{{
// <script type="text/javascript">
// window.code_illuminated.CREOLE.interwiki['Foo'] = 'http://example.com/';
// </script>
// }}}
App.CREOLE = {
forIE: document.all,
interwiki: {
WikiCreole: 'http://www.wikicreole.org/wiki/',
Wikipedia: 'http://en.wikipedia.org/wiki/',
MDNCSS: 'https://developer.mozilla.org/en-US/docs/CSS/',
MDNJavascript: 'https://developer.mozilla.org/en/JavaScript/Reference/',
MDN: 'https://developer.mozilla.org/en/'
},
linkFormat: ''
};
// == App.processCode() ==
//
// Splits {{{code}}} in documented blocks and puts them in {{{div}}}.
// The used structure for each block is:
// {{{
// <div class="documentation"> (...) </div>
// <pre class="code prettyprint"> (...) </pre>
// <div class="divider"/>
// }}}
// Documentation is parsed using [[http://wikicreole.org/|Creole]].
App.processCode = function processCode(code, div) {
var lines = code.replace(/\r\n/g,'\n').replace(/\r/g,'\n').split('\n');
var blocks = [];
var blockText = "";
var codeText = "";
var firstCommentLine;
var lastCommentLine;
function maybeAppendBlock() {
if (blockText)
blocks.push({text: blockText,
lineno: firstCommentLine,
numLines: lastCommentLine - firstCommentLine + 1,
code: codeText});
}
$.each(
lines,
function(lineNum) {
var line = this;
var isCode = true;
var isComment = (App.trim(line).indexOf("//") === 0);
if (isComment) {
var startIndex = line.indexOf("//");
var text = App.trim(line.slice(startIndex + 3));
if (lineNum == lastCommentLine + 1) {
blockText += text + "\n";
lastCommentLine += 1;
isCode = false;
} else if (text.charAt(0) == "=" || text.charAt(0) == "*") {
maybeAppendBlock();
firstCommentLine = lineNum;
lastCommentLine = lineNum;
blockText = text + "\n";
codeText = "";
isCode = false;
}
}
if (isCode) {
var sublines = 0;
var offset = 0;
while(offset < line.length) {
var subline_length = App.CHARS_PER_ROW-2;
if(sublines === 0)
subline_length = App.CHARS_PER_ROW-1;
var subline = line.substr(offset, subline_length);
offset += subline_length;
codeText += subline;
if((line.length - offset) > 0)
codeText += "↩\r\n↪";
else
codeText += "\r\n";
}
}
});
maybeAppendBlock();
var creole = new WikiCreole.Creole(App.CREOLE);
$.each(
blocks,
function(i) {
var docs = $('<div class="documentation">');
$(docs).css(App.docColumnCss);
creole.parse(docs.get(0), this.text);
$(div).append(docs);
var top_padding = 0;
$('p:first', docs).prevAll().each(function() {
top_padding += $(this).outerHeight(true);
});
var code = $('<pre class="code prettyprint">');
$(code).css(App.codeColumnCss);
if(top_padding)
$(code).css({ 'padding-top': top_padding });
code.text(this.code);
$(div).append(code);
// Make sure the block ends with a blank line to make it high
// enough. For IE8 an extra space is needed, because otherwise
// the \n is ignored. FIXME: This doesn't fix issue 13 in IE7
// yet.
code.append('\n ');
var docsSurplus = docs.height() - code.height() + 1;
if (docsSurplus > 0)
code.css({paddingBottom: docsSurplus + "px"});
$(div).append('<div class="divider">');
});
// Run the user-defined processors.
$.each(
App.processors,
function(i) {
App.processors[i]($(div).find(".documentation"));
});
};
// == App.currentPage ==
//
// The current displayed document.
App.currentPage = null;
// == App.currentPage ==
//
// The current {{{window.location.hash}}}.
// If the monitoring routing detects a difference between this value
// and {{{window.location.hash}}},
// the user is navigated to the new page or element.
App.currentHash = null;
// == App.pages ==
//
// The has containing all the pages (Javascript files) for this folder.
App.pages = {};
// == App.scrollTo() ==
//
// Searches the page for a title ({{{h[123456]}}} element)
// whose content contains {{{element}}}.
//
// If such an item is found, scrolls the page down to that element.
//
// This is used for intra-documentation links,
// as implemented by [[#[email protected]()|App.navigate()]]
App.scrollTo = function scrollTo(element) {
var selected = null;
if(element) {
$('h1,h2,h3,h4,h5,h6').each(function() {
if ((selected === null) &&
(App.trim($(this).text()) == element))
selected = $(this);
});
}
if (selected)
$(document).scrollTop(selected.offset().top);
else
$(document).scrollTop(0);
};
// == App.navigate() ==
//
// Navigates to a different view if needed. The appropriate view is
// fetched from the URL hash. If that is empty, the original page content
// is shown.
App.navigate = function navigate() {
var newPage;
var element = null;
if (window.location.hash) {
var components = window.location.hash.split('@', 2);
newPage = components[0].slice(1);
if (components.length > 0)
element = components[1];
}
else {
newPage = "overview";
}
if (App.currentPage)
$(App.pages[App.currentPage]).hide();
if (!App.pages[newPage]) {
var newDiv = $("<div>");
newDiv.attr("name", newPage);
$("#content").append(newDiv);
App.pages[newPage] = newDiv;
$.get(newPage,
{},
function(code) {
App.processCode(code, newDiv);
prettyPrint();
},
"text");
}
$(App.pages[newPage]).show();
App.currentPage = newPage;
App.scrollTo(element);
};
// == App.CHARS_PER_ROW ==
//
// The number of characters per row on the code pane.
// Code lines that are longer than {{{App.CHARS_PER_ROW}}}
// are wrapped a la Emacs.
App.CHARS_PER_ROW = 80;
// == App.initColumnSizes() ==
//
// Looks at the viewport and determines the width of the panes,
// appliying the needed CSS styles to them.
App.initColumnSizes = function initSizes() {
// Get the width of a single monospaced character of code.
var oneCodeCharacter = $('<div class="code">M</div>');
$("#content").append(oneCodeCharacter);
App.charWidth = oneCodeCharacter.width();
$(oneCodeCharacter).remove();
var totalWidth = $("#content").innerWidth();
var padding = App.charWidth * 2;
var paddingPercentage = Math.round(((padding * 2) / totalWidth) * 100);
App.columnWidth = Math.floor(
((App.charWidth * App.CHARS_PER_ROW) /
totalWidth) * 100) + paddingPercentage;
// Dynamically determine the column widths and padding based on
// the font size.
App.codeColumnCss = {
width: App.columnWidth+"%",
paddingLeft: padding,
paddingRight: padding
};
App.docColumnCss = {
width: (97 - paddingPercentage - App.columnWidth) + "%",
paddingLeft: padding,
paddingRight: padding
};
$(".documentation").css(App.docColumnCss);
$(".code").css(App.codeColumnCss);
};
// == Initialization code ==
//
// Loads the {{{App}}}, initializes it,
// sets up the location monitoring
// and launches navigation.
$(window).ready(
function() {
App.pages["overview"] = $("#overview").get(0);
App.initColumnSizes();
window.setInterval(
function() {
if(window.location.hash &&
window.location.hash != App.currentHash) {
App.navigate();
App.currentHash = window.location.hash;
}
},
100);
App.navigate();
});
})(jQuery);