Skip to content

Commit

Permalink
Add PrismJS monkey patch for terminal formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
jcubic committed May 21, 2018
1 parent 70adf2e commit 7bba6e6
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
4 changes: 2 additions & 2 deletions js/less.js
Expand Up @@ -8,9 +8,9 @@
* http://terminal.jcubic.pl
*
* This is example of how to create less like command for jQuery Terminal
* the code is based on the one from leash shell
* the code is based on the one from leash shell and written as jQuery plugin
*
* Copyright (c) 2014-2018 Jakub Jankiewicz <http://jcubic.pl/me>
* Copyright (c) 2018 Jakub Jankiewicz <http://jcubic.pl/me>
* Released under the MIT license
*
*/
Expand Down
75 changes: 75 additions & 0 deletions js/prism.js
@@ -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, '&quot;') + '"';
}).join(' ');
return env.content.split(/\n/).map(function(content) {
return '[[b;;;' + env.classes.join(' ') + ']' + $.terminal.escape_brackets(content) + ']';
}).join('\n');

};
})(Prism.Token, jQuery);

0 comments on commit 7bba6e6

Please sign in to comment.