padrino-angularjs 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format specdoc
2
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in padrino-angularjs.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Takeshi Yabe
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
1
+ # Padrino AngularJS
2
+
3
+ This gem wraps the [Andular.js](http://angularjs.org/) library for use in Padrino and above.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'padrino-angularjs'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install padrino-angularjs
18
+
19
+ ## Usage
20
+
21
+ Install Angular.js library for your app:
22
+
23
+ $ padrino g angularjs_install
24
+
25
+ Options|Default|Aliases|Description
26
+ -------|-------|-------|-----------
27
+ root |. |-r |specify the root destination path
28
+ app |/app |-a |specify the application
29
+ destroy|false |-d |removes all generated files
30
+
31
+ You'll be ready to go!
32
+
33
+ If you require optional angular files, you may include them as well in your Javascript manifest file (*app*/assets/javascripts/application.js)
34
+ For example:
35
+
36
+ //= require angular-resource
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,19 @@
1
+ require "padrino/angularjs/version"
2
+
3
+ module Padrino
4
+ module Angularjs
5
+ def self.assets_path
6
+ @assets_path ||= Dir[File.expand_path(File.dirname(__FILE__) + '../../../vendor/assets/{javascripts}')]
7
+ end
8
+ end
9
+ end
10
+
11
+ ##
12
+ # Now we need to add angularjs generators to padrino-gen
13
+ #
14
+ begin
15
+ require 'padrino-gen'
16
+ Padrino::Generators.load_paths << Dir[File.dirname(__FILE__) + '/angularjs/generators/{install}.rb']
17
+ rescue LoadError
18
+ # Fail silently
19
+ end
@@ -0,0 +1,74 @@
1
+ module Padrino
2
+ module Angularjs
3
+ module Generators
4
+ class Install < Thor::Group
5
+ Padrino::Generators.add_generator(:angularjs_install, self)
6
+
7
+ def self.source_root; File.expand_path(File.dirname(__FILE__)); end
8
+ def self.banner; 'padrino-gen angularjs_install'; end
9
+
10
+ include Thor::Actions
11
+ include Padrino::Generators::Actions
12
+
13
+ desc "Description:\n\n\tSetup AngularJS assets on your Padrino Apprication"
14
+
15
+ class_option :root, :desc => 'The root destination', :aliases => '-r', :default => '.', :type => :string
16
+ class_option :app, :desc => 'The application destination path', :aliases => '-a', :default => '/app', :type => :string
17
+ class_option :destroy, :aliases => '-d', :default => false, :type => :boolean
18
+ class_option :help, :desc => 'Show help usage', :type => :boolean
19
+
20
+ def add_assets
21
+ self.destination_root = options[:root]
22
+ if in_app_root?
23
+ app = options[:app]
24
+ check_app_existence(app)
25
+ self.behavior = :revoke if options[:destroy]
26
+
27
+ unless options[:destroy]
28
+ require_dependencies('padrino-sprockets', :require => 'padrino/sprockets', :git => 'https://github.com/nightsailer/padrino-sprockets.git')
29
+ end
30
+
31
+ javascript_root = destination_root(app, '/assets/javascripts')
32
+ javascript_file = File.join(javascript_root, 'application.js')
33
+ if File.exist?(javascript_file)
34
+ insert_into_file javascript_file, "//= require angular\n", :before => "//= require_tree \.\n"
35
+ else
36
+ empty_directory javascript_root
37
+ template 'templates/application.js.tt', javascript_file
38
+ end
39
+
40
+ angularjs_register = <<-ANGULARJS
41
+
42
+ register Padrino::Sprockets
43
+ sprockets :paths => Padrino::Angularjs.assets_path
44
+ ANGULARJS
45
+
46
+ inject_into_file destination_root(app, 'app.rb'), angularjs_register, :after => "register Padrino::Helpers\n"
47
+
48
+ return if self.behavior == :revoke
49
+
50
+ say
51
+ say '='*65, :green
52
+ say 'All set for use the AngularJS! Next, follow these steps:', :green
53
+ say '='*65, :green
54
+ say 'Run "bundle install"'
55
+ say '='*65, :green
56
+ say
57
+
58
+ else
59
+ say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
60
+ end
61
+ end
62
+
63
+ def detect_js_format
64
+
65
+ end
66
+
67
+ def detect_css_format
68
+
69
+ end
70
+
71
+ end # Install
72
+ end # Angularjs
73
+ end # Generators
74
+ end # Padrino
@@ -0,0 +1,14 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require angular
14
+ //= require_tree .
@@ -0,0 +1,5 @@
1
+ module Padrino
2
+ module Angularjs
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'padrino/angularjs/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "padrino-angularjs"
8
+ gem.version = Padrino::Angularjs::VERSION
9
+ gem.authors = ["Takeshi Yabe"]
10
+ gem.email = ["tyabe@nilidea.com"]
11
+ gem.description = %q{This gem to use AngularJS to a Padrino apps}
12
+ gem.summary = %q{AngularJS on Padrino}
13
+ gem.homepage = "https://github.com/tyabe/padrino-angularjs#readme"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency "rake"
22
+ end
@@ -0,0 +1,9 @@
1
+ describe Padrino::Angularjs do
2
+ it 'should have a version number' do
3
+ Padrino::Angularjs::VERSION.should_not be_nil
4
+ end
5
+
6
+ it 'should do something useful' do
7
+ false.should be_true
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'padrino/angularjs'
@@ -0,0 +1,1833 @@
1
+ /**
2
+ * @license AngularJS v1.0.4
3
+ * (c) 2010-2012 Google, Inc. http://angularjs.org
4
+ * License: MIT
5
+ */
6
+ (function(window, angular, undefined) {
7
+ 'use strict';
8
+
9
+ var directive = {};
10
+ var service = { value: {} };
11
+
12
+ var DEPENDENCIES = {
13
+ 'angular.js': 'http://code.angularjs.org/' + angular.version.full + 'angular.min.js',
14
+ 'angular-resource.js': 'http://code.angularjs.org/' + angular.version.full + 'angular-resource.min.js',
15
+ 'angular-sanitize.js': 'http://code.angularjs.org/' + angular.version.full + 'angular-sanitize.min.js',
16
+ 'angular-cookies.js': 'http://code.angularjs.org/' + angular.version.full + 'angular-cookies.min.js'
17
+ };
18
+
19
+
20
+ function escape(text) {
21
+ return text.
22
+ replace(/\&/g, '&amp;').
23
+ replace(/\</g, '&lt;').
24
+ replace(/\>/g, '&gt;').
25
+ replace(/"/g, '&quot;');
26
+ }
27
+
28
+ /**
29
+ * http://stackoverflow.com/questions/451486/pre-tag-loses-line-breaks-when-setting-innerhtml-in-ie
30
+ * http://stackoverflow.com/questions/195363/inserting-a-newline-into-a-pre-tag-ie-javascript
31
+ */
32
+ function setHtmlIe8SafeWay(element, html) {
33
+ var newElement = angular.element('<pre>' + html + '</pre>');
34
+
35
+ element.html('');
36
+ element.append(newElement.contents());
37
+ return element;
38
+ }
39
+
40
+
41
+ directive.jsFiddle = function(getEmbeddedTemplate, escape, script) {
42
+ return {
43
+ terminal: true,
44
+ link: function(scope, element, attr) {
45
+ var name = '',
46
+ stylesheet = '<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">\n',
47
+ fields = {
48
+ html: '',
49
+ css: '',
50
+ js: ''
51
+ };
52
+
53
+ angular.forEach(attr.jsFiddle.split(' '), function(file, index) {
54
+ var fileType = file.split('.')[1];
55
+
56
+ if (fileType == 'html') {
57
+ if (index == 0) {
58
+ fields[fileType] +=
59
+ '<div ng-app' + (attr.module ? '="' + attr.module + '"' : '') + '>\n' +
60
+ getEmbeddedTemplate(file, 2);
61
+ } else {
62
+ fields[fileType] += '\n\n\n <!-- CACHE FILE: ' + file + ' -->\n' +
63
+ ' <script type="text/ng-template" id="' + file + '">\n' +
64
+ getEmbeddedTemplate(file, 4) +
65
+ ' </script>\n';
66
+ }
67
+ } else {
68
+ fields[fileType] += getEmbeddedTemplate(file) + '\n';
69
+ }
70
+ });
71
+
72
+ fields.html += '</div>\n';
73
+
74
+ setHtmlIe8SafeWay(element,
75
+ '<form class="jsfiddle" method="post" action="http://jsfiddle.net/api/post/library/pure/" target="_blank">' +
76
+ hiddenField('title', 'AngularJS Example: ' + name) +
77
+ hiddenField('css', '</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> \n' +
78
+ stylesheet +
79
+ script.angular +
80
+ (attr.resource ? script.resource : '') +
81
+ '<style>\n' +
82
+ fields.css) +
83
+ hiddenField('html', fields.html) +
84
+ hiddenField('js', fields.js) +
85
+ '<button class="btn btn-primary"><i class="icon-white icon-pencil"></i> Edit Me</button>' +
86
+ '</form>');
87
+
88
+ function hiddenField(name, value) {
89
+ return '<input type="hidden" name="' + name + '" value="' + escape(value) + '">';
90
+ }
91
+ }
92
+ }
93
+ };
94
+
95
+
96
+ directive.code = function() {
97
+ return {restrict: 'E', terminal: true};
98
+ };
99
+
100
+
101
+ directive.prettyprint = ['reindentCode', function(reindentCode) {
102
+ return {
103
+ restrict: 'C',
104
+ terminal: true,
105
+ compile: function(element) {
106
+ element.html(window.prettyPrintOne(reindentCode(element.html()), undefined, true));
107
+ }
108
+ };
109
+ }];
110
+
111
+
112
+ directive.ngSetText = ['getEmbeddedTemplate', function(getEmbeddedTemplate) {
113
+ return {
114
+ restrict: 'CA',
115
+ priority: 10,
116
+ compile: function(element, attr) {
117
+ setHtmlIe8SafeWay(element, escape(getEmbeddedTemplate(attr.ngSetText)));
118
+ }
119
+ }
120
+ }]
121
+
122
+
123
+ directive.ngHtmlWrap = ['reindentCode', 'templateMerge', function(reindentCode, templateMerge) {
124
+ return {
125
+ compile: function(element, attr) {
126
+ var properties = {
127
+ head: '',
128
+ module: '',
129
+ body: element.text()
130
+ },
131
+ html = "<!doctype html>\n<html ng-app{{module}}>\n <head>\n{{head:4}} </head>\n <body>\n{{body:4}} </body>\n</html>";
132
+
133
+ angular.forEach((attr.ngHtmlWrap || '').split(' '), function(dep) {
134
+ if (!dep) return;
135
+ dep = DEPENDENCIES[dep] || dep;
136
+
137
+ var ext = dep.split(/\./).pop();
138
+
139
+ if (ext == 'css') {
140
+ properties.head += '<link rel="stylesheet" href="' + dep + '" type="text/css">\n';
141
+ } else if(ext == 'js') {
142
+ properties.head += '<script src="' + dep + '"></script>\n';
143
+ } else {
144
+ properties.module = '="' + dep + '"';
145
+ }
146
+ });
147
+
148
+ setHtmlIe8SafeWay(element, escape(templateMerge(html, properties)));
149
+ }
150
+ }
151
+ }];
152
+
153
+
154
+ directive.ngSetHtml = ['getEmbeddedTemplate', function(getEmbeddedTemplate) {
155
+ return {
156
+ restrict: 'CA',
157
+ priority: 10,
158
+ compile: function(element, attr) {
159
+ setHtmlIe8SafeWay(element, getEmbeddedTemplate(attr.ngSetHtml));
160
+ }
161
+ }
162
+ }];
163
+
164
+
165
+ directive.ngEvalJavascript = ['getEmbeddedTemplate', function(getEmbeddedTemplate) {
166
+ return {
167
+ compile: function (element, attr) {
168
+ var script = getEmbeddedTemplate(attr.ngEvalJavascript);
169
+
170
+ try {
171
+ if (window.execScript) { // IE
172
+ window.execScript(script || '""'); // IE complains when evaling empty string
173
+ } else {
174
+ window.eval(script);
175
+ }
176
+ } catch (e) {
177
+ if (window.console) {
178
+ window.console.log(script, '\n', e);
179
+ } else {
180
+ window.alert(e);
181
+ }
182
+ }
183
+ }
184
+ };
185
+ }];
186
+
187
+
188
+ directive.ngEmbedApp = ['$templateCache', '$browser', '$rootScope', '$location', function($templateCache, $browser, docsRootScope, $location) {
189
+ return {
190
+ terminal: true,
191
+ link: function(scope, element, attrs) {
192
+ var modules = [];
193
+
194
+ modules.push(['$provide', function($provide) {
195
+ $provide.value('$templateCache', $templateCache);
196
+ $provide.value('$anchorScroll', angular.noop);
197
+ $provide.value('$browser', $browser);
198
+ $provide.provider('$location', function() {
199
+ this.$get = ['$rootScope', function($rootScope) {
200
+ docsRootScope.$on('$locationChangeSuccess', function(event, oldUrl, newUrl) {
201
+ $rootScope.$broadcast('$locationChangeSuccess', oldUrl, newUrl);
202
+ });
203
+ return $location;
204
+ }];
205
+ this.html5Mode = angular.noop;
206
+ });
207
+ $provide.decorator('$timeout', ['$rootScope', '$delegate', function($rootScope, $delegate) {
208
+ return angular.extend(function(fn, delay) {
209
+ if (delay && delay > 50) {
210
+ return setTimeout(function() {
211
+ $rootScope.$apply(fn);
212
+ }, delay);
213
+ } else {
214
+ return $delegate.apply(this, arguments);
215
+ }
216
+ }, $delegate);
217
+ }]);
218
+ $provide.decorator('$rootScope', ['$delegate', function(embedRootScope) {
219
+ docsRootScope.$watch(function embedRootScopeDigestWatch() {
220
+ embedRootScope.$digest();
221
+ });
222
+ return embedRootScope;
223
+ }]);
224
+ }]);
225
+ if (attrs.ngEmbedApp) modules.push(attrs.ngEmbedApp);
226
+
227
+ element.bind('click', function(event) {
228
+ if (event.target.attributes.getNamedItem('ng-click')) {
229
+ event.preventDefault();
230
+ }
231
+ });
232
+ angular.bootstrap(element, modules);
233
+ }
234
+ };
235
+ }];
236
+
237
+ service.reindentCode = function() {
238
+ return function (text, spaces) {
239
+ if (!text) return text;
240
+ var lines = text.split(/\r?\n/);
241
+ var prefix = ' '.substr(0, spaces || 0);
242
+ var i;
243
+
244
+ // remove any leading blank lines
245
+ while (lines.length && lines[0].match(/^\s*$/)) lines.shift();
246
+ // remove any trailing blank lines
247
+ while (lines.length && lines[lines.length - 1].match(/^\s*$/)) lines.pop();
248
+ var minIndent = 999;
249
+ for (i = 0; i < lines.length; i++) {
250
+ var line = lines[0];
251
+ var reindentCode = line.match(/^\s*/)[0];
252
+ if (reindentCode !== line && reindentCode.length < minIndent) {
253
+ minIndent = reindentCode.length;
254
+ }
255
+ }
256
+
257
+ for (i = 0; i < lines.length; i++) {
258
+ lines[i] = prefix + lines[i].substring(minIndent);
259
+ }
260
+ lines.push('');
261
+ return lines.join('\n');
262
+ }
263
+ };
264
+
265
+ service.templateMerge = ['reindentCode', function(indentCode) {
266
+ return function(template, properties) {
267
+ return template.replace(/\{\{(\w+)(?:\:(\d+))?\}\}/g, function(_, key, indent) {
268
+ var value = properties[key];
269
+
270
+ if (indent) {
271
+ value = indentCode(value, indent);
272
+ }
273
+
274
+ return value == undefined ? '' : value;
275
+ });
276
+ };
277
+ }];
278
+
279
+ service.getEmbeddedTemplate = ['reindentCode', function(reindentCode) {
280
+ return function (id) {
281
+ var element = document.getElementById(id);
282
+
283
+ if (!element) {
284
+ return null;
285
+ }
286
+
287
+ return reindentCode(angular.element(element).html(), 0);
288
+ }
289
+ }];
290
+
291
+
292
+ angular.module('bootstrapPrettify', []).directive(directive).factory(service);
293
+ // Copyright (C) 2006 Google Inc.
294
+ //
295
+ // Licensed under the Apache License, Version 2.0 (the "License");
296
+ // you may not use this file except in compliance with the License.
297
+ // You may obtain a copy of the License at
298
+ //
299
+ // http://www.apache.org/licenses/LICENSE-2.0
300
+ //
301
+ // Unless required by applicable law or agreed to in writing, software
302
+ // distributed under the License is distributed on an "AS IS" BASIS,
303
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
304
+ // See the License for the specific language governing permissions and
305
+ // limitations under the License.
306
+
307
+
308
+ /**
309
+ * @fileoverview
310
+ * some functions for browser-side pretty printing of code contained in html.
311
+ *
312
+ * <p>
313
+ * For a fairly comprehensive set of languages see the
314
+ * <a href="http://google-code-prettify.googlecode.com/svn/trunk/README.html#langs">README</a>
315
+ * file that came with this source. At a minimum, the lexer should work on a
316
+ * number of languages including C and friends, Java, Python, Bash, SQL, HTML,
317
+ * XML, CSS, Javascript, and Makefiles. It works passably on Ruby, PHP and Awk
318
+ * and a subset of Perl, but, because of commenting conventions, doesn't work on
319
+ * Smalltalk, Lisp-like, or CAML-like languages without an explicit lang class.
320
+ * <p>
321
+ * Usage: <ol>
322
+ * <li> include this source file in an html page via
323
+ * {@code <script type="text/javascript" src="/path/to/prettify.js"></script>}
324
+ * <li> define style rules. See the example page for examples.
325
+ * <li> mark the {@code <pre>} and {@code <code>} tags in your source with
326
+ * {@code class=prettyprint.}
327
+ * You can also use the (html deprecated) {@code <xmp>} tag, but the pretty
328
+ * printer needs to do more substantial DOM manipulations to support that, so
329
+ * some css styles may not be preserved.
330
+ * </ol>
331
+ * That's it. I wanted to keep the API as simple as possible, so there's no
332
+ * need to specify which language the code is in, but if you wish, you can add
333
+ * another class to the {@code <pre>} or {@code <code>} element to specify the
334
+ * language, as in {@code <pre class="prettyprint lang-java">}. Any class that
335
+ * starts with "lang-" followed by a file extension, specifies the file type.
336
+ * See the "lang-*.js" files in this directory for code that implements
337
+ * per-language file handlers.
338
+ * <p>
339
+ * Change log:<br>
340
+ * cbeust, 2006/08/22
341
+ * <blockquote>
342
+ * Java annotations (start with "@") are now captured as literals ("lit")
343
+ * </blockquote>
344
+ * @requires console
345
+ */
346
+
347
+ // JSLint declarations
348
+ /*global console, document, navigator, setTimeout, window, define */
349
+
350
+ /**
351
+ * Split {@code prettyPrint} into multiple timeouts so as not to interfere with
352
+ * UI events.
353
+ * If set to {@code false}, {@code prettyPrint()} is synchronous.
354
+ */
355
+ window['PR_SHOULD_USE_CONTINUATION'] = true;
356
+
357
+ /**
358
+ * Find all the {@code <pre>} and {@code <code>} tags in the DOM with
359
+ * {@code class=prettyprint} and prettify them.
360
+ *
361
+ * @param {Function?} opt_whenDone if specified, called when the last entry
362
+ * has been finished.
363
+ */
364
+ var prettyPrintOne;
365
+ /**
366
+ * Pretty print a chunk of code.
367
+ *
368
+ * @param {string} sourceCodeHtml code as html
369
+ * @return {string} code as html, but prettier
370
+ */
371
+ var prettyPrint;
372
+
373
+
374
+ (function () {
375
+ var win = window;
376
+ // Keyword lists for various languages.
377
+ // We use things that coerce to strings to make them compact when minified
378
+ // and to defeat aggressive optimizers that fold large string constants.
379
+ var FLOW_CONTROL_KEYWORDS = ["break,continue,do,else,for,if,return,while"];
380
+ var C_KEYWORDS = [FLOW_CONTROL_KEYWORDS,"auto,case,char,const,default," +
381
+ "double,enum,extern,float,goto,int,long,register,short,signed,sizeof," +
382
+ "static,struct,switch,typedef,union,unsigned,void,volatile"];
383
+ var COMMON_KEYWORDS = [C_KEYWORDS,"catch,class,delete,false,import," +
384
+ "new,operator,private,protected,public,this,throw,true,try,typeof"];
385
+ var CPP_KEYWORDS = [COMMON_KEYWORDS,"alignof,align_union,asm,axiom,bool," +
386
+ "concept,concept_map,const_cast,constexpr,decltype," +
387
+ "dynamic_cast,explicit,export,friend,inline,late_check," +
388
+ "mutable,namespace,nullptr,reinterpret_cast,static_assert,static_cast," +
389
+ "template,typeid,typename,using,virtual,where"];
390
+ var JAVA_KEYWORDS = [COMMON_KEYWORDS,
391
+ "abstract,boolean,byte,extends,final,finally,implements,import," +
392
+ "instanceof,null,native,package,strictfp,super,synchronized,throws," +
393
+ "transient"];
394
+ var CSHARP_KEYWORDS = [JAVA_KEYWORDS,
395
+ "as,base,by,checked,decimal,delegate,descending,dynamic,event," +
396
+ "fixed,foreach,from,group,implicit,in,interface,internal,into,is,let," +
397
+ "lock,object,out,override,orderby,params,partial,readonly,ref,sbyte," +
398
+ "sealed,stackalloc,string,select,uint,ulong,unchecked,unsafe,ushort," +
399
+ "var,virtual,where"];
400
+ var COFFEE_KEYWORDS = "all,and,by,catch,class,else,extends,false,finally," +
401
+ "for,if,in,is,isnt,loop,new,no,not,null,of,off,on,or,return,super,then," +
402
+ "throw,true,try,unless,until,when,while,yes";
403
+ var JSCRIPT_KEYWORDS = [COMMON_KEYWORDS,
404
+ "debugger,eval,export,function,get,null,set,undefined,var,with," +
405
+ "Infinity,NaN"];
406
+ var PERL_KEYWORDS = "caller,delete,die,do,dump,elsif,eval,exit,foreach,for," +
407
+ "goto,if,import,last,local,my,next,no,our,print,package,redo,require," +
408
+ "sub,undef,unless,until,use,wantarray,while,BEGIN,END";
409
+ var PYTHON_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "and,as,assert,class,def,del," +
410
+ "elif,except,exec,finally,from,global,import,in,is,lambda," +
411
+ "nonlocal,not,or,pass,print,raise,try,with,yield," +
412
+ "False,True,None"];
413
+ var RUBY_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "alias,and,begin,case,class," +
414
+ "def,defined,elsif,end,ensure,false,in,module,next,nil,not,or,redo," +
415
+ "rescue,retry,self,super,then,true,undef,unless,until,when,yield," +
416
+ "BEGIN,END"];
417
+ var SH_KEYWORDS = [FLOW_CONTROL_KEYWORDS, "case,done,elif,esac,eval,fi," +
418
+ "function,in,local,set,then,until"];
419
+ var ALL_KEYWORDS = [
420
+ CPP_KEYWORDS, CSHARP_KEYWORDS, JSCRIPT_KEYWORDS, PERL_KEYWORDS +
421
+ PYTHON_KEYWORDS, RUBY_KEYWORDS, SH_KEYWORDS];
422
+ var C_TYPES = /^(DIR|FILE|vector|(de|priority_)?queue|list|stack|(const_)?iterator|(multi)?(set|map)|bitset|u?(int|float)\d*)\b/;
423
+
424
+ // token style names. correspond to css classes
425
+ /**
426
+ * token style for a string literal
427
+ * @const
428
+ */
429
+ var PR_STRING = 'str';
430
+ /**
431
+ * token style for a keyword
432
+ * @const
433
+ */
434
+ var PR_KEYWORD = 'kwd';
435
+ /**
436
+ * token style for a comment
437
+ * @const
438
+ */
439
+ var PR_COMMENT = 'com';
440
+ /**
441
+ * token style for a type
442
+ * @const
443
+ */
444
+ var PR_TYPE = 'typ';
445
+ /**
446
+ * token style for a literal value. e.g. 1, null, true.
447
+ * @const
448
+ */
449
+ var PR_LITERAL = 'lit';
450
+ /**
451
+ * token style for a punctuation string.
452
+ * @const
453
+ */
454
+ var PR_PUNCTUATION = 'pun';
455
+ /**
456
+ * token style for plain text.
457
+ * @const
458
+ */
459
+ var PR_PLAIN = 'pln';
460
+
461
+ /**
462
+ * token style for an sgml tag.
463
+ * @const
464
+ */
465
+ var PR_TAG = 'tag';
466
+ /**
467
+ * token style for a markup declaration such as a DOCTYPE.
468
+ * @const
469
+ */
470
+ var PR_DECLARATION = 'dec';
471
+ /**
472
+ * token style for embedded source.
473
+ * @const
474
+ */
475
+ var PR_SOURCE = 'src';
476
+ /**
477
+ * token style for an sgml attribute name.
478
+ * @const
479
+ */
480
+ var PR_ATTRIB_NAME = 'atn';
481
+ /**
482
+ * token style for an sgml attribute value.
483
+ * @const
484
+ */
485
+ var PR_ATTRIB_VALUE = 'atv';
486
+
487
+ /**
488
+ * A class that indicates a section of markup that is not code, e.g. to allow
489
+ * embedding of line numbers within code listings.
490
+ * @const
491
+ */
492
+ var PR_NOCODE = 'nocode';
493
+
494
+
495
+
496
+ /**
497
+ * A set of tokens that can precede a regular expression literal in
498
+ * javascript
499
+ * http://web.archive.org/web/20070717142515/http://www.mozilla.org/js/language/js20/rationale/syntax.html
500
+ * has the full list, but I've removed ones that might be problematic when
501
+ * seen in languages that don't support regular expression literals.
502
+ *
503
+ * <p>Specifically, I've removed any keywords that can't precede a regexp
504
+ * literal in a syntactically legal javascript program, and I've removed the
505
+ * "in" keyword since it's not a keyword in many languages, and might be used
506
+ * as a count of inches.
507
+ *
508
+ * <p>The link above does not accurately describe EcmaScript rules since
509
+ * it fails to distinguish between (a=++/b/i) and (a++/b/i) but it works
510
+ * very well in practice.
511
+ *
512
+ * @private
513
+ * @const
514
+ */
515
+ var REGEXP_PRECEDER_PATTERN = '(?:^^\\.?|[+-]|[!=]=?=?|\\#|%=?|&&?=?|\\(|\\*=?|[+\\-]=|->|\\/=?|::?|<<?=?|>>?>?=?|,|;|\\?|@|\\[|~|{|\\^\\^?=?|\\|\\|?=?|break|case|continue|delete|do|else|finally|instanceof|return|throw|try|typeof)\\s*';
516
+
517
+ // CAVEAT: this does not properly handle the case where a regular
518
+ // expression immediately follows another since a regular expression may
519
+ // have flags for case-sensitivity and the like. Having regexp tokens
520
+ // adjacent is not valid in any language I'm aware of, so I'm punting.
521
+ // TODO: maybe style special characters inside a regexp as punctuation.
522
+
523
+
524
+ /**
525
+ * Given a group of {@link RegExp}s, returns a {@code RegExp} that globally
526
+ * matches the union of the sets of strings matched by the input RegExp.
527
+ * Since it matches globally, if the input strings have a start-of-input
528
+ * anchor (/^.../), it is ignored for the purposes of unioning.
529
+ * @param {Array.<RegExp>} regexs non multiline, non-global regexs.
530
+ * @return {RegExp} a global regex.
531
+ */
532
+ function combinePrefixPatterns(regexs) {
533
+ var capturedGroupIndex = 0;
534
+
535
+ var needToFoldCase = false;
536
+ var ignoreCase = false;
537
+ for (var i = 0, n = regexs.length; i < n; ++i) {
538
+ var regex = regexs[i];
539
+ if (regex.ignoreCase) {
540
+ ignoreCase = true;
541
+ } else if (/[a-z]/i.test(regex.source.replace(
542
+ /\\u[0-9a-f]{4}|\\x[0-9a-f]{2}|\\[^ux]/gi, ''))) {
543
+ needToFoldCase = true;
544
+ ignoreCase = false;
545
+ break;
546
+ }
547
+ }
548
+
549
+ var escapeCharToCodeUnit = {
550
+ 'b': 8,
551
+ 't': 9,
552
+ 'n': 0xa,
553
+ 'v': 0xb,
554
+ 'f': 0xc,
555
+ 'r': 0xd
556
+ };
557
+
558
+ function decodeEscape(charsetPart) {
559
+ var cc0 = charsetPart.charCodeAt(0);
560
+ if (cc0 !== 92 /* \\ */) {
561
+ return cc0;
562
+ }
563
+ var c1 = charsetPart.charAt(1);
564
+ cc0 = escapeCharToCodeUnit[c1];
565
+ if (cc0) {
566
+ return cc0;
567
+ } else if ('0' <= c1 && c1 <= '7') {
568
+ return parseInt(charsetPart.substring(1), 8);
569
+ } else if (c1 === 'u' || c1 === 'x') {
570
+ return parseInt(charsetPart.substring(2), 16);
571
+ } else {
572
+ return charsetPart.charCodeAt(1);
573
+ }
574
+ }
575
+
576
+ function encodeEscape(charCode) {
577
+ if (charCode < 0x20) {
578
+ return (charCode < 0x10 ? '\\x0' : '\\x') + charCode.toString(16);
579
+ }
580
+ var ch = String.fromCharCode(charCode);
581
+ return (ch === '\\' || ch === '-' || ch === ']' || ch === '^')
582
+ ? "\\" + ch : ch;
583
+ }
584
+
585
+ function caseFoldCharset(charSet) {
586
+ var charsetParts = charSet.substring(1, charSet.length - 1).match(
587
+ new RegExp(
588
+ '\\\\u[0-9A-Fa-f]{4}'
589
+ + '|\\\\x[0-9A-Fa-f]{2}'
590
+ + '|\\\\[0-3][0-7]{0,2}'
591
+ + '|\\\\[0-7]{1,2}'
592
+ + '|\\\\[\\s\\S]'
593
+ + '|-'
594
+ + '|[^-\\\\]',
595
+ 'g'));
596
+ var ranges = [];
597
+ var inverse = charsetParts[0] === '^';
598
+
599
+ var out = ['['];
600
+ if (inverse) { out.push('^'); }
601
+
602
+ for (var i = inverse ? 1 : 0, n = charsetParts.length; i < n; ++i) {
603
+ var p = charsetParts[i];
604
+ if (/\\[bdsw]/i.test(p)) { // Don't muck with named groups.
605
+ out.push(p);
606
+ } else {
607
+ var start = decodeEscape(p);
608
+ var end;
609
+ if (i + 2 < n && '-' === charsetParts[i + 1]) {
610
+ end = decodeEscape(charsetParts[i + 2]);
611
+ i += 2;
612
+ } else {
613
+ end = start;
614
+ }
615
+ ranges.push([start, end]);
616
+ // If the range might intersect letters, then expand it.
617
+ // This case handling is too simplistic.
618
+ // It does not deal with non-latin case folding.
619
+ // It works for latin source code identifiers though.
620
+ if (!(end < 65 || start > 122)) {
621
+ if (!(end < 65 || start > 90)) {
622
+ ranges.push([Math.max(65, start) | 32, Math.min(end, 90) | 32]);
623
+ }
624
+ if (!(end < 97 || start > 122)) {
625
+ ranges.push([Math.max(97, start) & ~32, Math.min(end, 122) & ~32]);
626
+ }
627
+ }
628
+ }
629
+ }
630
+
631
+ // [[1, 10], [3, 4], [8, 12], [14, 14], [16, 16], [17, 17]]
632
+ // -> [[1, 12], [14, 14], [16, 17]]
633
+ ranges.sort(function (a, b) { return (a[0] - b[0]) || (b[1] - a[1]); });
634
+ var consolidatedRanges = [];
635
+ var lastRange = [];
636
+ for (var i = 0; i < ranges.length; ++i) {
637
+ var range = ranges[i];
638
+ if (range[0] <= lastRange[1] + 1) {
639
+ lastRange[1] = Math.max(lastRange[1], range[1]);
640
+ } else {
641
+ consolidatedRanges.push(lastRange = range);
642
+ }
643
+ }
644
+
645
+ for (var i = 0; i < consolidatedRanges.length; ++i) {
646
+ var range = consolidatedRanges[i];
647
+ out.push(encodeEscape(range[0]));
648
+ if (range[1] > range[0]) {
649
+ if (range[1] + 1 > range[0]) { out.push('-'); }
650
+ out.push(encodeEscape(range[1]));
651
+ }
652
+ }
653
+ out.push(']');
654
+ return out.join('');
655
+ }
656
+
657
+ function allowAnywhereFoldCaseAndRenumberGroups(regex) {
658
+ // Split into character sets, escape sequences, punctuation strings
659
+ // like ('(', '(?:', ')', '^'), and runs of characters that do not
660
+ // include any of the above.
661
+ var parts = regex.source.match(
662
+ new RegExp(
663
+ '(?:'
664
+ + '\\[(?:[^\\x5C\\x5D]|\\\\[\\s\\S])*\\]' // a character set
665
+ + '|\\\\u[A-Fa-f0-9]{4}' // a unicode escape
666
+ + '|\\\\x[A-Fa-f0-9]{2}' // a hex escape
667
+ + '|\\\\[0-9]+' // a back-reference or octal escape
668
+ + '|\\\\[^ux0-9]' // other escape sequence
669
+ + '|\\(\\?[:!=]' // start of a non-capturing group
670
+ + '|[\\(\\)\\^]' // start/end of a group, or line start
671
+ + '|[^\\x5B\\x5C\\(\\)\\^]+' // run of other characters
672
+ + ')',
673
+ 'g'));
674
+ var n = parts.length;
675
+
676
+ // Maps captured group numbers to the number they will occupy in
677
+ // the output or to -1 if that has not been determined, or to
678
+ // undefined if they need not be capturing in the output.
679
+ var capturedGroups = [];
680
+
681
+ // Walk over and identify back references to build the capturedGroups
682
+ // mapping.
683
+ for (var i = 0, groupIndex = 0; i < n; ++i) {
684
+ var p = parts[i];
685
+ if (p === '(') {
686
+ // groups are 1-indexed, so max group index is count of '('
687
+ ++groupIndex;
688
+ } else if ('\\' === p.charAt(0)) {
689
+ var decimalValue = +p.substring(1);
690
+ if (decimalValue) {
691
+ if (decimalValue <= groupIndex) {
692
+ capturedGroups[decimalValue] = -1;
693
+ } else {
694
+ // Replace with an unambiguous escape sequence so that
695
+ // an octal escape sequence does not turn into a backreference
696
+ // to a capturing group from an earlier regex.
697
+ parts[i] = encodeEscape(decimalValue);
698
+ }
699
+ }
700
+ }
701
+ }
702
+
703
+ // Renumber groups and reduce capturing groups to non-capturing groups
704
+ // where possible.
705
+ for (var i = 1; i < capturedGroups.length; ++i) {
706
+ if (-1 === capturedGroups[i]) {
707
+ capturedGroups[i] = ++capturedGroupIndex;
708
+ }
709
+ }
710
+ for (var i = 0, groupIndex = 0; i < n; ++i) {
711
+ var p = parts[i];
712
+ if (p === '(') {
713
+ ++groupIndex;
714
+ if (!capturedGroups[groupIndex]) {
715
+ parts[i] = '(?:';
716
+ }
717
+ } else if ('\\' === p.charAt(0)) {
718
+ var decimalValue = +p.substring(1);
719
+ if (decimalValue && decimalValue <= groupIndex) {
720
+ parts[i] = '\\' + capturedGroups[decimalValue];
721
+ }
722
+ }
723
+ }
724
+
725
+ // Remove any prefix anchors so that the output will match anywhere.
726
+ // ^^ really does mean an anchored match though.
727
+ for (var i = 0; i < n; ++i) {
728
+ if ('^' === parts[i] && '^' !== parts[i + 1]) { parts[i] = ''; }
729
+ }
730
+
731
+ // Expand letters to groups to handle mixing of case-sensitive and
732
+ // case-insensitive patterns if necessary.
733
+ if (regex.ignoreCase && needToFoldCase) {
734
+ for (var i = 0; i < n; ++i) {
735
+ var p = parts[i];
736
+ var ch0 = p.charAt(0);
737
+ if (p.length >= 2 && ch0 === '[') {
738
+ parts[i] = caseFoldCharset(p);
739
+ } else if (ch0 !== '\\') {
740
+ // TODO: handle letters in numeric escapes.
741
+ parts[i] = p.replace(
742
+ /[a-zA-Z]/g,
743
+ function (ch) {
744
+ var cc = ch.charCodeAt(0);
745
+ return '[' + String.fromCharCode(cc & ~32, cc | 32) + ']';
746
+ });
747
+ }
748
+ }
749
+ }
750
+
751
+ return parts.join('');
752
+ }
753
+
754
+ var rewritten = [];
755
+ for (var i = 0, n = regexs.length; i < n; ++i) {
756
+ var regex = regexs[i];
757
+ if (regex.global || regex.multiline) { throw new Error('' + regex); }
758
+ rewritten.push(
759
+ '(?:' + allowAnywhereFoldCaseAndRenumberGroups(regex) + ')');
760
+ }
761
+
762
+ return new RegExp(rewritten.join('|'), ignoreCase ? 'gi' : 'g');
763
+ }
764
+
765
+
766
+ /**
767
+ * Split markup into a string of source code and an array mapping ranges in
768
+ * that string to the text nodes in which they appear.
769
+ *
770
+ * <p>
771
+ * The HTML DOM structure:</p>
772
+ * <pre>
773
+ * (Element "p"
774
+ * (Element "b"
775
+ * (Text "print ")) ; #1
776
+ * (Text "'Hello '") ; #2
777
+ * (Element "br") ; #3
778
+ * (Text " + 'World';")) ; #4
779
+ * </pre>
780
+ * <p>
781
+ * corresponds to the HTML
782
+ * {@code <p><b>print </b>'Hello '<br> + 'World';</p>}.</p>
783
+ *
784
+ * <p>
785
+ * It will produce the output:</p>
786
+ * <pre>
787
+ * {
788
+ * sourceCode: "print 'Hello '\n + 'World';",
789
+ * // 1 2
790
+ * // 012345678901234 5678901234567
791
+ * spans: [0, #1, 6, #2, 14, #3, 15, #4]
792
+ * }
793
+ * </pre>
794
+ * <p>
795
+ * where #1 is a reference to the {@code "print "} text node above, and so
796
+ * on for the other text nodes.
797
+ * </p>
798
+ *
799
+ * <p>
800
+ * The {@code} spans array is an array of pairs. Even elements are the start
801
+ * indices of substrings, and odd elements are the text nodes (or BR elements)
802
+ * that contain the text for those substrings.
803
+ * Substrings continue until the next index or the end of the source.
804
+ * </p>
805
+ *
806
+ * @param {Node} node an HTML DOM subtree containing source-code.
807
+ * @param {boolean} isPreformatted true if white-space in text nodes should
808
+ * be considered significant.
809
+ * @return {Object} source code and the text nodes in which they occur.
810
+ */
811
+ function extractSourceSpans(node, isPreformatted) {
812
+ var nocode = /(?:^|\s)nocode(?:\s|$)/;
813
+
814
+ var chunks = [];
815
+ var length = 0;
816
+ var spans = [];
817
+ var k = 0;
818
+
819
+ function walk(node) {
820
+ switch (node.nodeType) {
821
+ case 1: // Element
822
+ if (nocode.test(node.className)) { return; }
823
+ for (var child = node.firstChild; child; child = child.nextSibling) {
824
+ walk(child);
825
+ }
826
+ var nodeName = node.nodeName.toLowerCase();
827
+ if ('br' === nodeName || 'li' === nodeName) {
828
+ chunks[k] = '\n';
829
+ spans[k << 1] = length++;
830
+ spans[(k++ << 1) | 1] = node;
831
+ }
832
+ break;
833
+ case 3: case 4: // Text
834
+ var text = node.nodeValue;
835
+ if (text.length) {
836
+ if (!isPreformatted) {
837
+ text = text.replace(/[ \t\r\n]+/g, ' ');
838
+ } else {
839
+ text = text.replace(/\r\n?/g, '\n'); // Normalize newlines.
840
+ }
841
+ // TODO: handle tabs here?
842
+ chunks[k] = text;
843
+ spans[k << 1] = length;
844
+ length += text.length;
845
+ spans[(k++ << 1) | 1] = node;
846
+ }
847
+ break;
848
+ }
849
+ }
850
+
851
+ walk(node);
852
+
853
+ return {
854
+ sourceCode: chunks.join('').replace(/\n$/, ''),
855
+ spans: spans
856
+ };
857
+ }
858
+
859
+
860
+ /**
861
+ * Apply the given language handler to sourceCode and add the resulting
862
+ * decorations to out.
863
+ * @param {number} basePos the index of sourceCode within the chunk of source
864
+ * whose decorations are already present on out.
865
+ */
866
+ function appendDecorations(basePos, sourceCode, langHandler, out) {
867
+ if (!sourceCode) { return; }
868
+ var job = {
869
+ sourceCode: sourceCode,
870
+ basePos: basePos
871
+ };
872
+ langHandler(job);
873
+ out.push.apply(out, job.decorations);
874
+ }
875
+
876
+ var notWs = /\S/;
877
+
878
+ /**
879
+ * Given an element, if it contains only one child element and any text nodes
880
+ * it contains contain only space characters, return the sole child element.
881
+ * Otherwise returns undefined.
882
+ * <p>
883
+ * This is meant to return the CODE element in {@code <pre><code ...>} when
884
+ * there is a single child element that contains all the non-space textual
885
+ * content, but not to return anything where there are multiple child elements
886
+ * as in {@code <pre><code>...</code><code>...</code></pre>} or when there
887
+ * is textual content.
888
+ */
889
+ function childContentWrapper(element) {
890
+ var wrapper = undefined;
891
+ for (var c = element.firstChild; c; c = c.nextSibling) {
892
+ var type = c.nodeType;
893
+ wrapper = (type === 1) // Element Node
894
+ ? (wrapper ? element : c)
895
+ : (type === 3) // Text Node
896
+ ? (notWs.test(c.nodeValue) ? element : wrapper)
897
+ : wrapper;
898
+ }
899
+ return wrapper === element ? undefined : wrapper;
900
+ }
901
+
902
+ /** Given triples of [style, pattern, context] returns a lexing function,
903
+ * The lexing function interprets the patterns to find token boundaries and
904
+ * returns a decoration list of the form
905
+ * [index_0, style_0, index_1, style_1, ..., index_n, style_n]
906
+ * where index_n is an index into the sourceCode, and style_n is a style
907
+ * constant like PR_PLAIN. index_n-1 <= index_n, and style_n-1 applies to
908
+ * all characters in sourceCode[index_n-1:index_n].
909
+ *
910
+ * The stylePatterns is a list whose elements have the form
911
+ * [style : string, pattern : RegExp, DEPRECATED, shortcut : string].
912
+ *
913
+ * Style is a style constant like PR_PLAIN, or can be a string of the
914
+ * form 'lang-FOO', where FOO is a language extension describing the
915
+ * language of the portion of the token in $1 after pattern executes.
916
+ * E.g., if style is 'lang-lisp', and group 1 contains the text
917
+ * '(hello (world))', then that portion of the token will be passed to the
918
+ * registered lisp handler for formatting.
919
+ * The text before and after group 1 will be restyled using this decorator
920
+ * so decorators should take care that this doesn't result in infinite
921
+ * recursion. For example, the HTML lexer rule for SCRIPT elements looks
922
+ * something like ['lang-js', /<[s]cript>(.+?)<\/script>/]. This may match
923
+ * '<script>foo()<\/script>', which would cause the current decorator to
924
+ * be called with '<script>' which would not match the same rule since
925
+ * group 1 must not be empty, so it would be instead styled as PR_TAG by
926
+ * the generic tag rule. The handler registered for the 'js' extension would
927
+ * then be called with 'foo()', and finally, the current decorator would
928
+ * be called with '<\/script>' which would not match the original rule and
929
+ * so the generic tag rule would identify it as a tag.
930
+ *
931
+ * Pattern must only match prefixes, and if it matches a prefix, then that
932
+ * match is considered a token with the same style.
933
+ *
934
+ * Context is applied to the last non-whitespace, non-comment token
935
+ * recognized.
936
+ *
937
+ * Shortcut is an optional string of characters, any of which, if the first
938
+ * character, gurantee that this pattern and only this pattern matches.
939
+ *
940
+ * @param {Array} shortcutStylePatterns patterns that always start with
941
+ * a known character. Must have a shortcut string.
942
+ * @param {Array} fallthroughStylePatterns patterns that will be tried in
943
+ * order if the shortcut ones fail. May have shortcuts.
944
+ *
945
+ * @return {function (Object)} a
946
+ * function that takes source code and returns a list of decorations.
947
+ */
948
+ function createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns) {
949
+ var shortcuts = {};
950
+ var tokenizer;
951
+ (function () {
952
+ var allPatterns = shortcutStylePatterns.concat(fallthroughStylePatterns);
953
+ var allRegexs = [];
954
+ var regexKeys = {};
955
+ for (var i = 0, n = allPatterns.length; i < n; ++i) {
956
+ var patternParts = allPatterns[i];
957
+ var shortcutChars = patternParts[3];
958
+ if (shortcutChars) {
959
+ for (var c = shortcutChars.length; --c >= 0;) {
960
+ shortcuts[shortcutChars.charAt(c)] = patternParts;
961
+ }
962
+ }
963
+ var regex = patternParts[1];
964
+ var k = '' + regex;
965
+ if (!regexKeys.hasOwnProperty(k)) {
966
+ allRegexs.push(regex);
967
+ regexKeys[k] = null;
968
+ }
969
+ }
970
+ allRegexs.push(/[\0-\uffff]/);
971
+ tokenizer = combinePrefixPatterns(allRegexs);
972
+ })();
973
+
974
+ var nPatterns = fallthroughStylePatterns.length;
975
+
976
+ /**
977
+ * Lexes job.sourceCode and produces an output array job.decorations of
978
+ * style classes preceded by the position at which they start in
979
+ * job.sourceCode in order.
980
+ *
981
+ * @param {Object} job an object like <pre>{
982
+ * sourceCode: {string} sourceText plain text,
983
+ * basePos: {int} position of job.sourceCode in the larger chunk of
984
+ * sourceCode.
985
+ * }</pre>
986
+ */
987
+ var decorate = function (job) {
988
+ var sourceCode = job.sourceCode, basePos = job.basePos;
989
+ /** Even entries are positions in source in ascending order. Odd enties
990
+ * are style markers (e.g., PR_COMMENT) that run from that position until
991
+ * the end.
992
+ * @type {Array.<number|string>}
993
+ */
994
+ var decorations = [basePos, PR_PLAIN];
995
+ var pos = 0; // index into sourceCode
996
+ var tokens = sourceCode.match(tokenizer) || [];
997
+ var styleCache = {};
998
+
999
+ for (var ti = 0, nTokens = tokens.length; ti < nTokens; ++ti) {
1000
+ var token = tokens[ti];
1001
+ var style = styleCache[token];
1002
+ var match = void 0;
1003
+
1004
+ var isEmbedded;
1005
+ if (typeof style === 'string') {
1006
+ isEmbedded = false;
1007
+ } else {
1008
+ var patternParts = shortcuts[token.charAt(0)];
1009
+ if (patternParts) {
1010
+ match = token.match(patternParts[1]);
1011
+ style = patternParts[0];
1012
+ } else {
1013
+ for (var i = 0; i < nPatterns; ++i) {
1014
+ patternParts = fallthroughStylePatterns[i];
1015
+ match = token.match(patternParts[1]);
1016
+ if (match) {
1017
+ style = patternParts[0];
1018
+ break;
1019
+ }
1020
+ }
1021
+
1022
+ if (!match) { // make sure that we make progress
1023
+ style = PR_PLAIN;
1024
+ }
1025
+ }
1026
+
1027
+ isEmbedded = style.length >= 5 && 'lang-' === style.substring(0, 5);
1028
+ if (isEmbedded && !(match && typeof match[1] === 'string')) {
1029
+ isEmbedded = false;
1030
+ style = PR_SOURCE;
1031
+ }
1032
+
1033
+ if (!isEmbedded) { styleCache[token] = style; }
1034
+ }
1035
+
1036
+ var tokenStart = pos;
1037
+ pos += token.length;
1038
+
1039
+ if (!isEmbedded) {
1040
+ decorations.push(basePos + tokenStart, style);
1041
+ } else { // Treat group 1 as an embedded block of source code.
1042
+ var embeddedSource = match[1];
1043
+ var embeddedSourceStart = token.indexOf(embeddedSource);
1044
+ var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length;
1045
+ if (match[2]) {
1046
+ // If embeddedSource can be blank, then it would match at the
1047
+ // beginning which would cause us to infinitely recurse on the
1048
+ // entire token, so we catch the right context in match[2].
1049
+ embeddedSourceEnd = token.length - match[2].length;
1050
+ embeddedSourceStart = embeddedSourceEnd - embeddedSource.length;
1051
+ }
1052
+ var lang = style.substring(5);
1053
+ // Decorate the left of the embedded source
1054
+ appendDecorations(
1055
+ basePos + tokenStart,
1056
+ token.substring(0, embeddedSourceStart),
1057
+ decorate, decorations);
1058
+ // Decorate the embedded source
1059
+ appendDecorations(
1060
+ basePos + tokenStart + embeddedSourceStart,
1061
+ embeddedSource,
1062
+ langHandlerForExtension(lang, embeddedSource),
1063
+ decorations);
1064
+ // Decorate the right of the embedded section
1065
+ appendDecorations(
1066
+ basePos + tokenStart + embeddedSourceEnd,
1067
+ token.substring(embeddedSourceEnd),
1068
+ decorate, decorations);
1069
+ }
1070
+ }
1071
+ job.decorations = decorations;
1072
+ };
1073
+ return decorate;
1074
+ }
1075
+
1076
+ /** returns a function that produces a list of decorations from source text.
1077
+ *
1078
+ * This code treats ", ', and ` as string delimiters, and \ as a string
1079
+ * escape. It does not recognize perl's qq() style strings.
1080
+ * It has no special handling for double delimiter escapes as in basic, or
1081
+ * the tripled delimiters used in python, but should work on those regardless
1082
+ * although in those cases a single string literal may be broken up into
1083
+ * multiple adjacent string literals.
1084
+ *
1085
+ * It recognizes C, C++, and shell style comments.
1086
+ *
1087
+ * @param {Object} options a set of optional parameters.
1088
+ * @return {function (Object)} a function that examines the source code
1089
+ * in the input job and builds the decoration list.
1090
+ */
1091
+ function sourceDecorator(options) {
1092
+ var shortcutStylePatterns = [], fallthroughStylePatterns = [];
1093
+ if (options['tripleQuotedStrings']) {
1094
+ // '''multi-line-string''', 'single-line-string', and double-quoted
1095
+ shortcutStylePatterns.push(
1096
+ [PR_STRING, /^(?:\'\'\'(?:[^\'\\]|\\[\s\S]|\'{1,2}(?=[^\']))*(?:\'\'\'|$)|\"\"\"(?:[^\"\\]|\\[\s\S]|\"{1,2}(?=[^\"]))*(?:\"\"\"|$)|\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$))/,
1097
+ null, '\'"']);
1098
+ } else if (options['multiLineStrings']) {
1099
+ // 'multi-line-string', "multi-line-string"
1100
+ shortcutStylePatterns.push(
1101
+ [PR_STRING, /^(?:\'(?:[^\\\']|\\[\s\S])*(?:\'|$)|\"(?:[^\\\"]|\\[\s\S])*(?:\"|$)|\`(?:[^\\\`]|\\[\s\S])*(?:\`|$))/,
1102
+ null, '\'"`']);
1103
+ } else {
1104
+ // 'single-line-string', "single-line-string"
1105
+ shortcutStylePatterns.push(
1106
+ [PR_STRING,
1107
+ /^(?:\'(?:[^\\\'\r\n]|\\.)*(?:\'|$)|\"(?:[^\\\"\r\n]|\\.)*(?:\"|$))/,
1108
+ null, '"\'']);
1109
+ }
1110
+ if (options['verbatimStrings']) {
1111
+ // verbatim-string-literal production from the C# grammar. See issue 93.
1112
+ fallthroughStylePatterns.push(
1113
+ [PR_STRING, /^@\"(?:[^\"]|\"\")*(?:\"|$)/, null]);
1114
+ }
1115
+ var hc = options['hashComments'];
1116
+ if (hc) {
1117
+ if (options['cStyleComments']) {
1118
+ if (hc > 1) { // multiline hash comments
1119
+ shortcutStylePatterns.push(
1120
+ [PR_COMMENT, /^#(?:##(?:[^#]|#(?!##))*(?:###|$)|.*)/, null, '#']);
1121
+ } else {
1122
+ // Stop C preprocessor declarations at an unclosed open comment
1123
+ shortcutStylePatterns.push(
1124
+ [PR_COMMENT, /^#(?:(?:define|elif|else|endif|error|ifdef|include|ifndef|line|pragma|undef|warning)\b|[^\r\n]*)/,
1125
+ null, '#']);
1126
+ }
1127
+ // #include <stdio.h>
1128
+ fallthroughStylePatterns.push(
1129
+ [PR_STRING,
1130
+ /^<(?:(?:(?:\.\.\/)*|\/?)(?:[\w-]+(?:\/[\w-]+)+)?[\w-]+\.h(?:h|pp|\+\+)?|[a-z]\w*)>/,
1131
+ null]);
1132
+ } else {
1133
+ shortcutStylePatterns.push([PR_COMMENT, /^#[^\r\n]*/, null, '#']);
1134
+ }
1135
+ }
1136
+ if (options['cStyleComments']) {
1137
+ fallthroughStylePatterns.push([PR_COMMENT, /^\/\/[^\r\n]*/, null]);
1138
+ fallthroughStylePatterns.push(
1139
+ [PR_COMMENT, /^\/\*[\s\S]*?(?:\*\/|$)/, null]);
1140
+ }
1141
+ if (options['regexLiterals']) {
1142
+ /**
1143
+ * @const
1144
+ */
1145
+ var REGEX_LITERAL = (
1146
+ // A regular expression literal starts with a slash that is
1147
+ // not followed by * or / so that it is not confused with
1148
+ // comments.
1149
+ '/(?=[^/*])'
1150
+ // and then contains any number of raw characters,
1151
+ + '(?:[^/\\x5B\\x5C]'
1152
+ // escape sequences (\x5C),
1153
+ + '|\\x5C[\\s\\S]'
1154
+ // or non-nesting character sets (\x5B\x5D);
1155
+ + '|\\x5B(?:[^\\x5C\\x5D]|\\x5C[\\s\\S])*(?:\\x5D|$))+'
1156
+ // finally closed by a /.
1157
+ + '/');
1158
+ fallthroughStylePatterns.push(
1159
+ ['lang-regex',
1160
+ new RegExp('^' + REGEXP_PRECEDER_PATTERN + '(' + REGEX_LITERAL + ')')
1161
+ ]);
1162
+ }
1163
+
1164
+ var types = options['types'];
1165
+ if (types) {
1166
+ fallthroughStylePatterns.push([PR_TYPE, types]);
1167
+ }
1168
+
1169
+ var keywords = ("" + options['keywords']).replace(/^ | $/g, '');
1170
+ if (keywords.length) {
1171
+ fallthroughStylePatterns.push(
1172
+ [PR_KEYWORD,
1173
+ new RegExp('^(?:' + keywords.replace(/[\s,]+/g, '|') + ')\\b'),
1174
+ null]);
1175
+ }
1176
+
1177
+ shortcutStylePatterns.push([PR_PLAIN, /^\s+/, null, ' \r\n\t\xA0']);
1178
+ fallthroughStylePatterns.push(
1179
+ // TODO(mikesamuel): recognize non-latin letters and numerals in idents
1180
+ [PR_LITERAL, /^@[a-z_$][a-z_$@0-9]*/i, null],
1181
+ [PR_TYPE, /^(?:[@_]?[A-Z]+[a-z][A-Za-z_$@0-9]*|\w+_t\b)/, null],
1182
+ [PR_PLAIN, /^[a-z_$][a-z_$@0-9]*/i, null],
1183
+ [PR_LITERAL,
1184
+ new RegExp(
1185
+ '^(?:'
1186
+ // A hex number
1187
+ + '0x[a-f0-9]+'
1188
+ // or an octal or decimal number,
1189
+ + '|(?:\\d(?:_\\d+)*\\d*(?:\\.\\d*)?|\\.\\d\\+)'
1190
+ // possibly in scientific notation
1191
+ + '(?:e[+\\-]?\\d+)?'
1192
+ + ')'
1193
+ // with an optional modifier like UL for unsigned long
1194
+ + '[a-z]*', 'i'),
1195
+ null, '0123456789'],
1196
+ // Don't treat escaped quotes in bash as starting strings. See issue 144.
1197
+ [PR_PLAIN, /^\\[\s\S]?/, null],
1198
+ [PR_PUNCTUATION, /^.[^\s\w\.$@\'\"\`\/\#\\]*/, null]);
1199
+
1200
+ return createSimpleLexer(shortcutStylePatterns, fallthroughStylePatterns);
1201
+ }
1202
+
1203
+ var decorateSource = sourceDecorator({
1204
+ 'keywords': ALL_KEYWORDS,
1205
+ 'hashComments': true,
1206
+ 'cStyleComments': true,
1207
+ 'multiLineStrings': true,
1208
+ 'regexLiterals': true
1209
+ });
1210
+
1211
+ /**
1212
+ * Given a DOM subtree, wraps it in a list, and puts each line into its own
1213
+ * list item.
1214
+ *
1215
+ * @param {Node} node modified in place. Its content is pulled into an
1216
+ * HTMLOListElement, and each line is moved into a separate list item.
1217
+ * This requires cloning elements, so the input might not have unique
1218
+ * IDs after numbering.
1219
+ * @param {boolean} isPreformatted true iff white-space in text nodes should
1220
+ * be treated as significant.
1221
+ */
1222
+ function numberLines(node, opt_startLineNum, isPreformatted) {
1223
+ var nocode = /(?:^|\s)nocode(?:\s|$)/;
1224
+ var lineBreak = /\r\n?|\n/;
1225
+
1226
+ var document = node.ownerDocument;
1227
+
1228
+ var li = document.createElement('li');
1229
+ while (node.firstChild) {
1230
+ li.appendChild(node.firstChild);
1231
+ }
1232
+ // An array of lines. We split below, so this is initialized to one
1233
+ // un-split line.
1234
+ var listItems = [li];
1235
+
1236
+ function walk(node) {
1237
+ switch (node.nodeType) {
1238
+ case 1: // Element
1239
+ if (nocode.test(node.className)) { break; }
1240
+ if ('br' === node.nodeName) {
1241
+ breakAfter(node);
1242
+ // Discard the <BR> since it is now flush against a </LI>.
1243
+ if (node.parentNode) {
1244
+ node.parentNode.removeChild(node);
1245
+ }
1246
+ } else {
1247
+ for (var child = node.firstChild; child; child = child.nextSibling) {
1248
+ walk(child);
1249
+ }
1250
+ }
1251
+ break;
1252
+ case 3: case 4: // Text
1253
+ if (isPreformatted) {
1254
+ var text = node.nodeValue;
1255
+ var match = text.match(lineBreak);
1256
+ if (match) {
1257
+ var firstLine = text.substring(0, match.index);
1258
+ node.nodeValue = firstLine;
1259
+ var tail = text.substring(match.index + match[0].length);
1260
+ if (tail) {
1261
+ var parent = node.parentNode;
1262
+ parent.insertBefore(
1263
+ document.createTextNode(tail), node.nextSibling);
1264
+ }
1265
+ breakAfter(node);
1266
+ if (!firstLine) {
1267
+ // Don't leave blank text nodes in the DOM.
1268
+ node.parentNode.removeChild(node);
1269
+ }
1270
+ }
1271
+ }
1272
+ break;
1273
+ }
1274
+ }
1275
+
1276
+ // Split a line after the given node.
1277
+ function breakAfter(lineEndNode) {
1278
+ // If there's nothing to the right, then we can skip ending the line
1279
+ // here, and move root-wards since splitting just before an end-tag
1280
+ // would require us to create a bunch of empty copies.
1281
+ while (!lineEndNode.nextSibling) {
1282
+ lineEndNode = lineEndNode.parentNode;
1283
+ if (!lineEndNode) { return; }
1284
+ }
1285
+
1286
+ function breakLeftOf(limit, copy) {
1287
+ // Clone shallowly if this node needs to be on both sides of the break.
1288
+ var rightSide = copy ? limit.cloneNode(false) : limit;
1289
+ var parent = limit.parentNode;
1290
+ if (parent) {
1291
+ // We clone the parent chain.
1292
+ // This helps us resurrect important styling elements that cross lines.
1293
+ // E.g. in <i>Foo<br>Bar</i>
1294
+ // should be rewritten to <li><i>Foo</i></li><li><i>Bar</i></li>.
1295
+ var parentClone = breakLeftOf(parent, 1);
1296
+ // Move the clone and everything to the right of the original
1297
+ // onto the cloned parent.
1298
+ var next = limit.nextSibling;
1299
+ parentClone.appendChild(rightSide);
1300
+ for (var sibling = next; sibling; sibling = next) {
1301
+ next = sibling.nextSibling;
1302
+ parentClone.appendChild(sibling);
1303
+ }
1304
+ }
1305
+ return rightSide;
1306
+ }
1307
+
1308
+ var copiedListItem = breakLeftOf(lineEndNode.nextSibling, 0);
1309
+
1310
+ // Walk the parent chain until we reach an unattached LI.
1311
+ for (var parent;
1312
+ // Check nodeType since IE invents document fragments.
1313
+ (parent = copiedListItem.parentNode) && parent.nodeType === 1;) {
1314
+ copiedListItem = parent;
1315
+ }
1316
+ // Put it on the list of lines for later processing.
1317
+ listItems.push(copiedListItem);
1318
+ }
1319
+
1320
+ // Split lines while there are lines left to split.
1321
+ for (var i = 0; // Number of lines that have been split so far.
1322
+ i < listItems.length; // length updated by breakAfter calls.
1323
+ ++i) {
1324
+ walk(listItems[i]);
1325
+ }
1326
+
1327
+ // Make sure numeric indices show correctly.
1328
+ if (opt_startLineNum === (opt_startLineNum|0)) {
1329
+ listItems[0].setAttribute('value', opt_startLineNum);
1330
+ }
1331
+
1332
+ var ol = document.createElement('ol');
1333
+ ol.className = 'linenums';
1334
+ var offset = Math.max(0, ((opt_startLineNum - 1 /* zero index */)) | 0) || 0;
1335
+ for (var i = 0, n = listItems.length; i < n; ++i) {
1336
+ li = listItems[i];
1337
+ // Stick a class on the LIs so that stylesheets can
1338
+ // color odd/even rows, or any other row pattern that
1339
+ // is co-prime with 10.
1340
+ li.className = 'L' + ((i + offset) % 10);
1341
+ if (!li.firstChild) {
1342
+ li.appendChild(document.createTextNode('\xA0'));
1343
+ }
1344
+ ol.appendChild(li);
1345
+ }
1346
+
1347
+ node.appendChild(ol);
1348
+ }
1349
+
1350
+ /**
1351
+ * Breaks {@code job.sourceCode} around style boundaries in
1352
+ * {@code job.decorations} and modifies {@code job.sourceNode} in place.
1353
+ * @param {Object} job like <pre>{
1354
+ * sourceCode: {string} source as plain text,
1355
+ * spans: {Array.<number|Node>} alternating span start indices into source
1356
+ * and the text node or element (e.g. {@code <BR>}) corresponding to that
1357
+ * span.
1358
+ * decorations: {Array.<number|string} an array of style classes preceded
1359
+ * by the position at which they start in job.sourceCode in order
1360
+ * }</pre>
1361
+ * @private
1362
+ */
1363
+ function recombineTagsAndDecorations(job) {
1364
+ var isIE8OrEarlier = /\bMSIE\s(\d+)/.exec(navigator.userAgent);
1365
+ isIE8OrEarlier = isIE8OrEarlier && +isIE8OrEarlier[1] <= 8;
1366
+ var newlineRe = /\n/g;
1367
+
1368
+ var source = job.sourceCode;
1369
+ var sourceLength = source.length;
1370
+ // Index into source after the last code-unit recombined.
1371
+ var sourceIndex = 0;
1372
+
1373
+ var spans = job.spans;
1374
+ var nSpans = spans.length;
1375
+ // Index into spans after the last span which ends at or before sourceIndex.
1376
+ var spanIndex = 0;
1377
+
1378
+ var decorations = job.decorations;
1379
+ var nDecorations = decorations.length;
1380
+ // Index into decorations after the last decoration which ends at or before
1381
+ // sourceIndex.
1382
+ var decorationIndex = 0;
1383
+
1384
+ // Remove all zero-length decorations.
1385
+ decorations[nDecorations] = sourceLength;
1386
+ var decPos, i;
1387
+ for (i = decPos = 0; i < nDecorations;) {
1388
+ if (decorations[i] !== decorations[i + 2]) {
1389
+ decorations[decPos++] = decorations[i++];
1390
+ decorations[decPos++] = decorations[i++];
1391
+ } else {
1392
+ i += 2;
1393
+ }
1394
+ }
1395
+ nDecorations = decPos;
1396
+
1397
+ // Simplify decorations.
1398
+ for (i = decPos = 0; i < nDecorations;) {
1399
+ var startPos = decorations[i];
1400
+ // Conflate all adjacent decorations that use the same style.
1401
+ var startDec = decorations[i + 1];
1402
+ var end = i + 2;
1403
+ while (end + 2 <= nDecorations && decorations[end + 1] === startDec) {
1404
+ end += 2;
1405
+ }
1406
+ decorations[decPos++] = startPos;
1407
+ decorations[decPos++] = startDec;
1408
+ i = end;
1409
+ }
1410
+
1411
+ nDecorations = decorations.length = decPos;
1412
+
1413
+ var sourceNode = job.sourceNode;
1414
+ var oldDisplay;
1415
+ if (sourceNode) {
1416
+ oldDisplay = sourceNode.style.display;
1417
+ sourceNode.style.display = 'none';
1418
+ }
1419
+ try {
1420
+ var decoration = null;
1421
+ while (spanIndex < nSpans) {
1422
+ var spanStart = spans[spanIndex];
1423
+ var spanEnd = spans[spanIndex + 2] || sourceLength;
1424
+
1425
+ var decEnd = decorations[decorationIndex + 2] || sourceLength;
1426
+
1427
+ var end = Math.min(spanEnd, decEnd);
1428
+
1429
+ var textNode = spans[spanIndex + 1];
1430
+ var styledText;
1431
+ if (textNode.nodeType !== 1 // Don't muck with <BR>s or <LI>s
1432
+ // Don't introduce spans around empty text nodes.
1433
+ && (styledText = source.substring(sourceIndex, end))) {
1434
+ // This may seem bizarre, and it is. Emitting LF on IE causes the
1435
+ // code to display with spaces instead of line breaks.
1436
+ // Emitting Windows standard issue linebreaks (CRLF) causes a blank
1437
+ // space to appear at the beginning of every line but the first.
1438
+ // Emitting an old Mac OS 9 line separator makes everything spiffy.
1439
+ if (isIE8OrEarlier) {
1440
+ styledText = styledText.replace(newlineRe, '\r');
1441
+ }
1442
+ textNode.nodeValue = styledText;
1443
+ var document = textNode.ownerDocument;
1444
+ var span = document.createElement('span');
1445
+ span.className = decorations[decorationIndex + 1];
1446
+ var parentNode = textNode.parentNode;
1447
+ parentNode.replaceChild(span, textNode);
1448
+ span.appendChild(textNode);
1449
+ if (sourceIndex < spanEnd) { // Split off a text node.
1450
+ spans[spanIndex + 1] = textNode
1451
+ // TODO: Possibly optimize by using '' if there's no flicker.
1452
+ = document.createTextNode(source.substring(end, spanEnd));
1453
+ parentNode.insertBefore(textNode, span.nextSibling);
1454
+ }
1455
+ }
1456
+
1457
+ sourceIndex = end;
1458
+
1459
+ if (sourceIndex >= spanEnd) {
1460
+ spanIndex += 2;
1461
+ }
1462
+ if (sourceIndex >= decEnd) {
1463
+ decorationIndex += 2;
1464
+ }
1465
+ }
1466
+ } finally {
1467
+ if (sourceNode) {
1468
+ sourceNode.style.display = oldDisplay;
1469
+ }
1470
+ }
1471
+ }
1472
+
1473
+
1474
+ /** Maps language-specific file extensions to handlers. */
1475
+ var langHandlerRegistry = {};
1476
+ /** Register a language handler for the given file extensions.
1477
+ * @param {function (Object)} handler a function from source code to a list
1478
+ * of decorations. Takes a single argument job which describes the
1479
+ * state of the computation. The single parameter has the form
1480
+ * {@code {
1481
+ * sourceCode: {string} as plain text.
1482
+ * decorations: {Array.<number|string>} an array of style classes
1483
+ * preceded by the position at which they start in
1484
+ * job.sourceCode in order.
1485
+ * The language handler should assigned this field.
1486
+ * basePos: {int} the position of source in the larger source chunk.
1487
+ * All positions in the output decorations array are relative
1488
+ * to the larger source chunk.
1489
+ * } }
1490
+ * @param {Array.<string>} fileExtensions
1491
+ */
1492
+ function registerLangHandler(handler, fileExtensions) {
1493
+ for (var i = fileExtensions.length; --i >= 0;) {
1494
+ var ext = fileExtensions[i];
1495
+ if (!langHandlerRegistry.hasOwnProperty(ext)) {
1496
+ langHandlerRegistry[ext] = handler;
1497
+ } else if (win['console']) {
1498
+ console['warn']('cannot override language handler %s', ext);
1499
+ }
1500
+ }
1501
+ }
1502
+ function langHandlerForExtension(extension, source) {
1503
+ if (!(extension && langHandlerRegistry.hasOwnProperty(extension))) {
1504
+ // Treat it as markup if the first non whitespace character is a < and
1505
+ // the last non-whitespace character is a >.
1506
+ extension = /^\s*</.test(source)
1507
+ ? 'default-markup'
1508
+ : 'default-code';
1509
+ }
1510
+ return langHandlerRegistry[extension];
1511
+ }
1512
+ registerLangHandler(decorateSource, ['default-code']);
1513
+ registerLangHandler(
1514
+ createSimpleLexer(
1515
+ [],
1516
+ [
1517
+ [PR_PLAIN, /^[^<?]+/],
1518
+ [PR_DECLARATION, /^<!\w[^>]*(?:>|$)/],
1519
+ [PR_COMMENT, /^<\!--[\s\S]*?(?:-\->|$)/],
1520
+ // Unescaped content in an unknown language
1521
+ ['lang-', /^<\?([\s\S]+?)(?:\?>|$)/],
1522
+ ['lang-', /^<%([\s\S]+?)(?:%>|$)/],
1523
+ [PR_PUNCTUATION, /^(?:<[%?]|[%?]>)/],
1524
+ ['lang-', /^<xmp\b[^>]*>([\s\S]+?)<\/xmp\b[^>]*>/i],
1525
+ // Unescaped content in javascript. (Or possibly vbscript).
1526
+ ['lang-js', /^<script\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i],
1527
+ // Contains unescaped stylesheet content
1528
+ ['lang-css', /^<style\b[^>]*>([\s\S]*?)(<\/style\b[^>]*>)/i],
1529
+ ['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i]
1530
+ ]),
1531
+ ['default-markup', 'htm', 'html', 'mxml', 'xhtml', 'xml', 'xsl']);
1532
+ registerLangHandler(
1533
+ createSimpleLexer(
1534
+ [
1535
+ [PR_PLAIN, /^[\s]+/, null, ' \t\r\n'],
1536
+ [PR_ATTRIB_VALUE, /^(?:\"[^\"]*\"?|\'[^\']*\'?)/, null, '\"\'']
1537
+ ],
1538
+ [
1539
+ [PR_TAG, /^^<\/?[a-z](?:[\w.:-]*\w)?|\/?>$/i],
1540
+ [PR_ATTRIB_NAME, /^(?!style[\s=]|on)[a-z](?:[\w:-]*\w)?/i],
1541
+ ['lang-uq.val', /^=\s*([^>\'\"\s]*(?:[^>\'\"\s\/]|\/(?=\s)))/],
1542
+ [PR_PUNCTUATION, /^[=<>\/]+/],
1543
+ ['lang-js', /^on\w+\s*=\s*\"([^\"]+)\"/i],
1544
+ ['lang-js', /^on\w+\s*=\s*\'([^\']+)\'/i],
1545
+ ['lang-js', /^on\w+\s*=\s*([^\"\'>\s]+)/i],
1546
+ ['lang-css', /^style\s*=\s*\"([^\"]+)\"/i],
1547
+ ['lang-css', /^style\s*=\s*\'([^\']+)\'/i],
1548
+ ['lang-css', /^style\s*=\s*([^\"\'>\s]+)/i]
1549
+ ]),
1550
+ ['in.tag']);
1551
+ registerLangHandler(
1552
+ createSimpleLexer([], [[PR_ATTRIB_VALUE, /^[\s\S]+/]]), ['uq.val']);
1553
+ registerLangHandler(sourceDecorator({
1554
+ 'keywords': CPP_KEYWORDS,
1555
+ 'hashComments': true,
1556
+ 'cStyleComments': true,
1557
+ 'types': C_TYPES
1558
+ }), ['c', 'cc', 'cpp', 'cxx', 'cyc', 'm']);
1559
+ registerLangHandler(sourceDecorator({
1560
+ 'keywords': 'null,true,false'
1561
+ }), ['json']);
1562
+ registerLangHandler(sourceDecorator({
1563
+ 'keywords': CSHARP_KEYWORDS,
1564
+ 'hashComments': true,
1565
+ 'cStyleComments': true,
1566
+ 'verbatimStrings': true,
1567
+ 'types': C_TYPES
1568
+ }), ['cs']);
1569
+ registerLangHandler(sourceDecorator({
1570
+ 'keywords': JAVA_KEYWORDS,
1571
+ 'cStyleComments': true
1572
+ }), ['java']);
1573
+ registerLangHandler(sourceDecorator({
1574
+ 'keywords': SH_KEYWORDS,
1575
+ 'hashComments': true,
1576
+ 'multiLineStrings': true
1577
+ }), ['bsh', 'csh', 'sh']);
1578
+ registerLangHandler(sourceDecorator({
1579
+ 'keywords': PYTHON_KEYWORDS,
1580
+ 'hashComments': true,
1581
+ 'multiLineStrings': true,
1582
+ 'tripleQuotedStrings': true
1583
+ }), ['cv', 'py']);
1584
+ registerLangHandler(sourceDecorator({
1585
+ 'keywords': PERL_KEYWORDS,
1586
+ 'hashComments': true,
1587
+ 'multiLineStrings': true,
1588
+ 'regexLiterals': true
1589
+ }), ['perl', 'pl', 'pm']);
1590
+ registerLangHandler(sourceDecorator({
1591
+ 'keywords': RUBY_KEYWORDS,
1592
+ 'hashComments': true,
1593
+ 'multiLineStrings': true,
1594
+ 'regexLiterals': true
1595
+ }), ['rb']);
1596
+ registerLangHandler(sourceDecorator({
1597
+ 'keywords': JSCRIPT_KEYWORDS,
1598
+ 'cStyleComments': true,
1599
+ 'regexLiterals': true
1600
+ }), ['js']);
1601
+ registerLangHandler(sourceDecorator({
1602
+ 'keywords': COFFEE_KEYWORDS,
1603
+ 'hashComments': 3, // ### style block comments
1604
+ 'cStyleComments': true,
1605
+ 'multilineStrings': true,
1606
+ 'tripleQuotedStrings': true,
1607
+ 'regexLiterals': true
1608
+ }), ['coffee']);
1609
+ registerLangHandler(
1610
+ createSimpleLexer([], [[PR_STRING, /^[\s\S]+/]]), ['regex']);
1611
+
1612
+ function applyDecorator(job) {
1613
+ var opt_langExtension = job.langExtension;
1614
+
1615
+ try {
1616
+ // Extract tags, and convert the source code to plain text.
1617
+ var sourceAndSpans = extractSourceSpans(job.sourceNode, job.pre);
1618
+ /** Plain text. @type {string} */
1619
+ var source = sourceAndSpans.sourceCode;
1620
+ job.sourceCode = source;
1621
+ job.spans = sourceAndSpans.spans;
1622
+ job.basePos = 0;
1623
+
1624
+ // Apply the appropriate language handler
1625
+ langHandlerForExtension(opt_langExtension, source)(job);
1626
+
1627
+ // Integrate the decorations and tags back into the source code,
1628
+ // modifying the sourceNode in place.
1629
+ recombineTagsAndDecorations(job);
1630
+ } catch (e) {
1631
+ if (win['console']) {
1632
+ console['log'](e && e['stack'] ? e['stack'] : e);
1633
+ }
1634
+ }
1635
+ }
1636
+
1637
+ /**
1638
+ * @param sourceCodeHtml {string} The HTML to pretty print.
1639
+ * @param opt_langExtension {string} The language name to use.
1640
+ * Typically, a filename extension like 'cpp' or 'java'.
1641
+ * @param opt_numberLines {number|boolean} True to number lines,
1642
+ * or the 1-indexed number of the first line in sourceCodeHtml.
1643
+ */
1644
+ function prettyPrintOne(sourceCodeHtml, opt_langExtension, opt_numberLines) {
1645
+ // PATCHED: http://code.google.com/p/google-code-prettify/issues/detail?id=213
1646
+ var container = document.createElement('div');
1647
+ // This could cause images to load and onload listeners to fire.
1648
+ // E.g. <img onerror="alert(1337)" src="nosuchimage.png">.
1649
+ // We assume that the inner HTML is from a trusted source.
1650
+ container.innerHTML = '<pre>' + sourceCodeHtml + '</pre>';
1651
+ container = container.firstChild;
1652
+ if (opt_numberLines) {
1653
+ numberLines(container, opt_numberLines, true);
1654
+ }
1655
+
1656
+ var job = {
1657
+ langExtension: opt_langExtension,
1658
+ numberLines: opt_numberLines,
1659
+ sourceNode: container,
1660
+ pre: 1
1661
+ };
1662
+ applyDecorator(job);
1663
+ return container.innerHTML;
1664
+ }
1665
+
1666
+ function prettyPrint(opt_whenDone) {
1667
+ function byTagName(tn) { return document.getElementsByTagName(tn); }
1668
+ // fetch a list of nodes to rewrite
1669
+ var codeSegments = [byTagName('pre'), byTagName('code'), byTagName('xmp')];
1670
+ var elements = [];
1671
+ for (var i = 0; i < codeSegments.length; ++i) {
1672
+ for (var j = 0, n = codeSegments[i].length; j < n; ++j) {
1673
+ elements.push(codeSegments[i][j]);
1674
+ }
1675
+ }
1676
+ codeSegments = null;
1677
+
1678
+ var clock = Date;
1679
+ if (!clock['now']) {
1680
+ clock = { 'now': function () { return +(new Date); } };
1681
+ }
1682
+
1683
+ // The loop is broken into a series of continuations to make sure that we
1684
+ // don't make the browser unresponsive when rewriting a large page.
1685
+ var k = 0;
1686
+ var prettyPrintingJob;
1687
+
1688
+ var langExtensionRe = /\blang(?:uage)?-([\w.]+)(?!\S)/;
1689
+ var prettyPrintRe = /\bprettyprint\b/;
1690
+ var prettyPrintedRe = /\bprettyprinted\b/;
1691
+ var preformattedTagNameRe = /pre|xmp/i;
1692
+ var codeRe = /^code$/i;
1693
+ var preCodeXmpRe = /^(?:pre|code|xmp)$/i;
1694
+
1695
+ function doWork() {
1696
+ var endTime = (win['PR_SHOULD_USE_CONTINUATION'] ?
1697
+ clock['now']() + 250 /* ms */ :
1698
+ Infinity);
1699
+ for (; k < elements.length && clock['now']() < endTime; k++) {
1700
+ var cs = elements[k];
1701
+ var className = cs.className;
1702
+ if (prettyPrintRe.test(className)
1703
+ // Don't redo this if we've already done it.
1704
+ // This allows recalling pretty print to just prettyprint elements
1705
+ // that have been added to the page since last call.
1706
+ && !prettyPrintedRe.test(className)) {
1707
+
1708
+ // make sure this is not nested in an already prettified element
1709
+ var nested = false;
1710
+ for (var p = cs.parentNode; p; p = p.parentNode) {
1711
+ var tn = p.tagName;
1712
+ if (preCodeXmpRe.test(tn)
1713
+ && p.className && prettyPrintRe.test(p.className)) {
1714
+ nested = true;
1715
+ break;
1716
+ }
1717
+ }
1718
+ if (!nested) {
1719
+ // Mark done. If we fail to prettyprint for whatever reason,
1720
+ // we shouldn't try again.
1721
+ cs.className += ' prettyprinted';
1722
+
1723
+ // If the classes includes a language extensions, use it.
1724
+ // Language extensions can be specified like
1725
+ // <pre class="prettyprint lang-cpp">
1726
+ // the language extension "cpp" is used to find a language handler
1727
+ // as passed to PR.registerLangHandler.
1728
+ // HTML5 recommends that a language be specified using "language-"
1729
+ // as the prefix instead. Google Code Prettify supports both.
1730
+ // http://dev.w3.org/html5/spec-author-view/the-code-element.html
1731
+ var langExtension = className.match(langExtensionRe);
1732
+ // Support <pre class="prettyprint"><code class="language-c">
1733
+ var wrapper;
1734
+ if (!langExtension && (wrapper = childContentWrapper(cs))
1735
+ && codeRe.test(wrapper.tagName)) {
1736
+ langExtension = wrapper.className.match(langExtensionRe);
1737
+ }
1738
+
1739
+ if (langExtension) { langExtension = langExtension[1]; }
1740
+
1741
+ var preformatted;
1742
+ if (preformattedTagNameRe.test(cs.tagName)) {
1743
+ preformatted = 1;
1744
+ } else {
1745
+ var currentStyle = cs['currentStyle'];
1746
+ var whitespace = (
1747
+ currentStyle
1748
+ ? currentStyle['whiteSpace']
1749
+ : (document.defaultView
1750
+ && document.defaultView.getComputedStyle)
1751
+ ? document.defaultView.getComputedStyle(cs, null)
1752
+ .getPropertyValue('white-space')
1753
+ : 0);
1754
+ preformatted = whitespace
1755
+ && 'pre' === whitespace.substring(0, 3);
1756
+ }
1757
+
1758
+ // Look for a class like linenums or linenums:<n> where <n> is the
1759
+ // 1-indexed number of the first line.
1760
+ var lineNums = cs.className.match(/\blinenums\b(?::(\d+))?/);
1761
+ lineNums = lineNums
1762
+ ? lineNums[1] && lineNums[1].length ? +lineNums[1] : true
1763
+ : false;
1764
+ if (lineNums) { numberLines(cs, lineNums, preformatted); }
1765
+
1766
+ // do the pretty printing
1767
+ prettyPrintingJob = {
1768
+ langExtension: langExtension,
1769
+ sourceNode: cs,
1770
+ numberLines: lineNums,
1771
+ pre: preformatted
1772
+ };
1773
+ applyDecorator(prettyPrintingJob);
1774
+ }
1775
+ }
1776
+ }
1777
+ if (k < elements.length) {
1778
+ // finish up in a continuation
1779
+ setTimeout(doWork, 250);
1780
+ } else if (opt_whenDone) {
1781
+ opt_whenDone();
1782
+ }
1783
+ }
1784
+
1785
+ doWork();
1786
+ }
1787
+
1788
+ /**
1789
+ * Contains functions for creating and registering new language handlers.
1790
+ * @type {Object}
1791
+ */
1792
+ var PR = win['PR'] = {
1793
+ 'createSimpleLexer': createSimpleLexer,
1794
+ 'registerLangHandler': registerLangHandler,
1795
+ 'sourceDecorator': sourceDecorator,
1796
+ 'PR_ATTRIB_NAME': PR_ATTRIB_NAME,
1797
+ 'PR_ATTRIB_VALUE': PR_ATTRIB_VALUE,
1798
+ 'PR_COMMENT': PR_COMMENT,
1799
+ 'PR_DECLARATION': PR_DECLARATION,
1800
+ 'PR_KEYWORD': PR_KEYWORD,
1801
+ 'PR_LITERAL': PR_LITERAL,
1802
+ 'PR_NOCODE': PR_NOCODE,
1803
+ 'PR_PLAIN': PR_PLAIN,
1804
+ 'PR_PUNCTUATION': PR_PUNCTUATION,
1805
+ 'PR_SOURCE': PR_SOURCE,
1806
+ 'PR_STRING': PR_STRING,
1807
+ 'PR_TAG': PR_TAG,
1808
+ 'PR_TYPE': PR_TYPE,
1809
+ 'prettyPrintOne': win['prettyPrintOne'] = prettyPrintOne,
1810
+ 'prettyPrint': win['prettyPrint'] = prettyPrint
1811
+ };
1812
+
1813
+ // Make PR available via the Asynchronous Module Definition (AMD) API.
1814
+ // Per https://github.com/amdjs/amdjs-api/wiki/AMD:
1815
+ // The Asynchronous Module Definition (AMD) API specifies a
1816
+ // mechanism for defining modules such that the module and its
1817
+ // dependencies can be asynchronously loaded.
1818
+ // ...
1819
+ // To allow a clear indicator that a global define function (as
1820
+ // needed for script src browser loading) conforms to the AMD API,
1821
+ // any global define function SHOULD have a property called "amd"
1822
+ // whose value is an object. This helps avoid conflict with any
1823
+ // other existing JavaScript code that could have defined a define()
1824
+ // function that does not conform to the AMD API.
1825
+ if (typeof define === "function" && define['amd']) {
1826
+ define("google-code-prettify", [], function () {
1827
+ return PR;
1828
+ });
1829
+ }
1830
+ })();
1831
+
1832
+ })(window, window.angular);
1833
+ angular.element(document).find('head').append('<style type="text/css">.com{color:#93a1a1;}.lit{color:#195f91;}.pun,.opn,.clo{color:#93a1a1;}.fun{color:#dc322f;}.str,.atv{color:#D14;}.kwd,.linenums .tag{color:#1e347b;}.typ,.atn,.dec,.var{color:teal;}.pln{color:#48484c;}.prettyprint{padding:8px;background-color:#f7f7f9;border:1px solid #e1e1e8;}.prettyprint.linenums{-webkit-box-shadow:inset 40px 0 0 #fbfbfc,inset 41px 0 0 #ececf0;-moz-box-shadow:inset 40px 0 0 #fbfbfc,inset 41px 0 0 #ececf0;box-shadow:inset 40px 0 0 #fbfbfc,inset 41px 0 0 #ececf0;}ol.linenums{margin:0 0 0 33px;}ol.linenums li{padding-left:12px;color:#bebec5;line-height:18px;text-shadow:0 1px 0 #fff;}</style>');