~ubuntu-fr-webteam/ubuntu-fr-guide/trunk

« back to all changes in this revision

Viewing changes to htdocs/14.04/desktop/jquery.syntax.js

  • Committer: YoBoY
  • Date: 2014-05-29 08:07:34 UTC
  • mfrom: (3.1.1 maj1404)
  • Revision ID: yoboy.leguesh@gmail.com-20140529080734-6r08d0p6a7n4culp
Ajout des documentations Bureau et Serveur de la 14.04
Mise à jour des documentations de la 12.04
Suppression de liens des versions non supportées

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
        This file is part of the "jQuery.Syntax" project, and is distributed under the MIT License.
 
3
        For more information, please see http://www.oriontransfer.co.nz/software/jquery-syntax
 
4
        
 
5
        Copyright (c) 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
 
6
 
 
7
        Permission is hereby granted, free of charge, to any person obtaining a copy
 
8
        of this software and associated documentation files (the "Software"), to deal
 
9
        in the Software without restriction, including without limitation the rights
 
10
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 
11
        copies of the Software, and to permit persons to whom the Software is
 
12
        furnished to do so, subject to the following conditions:
 
13
 
 
14
        The above copyright notice and this permission notice shall be included in
 
15
        all copies or substantial portions of the Software.
 
16
 
 
17
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
18
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
19
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
20
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
21
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
22
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 
23
        THE SOFTWARE.
 
24
*/
 
25
 
 
26
/*global Function: true, ResourceLoader: true, Syntax: true, alert: false, jQuery: true */
 
27
 
 
28
// ECMAScript 5! Why wasn't this done before!?
 
29
if (!Function.prototype.bind) {
 
30
        Function.prototype.bind = function (target) {
 
31
                var args = Array.prototype.slice.call(arguments, 1), fn = this;
 
32
 
 
33
                return function () {
 
34
                        return fn.apply(target, args);
 
35
                };
 
36
        };
 
37
}
 
38
 
 
39
function ResourceLoader (loader) {
 
40
        this.dependencies = {};
 
41
        this.loading = {};
 
42
        this.loader = loader;
 
43
}
 
44
 
 
45
ResourceLoader.prototype._finish = function (name) {
 
46
        var deps = this.dependencies[name];
 
47
        
 
48
        if (deps) {
 
49
                // I'm not sure if this makes me want to cry... or laugh... or kill!?
 
50
                var chain = this._loaded.bind(this, name);
 
51
                
 
52
                for (var i = 0; i < deps.length; i += 1) {
 
53
                        chain = this.get.bind(this, deps[i], chain);
 
54
                }
 
55
                
 
56
                chain();
 
57
        } else {
 
58
                this._loaded(name);
 
59
        }
 
60
};
 
61
 
 
62
ResourceLoader.prototype._loaded = function (name) {
 
63
        // When the script has been succesfully loaded, we expect the script
 
64
        // to register with this loader (i.e. this[name]).
 
65
        var resource = this[name], loading = this.loading[name];
 
66
 
 
67
        // Clear the loading list
 
68
        this.loading[name] = null;
 
69
 
 
70
        if (!resource) {
 
71
                alert("ResourceLoader: Could not load resource named " + name);
 
72
        } else {
 
73
                for (var i = 0; i < loading.length; i += 1) {
 
74
                        loading[i](resource);
 
75
                }
 
76
        }
 
77
};
 
78
 
 
79
// This function must ensure that current cannot be completely loaded until next
 
80
// is completely loaded.
 
81
ResourceLoader.prototype.dependency = function (current, next) {
 
82
        // If the resource has completely loaded, then we don't need to queue it
 
83
        // as a dependency
 
84
        if (this[next] && !this.loading[name]) {
 
85
                return;
 
86
        }
 
87
        
 
88
        if (this.dependencies[current]) {
 
89
                this.dependencies[current].push(next);
 
90
        } else {
 
91
                this.dependencies[current] = [next];
 
92
        }
 
93
};
 
