Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PrismJS monkey patch for terminal formatting
- Loading branch information
Showing
2 changed files
with
77 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/**@license | ||
* __ _____ ________ __ | ||
* / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / / | ||
* __ / // // // // // _ // _// // / / // _ // _// // // \/ // _ \/ / | ||
* / / // // // // // ___// / / // / / // ___// / / / / // // /\ // // / /__ | ||
* \___//____ \\___//____//_/ _\_ / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/ | ||
* \/ /____/ | ||
* http://terminal.jcubic.pl | ||
* | ||
* this is utility that monkey patch Prism functions to output | ||
* terminal formatting (it was first created here https://codepen.io/jcubic/pen/zEyxjJ) | ||
* | ||
* usage: | ||
* | ||
* you need to include both css and js (it need to be before this file) | ||
* | ||
* js code: | ||
* | ||
* var grammar = Prism.languages[language]; | ||
* var tokens = Prism.tokenize(file, grammar); | ||
* var code = Prism.Token.stringify(tokens, language); | ||
* | ||
* term.echo(code); // or term.less(code) if you include less.js | ||
* | ||
* Copyright (c) 2018 Jakub Jankiewicz <http://jcubic.pl/me> | ||
* Released under the MIT license | ||
* | ||
*/ | ||
/* global jQuery, Prism */ | ||
(function(Token, $) { | ||
var _ = Prism; | ||
_.Token = function(type, content, alias, matchedStr, greedy) { | ||
Token.apply(this, [].slice.call(arguments)); | ||
}; | ||
_.Token.stringify = function(o, language, parent) { | ||
if (typeof o == 'string') { | ||
return o; | ||
} | ||
|
||
if (_.util.type(o) === 'Array') { | ||
return o.map(function(element) { | ||
return _.Token.stringify(element, language, o); | ||
}).join(''); | ||
} | ||
|
||
var env = { | ||
type: o.type, | ||
content: Token.stringify(o.content, language, parent), | ||
tag: 'span', | ||
classes: ['token', o.type], | ||
attributes: {}, | ||
language: language, | ||
parent: parent | ||
}; | ||
|
||
if (env.type == 'comment') { | ||
env.attributes['spellcheck'] = 'true'; | ||
} | ||
|
||
if (o.alias) { | ||
var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias]; | ||
Array.prototype.push.apply(env.classes, aliases); | ||
} | ||
|
||
_.hooks.run('wrap', env); | ||
|
||
var attributes = Object.keys(env.attributes).map(function(name) { | ||
return name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"'; | ||
}).join(' '); | ||
return env.content.split(/\n/).map(function(content) { | ||
return '[[b;;;' + env.classes.join(' ') + ']' + $.terminal.escape_brackets(content) + ']'; | ||
}).join('\n'); | ||
|
||
}; | ||
})(Prism.Token, jQuery); |