| 1 |
/* |
| 2 |
Syntax highlighting with language autodetection. |
| 3 |
https://highlightjs.org/ |
| 4 |
*/ |
| 5 |
|
| 6 |
(function(factory) { |
| 7 |
|
| 8 |
// Find the global object for export to both the browser and web workers. |
| 9 |
var globalObject = typeof window === 'object' && window || |
| 10 |
typeof self === 'object' && self; |
| 11 |
|
| 12 |
// Setup highlight.js for different environments. First is Node.js or |
| 13 |
// CommonJS. |
| 14 |
if(typeof exports !== 'undefined') { |
| 15 |
factory(exports); |
| 16 |
} else if(globalObject) { |
| 17 |
// Export hljs globally even when using AMD for cases when this script |
| 18 |
// is loaded with others that may still expect a global hljs. |
| 19 |
globalObject.hljs = factory({}); |
| 20 |
|
| 21 |
// Finally register the global hljs with AMD. |
| 22 |
if(typeof define === 'function' && define.amd) { |
| 23 |
define([], function() { |
| 24 |
return globalObject.hljs; |
| 25 |
}); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
}(function(hljs) { |
| 30 |
// Convenience variables for build-in objects |
| 31 |
var ArrayProto = [], |
| 32 |
objectKeys = Object.keys; |
| 33 |
|
| 34 |
// Global internal variables used within the highlight.js library. |
| 35 |
var languages = {}, |
| 36 |
aliases = {}; |
| 37 |
|
| 38 |
// Regular expressions used throughout the highlight.js library. |
| 39 |
var noHighlightRe = /^(no-?highlight|plain|text)$/i, |
| 40 |
languagePrefixRe = /\blang(?:uage)?-([\w-]+)\b/i, |
| 41 |
fixMarkupRe = /((^(<[^>]+>|\t|)+|(?:\n)))/gm; |
| 42 |
|
| 43 |
var spanEndTag = '</span>'; |
| 44 |
|
| 45 |
// Global options used when within external APIs. This is modified when |
| 46 |
// calling the `hljs.configure` function. |
| 47 |
var options = { |
| 48 |
classPrefix: 'hljs-', |
| 49 |
tabReplace: null, |
| 50 |
useBR: false, |
| 51 |
languages: undefined |
| 52 |
}; |
| 53 |
|
| 54 |
// Object map that is used to escape some common HTML characters. |
| 55 |
var escapeRegexMap = { |
| 56 |
'&': '&', |
| 57 |
'<': '<', |
| 58 |
'>': '>' |
| 59 |
}; |
| 60 |
|
| 61 |
/* Utility functions */ |
| 62 |
|
| 63 |
function escape(value) { |
| 64 |
return value.replace(/[&<>]/gm, function(character) { |
| 65 |
return escapeRegexMap[character]; |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
function tag(node) { |
| 70 |
return node.nodeName.toLowerCase(); |
| 71 |
} |
| 72 |
|
| 73 |
function testRe(re, lexeme) { |
| 74 |
var match = re && re.exec(lexeme); |
| 75 |
return match && match.index === 0; |
| 76 |
} |
| 77 |
|
| 78 |
function isNotHighlighted(language) { |
| 79 |
return noHighlightRe.test(language); |
| 80 |
} |
| 81 |
|
| 82 |
function blockLanguage(block) { |
| 83 |
var i, match, length, _class; |
| 84 |
var classes = block.className + ' '; |
| 85 |
|
| 86 |
classes += block.parentNode ? block.parentNode.className : ''; |
| 87 |
|
| 88 |
// language-* takes precedence over non-prefixed class names. |
| 89 |
match = languagePrefixRe.exec(classes); |
| 90 |
if (match) { |
| 91 |
return getLanguage(match[1]) ? match[1] : 'no-highlight'; |
| 92 |
} |
| 93 |
|
| 94 |
classes = classes.split(/\s+/); |
| 95 |
|
| 96 |
for (i = 0, length = classes.length; i < length; i++) { |
| 97 |
_class = classes[i] |
| 98 |
|
| 99 |
if (isNotHighlighted(_class) || getLanguage(_class)) { |
| 100 |
return _class; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
function inherit(parent, obj) { |
| 106 |
var key; |
| 107 |
var result = {}; |
| 108 |
|
| 109 |
for (key in parent) |
| 110 |
result[key] = parent[key]; |
| 111 |
if (obj) |
| 112 |
for (key in obj) |
| 113 |
result[key] = obj[key]; |
| 114 |
return result; |
| 115 |
} |
| 116 |
|
| 117 |
/* Stream merging */ |
| 118 |
|
| 119 |
function nodeStream(node) { |
| 120 |
var result = []; |
| 121 |
(function _nodeStream(node, offset) { |
| 122 |
for (var child = node.firstChild; child; child = child.nextSibling) { |
| 123 |
if (child.nodeType === 3) |
| 124 |
offset += child.nodeValue.length; |
| 125 |
else if (child.nodeType === 1) { |
| 126 |
result.push({ |
| 127 |
event: 'start', |
| 128 |
offset: offset, |
| 129 |
node: child |
| 130 |
}); |
| 131 |
offset = _nodeStream(child, offset); |
| 132 |
// Prevent void elements from having an end tag that would actually |
| 133 |
// double them in the output. There are more void elements in HTML |
| 134 |
// but we list only those realistically expected in code display. |
| 135 |
if (!tag(child).match(/br|hr|img|input/)) { |
| 136 |
result.push({ |
| 137 |
event: 'stop', |
| 138 |
offset: offset, |
| 139 |
node: child |
| 140 |
}); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
return offset; |
| 145 |
})(node, 0); |
| 146 |
return result; |
| 147 |
} |
| 148 |
|
| 149 |
function mergeStreams(original, highlighted, value) { |
| 150 |
var processed = 0; |
| 151 |
var result = ''; |
| 152 |
var nodeStack = []; |
| 153 |
|
| 154 |
function selectStream() { |
| 155 |
if (!original.length || !highlighted.length) { |
| 156 |
return original.length ? original : highlighted; |
| 157 |
} |
| 158 |
if (original[0].offset !== highlighted[0].offset) { |
| 159 |
return (original[0].offset < highlighted[0].offset) ? original : highlighted; |
| 160 |
} |
| 161 |
|
| 162 |
/* |
| 163 |
To avoid starting the stream just before it should stop the order is |
| 164 |
ensured that original always starts first and closes last: |
| 165 |
|
| 166 |
if (event1 == 'start' && event2 == 'start') |
| 167 |
return original; |
| 168 |
if (event1 == 'start' && event2 == 'stop') |
| 169 |
return highlighted; |
| 170 |
if (event1 == 'stop' && event2 == 'start') |
| 171 |
return original; |
| 172 |
if (event1 == 'stop' && event2 == 'stop') |
| 173 |
return highlighted; |
| 174 |
|
| 175 |
... which is collapsed to: |
| 176 |
*/ |
| 177 |
return highlighted[0].event === 'start' ? original : highlighted; |
| 178 |
} |
| 179 |
|
| 180 |
function open(node) { |
| 181 |
function attr_str(a) {return ' ' + a.nodeName + '="' + escape(a.value) + '"';} |
| 182 |
result += '<' + tag(node) + ArrayProto.map.call(node.attributes, attr_str).join('') + '>'; |
| 183 |
} |
| 184 |
|
| 185 |
function close(node) { |
| 186 |
result += '</' + tag(node) + '>'; |
| 187 |
} |
| 188 |
|
| 189 |
function render(event) { |
| 190 |
(event.event === 'start' ? open : close)(event.node); |
| 191 |
} |
| 192 |
|
| 193 |
while (original.length || highlighted.length) { |
| 194 |
var stream = selectStream(); |
| 195 |
result += escape(value.substring(processed, stream[0].offset)); |
| 196 |
processed = stream[0].offset; |
| 197 |
if (stream === original) { |
| 198 |
/* |
| 199 |
On any opening or closing tag of the original markup we first close |
| 200 |
the entire highlighted node stack, then render the original tag along |
| 201 |
with all the following original tags at the same offset and then |
| 202 |
reopen all the tags on the highlighted stack. |
| 203 |
*/ |
| 204 |
nodeStack.reverse().forEach(close); |
| 205 |
do { |
| 206 |
render(stream.splice(0, 1)[0]); |
| 207 |
stream = selectStream(); |
| 208 |
} while (stream === original && stream.length && stream[0].offset === processed); |
| 209 |
nodeStack.reverse().forEach(open); |
| 210 |
} else { |
| 211 |
if (stream[0].event === 'start') { |
| 212 |
nodeStack.push(stream[0].node); |
| 213 |
} else { |
| 214 |
nodeStack.pop(); |
| 215 |
} |
| 216 |
render(stream.splice(0, 1)[0]); |
| 217 |
} |
| 218 |
} |
| 219 |
return result + escape(value.substr(processed)); |
| 220 |
} |
| 221 |
|
| 222 |
/* Initialization */ |
| 223 |
|
| 224 |
function compileLanguage(language) { |
| 225 |
|
| 226 |
function reStr(re) { |
| 227 |
return (re && re.source) || re; |
| 228 |
} |
| 229 |
|
| 230 |
function langRe(value, global) { |
| 231 |
return new RegExp( |
| 232 |
reStr(value), |
| 233 |
'm' + (language.case_insensitive ? 'i' : '') + (global ? 'g' : '') |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
function compileMode(mode, parent) { |
| 238 |
if (mode.compiled) |
| 239 |
return; |
| 240 |
mode.compiled = true; |
| 241 |
|
| 242 |
mode.keywords = mode.keywords || mode.beginKeywords; |
| 243 |
if (mode.keywords) { |
| 244 |
var compiled_keywords = {}; |
| 245 |
|
| 246 |
var flatten = function(className, str) { |
| 247 |
if (language.case_insensitive) { |
| 248 |
str = str.toLowerCase(); |
| 249 |
} |
| 250 |
str.split(' ').forEach(function(kw) { |
| 251 |
var pair = kw.split('|'); |
| 252 |
compiled_keywords[pair[0]] = [className, pair[1] ? Number(pair[1]) : 1]; |
| 253 |
}); |
| 254 |
}; |
| 255 |
|
| 256 |
if (typeof mode.keywords === 'string') { // string |
| 257 |
flatten('keyword', mode.keywords); |
| 258 |
} else { |
| 259 |
objectKeys(mode.keywords).forEach(function (className) { |
| 260 |
flatten(className, mode.keywords[className]); |
| 261 |
}); |
| 262 |
} |
| 263 |
mode.keywords = compiled_keywords; |
| 264 |
} |
| 265 |
mode.lexemesRe = langRe(mode.lexemes || /\w+/, true); |
| 266 |
|
| 267 |
if (parent) { |
| 268 |
if (mode.beginKeywords) { |
| 269 |
mode.begin = '\\b(' + mode.beginKeywords.split(' ').join('|') + ')\\b'; |
| 270 |
} |
| 271 |
if (!mode.begin) |
| 272 |
mode.begin = /\B|\b/; |
| 273 |
mode.beginRe = langRe(mode.begin); |
| 274 |
if (!mode.end && !mode.endsWithParent) |
| 275 |
mode.end = /\B|\b/; |
| 276 |
if (mode.end) |
| 277 |
mode.endRe = langRe(mode.end); |
| 278 |
mode.terminator_end = reStr(mode.end) || ''; |
| 279 |
if (mode.endsWithParent && parent.terminator_end) |
| 280 |
mode.terminator_end += (mode.end ? '|' : '') + parent.terminator_end; |
| 281 |
} |
| 282 |
if (mode.illegal) |
| 283 |
mode.illegalRe = langRe(mode.illegal); |
| 284 |
if (mode.relevance == null) |
| 285 |
mode.relevance = 1; |
| 286 |
if (!mode.contains) { |
| 287 |
mode.contains = []; |
| 288 |
} |
| 289 |
var expanded_contains = []; |
| 290 |
mode.contains.forEach(function(c) { |
| 291 |
if (c.variants) { |
| 292 |
c.variants.forEach(function(v) {expanded_contains.push(inherit(c, v));}); |
| 293 |
} else { |
| 294 |
expanded_contains.push(c === 'self' ? mode : c); |
| 295 |
} |
| 296 |
}); |
| 297 |
mode.contains = expanded_contains; |
| 298 |
mode.contains.forEach(function(c) {compileMode(c, mode);}); |
| 299 |
|
| 300 |
if (mode.starts) { |
| 301 |
compileMode(mode.starts, parent); |
| 302 |
} |
| 303 |
|
| 304 |
var terminators = |
| 305 |
mode.contains.map(function(c) { |
| 306 |
return c.beginKeywords ? '\\.?(' + c.begin + ')\\.?' : c.begin; |
| 307 |
}) |
| 308 |
.concat([mode.terminator_end, mode.illegal]) |
| 309 |
.map(reStr) |
| 310 |
.filter(Boolean); |
| 311 |
mode.terminators = terminators.length ? langRe(terminators.join('|'), true) : {exec: function(/*s*/) {return null;}}; |
| 312 |
} |
| 313 |
|
| 314 |
compileMode(language); |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
Core highlighting function. Accepts a language name, or an alias, and a |
| 319 |
string with the code to highlight. Returns an object with the following |
| 320 |
properties: |
| 321 |
|
| 322 |
- relevance (int) |
| 323 |
- value (an HTML string with highlighting markup) |
| 324 |
|
| 325 |
*/ |
| 326 |
function highlight(name, value, ignore_illegals, continuation) { |
| 327 |
|
| 328 |
function subMode(lexeme, mode) { |
| 329 |
var i, length; |
| 330 |
|
| 331 |
for (i = 0, length = mode.contains.length; i < length; i++) { |
| 332 |
if (testRe(mode.contains[i].beginRe, lexeme)) { |
| 333 |
return mode.contains[i]; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
function endOfMode(mode, lexeme) { |
| 339 |
if (testRe(mode.endRe, lexeme)) { |
| 340 |
while (mode.endsParent && mode.parent) { |
| 341 |
mode = mode.parent; |
| 342 |
} |
| 343 |
return mode; |
| 344 |
} |
| 345 |
if (mode.endsWithParent) { |
| 346 |
return endOfMode(mode.parent, lexeme); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
function isIllegal(lexeme, mode) { |
| 351 |
return !ignore_illegals && testRe(mode.illegalRe, lexeme); |
| 352 |
} |
| 353 |
|
| 354 |
function keywordMatch(mode, match) { |
| 355 |
var match_str = language.case_insensitive ? match[0].toLowerCase() : match[0]; |
| 356 |
return mode.keywords.hasOwnProperty(match_str) && mode.keywords[match_str]; |
| 357 |
} |
| 358 |
|
| 359 |
function buildSpan(classname, insideSpan, leaveOpen, noPrefix) { |
| 360 |
var classPrefix = noPrefix ? '' : options.classPrefix, |
| 361 |
openSpan = '<span class="' + classPrefix, |
| 362 |
closeSpan = leaveOpen ? '' : spanEndTag |
| 363 |
|
| 364 |
openSpan += classname + '">'; |
| 365 |
|
| 366 |
return openSpan + insideSpan + closeSpan; |
| 367 |
} |
| 368 |
|
| 369 |
function processKeywords() { |
| 370 |
var keyword_match, last_index, match, result; |
| 371 |
|
| 372 |
if (!top.keywords) |
| 373 |
return escape(mode_buffer); |
| 374 |
|
| 375 |
result = ''; |
| 376 |
last_index = 0; |
| 377 |
top.lexemesRe.lastIndex = 0; |
| 378 |
match = top.lexemesRe.exec(mode_buffer); |
| 379 |
|
| 380 |
while (match) { |
| 381 |
result += escape(mode_buffer.substring(last_index, match.index)); |
| 382 |
keyword_match = keywordMatch(top, match); |
| 383 |
if (keyword_match) { |
| 384 |
relevance += keyword_match[1]; |
| 385 |
result += buildSpan(keyword_match[0], escape(match[0])); |
| 386 |
} else { |
| 387 |
result += escape(match[0]); |
| 388 |
} |
| 389 |
last_index = top.lexemesRe.lastIndex; |
| 390 |
match = top.lexemesRe.exec(mode_buffer); |
| 391 |
} |
| 392 |
return result + escape(mode_buffer.substr(last_index)); |
| 393 |
} |
| 394 |
|
| 395 |
function processSubLanguage() { |
| 396 |
var explicit = typeof top.subLanguage === 'string'; |
| 397 |
if (explicit && !languages[top.subLanguage]) { |
| 398 |
return escape(mode_buffer); |
| 399 |
} |
| 400 |
|
| 401 |
var result = explicit ? |
| 402 |
highlight(top.subLanguage, mode_buffer, true, continuations[top.subLanguage]) : |
| 403 |
highlightAuto(mode_buffer, top.subLanguage.length ? top.subLanguage : undefined); |
| 404 |
|
| 405 |
// Counting embedded language score towards the host language may be disabled |
| 406 |
// with zeroing the containing mode relevance. Usecase in point is Markdown that |
| 407 |
// allows XML everywhere and makes every XML snippet to have a much larger Markdown |
| 408 |
// score. |
| 409 |
if (top.relevance > 0) { |
| 410 |
relevance += result.relevance; |
| 411 |
} |
| 412 |
if (explicit) { |
| 413 |
continuations[top.subLanguage] = result.top; |
| 414 |
} |
| 415 |
return buildSpan(result.language, result.value, false, true); |
| 416 |
} |
| 417 |
|
| 418 |
function processBuffer() { |
| 419 |
result += (top.subLanguage != null ? processSubLanguage() : processKeywords()); |
| 420 |
mode_buffer = ''; |
| 421 |
} |
| 422 |
|
| 423 |
function startNewMode(mode) { |
| 424 |
result += mode.className? buildSpan(mode.className, '', true): ''; |
| 425 |
top = Object.create(mode, {parent: {value: top}}); |
| 426 |
} |
| 427 |
|
| 428 |
function processLexeme(buffer, lexeme) { |
| 429 |
|
| 430 |
mode_buffer += buffer; |
| 431 |
|
| 432 |
if (lexeme == null) { |
| 433 |
processBuffer(); |
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
var new_mode = subMode(lexeme, top); |
| 438 |
if (new_mode) { |
| 439 |
if (new_mode.skip) { |
| 440 |
mode_buffer += lexeme; |
| 441 |
} else { |
| 442 |
if (new_mode.excludeBegin) { |
| 443 |
mode_buffer += lexeme; |
| 444 |
} |
| 445 |
processBuffer(); |
| 446 |
if (!new_mode.returnBegin && !new_mode.excludeBegin) { |
| 447 |
mode_buffer = lexeme; |
| 448 |
} |
| 449 |
} |
| 450 |
startNewMode(new_mode, lexeme); |
| 451 |
return new_mode.returnBegin ? 0 : lexeme.length; |
| 452 |
} |
| 453 |
|
| 454 |
var end_mode = endOfMode(top, lexeme); |
| 455 |
if (end_mode) { |
| 456 |
var origin = top; |
| 457 |
if (origin.skip) { |
| 458 |
mode_buffer += lexeme; |
| 459 |
} else { |
| 460 |
if (!(origin.returnEnd || origin.excludeEnd)) { |
| 461 |
mode_buffer += lexeme; |
| 462 |
} |
| 463 |
processBuffer(); |
| 464 |
if (origin.excludeEnd) { |
| 465 |
mode_buffer = lexeme; |
| 466 |
} |
| 467 |
} |
| 468 |
do { |
| 469 |
if (top.className) { |
| 470 |
result += spanEndTag; |
| 471 |
} |
| 472 |
if (!top.skip) { |
| 473 |
relevance += top.relevance; |
| 474 |
} |
| 475 |
top = top.parent; |
| 476 |
} while (top !== end_mode.parent); |
| 477 |
if (end_mode.starts) { |
| 478 |
startNewMode(end_mode.starts, ''); |
| 479 |
} |
| 480 |
return origin.returnEnd ? 0 : lexeme.length; |
| 481 |
} |
| 482 |
|
| 483 |
if (isIllegal(lexeme, top)) |
| 484 |
throw new Error('Illegal lexeme "' + lexeme + '" for mode "' + (top.className || '<unnamed>') + '"'); |
| 485 |
|
| 486 |
/* |
| 487 |
Parser should not reach this point as all types of lexemes should be caught |
| 488 |
earlier, but if it does due to some bug make sure it advances at least one |
| 489 |
character forward to prevent infinite looping. |
| 490 |
*/ |
| 491 |
mode_buffer += lexeme; |
| 492 |
return lexeme.length || 1; |
| 493 |
} |
| 494 |
|
| 495 |
var language = getLanguage(name); |
| 496 |
if (!language) { |
| 497 |
throw new Error('Unknown language: "' + name + '"'); |
| 498 |
} |
| 499 |
|
| 500 |
compileLanguage(language); |
| 501 |
var top = continuation || language; |
| 502 |
var continuations = {}; // keep continuations for sub-languages |
| 503 |
var result = '', current; |
| 504 |
for(current = top; current !== language; current = current.parent) { |
| 505 |
if (current.className) { |
| 506 |
result = buildSpan(current.className, '', true) + result; |
| 507 |
} |
| 508 |
} |
| 509 |
var mode_buffer = ''; |
| 510 |
var relevance = 0; |
| 511 |
try { |
| 512 |
var match, count, index = 0; |
| 513 |
while (true) { |
| 514 |
top.terminators.lastIndex = index; |
| 515 |
match = top.terminators.exec(value); |
| 516 |
if (!match) |
| 517 |
break; |
| 518 |
count = processLexeme(value.substring(index, match.index), match[0]); |
| 519 |
index = match.index + count; |
| 520 |
} |
| 521 |
processLexeme(value.substr(index)); |
| 522 |
for(current = top; current.parent; current = current.parent) { // close dangling modes |
| 523 |
if (current.className) { |
| 524 |
result += spanEndTag; |
| 525 |
} |
| 526 |
} |
| 527 |
return { |
| 528 |
relevance: relevance, |
| 529 |
value: result, |
| 530 |
language: name, |
| 531 |
top: top |
| 532 |
}; |
| 533 |
} catch (e) { |
| 534 |
if (e.message && e.message.indexOf('Illegal') !== -1) { |
| 535 |
return { |
| 536 |
relevance: 0, |
| 537 |
value: escape(value) |
| 538 |
}; |
| 539 |
} else { |
| 540 |
throw e; |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
/* |
| 546 |
Highlighting with language detection. Accepts a string with the code to |
| 547 |
highlight. Returns an object with the following properties: |
| 548 |
|
| 549 |
- language (detected language) |
| 550 |
- relevance (int) |
| 551 |
- value (an HTML string with highlighting markup) |
| 552 |
- second_best (object with the same structure for second-best heuristically |
| 553 |
detected language, may be absent) |
| 554 |
|
| 555 |
*/ |
| 556 |
function highlightAuto(text, languageSubset) { |
| 557 |
languageSubset = languageSubset || options.languages || objectKeys(languages); |
| 558 |
var result = { |
| 559 |
relevance: 0, |
| 560 |
value: escape(text) |
| 561 |
}; |
| 562 |
var second_best = result; |
| 563 |
languageSubset.filter(getLanguage).forEach(function(name) { |
| 564 |
var current = highlight(name, text, false); |
| 565 |
current.language = name; |
| 566 |
if (current.relevance > second_best.relevance) { |
| 567 |
second_best = current; |
| 568 |
} |
| 569 |
if (current.relevance > result.relevance) { |
| 570 |
second_best = result; |
| 571 |
result = current; |
| 572 |
} |
| 573 |
}); |
| 574 |
if (second_best.language) { |
| 575 |
result.second_best = second_best; |
| 576 |
} |
| 577 |
return result; |
| 578 |
} |
| 579 |
|
| 580 |
/* |
| 581 |
Post-processing of the highlighted markup: |
| 582 |
|
| 583 |
- replace TABs with something more useful |
| 584 |
- replace real line-breaks with '<br>' for non-pre containers |
| 585 |
|
| 586 |
*/ |
| 587 |
function fixMarkup(value) { |
| 588 |
return !(options.tabReplace || options.useBR) |
| 589 |
? value |
| 590 |
: value.replace(fixMarkupRe, function(match, p1) { |
| 591 |
if (options.useBR && match === '\n') { |
| 592 |
return '<br>'; |
| 593 |
} else if (options.tabReplace) { |
| 594 |
return p1.replace(/\t/g, options.tabReplace); |
| 595 |
} |
| 596 |
}); |
| 597 |
} |
| 598 |
|
| 599 |
function buildClassName(prevClassName, currentLang, resultLang) { |
| 600 |
var language = currentLang ? aliases[currentLang] : resultLang, |
| 601 |
result = [prevClassName.trim()]; |
| 602 |
|
| 603 |
if (!prevClassName.match(/\bhljs\b/)) { |
| 604 |
result.push('hljs'); |
| 605 |
} |
| 606 |
|
| 607 |
if (prevClassName.indexOf(language) === -1) { |
| 608 |
result.push(language); |
| 609 |
} |
| 610 |
|
| 611 |
return result.join(' ').trim(); |
| 612 |
} |
| 613 |
|
| 614 |
/* |
| 615 |
Applies highlighting to a DOM node containing code. Accepts a DOM node and |
| 616 |
two optional parameters for fixMarkup. |
| 617 |
*/ |
| 618 |
function highlightBlock(block) { |
| 619 |
var node, originalStream, result, resultNode, text; |
| 620 |
var language = blockLanguage(block); |
| 621 |
|
| 622 |
if (isNotHighlighted(language)) |
| 623 |
return; |
| 624 |
|
| 625 |
if (options.useBR) { |
| 626 |
node = document.createElementNS('http://www.w3.org/1999/xhtml', 'div'); |
| 627 |
node.innerHTML = block.innerHTML.replace(/\n/g, '').replace(/<br[ \/]*>/g, '\n'); |
| 628 |
} else { |
| 629 |
node = block; |
| 630 |
} |
| 631 |
text = node.textContent; |
| 632 |
result = language ? highlight(language, text, true) : highlightAuto(text); |
| 633 |
|
| 634 |
originalStream = nodeStream(node); |
| 635 |
if (originalStream.length) { |
| 636 |
resultNode = document.createElementNS('http://www.w3.org/1999/xhtml', 'div'); |
| 637 |
resultNode.innerHTML = result.value; |
| 638 |
result.value = mergeStreams(originalStream, nodeStream(resultNode), text); |
| 639 |
} |
| 640 |
result.value = fixMarkup(result.value); |
| 641 |
|
| 642 |
block.innerHTML = result.value; |
| 643 |
block.className = buildClassName(block.className, language, result.language); |
| 644 |
block.result = { |
| 645 |
language: result.language, |
| 646 |
re: result.relevance |
| 647 |
}; |
| 648 |
if (result.second_best) { |
| 649 |
block.second_best = { |
| 650 |
language: result.second_best.language, |
| 651 |
re: result.second_best.relevance |
| 652 |
}; |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
/* |
| 657 |
Updates highlight.js global options with values passed in the form of an object. |
| 658 |
*/ |
| 659 |
function configure(user_options) { |
| 660 |
options = inherit(options, user_options); |
| 661 |
} |
| 662 |
|
| 663 |
/* |
| 664 |
Applies highlighting to all <pre><code>..</code></pre> blocks on a page. |
| 665 |
*/ |
| 666 |
function initHighlighting() { |
| 667 |
if (initHighlighting.called) |
| 668 |
return; |
| 669 |
initHighlighting.called = true; |
| 670 |
|
| 671 |
var blocks = document.querySelectorAll('pre code'); |
| 672 |
ArrayProto.forEach.call(blocks, highlightBlock); |
| 673 |
} |
| 674 |
|
| 675 |
/* |
| 676 |
Attaches highlighting to the page load event. |
| 677 |
*/ |
| 678 |
function initHighlightingOnLoad() { |
| 679 |
addEventListener('DOMContentLoaded', initHighlighting, false); |
| 680 |
addEventListener('load', initHighlighting, false); |
| 681 |
} |
| 682 |
|
| 683 |
function registerLanguage(name, language) { |
| 684 |
var lang = languages[name] = language(hljs); |
| 685 |
if (lang.aliases) { |
| 686 |
lang.aliases.forEach(function(alias) {aliases[alias] = name;}); |
| 687 |
} |
| 688 |
} |
| 689 |
|
| 690 |
function listLanguages() { |
| 691 |
return objectKeys(languages); |
| 692 |
} |
| 693 |
|
| 694 |
function getLanguage(name) { |
| 695 |
name = (name || '').toLowerCase(); |
| 696 |
return languages[name] || languages[aliases[name]]; |
| 697 |
} |
| 698 |
|
| 699 |
/* Interface definition */ |
| 700 |
|
| 701 |
hljs.highlight = highlight; |
| 702 |
hljs.highlightAuto = highlightAuto; |
| 703 |
hljs.fixMarkup = fixMarkup; |
| 704 |
hljs.highlightBlock = highlightBlock; |
| 705 |
hljs.configure = configure; |
| 706 |
hljs.initHighlighting = initHighlighting; |
| 707 |
hljs.initHighlightingOnLoad = initHighlightingOnLoad; |
| 708 |
hljs.registerLanguage = registerLanguage; |
| 709 |
hljs.listLanguages = listLanguages; |
| 710 |
hljs.getLanguage = getLanguage; |
| 711 |
hljs.inherit = inherit; |
| 712 |
|
| 713 |
// Common regexps |
| 714 |
hljs.IDENT_RE = '[a-zA-Z]\\w*'; |
| 715 |
hljs.UNDERSCORE_IDENT_RE = '[a-zA-Z_]\\w*'; |
| 716 |
hljs.NUMBER_RE = '\\b\\d+(\\.\\d+)?'; |
| 717 |
hljs.C_NUMBER_RE = '(-?)(\\b0[xX][a-fA-F0-9]+|(\\b\\d+(\\.\\d*)?|\\.\\d+)([eE][-+]?\\d+)?)'; // 0x..., 0..., decimal, float |
| 718 |
hljs.BINARY_NUMBER_RE = '\\b(0b[01]+)'; // 0b... |
| 719 |
hljs.RE_STARTERS_RE = '!|!=|!==|%|%=|&|&&|&=|\\*|\\*=|\\+|\\+=|,|-|-=|/=|/|:|;|<<|<<=|<=|<|===|==|=|>>>=|>>=|>=|>>>|>>|>|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~'; |
| 720 |
|
| 721 |
// Common modes |
| 722 |
hljs.BACKSLASH_ESCAPE = { |
| 723 |
begin: '\\\\[\\s\\S]', relevance: 0 |
| 724 |
}; |
| 725 |
hljs.APOS_STRING_MODE = { |
| 726 |
className: 'string', |
| 727 |
begin: '\'', end: '\'', |
| 728 |
illegal: '\\n', |
| 729 |
contains: [hljs.BACKSLASH_ESCAPE] |
| 730 |
}; |
| 731 |
hljs.QUOTE_STRING_MODE = { |
| 732 |
className: 'string', |
| 733 |
begin: '"', end: '"', |
| 734 |
illegal: '\\n', |
| 735 |
contains: [hljs.BACKSLASH_ESCAPE] |
| 736 |
}; |
| 737 |
hljs.PHRASAL_WORDS_MODE = { |
| 738 |
begin: /\b(a|an|the|are|I'm|isn't|don't|doesn't|won't|but|just|should|pretty|simply|enough|gonna|going|wtf|so|such|will|you|your|like)\b/ |
| 739 |
}; |
| 740 |
hljs.COMMENT = function (begin, end, inherits) { |
| 741 |
var mode = hljs.inherit( |
| 742 |
{ |
| 743 |
className: 'comment', |
| 744 |
begin: begin, end: end, |
| 745 |
contains: [] |
| 746 |
}, |
| 747 |
inherits || {} |
| 748 |
); |
| 749 |
mode.contains.push(hljs.PHRASAL_WORDS_MODE); |
| 750 |
mode.contains.push({ |
| 751 |
className: 'doctag', |
| 752 |
begin: '(?:TODO|FIXME|NOTE|BUG|XXX):', |
| 753 |
relevance: 0 |
| 754 |
}); |
| 755 |
return mode; |
| 756 |
}; |
| 757 |
hljs.C_LINE_COMMENT_MODE = hljs.COMMENT('//', '$'); |
| 758 |
hljs.C_BLOCK_COMMENT_MODE = hljs.COMMENT('/\\*', '\\*/'); |
| 759 |
hljs.HASH_COMMENT_MODE = hljs.COMMENT('#', '$'); |
| 760 |
hljs.NUMBER_MODE = { |
| 761 |
className: 'number', |
| 762 |
begin: hljs.NUMBER_RE, |
| 763 |
relevance: 0 |
| 764 |
}; |
| 765 |
hljs.C_NUMBER_MODE = { |
| 766 |
className: 'number', |
| 767 |
begin: hljs.C_NUMBER_RE, |
| 768 |
relevance: 0 |
| 769 |
}; |
| 770 |
hljs.BINARY_NUMBER_MODE = { |
| 771 |
className: 'number', |
| 772 |
begin: hljs.BINARY_NUMBER_RE, |
| 773 |
relevance: 0 |
| 774 |
}; |
| 775 |
hljs.CSS_NUMBER_MODE = { |
| 776 |
className: 'number', |
| 777 |
begin: hljs.NUMBER_RE + '(' + |
| 778 |
'%|em|ex|ch|rem' + |
| 779 |
'|vw|vh|vmin|vmax' + |
| 780 |
'|cm|mm|in|pt|pc|px' + |
| 781 |
'|deg|grad|rad|turn' + |
| 782 |
'|s|ms' + |
| 783 |
'|Hz|kHz' + |
| 784 |
'|dpi|dpcm|dppx' + |
| 785 |
')?', |
| 786 |
relevance: 0 |
| 787 |
}; |
| 788 |
hljs.REGEXP_MODE = { |
| 789 |
className: 'regexp', |
| 790 |
begin: /\//, end: /\/[gimuy]*/, |
| 791 |
illegal: /\n/, |
| 792 |
contains: [ |
| 793 |
hljs.BACKSLASH_ESCAPE, |
| 794 |
{ |
| 795 |
begin: /\[/, end: /\]/, |
| 796 |
relevance: 0, |
| 797 |
contains: [hljs.BACKSLASH_ESCAPE] |
| 798 |
} |
| 799 |
] |
| 800 |
}; |
| 801 |
hljs.TITLE_MODE = { |
| 802 |
className: 'title', |
| 803 |
begin: hljs.IDENT_RE, |
| 804 |
relevance: 0 |
| 805 |
}; |
| 806 |
hljs.UNDERSCORE_TITLE_MODE = { |
| 807 |
className: 'title', |
| 808 |
begin: hljs.UNDERSCORE_IDENT_RE, |
| 809 |
relevance: 0 |
| 810 |
}; |
| 811 |
hljs.METHOD_GUARD = { |
| 812 |
// excludes method names from keyword processing |
| 813 |
begin: '\\.\\s*' + hljs.UNDERSCORE_IDENT_RE, |
| 814 |
relevance: 0 |
| 815 |
}; |
| 816 |
|
| 817 |
return hljs; |
| 818 |
})); |