Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function highlight(input) {
- var output = '',
- string = false,
- lineComment = false,
- capVar = false,
- warnings = [];
- for (var i = 0; i < input.length; i++) {
- var c = input[i];
- if (c == '/' && input[i + 1] == '/') {
- lineComment = true;
- output += '<span class="comment">' + c + input[++i];
- } else if (lineComment) {
- if (c == '\n') {
- lineComment = false;
- output += '</span>';
- }
- output += c;
- } else if (c === string) {
- string = false;
- output += c + '</span>';
- } else if (string && c == '\n') {
- warnings.push([i, 'Unexpected line end, string literal not terminated.']);
- string = false;
- output += '</span>' + c;
- } else if (string) {
- if (c == '\\') output += '<span class="escape">';
- output += c;
- if (c == '\\') {
- output += c = input[++i];
- if (c == 'u') {
- output += c = input[++i];
- if (c == '{') {
- while ((c = input[++i]) && c != '}') output += c;
- output += '}';
- } else output += input[++i] + input[++i] + input[++i];
- } else if (c == 'x') output += input[++i] + input[++i];
- output += '</span>';
- }
- } else if (c == '"' || c == "'") {
- string = c;
- output += '<span class="string">' + c;
- } else if (c != c.toLowerCase() && (input[i - 1] || ' ').match(/\W/)) {
- capVar = true;
- output += '<span class="cap-var">' + c;
- } else if (capVar && c.match(/\W/)) {
- capVar = false;
- output += '</span>';
- i--;
- } else if (input.substr(i, 9) == 'prototype') {
- output += '<span class="prototype">prototype</span>';
- i += 8;
- } else if (input.substr(i, 8) == 'function') {
- var j = i,
- l = c;
- while (l = input[--j]) {
- if (l == '=') {
- var end = output.length + j - i;
- output = output.substring(0, end) + '<span class="operator assigns">=</span>' + output.substr(end + 1);
- j = end;
- while (output[--j].match(/\s/)) end--;
- var start = end;
- j++;
- while (j && !output.replaceAll('<span class=', '<span-class-')[--j].match(/[\s=(]/)) start--;
- output = output.substring(0, start) + '<span class="function-name">' + output.substring(start, end) + '</span>' + output.substr(end);
- break;
- } else if (!l.match(/\s/)) break;
- }
- output += '<span class="keyword">function</span>';
- i += 6;
- var stop = ++i + input.substr(i).indexOf('(');
- if (!stop) return warnings.push([i, 'Function arguments not found.']);
- if ((c = input[++i]).match(/\s/)) {
- output += c + '<span class="function-name">';
- while ((c = input[++i]) && c != '(') output += c;
- output += '</span>';
- }
- while (i < stop) output += input[++i];
- output += '<span class="punctuation">(</span>';
- var argString = '';
- while ((c = input[++i]) && c != ')') argString += c;
- output += argString.replace(/[^\s,]+/g, '<span class="arguments">$&</span>').replaceAll(',', '<span class="punctuation">,</span>') + '<span class="punctuation">)</span>';
- } else if (',.;()[]{}'.indexOf(c) != -1) {
- output += '<span class="punctuation">' + c + '</span>';
- } else output += c;
- }
- console.log(output);
- console.log(warnings);
- return output.split('\n');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement