PrismJS Command-Line

by derek_shnosh

HTML

<h4>Host/User prompt.</h4>
<code>&lt;pre class="command-line language-bash" data-user="root" data-host="localhost"></code>
<pre class="command-line language-bash" data-user="root" data-host="localhost">
<code>conf
feature nxapi</code>
</pre>
<hr>

<h4>Single prompt defined.</h4>
<code>&lt;pre class="command-line language-bash" data-prompt="NXOSv#"></code>
<pre class="command-line language-bash" data-prompt="NXOSv#">
<code>conf
feature nxapi</code>
</pre>
<hr>

<h4>Omitting prompt</h4>
<ul>
  <li>Line 1 should have prompt, line 2 should not.</li>
</ul>
<code>&lt;pre class="command-line language-bash" data-prompt="NXOSv#" data-filter-output="(o)"></code>
<pre class="command-line language-bash" data-prompt="NXOSv#" data-filter-output="(o)">
<code>conf
(o)feature nxapi</code>
</pre>
<hr>

<h4>Different prompts defined.</h4>
<ul>
  <li>Lines 1,4 should be: <b><code>NXOSv#</code></b></li>
  <li>Lines 2-3 should be: <b><code>NXOSv(config)#</code></b></li>
</ul>
<code>&lt;pre class="command-line language-bash" data-prompt="1,4|NXOSv#;2-3|NXOSv(config)#"></code>
<pre class="command-line language-bash" data-prompt="1,4|NXOSv#;2-3|NXOSv(config)#">
<code>conf
feature nxapi
end
copy ru st</code>
</pre>

CSS

.command-line-prompt {
  border-right: 1px solid #999;
  display: block;
  float: left;
  font-size: 100%;
  letter-spacing: -1px;
  margin-right: 1em;
  pointer-events: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

.command-line-prompt>span::before {
  color: #999;
  content: ' ';
  display: block;
  padding-right: 0.8em;
}

.command-line-prompt>span[data-user]:before {
  content: "[" attr(data-user) "@" attr(data-host) "] $";
}

.command-line-prompt>span[data-user="root"]:before {
  content: "[" attr(data-user) "@" attr(data-host) "] #";
}

.command-line-prompt>span[data-prompt]:before {
  content: attr(data-prompt);
}

code {
  background: black;
  color: lightblue;
  padding: 3px 5px;
  border-radius: 3px;
}

pre code {
  color: orange;
  background: #303030;
  padding: 0;
  border: none;
}

pre {
  background: #303030;
  font-weight: 900;
  color: orange;
  border: 1px white;
  border-radius: 5px;
  padding: 5px;
}

JavaScript

/* PrismJS 1.15.0
https://prismjs.com/download.html#themes=prism-okaidia&languages=bash&plugins=command-line */
var _self = (typeof window !== 'undefined') ?
  window // if in browser
  :
  (
    (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) ?
    self // if in worker
    :
    {} // if in node js
  );

/**
 * Prism: Lightweight, robust, elegant syntax highlighting
 * MIT license http://www.opensource.org/licenses/mit-license.php/
 * @author Lea Verou http://lea.verou.me
 */

var Prism = (function(_self) {

  // Private helper vars
  var lang = /\blang(?:uage)?-([\w-]+)\b/i;
  var uniqueId = 0;

  var _ = {
    manual: _self.Prism && _self.Prism.manual,
    disableWorkerMessageHandler: _self.Prism && _self.Prism.disableWorkerMessageHandler,
    util: {
      encode: function(tokens) {
        if (tokens instanceof Token) {
          return new Token(tokens.type, _.util.encode(tokens.content), tokens.alias);
        } else if (Array.isArray(tokens)) {
          return tokens.map(_.util.encode);
        } else {
          return tokens.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/\u00a0/g, ' ');
        }
      },

      type: function(o) {
        return Object.prototype.toString.call(o).slice(8, -1);
      },

      objId: function(obj) {
        if (!obj['__id']) {
          Object.defineProperty(obj, '__id', {
            value: ++uniqueId
          });
        }
        return obj['__id'];
      },

      // Deep clone a language definition (e.g. to extend it)
      clone: function deepClone(o, visited) {
        var clone, id, type = _.util.type(o);
        visited = visited || {};

        switch (type) {
          case 'Object':
            id = _.util.objId(o);
            if (visited[id]) {
              return visited[id];
            }
            clone = {};
            visited[id] = clone;

            for (var key in o) {
              if (o.hasOwnProperty(key)) {
                clone[key] =...