94
 
 
95
// This function must be reentrant for the same name and different callbacks.
 
96
ResourceLoader.prototype.get = function (name, callback) {
 
97
        if (this.loading[name]) {
 
98
                this.loading[name].push(callback)
 
99
        } else if (this[name]) {
 
100
                callback(this[name]);
 
101
        } else {
 
102
                this.loading[name] = [callback];
 
103
                this.loader(name, this._finish.bind(this, name));
 
104
        }
 
105
};
 
106
 
 
107
var Syntax = {
 
108
        root: null, 
 
109
        aliases: {},
 
110
        styles: {},
 
111
        themes: {},
 
112
        lib: {},
 
113
        defaultOptions: {
 
114
                cacheScripts: true,
 
115
                cacheStyleSheets: true,
 
116
                theme: "base"
 
117
        },
 
118
        
 
119
        brushes: new ResourceLoader(function (name, callback) {
 
120
                name = Syntax.aliases[name] || name;
 
121
                
 
122
                Syntax.getResource('jquery.syntax.brush', name, callback);
 
123
        }),
 
124
        
 
125
        layouts: new ResourceLoader(function (name, callback) {
 
126
                Syntax.getResource('jquery.syntax.layout', name, callback);
 
127
        }),
 
128
        
 
129
        loader: new ResourceLoader(function (name, callback) {
 
130
                Syntax.getResource('jquery.syntax', name, callback);
 
131
        }),
 
132
        
 
133
        getStyles: function (path) {
 
134
                var link = jQuery('<link>');
 
135
                jQuery("head").append(link);
 
136
 
 
137
                if (!Syntax.defaultOptions.cacheStyleSheets) {
 
138
                        path = path + "?" + Math.random()
 
139
                }
 
140
                
 
141
                link.attr({
 
142
                        rel: "stylesheet",
 
143
                        type: "text/css",
 
144
                        href: path
 
145
                });
 
146
        },
 
147
        
 
148
        getScript: function (path, callback) {
 
149
                var script = document.createElement('script');
 
150
                
 
151
                // Internet Exploder
 
152
                script.onreadystatechange = function() {
 
153
                        if (this.onload && (this.readyState == 'loaded' || this.readyState == 'complete')) {
 
154
                                this.onload();
 
155
                                
 
156
                                // Ensure the function is only called once.
 
157
                                this.onload = null;
 
158
                        }
 
159
                };
 
160
                
 
161
                // Every other modern browser
 
162
                script.onload = callback;
 
163
                script.type = "text/javascript";
 
164
                
 
165
                if (!Syntax.defaultOptions.cacheScripts)
 
166
                        path = path + '?' + Math.random()
 
167
                
 
168
                script.src = path;
 
169
                
 
170
                document.getElementsByTagName('head')[0].appendChild(script);
 
171
        },
 
172
        
 
173
        getResource: function (prefix, name, callback) {
 
174
                var basename = prefix + "." + name;
 
175
                var styles = this.styles[basename];
 
176
                
 
177
                if (styles) {
 
178
                        for (var i = 0; i < styles.length; i += 1) {
 
179
                                this.getStyles(this.root + styles[i]);
 
180
                        }
 
181
                }
 
182
                
 
183
                Syntax.getScript(this.root + basename + '.js', callback);
 
184
        },
 
185
        
 
186
        alias: function (name, aliases) {
 
187
                Syntax.aliases[name] = name;
 
188
                
 
189
                for (var i = 0; i < aliases.length; i += 1) {
 
190
                        Syntax.aliases[aliases[i]] = name;
 
191
                }
 
192
        },
 
193
        
 
194
        brushAliases: function (brush) {
 
195
                var aliases = [];
 
196
                
 
197
                for (var name in Syntax.aliases) {
 
198
                        if (Syntax.aliases[name] === brush) {
 
199
                                aliases.push(name);
 
200
                        }
 
201
                }
 
202
                
 
203
                return aliases;
 
204
        },
 
205
        
 
206
        brushNames: function () {
 
207
                var names = [];
 
208
                
 
209
                for (var name in Syntax.aliases) {
 
210
                        if (name === Syntax.aliases[name]) {
 
211
                                names.push(name);
 
212
                        }
 
213
                }
 
214
                
 
215
                return names;
 
216
        },
 
217
        
 
218
        extractBrushName: function (className) {
 
219
                // brush names are by default lower case - normalize so we can detect it.
 
220
                className = className.toLowerCase();
 
221
                
 
222
                var match = className.match(/brush-([\S]+)/);
 
223
                
 
224
                if (match) {
 
225
                        return match[1];
 
226
                } else {
 
227
                        var classes = className.split(/ /);
 
228
                        
 
229
                        if (jQuery.inArray("syntax", classes) !== -1) {
 
230
                                for (var i = 0; i < classes.length; i += 1) {
 
231
                                        var name = Syntax.aliases[classes[i]];
 
232
                                        
 
233
                                        if (name) {
 
234
                                                return name;
 
235
                                        }
 
236
                                }
 
237
                        }
 
238
                }
 
239
                
 
240
                return null;
 
241
        },
 
242
        
 
243
        detectRoot: function () {
 
244
                if (Syntax.root == null) {
 
245
                        // Initialize root based on current script path.
 
246
                        var scripts = jQuery('script').filter(function(){
 
247
                                return this.src.match(/jquery\.syntax/);
 
248
                        });
 
249
 
 
250
                        var first = scripts.get(0);
 
251
 
 
252
                        if (first) {
 
253
                                // Calculate the basename for the given script src.
 
254
                                var root = first.src.match(/.*\//);
 
255
 
 
256
                                if (root) {
 
257
                                        Syntax.root = root[0];
 
258
                                }
 
259
                        }
 
260
                }
 
261
        },
 
262
        
 
263
        log: function() {
 
264
                if (typeof(console) != "undefined" && console.log) {
 
265
                        console.log.apply(console, arguments);
 
266
                } else if (window.console && window.console.log) {
 
267
                        window.console.log.apply(window.console, arguments);
 
268
                }
 
269
        }
 
270
};
 
271
 
 
272
jQuery.fn.syntax = function (options, callback) {
 
273
        Syntax.detectRoot();
 
274
        
 
275
        var elements = this;
 
276
        
 
277
        Syntax.loader.get('core', function () {
 
278
                Syntax.highlight(elements, options, callback);
 
279
        });
 
280
};
 
281
 
 
282
jQuery.syntax = function (options, callback) {
 
283
        options = options || {};
 
284
        var context = options.context;
 
285
        
 
286
        if (options.root) {
 
287
                Syntax.root = options.root;
 
288
        } else {
 
289
                Syntax.detectRoot();
 
290
        }
 
291
        
 
292
        options = jQuery.extend(Syntax.defaultOptions, options)
 
293
        
 
294
        options.blockSelector = options.blockSelector || 'pre.syntax:not(.highlighted)';
 
295
        options.inlineSelector = options.inlineSelector || 'code.syntax:not(.highlighted)';
 
296
        
 
297
        options.blockLayout = options.blockLayout || 'list';
 
298
        options.inlineLayout = options.inlineLayout || 'inline';
 
299
        
 
300
        // Allow the user to specify callbacks without replacement.
 
301
        if (typeof options.replace == "undefined")
 
302
                options.replace = true;
 
303
        
 
304
        jQuery(options.blockSelector, context).each(function () {
 
305
                jQuery(this).syntax(jQuery.extend({}, options, {
 
306
                        brush: Syntax.extractBrushName(this.className),
 
307
                        layout: options.blockLayout
 
308
                }), callback);
 
309
        });
 
310
        
 
311
        jQuery(options.inlineSelector, context).each(function () {
 
312
                jQuery(this).syntax(jQuery.extend({}, options, {
 
313
                        brush: Syntax.extractBrushName(this.className),
 
314
                        layout: options.inlineLayout
 
315
                }), callback);
 
316
        });
 
317
};