diff --git a/js/less.js b/js/less.js index c5ff169b6..444466ec4 100644 --- a/js/less.js +++ b/js/less.js @@ -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 + * Copyright (c) 2018 Jakub Jankiewicz * Released under the MIT license * */ diff --git a/js/prism.js b/js/prism.js new file mode 100644 index 000000000..f10272d21 --- /dev/null +++ b/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 + * 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);