forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ba6226
commit 2d622f1
Showing
3 changed files
with
82 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,80 @@ | ||
(function(){ | ||
|
||
if ( | ||
typeof self !== 'undefined' && !self.Prism || | ||
typeof global !== 'undefined' && !global.Prism | ||
) { | ||
return; | ||
} | ||
|
||
Prism.hooks.add('before-highlight', function(env) { | ||
var tokens = env.grammar; | ||
|
||
if (!tokens) return; | ||
|
||
tokens.tab = /\t/g; | ||
tokens.crlf = /\r\n/g; | ||
tokens.lf = /\n/g; | ||
tokens.cr = /\r/g; | ||
tokens.space = / /g; | ||
}); | ||
(function () { | ||
|
||
if ( | ||
typeof self !== 'undefined' && !self.Prism || | ||
typeof global !== 'undefined' && !global.Prism | ||
) { | ||
return; | ||
} | ||
|
||
|
||
var invisibles = { | ||
'tab': /\t/, | ||
'crlf': /\r\n/, | ||
'lf': /\n/, | ||
'cr': /\r/, | ||
'space': / / | ||
}; | ||
|
||
|
||
/** | ||
* Handles the recursive calling of `addInvisibles` for one token. | ||
* | ||
* @param {Object|Array} tokens The grammar or array which contains the token. | ||
* @param {string|number} name The name or index of the token in `tokens`. | ||
*/ | ||
function handleToken(tokens, name) { | ||
var value = tokens[name]; | ||
|
||
var type = Prism.util.type(value); | ||
switch (type) { | ||
case 'RegExp': | ||
var inside = {}; | ||
tokens[name] = { | ||
pattern: value, | ||
inside: inside | ||
}; | ||
addInvisibles(inside); | ||
break; | ||
|
||
case 'Array': | ||
for (var i = 0, l = value.length; i < l; i++) | ||
handleToken(value, i); | ||
break; | ||
|
||
default: // 'Object' | ||
var inside = value.inside || (value.inside = {}); | ||
addInvisibles(inside); | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* Recursively adds patterns to match invisible characters to the given grammar (if not added already). | ||
* | ||
* @param {Object} grammar | ||
*/ | ||
function addInvisibles(grammar) { | ||
if (!grammar || grammar['tab']) | ||
return; | ||
|
||
// assign invisibles here to "mark" the grammar in case of self references | ||
for (var name in invisibles) { | ||
if (invisibles.hasOwnProperty(name)) | ||
grammar[name] = invisibles[name]; | ||
} | ||
|
||
for (var name in grammar) { | ||
if (grammar.hasOwnProperty(name) && !invisibles[name]) { | ||
if (name === 'rest') | ||
addInvisibles(grammar['rest']); | ||
else | ||
handleToken(grammar, name); | ||
} | ||
} | ||
} | ||
|
||
Prism.hooks.add('before-highlight', function (env) { | ||
addInvisibles(env.grammar); | ||
}); | ||
})(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.