-
Notifications
You must be signed in to change notification settings - Fork 1
/
my-highlighter.js
777 lines (636 loc) · 32.7 KB
/
my-highlighter.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
// My highlighter
// Version 1.36
// (c) 2012-2014
// Author: PhDr. Matej Lednár, PhD.
//
// JavaScript simple syntax highlighter with support XML, HTML, Javascript, and DOM languages.
// This library supports only latest web browsers.
// User can use universal highlighter with class code, or special
// highlighters for HTML and JavaScript
// My highlighter features
// User can use own library prefix
// User can use own class name prefix
// User can run library manually or automatic
// User can define colors and statements for HTML, XML, JavaScript and DOM
// User can use own CSS rules
// User can display and highlight website's source code (must be a descendant of the <body> element)
// User can display and highlight inserted content into <textarea> element
// User can define that content may displays only once, add data-code="once" to the target element
// TODO add JS/DOM support - more statements
// TODO new option - change display order: output source <--> source output
// TODO highlight defined lines, e.g. data-highlight="2,5,6-8,11"
// TODO display plain text
// Latest updates:
// see official documentation
// Use HTML script element for library activation.
// <script [data-code="false|true"] [data-class="className"] [data-indent="true"]
// [data-conflict="libraryPrefix"] [data-highlight-only="false|true"] src="my-highlighter.js"></script>
// Default library settings.
// <link rel="StyleSheet" type="text/css" href="default.css"/>
// <script src="my-highlighter.js"></script>
// Recommended settings for JavaScript frameworks (e.g. Dojo)
// Run showCode(); manually. showCode() method will be deleted from the output.
// <script src="my-highlighter.js" data-code="false"></script>
// _.showCode(); // see demos
// HTML attributes description:
// data-code="false" - disables automatic highlighting, user can run the highlighter manually
// data-class="className" - changes highlighter's class name
// data-conflict="prefix" - changes library's prefix
// data-highlight-only="true" - highlights only elements with class="code", no node clone
// data-indent="true" - allows left indentation of the content
// Prohibited string
// ^^|^ - semicolon replacer
/* Library's functions overview
This is a part of the mllibrary.js (author: Matej Lednár)
The library prefix is _ (underscore).
Method name Method description
_.tag(element, index) // gets an element from nodelist by index
_.log(data) // writes an argument into console
_.regEvent(element, event,
function, phase) // registers an event listener
_.showCode() // if the script element contains data-code="true", displays content of all elements with class="code"
_.initLibrary() // allows change the library prefix via data-conflict attribute and enable / disable run _.showCode() automatically.
_.runLibrary() // runs showCode() and defines prefix of the library
*/
(function() {
// The library prefix is _ (underscore).
var _ = {
};
_.prefix = "_"; // default library prefix
_.code = true; // runs highlighter automatically for all elements with code class
_.className = "code"; // defualt class for highlighter
_.highlightOnly = false; // default settings for automatic highlighter run
_.classCode = false;
_.classCodeHTML = false;
_.classCodeJS = false;
_.classCodeXML = false;
_.counter = 0; // code section identifier
_.textareaCounter = 0; // textarea section identifier
/** _.tag
* gets an element from nodelist by index
* @param {String} element - the name of the element
* @param {Number} index - index of the element in the nodelist
*/
_.tag = function(element, index) {
return document.getElementsByTagName(element)[index];
};
/** _.log
* writes an argument into console
* @param {String} data - text for the console
*/
_.log = function(data) {
console.log(data);
};
/**_.regEvent
* create event listener
* @param {String} element - DOM node
* @param {String} event - name of the event
* @param {Function} fun - name of the registered function
* @param {Boolean} phase - has capture phase? [true | false]
*/
_.regEvent = function(element, event, fun, phase) {
phase = !!phase;
if (element.addEventListener == undefined) {
element.attachEvent("on" + event, fun);
} else {
element.addEventListener(event, fun, phase);
}
};
/** _.unregEvent
* remove event listener
* @param {String} element - DOM node
* @param {String} event - name of the event
* @param {Function} fun - name of the registered function
* @param {Boolean} phase - has capture phase? [true | false]
*/
_.unregEvent = function(element, event, fun, phase) {
phase = !!phase;
if (element.removeEventListener == undefined)
element.detachEvent("on" + event, fun);
else {
element.removeEventListener(event, fun, phase);
}
};
/** _.showCode() content highlighter
* highlights element's conntent
* default class: code
* data-code attribute with false value deactivates highlighting
* @param {String} className - value of the data-class attribute if was set
*/
_.showCode = function(className) {
var self = this;
// set class for highlighter
if (!className) {
className = this.className;
} else {
this.className = className;
}
className = "." + className;
var highlightOnly = this.highlightOnly;
// detects highlighting sections
var codeElements = document.querySelectorAll(className);
var numberOfCodeElements = codeElements.length;
var htmlElements = document.querySelectorAll(className + "-html");
var numberOfHTMLElements = htmlElements.length;
var jsElements = document.querySelectorAll(className + "-js");
var numberOfJSElements = jsElements.length;
var xmlElements = document.querySelectorAll(className + "-xml");
var numberOfXMLElements = xmlElements.length;
function highlight(code, background, self) {
var classCode = self.classCode;
var classCodeJS = self.classCodeJS;
var classCodeHTML = self.classCodeHTML;
var classCodeXML = self.classCodeXML;
function highlightCode(data, self) {
if (classCodeJS || classCode) {
var JSObjects = ["RegExp", "Object", "Array", "Math",
"Boolean", "Date", "Function", "Number", "String"];
var JSProperties = ["search", "replace", "length", "value",
"slice", "split", "round", "ceil", "floor"];
var JSStatements = ["var", "function", "if", "else",
"switch", "case", "return", "for", "new"];
var DOMObjects = ["this", "document", "window", "history",
"console"];
var DOMProperties = ["write", "submit", "reset", "forms",
"writeln", "getElementById", "childNodes", "value",
"nodeValue", "innerText", "innerHTML", "firstChild",
"createElement", "log", "style",
"getElementsByTagName", "setSelectionRange",
"selectionStart", "selectionEnd", "focus"];
// \s as a first char, \s as a last char
var JSOperators1 = ["/", "-", "\\?", "\\*", "\\+", "in",
"==", ">", "<", ">=", "<=", "=", "\\!==",
"===", "\\|\\|"];
// \( | \s | ^ as a first char, \s as a last char
var JSOperators2 = ["delete", "typeof", "instanceof",
"new"];
// no \s | ^
var JSOperators3 = ["\\.", "\\(", "\\)", "\\[", "\\]",
"\\{", "\\}"];
}
var regexp;
var Fix = "class"; // safety class attribute declaration
regexp = new RegExp(" " + Fix + "=", "g");
data = data.replace(regexp, " <span class='my-highlight-attribute'>" + Fix + "=</span>");
// safety single comment section // replacement
data = data.replace(/(http:|https:|ftp:|file:)\/\//g, "$1//");
// marks elements
function highlightElements(classPostfix) {
// data = data.replace(/(<\/?[a-zA-Z-0-9\s-\";:,\.'\(\)=\^\|]*>)/g, "<span class='my-highlight-tag" + classPostfix + "'>$1</span>");
data = data.replace(/(<\/?[a-zA-Z-0-9\-]*?>)/g, "<span class='my-highlight-tag" + classPostfix + "'>$1</span>");
data = data.replace(/(<\/?[a-zA-Z-0-9\-\";:,\.'=\(\)\^\|\s<>_áéíóúôýľščťžäÁÉÍÓÚÔÝĽŠČŤŽÄ\\\/]*?>)/g, "<span class='my-highlight-tag" + classPostfix + "'>$1</span>");
data = data.replace(/(<!DOCTYPE?[a-zA-Z-0-9\-\"\/;:,\.'=\(\)\^\|\s]*>)/g, "<span class='my-highlight-tag" + classPostfix + "'>$1</span>");
}
if (classCodeHTML) {
highlightElements("-html");
}
if (classCode) {
highlightElements("");
}
if (classCodeXML) {
highlightElements("-xml");
}
// marks semicolon ;
data = data.replace(/\^\^\|\^/g, "<span class='my-highlight-semicolon'>;</span>");
// marks atributtes
function highlightAttributes(classPostfix) {
data = data.replace(/\s([a-z-]+=)\"/g, "<span class='my-highlight-attribute" + classPostfix + "'> $1</span>\"");
}
if (classCodeHTML) {
highlightAttributes("-html");
}
if (classCode) {
highlightAttributes("");
}
if (classCodeXML) {
highlightAttributes("-xml");
}
// marks strings, user can define special colors for code, JS and HTML
function highlightString(classPostfix) {
// empty string preventions
data = data.replace(/""/g, "^^|^");
data = data.replace(/\"([a-zA-Z0-9;<\-\/\.':,\s\(\)\[\]\+=\?!>_#\$&\^\\%áéíóúôýľščťžäÁÉÍÓÚÔÝĽŠČŤŽÄ]+?)\"/g, "\"<span class='my-highlight-string" + classPostfix + "'>$1</span>\"");
// returns back empty string
data = data.replace(/\^\^\|\^/g, "\"\"");
}
if (classCodeHTML) {
highlightString("-html");
}
if (classCode) {
highlightString("");
}
if (classCodeJS) {
highlightString("-js");
}
if (classCodeXML) {
highlightString("-xml");
}
// mark quotation marks
data = data.replace(/\"/g, "<span class='my-highlight-quotation'>\"</span>");
if (classCodeJS || classCode) {
var arrLength = JSObjects.length;
var index = 0;
// JavaScript objects
for (index = 0; index < arrLength; index++) {
// with || without whitespace as a first char, .|;|( as a last char
regexp = new RegExp("(\\(|\\s|^)" + JSObjects[index] + "(\\.|;|\\(|$)", "g");
data = data.replace(regexp, "$1<span class='my-highlight-js-object'>" + JSObjects[index] + "</span>$2");
}
// JavaScript statements
arrLength = JSStatements.length;
for (index = 0; index < arrLength; index++) {
regexp = new RegExp("(\\(|\\s|^)" + JSStatements[index] + "(\\s|\\()", "g");
data = data.replace(regexp, "$1<span class='my-highlight-js-statement'>" + JSStatements[index] + "</span>$2");
}
// JavaScript properties
arrLength = JSProperties.length;
for (index = 0; index < arrLength; index++) {
// .property | .property; | .method( | .property) - condition | .property = | .property
regexp = new RegExp("\\." + JSProperties[index] + "(\\)|\\(|;|\\.|<span class='my-highlight-|\\s=\\s|,)", "g");
data = data.replace(regexp, ".<span class='my-highlight-js-property'>" + JSProperties[index] + "</span>$1");
}
// DOM objects
arrLength = DOMObjects.length;
for (index = 0; index < arrLength; index++) {
// with || without whitespace as a first char, .|; as a last char
regexp = new RegExp("(\\(|\\s|^)" + DOMObjects[index] + "(\\.|;)", "g");
data = data.replace(regexp, "$1<span class='my-highlight-dom-object'>" + DOMObjects[index] + "</span>$2");
}
// DOM properties
arrLength = DOMProperties.length;
for (index = 0; index < arrLength; index++) {
// .property | .property; | .method( | .property) - condition | .property = | .property
regexp = new RegExp("\\." + DOMProperties[index] + "(\\)|\\(|;|\\.|<span class='my-highlight-|\\s=\\s|,)", "g");
data = data.replace(regexp, ".<span class='my-highlight-dom-property'>" + DOMProperties[index] + "</span>$1");
}
// JavaScript operators
arrLength = JSOperators1.length;
for (index = 0; index < arrLength; index++) {
regexp = new RegExp("(\\s)" + JSOperators1[index] + "(\\s)", "g");
data = data.replace(regexp, "$1<span class='my-highlight-javascript-operator'>" + JSOperators1[index].replace(/\\/g, "") + "</span>$2");
}
arrLength = JSOperators2.length;
for (index = 0; index < arrLength; index++) {
regexp = new RegExp("(\\(|\\s|^)" + JSOperators2[index] + "(\\s)", "g");
data = data.replace(regexp, "$1<span class='my-highlight-javascript-operator'>" + JSOperators2[index] + "</span>$2");
}
arrLength = JSOperators3.length;
for (index = 0; index < arrLength; index++) {
regexp = new RegExp(JSOperators3[index], "g");
data = data.replace(regexp, "<span class='my-highlight-javascript-operator'>" + JSOperators3[index].replace(/\\/g, "") + "</span>");
}
}
if (classCodeJS || classCode) {
// JavaScript single line comments
// checks if comment exists
if (data.indexOf("//") != -1) {
var tmp = data.split("//"); // checks for statements before comments
var firstPart = tmp.shift(); // removes highlighted statements
var lastPart = "//" + tmp.join("//");
// removes highlighting after //
lastPart = lastPart.replace(/class='my-highlight-[a-z-]*'/g, "");
// highlights comment
data = firstPart + "<span class='my-highlight-comment'>" + lastPart + "</span>";
}
}
// string highlighting fix, removes highlighted words and chars from strings
tmp = data.split("<span class='my-highlight-quotation'>\"</span>");
var tmpLength = tmp.length;
if (tmpLength > 0) {
for (var tmpPart = 0; tmpPart < tmpLength; tmpPart++) {
// every second part is string
if (tmpPart % 2 == 1) {
tmp[tmpPart] = tmp[tmpPart].replace(/my-highlight-string/g, "my-highlightString");
tmp[tmpPart] = tmp[tmpPart].replace(/class='my-highlight-[a-z-]*'/g, "");
tmp[tmpPart] = tmp[tmpPart].replace(/my-highlightString/g, "my-highlight-string");
}
}
}
// adds quotation mark with highlighting
data = tmp.join("<span class='my-highlight-quotation'>\"</span>");
// multiline comments marker
//
// removes highlighter classes from comment
if (data.indexOf("/*") != -1 || data.indexOf("*/") != -1) {
function removeHighligtingFromComments(matchedData) {
var result = matchedData.replace(/class='my-highlight-[a-z-]*'/g, "");
return "<span class='my-highlight-comment'>" + result + "</span>";
}
// flag for one or more /* */ comments in a line
var isFullComment = data.match(/(\/\*)([a-zA-Z0-9;<\-\/\.':,\(\)\[\]\+=\?>#\$&\^%\s|]*)(\*\/)/g);
// flag for highlight content after /*
var isStartComment = data.match(/(\/\*)([a-zA-Z0-9;<\-\/\.':,\(\)\[\]\+=\?>#\$&\^%\s]*)/g);
// flag for highlight content before */
var isEndComment = data.match(/([a-zA-Z0-9;<\-\/\.':,\(\)\[\]\+=\?>#\$&\^%\s|]*)(\*\/)/g);
// one or more /* */ comments in a line
if (isFullComment && isFullComment.length > 0) {
data = data.replace(/(\/\*)([a-zA-Z0-9;<\-\/\.':,\(\)\[\]\+=\?>#\$&\^%\s|]*)(\*\/)/g, removeHighligtingFromComments);
}
// highlights content after /*
else if (isStartComment && (isStartComment.length === 1)) {
data = data.replace(/(\/\*)([a-zA-Z0-9;<\-\/\.':,\(\)\[\]\+=\?>#\$&\^%\s]*)/g, removeHighligtingFromComments);
}
// highlights content before */
else if (isEndComment && isEndComment.length === 1) {
data = data.replace(/([a-zA-Z0-9;<\-\/\.':,\(\)\[\]\+=\?>#\$&\^%\s|]*)(\*\/)/g, removeHighligtingFromComments);
}
}
// end of multiline comments
// returns higlighted line
return data;
}
// deletes left indentation
code = code.replace(/\t/g, ""); // removes tabs
if (!self.codeIndent) {
code = code.replace(/(^\s{6})|(^\s{4})/, ""); // removes spaces 4 or 6
}
code = highlightCode(code, self); // invoke highlighter
// entity   creates correct empty line height
if (code == "") {
code = code + " ";
}
code = "<tr class='" + background + "'><td class='my-table-right-column-content'>" + code + "</td></tr>";
return code;
}
// deletes empty lines before and after the content
function deleteEmptyLinesEnds() {
var code = source.innerHTML;
var first_new_line_flag = code.indexOf("\n");
var last_new_line_flag = code.lastIndexOf("\n");
// checks if exists any text after the last \n
function isCode(text) {
var regExp = /[^\s]/g;
return regExp.test(text);
}
var blankLines;
// deletes empty lines at the start
do {
blankLines = isCode(code.slice(0, first_new_line_flag));
if (!blankLines) {
source.innerHTML = code.slice(first_new_line_flag + 1, code.length);
// removes first \n
code = source.innerHTML;
first_new_line_flag = code.indexOf("\n");
} else {
blankLines = true;
}
} while (!blankLines);
// deletes empty lines at the end
do {
blankLines = isCode(code.slice(last_new_line_flag, code.length));
if (!blankLines) {
source.innerHTML = code.slice(0, last_new_line_flag);
// delete last \n
code = source.innerHTML;
last_new_line_flag = code.lastIndexOf("\n");
} else {
blankLines = true;
}
} while (!blankLines);
}
function runHighlighterCore(className, self) {
var content;
// from document - highlights document fragment
if (highlightOnly == false) {
var target = self.tag("body", 0);
var pre_element = document.createElement("section");
pre_element.setAttribute("class", "my-highlight-show-as-pre-element show-code-" + self.counter);
target.appendChild(pre_element);
target.insertBefore(document.querySelector(".show-code-" + self.counter), document.querySelectorAll(className)[(i)]);
target = document.querySelector(".show-code-" + self.counter);
var code_element = document.createElement("section");
code_element.setAttribute("class", "code-element-" + self.counter);
target.appendChild(code_element);
content = document.querySelectorAll(className)[(i)].innerHTML;
}
// from element textarea - highlights textarea content
// creates new section element, inserts textarea content into the new section element, hides textarea element
else {
var codeNode = document.querySelectorAll(className)[0];
var codeNodeName = codeNode.nodeName;
if (codeNodeName.toLowerCase() == "textarea") {
self.textareaCounter++;
target = codeNode.parentElement;
content = document.querySelectorAll(className)[0].innerHTML;
var section_element = document.createElement("section");
section_element.setAttribute("class", "my-highlight-show-as-pre-element my-highlight-inserted-section-element-" + self.textareaCounter);
content = content.replace(/</g, "<").replace(/>/g, ">");
section_element.innerHTML = content;
target.insertBefore(section_element, codeNode);
content = section_element.innerHTML;
codeNode.setAttribute("class", "my-highlight-hide");
}
}
// has element content? no content == no highlight
var tmp = /\w|[\]\[!"#\$%&'\(\)*+,\.\/:;<=>?@\^_`\{\|\}~-]/g;
if (!tmp.test(content)) {
return;
}
// deletes showCode(); statement from content
var tmpRegExp = new RegExp(self.prefix + "\\.showCode\\(\\);\\n?", "g");
content = content.replace(tmpRegExp, "");
content = content.replace(/(_\.showCode\()\);\n?/g, "");
// escapes HTML/XML elements
content = content.replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
content = content.replace(/;/g, "^^|^").replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
if (highlightOnly == false) {
// gets content from section element as copy of the document element
source = document.querySelector(".code-element-" + self.counter)
} else {
// gets content from new section element
source = document.querySelectorAll(".my-highlight-inserted-section-element-" + self.textareaCounter)[0];
}
source.innerHTML = content;
// deletes empty first and last lines
deleteEmptyLinesEnds();
content = source.innerHTML;
var numberOfLines = content.match(/\n/g);
// checks if an element is empty
if (numberOfLines == null) {
numberOfLines = 1;
} else {
numberOfLines = numberOfLines.length + 1;
}
// this section build tables with line numbers and code
function getTableOpenTag(id) {
var tableOpenTag = "<table class='my-table-column-" + id + " my-table-cell-spacing'>";
return tableOpenTag
}
var tableCloseTag = "</table>";
// column 1
var table = getTableOpenTag(1);
// sets background colors
var backgroundColor1 = "my-line-background-color-1 my-odd";
var backgroundColor2 = "my-line-background-color-2 my-even";
// creates numbers and lines for numbers
for (var line = 1; line < numberOfLines + 1; line++) {
if ((line % 2) == 1) {
background = backgroundColor1;
} else {
background = backgroundColor2;
}
// sets a special class e.g. for line spacing
if (line == 1) {
background = background + " my-special-begin";
}
if (line > 1 && line < numberOfLines) {
background = background + " my-special";
}
if (line == numberOfLines) {
background = background + " my-special-end";
}
table = table + "<tr class='" + background + "'><td class='my-table-left-column-numbers'>" + line + "</td></tr>";
}
var numbersTable = table + tableCloseTag;
// splits the content into lines
var ContentLines = content.split(/\n/g);
// column 1
table = getTableOpenTag(2);
// creates lines for code
for (line = 0; line < numberOfLines; line++) {
if ((line % 2) == 0) {
background = backgroundColor1;
} else {
background = backgroundColor2;
}
// sets a special class e.g. for line spacing
if (line == 0) {
background = background + " my-special-begin";
}
if (line > 0 && line < (numberOfLines - 1)) {
background = background + " my-special";
}
if (line == (numberOfLines - 1)) {
background = background + " my-special-end";
}
ContentLines[line] = highlight(ContentLines[line], background, self);
}
// joins lines into the final content and build table
var codeTable = table + ContentLines.join("") + tableCloseTag;
// displays final result (number and content)
source.innerHTML = "<table class='my-highlighter my-table-cell-spacing'><tr><td class='my-table-left-column'>" + numbersTable + "</td><td class='my-columns-separator'>" + codeTable + "</td></tr></table>";
// memory "clean"
content = "";
table = "";
}
// variable for source code
var source = null;
// highlights each element's content with code class
if (numberOfCodeElements) {
this.classCode = true;
for (var i = 0; i < numberOfCodeElements; i++) {
this.counter++;
// display once checker, if data-code="once", sets data-code-done="true"
if (!codeElements[i].getAttribute("data-code-done")) {
if (codeElements[i].getAttribute("data-code") == "once") {
codeElements[i].setAttribute("data-code-done", "true");
}
runHighlighterCore("." + this.className, self);
}
}
this.classCode = false;
}
// highlights each element's content with code-html class
if (numberOfHTMLElements) {
this.classCodeHTML = true;
for (var i = 0; i < numberOfHTMLElements; i++) {
this.counter++;
// display once checker, if data-code="once", sets data-code-done="true"
if (!htmlElements[i].getAttribute("data-code-done")) {
if (htmlElements[i].getAttribute("data-code") == "once") {
htmlElements[i].setAttribute("data-code-done", "true");
}
runHighlighterCore("." + this.className + "-html", self);
}
}
this.classCodeHTML = false;
}
// highlights each element's content with code-js class
if (numberOfJSElements) {
this.classCodeJS = true;
for (var i = 0; i < numberOfJSElements; i++) {
this.counter++;
// display once checker, if data-code="once", sets data-code-done="true"
if (!jsElements[i].getAttribute("data-code-done")) {
if (jsElements[i].getAttribute("data-code") == "once") {
jsElements[i].setAttribute("data-code-done", "true");
}
runHighlighterCore("." + this.className + "-js", self);
}
}
this.classCodeJS = false;
}
// highlights each element's content with code-xml class
if (numberOfXMLElements) {
this.classCodeXML = true;
for (var i = 0; i < numberOfXMLElements; i++) {
this.counter++;
// display once checker, if data-code="once", sets data-code-done="true"
if (!xmlElements[i].dataset.codeDone) {
if (xmlElements[i].dataset.code == "once") {
xmlElements[i].dataset.codeDone = "true";
}
runHighlighterCore("." + this.className + "-xml", self);
}
}
this.classCodeXML = false;
}
this.textareaCounter = 0;
};
// onload library settings
_.initLibrary = function() {
var scriptElements = document.getElementsByTagName("script");
var scriptElements_number = scriptElements.length;
for (var i = 0; i < scriptElements_number; i++) {
var srcAttr = scriptElements[i].getAttribute("src");
if (srcAttr) {
var isItMyHighlighter = srcAttr.lastIndexOf("my-highlighter");
if (srcAttr && isItMyHighlighter != -1) {
this.prefix = scriptElements[i].getAttribute("data-conflict");
// if code is false user will call _.showCode(); manually
var code = scriptElements[i].getAttribute("data-code");
if (code == "false") {
this.code = false;
}
var highlightOnly = scriptElements[i].getAttribute("data-highlight-only");
if (highlightOnly == "true") {
this.highlightOnly = true;
}
var codeIndent = scriptElements[i].getAttribute("data-indent");
if (codeIndent == "true") {
this.codeIndent = true;
}
var className = scriptElements[i].getAttribute("data-class");
if (className != null) {
console.log(className);
this.className = className;
}
}
}
}
}
// gets library setting and creates global object _ or object
// defined by data-conflict attribute
_.runLibrary = function() {
this.initLibrary();
// displays all elements content with class="code"
function showCode(className) {
return function() {
_.showCode(className);
}
}
if (this.code) {
this.regEvent(window, "load", showCode(this.className), true);
}
// get library prefix, default is _
var prefix = this.prefix;
if (prefix != null) {
_.log("My highlighter is loaded with prefix " + prefix);
window[prefix] = _;
} else {
_.log("My highlighter is loaded with prefix _");
window["_"] = _;
}
};
_.runLibrary();
})();