2015-02-12 04:26:43 +00:00
|
|
|
|
/**
|
|
|
|
|
* Prism: Lightweight, robust, elegant syntax highlighting
|
|
|
|
|
*
|
2016-11-21 01:49:21 +00:00
|
|
|
|
* @copyright Lea Verou <http://lea.verou.me>. MIT License.
|
2015-02-12 04:26:43 +00:00
|
|
|
|
* @providesModule Prism
|
2016-11-21 01:49:21 +00:00
|
|
|
|
* @noflow
|
2015-02-12 04:26:43 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2016-11-21 01:49:21 +00:00
|
|
|
|
/* eslint-disable */
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2015-02-12 04:26:43 +00:00
|
|
|
|
var React = require('React');
|
|
|
|
|
|
|
|
|
|
var _ = {
|
|
|
|
|
util: {
|
|
|
|
|
type: function (o) {
|
|
|
|
|
return Object.prototype.toString.call(o).match(/\[object (\w+)\]/)[1];
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Deep clone a language definition (e.g. to extend it)
|
|
|
|
|
clone: function (o) {
|
|
|
|
|
var type = _.util.type(o);
|
|
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
|
case 'Object':
|
|
|
|
|
var clone = {};
|
|
|
|
|
|
|
|
|
|
for (var key in o) {
|
|
|
|
|
if (o.hasOwnProperty(key)) {
|
|
|
|
|
clone[key] = _.util.clone(o[key]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
|
|
|
|
|
|
case 'Array':
|
|
|
|
|
return o.slice();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
languages: {
|
|
|
|
|
extend: function (id, redef) {
|
|
|
|
|
var lang = _.util.clone(_.languages[id]);
|
|
|
|
|
|
|
|
|
|
for (var key in redef) {
|
|
|
|
|
lang[key] = redef[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return lang;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Insert a token before another token in a language literal
|
|
|
|
|
insertBefore: function (inside, before, insert, root) {
|
|
|
|
|
root = root || _.languages;
|
|
|
|
|
var grammar = root[inside];
|
|
|
|
|
var ret = {};
|
|
|
|
|
|
|
|
|
|
for (var token in grammar) {
|
|
|
|
|
|
|
|
|
|
if (grammar.hasOwnProperty(token)) {
|
|
|
|
|
|
|
|
|
|
if (token == before) {
|
|
|
|
|
|
|
|
|
|
for (var newToken in insert) {
|
|
|
|
|
|
|
|
|
|
if (insert.hasOwnProperty(newToken)) {
|
|
|
|
|
ret[newToken] = insert[newToken];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret[token] = grammar[token];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return root[inside] = ret;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// Traverse a language definition with Depth First Search
|
|
|
|
|
DFS: function(o, callback) {
|
|
|
|
|
for (var i in o) {
|
|
|
|
|
callback.call(o, i, o[i]);
|
|
|
|
|
|
|
|
|
|
if (_.util.type(o) === 'Object') {
|
|
|
|
|
_.languages.DFS(o[i], callback);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
tokenize: function(text, grammar) {
|
|
|
|
|
var Token = _.Token;
|
|
|
|
|
|
|
|
|
|
var strarr = [text];
|
|
|
|
|
|
|
|
|
|
var rest = grammar.rest;
|
|
|
|
|
|
|
|
|
|
if (rest) {
|
|
|
|
|
for (var token in rest) {
|
|
|
|
|
grammar[token] = rest[token];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delete grammar.rest;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokenloop: for (var token in grammar) {
|
|
|
|
|
if(!grammar.hasOwnProperty(token) || !grammar[token]) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var pattern = grammar[token],
|
|
|
|
|
inside = pattern.inside,
|
|
|
|
|
lookbehind = !!pattern.lookbehind,
|
|
|
|
|
lookbehindLength = 0;
|
|
|
|
|
|
|
|
|
|
pattern = pattern.pattern || pattern;
|
|
|
|
|
|
|
|
|
|
for (var i=0; i<strarr.length; i++) { // Don’t cache length as it changes during the loop
|
|
|
|
|
|
|
|
|
|
var str = strarr[i];
|
|
|
|
|
|
|
|
|
|
if (strarr.length > text.length) {
|
|
|
|
|
// Something went terribly wrong, ABORT, ABORT!
|
|
|
|
|
break tokenloop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (str instanceof Token) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pattern.lastIndex = 0;
|
|
|
|
|
|
|
|
|
|
var match = pattern.exec(str);
|
|
|
|
|
|
|
|
|
|
if (match) {
|
|
|
|
|
if(lookbehind) {
|
|
|
|
|
lookbehindLength = match[1].length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var from = match.index - 1 + lookbehindLength,
|
|
|
|
|
match = match[0].slice(lookbehindLength),
|
|
|
|
|
len = match.length,
|
|
|
|
|
to = from + len,
|
|
|
|
|
before = str.slice(0, from + 1),
|
|
|
|
|
after = str.slice(to + 1);
|
|
|
|
|
|
|
|
|
|
var args = [i, 1];
|
|
|
|
|
|
|
|
|
|
if (before) {
|
|
|
|
|
args.push(before);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var wrapped = new Token(token, inside? _.tokenize(match, inside) : match);
|
|
|
|
|
|
|
|
|
|
args.push(wrapped);
|
|
|
|
|
|
|
|
|
|
if (after) {
|
|
|
|
|
args.push(after);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Array.prototype.splice.apply(strarr, args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return strarr;
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
hooks: {
|
|
|
|
|
all: {},
|
|
|
|
|
|
|
|
|
|
add: function (name, callback) {
|
|
|
|
|
var hooks = _.hooks.all;
|
|
|
|
|
|
|
|
|
|
hooks[name] = hooks[name] || [];
|
|
|
|
|
|
|
|
|
|
hooks[name].push(callback);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
run: function (name, env) {
|
|
|
|
|
var callbacks = _.hooks.all[name];
|
|
|
|
|
|
|
|
|
|
if (!callbacks || !callbacks.length) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (var i=0, callback; callback = callbacks[i++];) {
|
|
|
|
|
callback(env);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var Token = _.Token = function(type, content) {
|
|
|
|
|
this.type = type;
|
|
|
|
|
this.content = content;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Token.reactify = function(o, key) {
|
|
|
|
|
if (typeof o == 'string') {
|
|
|
|
|
return o;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Array.isArray(o)) {
|
|
|
|
|
return o.map(function(element, i) {
|
|
|
|
|
return Token.reactify(element, i);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var attributes = {
|
|
|
|
|
className: 'token ' + o.type,
|
|
|
|
|
key: key
|
|
|
|
|
};
|
|
|
|
|
if (o.type == 'comment') {
|
|
|
|
|
attributes.spellCheck = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return React.DOM.span(attributes, Token.reactify(o.content));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_.languages.markup = {
|
|
|
|
|
'comment': /<!--[\w\W]*?-->/g,
|
|
|
|
|
'prolog': /<\?.+?\?>/,
|
|
|
|
|
'doctype': /<!DOCTYPE.+?>/,
|
|
|
|
|
'cdata': /<!\[CDATA\[[\w\W]*?]]>/i,
|
|
|
|
|
'tag': {
|
|
|
|
|
pattern: /<\/?[\w:-]+\s*(?:\s+[\w:-]+(?:=(?:("|')(\\?[\w\W])*?\1|[^\s'">=]+))?\s*)*\/?>/gi,
|
|
|
|
|
inside: {
|
|
|
|
|
'tag': {
|
|
|
|
|
pattern: /^<\/?[\w:-]+/i,
|
|
|
|
|
inside: {
|
|
|
|
|
'punctuation': /^<\/?/,
|
|
|
|
|
'namespace': /^[\w-]+?:/
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'attr-value': {
|
|
|
|
|
pattern: /=(?:('|")[\w\W]*?(\1)|[^\s>]+)/gi,
|
|
|
|
|
inside: {
|
|
|
|
|
'punctuation': /=|>|"/g
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'punctuation': /\/?>/g,
|
|
|
|
|
'attr-name': {
|
|
|
|
|
pattern: /[\w:-]+/g,
|
|
|
|
|
inside: {
|
|
|
|
|
'namespace': /^[\w-]+?:/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'entity': /&#?[\da-z]{1,8};/gi
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_.languages.css = {
|
|
|
|
|
'comment': /\/\*[\w\W]*?\*\//g,
|
|
|
|
|
'atrule': {
|
|
|
|
|
pattern: /@[\w-]+?.*?(;|(?=\s*{))/gi,
|
|
|
|
|
inside: {
|
|
|
|
|
'punctuation': /[;:]/g
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'url': /url\((["']?).*?\1\)/gi,
|
|
|
|
|
'selector': /[^\{\}\s][^\{\};]*(?=\s*\{)/g,
|
|
|
|
|
'property': /(\b|\B)[\w-]+(?=\s*:)/ig,
|
|
|
|
|
'string': /("|')(\\?.)*?\1/g,
|
|
|
|
|
'important': /\B!important\b/gi,
|
|
|
|
|
'ignore': /&(lt|gt|amp);/gi,
|
|
|
|
|
'punctuation': /[\{\};:]/g
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_.languages.insertBefore('markup', 'tag', {
|
|
|
|
|
'style': {
|
|
|
|
|
pattern: /(<|<)style[\w\W]*?(>|>)[\w\W]*?(<|<)\/style(>|>)/ig,
|
|
|
|
|
inside: {
|
|
|
|
|
'tag': {
|
|
|
|
|
pattern: /(<|<)style[\w\W]*?(>|>)|(<|<)\/style(>|>)/ig,
|
|
|
|
|
inside: _.languages.markup.tag.inside
|
|
|
|
|
},
|
|
|
|
|
rest: _.languages.css
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_.languages.clike = {
|
|
|
|
|
'comment': {
|
|
|
|
|
pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|(^|[^:])\/\/.*?(\r?\n|$))/g,
|
|
|
|
|
lookbehind: true
|
|
|
|
|
},
|
|
|
|
|
'string': /("|')(\\?.)*?\1/g,
|
|
|
|
|
'class-name': {
|
|
|
|
|
pattern: /((?:(?:class|interface|extends|implements|trait|instanceof|new)\s+)|(?:catch\s+\())[a-z0-9_\.\\]+/ig,
|
|
|
|
|
lookbehind: true,
|
|
|
|
|
inside: {
|
|
|
|
|
punctuation: /(\.|\\)/
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'keyword': /\b(if|else|while|do|for|return|in|instanceof|function|new|try|throw|catch|finally|null|break|continue)\b/g,
|
|
|
|
|
'boolean': /\b(true|false)\b/g,
|
|
|
|
|
'function': {
|
|
|
|
|
pattern: /[a-z0-9_]+\(/ig,
|
|
|
|
|
inside: {
|
|
|
|
|
punctuation: /\(/
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?)\b/g,
|
|
|
|
|
'operator': /[-+]{1,2}|!|<=?|>=?|={1,3}|(&){1,2}|\|?\||\?|\*|\/|\~|\^|\%/g,
|
|
|
|
|
'ignore': /&(lt|gt|amp);/gi,
|
|
|
|
|
'punctuation': /[{}[\];(),.:]/g
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_.languages.javascript = _.languages.extend('clike', {
|
|
|
|
|
'keyword': /\b(var|let|if|else|while|do|for|return|in|instanceof|function|get|set|new|with|typeof|try|throw|catch|finally|null|break|continue|this)\b/g,
|
|
|
|
|
'number': /\b-?(0x[\dA-Fa-f]+|\d*\.?\d+([Ee]-?\d+)?|NaN|-?Infinity)\b/g
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_.languages.insertBefore('javascript', 'keyword', {
|
|
|
|
|
'regex': {
|
|
|
|
|
pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/g,
|
|
|
|
|
lookbehind: true
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_.languages.insertBefore('markup', 'tag', {
|
|
|
|
|
'script': {
|
|
|
|
|
pattern: /(<|<)script[\w\W]*?(>|>)[\w\W]*?(<|<)\/script(>|>)/ig,
|
|
|
|
|
inside: {
|
|
|
|
|
'tag': {
|
|
|
|
|
pattern: /(<|<)script[\w\W]*?(>|>)|(<|<)\/script(>|>)/ig,
|
|
|
|
|
inside: _.languages.markup.tag.inside
|
|
|
|
|
},
|
|
|
|
|
rest: _.languages.javascript
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var Prism = React.createClass({
|
|
|
|
|
statics: {
|
|
|
|
|
_: _
|
|
|
|
|
},
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
|
return {
|
|
|
|
|
language: 'javascript'
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
render: function() {
|
|
|
|
|
var grammar = _.languages[this.props.language];
|
|
|
|
|
return (
|
|
|
|
|
<div className={'prism language-' + this.props.language}>
|
|
|
|
|
{Token.reactify(_.tokenize(this.props.children, grammar))}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = Prism;
|