prism-command-line.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. (function () {
  2. if (typeof Prism === 'undefined' || typeof document === 'undefined') {
  3. return;
  4. }
  5. var CLASS_PATTERN = /(?:^|\s)command-line(?:\s|$)/;
  6. var PROMPT_CLASS = 'command-line-prompt';
  7. /** @type {(str: string, prefix: string) => boolean} */
  8. var startsWith = ''.startsWith
  9. ? function (s, p) { return s.startsWith(p); }
  10. : function (s, p) { return s.indexOf(p) === 0; };
  11. // Support for IE11 that has no endsWith()
  12. /** @type {(str: string, suffix: string) => boolean} */
  13. var endsWith = ''.endsWith
  14. ? function (str, suffix) {
  15. return str.endsWith(suffix);
  16. }
  17. : function (str, suffix) {
  18. var len = str.length;
  19. return str.substring(len - suffix.length, len) === suffix;
  20. };
  21. /**
  22. * Returns whether the given hook environment has a command line info object.
  23. *
  24. * @param {any} env
  25. * @returns {boolean}
  26. */
  27. function hasCommandLineInfo(env) {
  28. var vars = env.vars = env.vars || {};
  29. return 'command-line' in vars;
  30. }
  31. /**
  32. * Returns the command line info object from the given hook environment.
  33. *
  34. * @param {any} env
  35. * @returns {CommandLineInfo}
  36. *
  37. * @typedef CommandLineInfo
  38. * @property {boolean} [complete]
  39. * @property {number} [numberOfLines]
  40. * @property {string[]} [outputLines]
  41. */
  42. function getCommandLineInfo(env) {
  43. var vars = env.vars = env.vars || {};
  44. return vars['command-line'] = vars['command-line'] || {};
  45. }
  46. Prism.hooks.add('before-highlight', function (env) {
  47. var commandLine = getCommandLineInfo(env);
  48. if (commandLine.complete || !env.code) {
  49. commandLine.complete = true;
  50. return;
  51. }
  52. // Works only for <code> wrapped inside <pre> (not inline).
  53. var pre = env.element.parentElement;
  54. if (!pre || !/pre/i.test(pre.nodeName) || // Abort only if neither the <pre> nor the <code> have the class
  55. (!CLASS_PATTERN.test(pre.className) && !CLASS_PATTERN.test(env.element.className))) {
  56. commandLine.complete = true;
  57. return;
  58. }
  59. // The element might be highlighted multiple times, so we just remove the previous prompt
  60. var existingPrompt = env.element.querySelector('.' + PROMPT_CLASS);
  61. if (existingPrompt) {
  62. existingPrompt.remove();
  63. }
  64. var codeLines = env.code.split('\n');
  65. commandLine.numberOfLines = codeLines.length;
  66. /** @type {string[]} */
  67. var outputLines = commandLine.outputLines = [];
  68. var outputSections = pre.getAttribute('data-output');
  69. var outputFilter = pre.getAttribute('data-filter-output');
  70. if (outputSections !== null) { // The user specified the output lines. -- cwells
  71. outputSections.split(',').forEach(function (section) {
  72. var range = section.split('-');
  73. var outputStart = parseInt(range[0], 10);
  74. var outputEnd = range.length === 2 ? parseInt(range[1], 10) : outputStart;
  75. if (!isNaN(outputStart) && !isNaN(outputEnd)) {
  76. if (outputStart < 1) {
  77. outputStart = 1;
  78. }
  79. if (outputEnd > codeLines.length) {
  80. outputEnd = codeLines.length;
  81. }
  82. // Convert start and end to 0-based to simplify the arrays. -- cwells
  83. outputStart--;
  84. outputEnd--;
  85. // Save the output line in an array and clear it in the code so it's not highlighted. -- cwells
  86. for (var j = outputStart; j <= outputEnd; j++) {
  87. outputLines[j] = codeLines[j];
  88. codeLines[j] = '';
  89. }
  90. }
  91. });
  92. } else if (outputFilter) { // Treat lines beginning with this string as output. -- cwells
  93. for (var i = 0; i < codeLines.length; i++) {
  94. if (startsWith(codeLines[i], outputFilter)) { // This line is output. -- cwells
  95. outputLines[i] = codeLines[i].slice(outputFilter.length);
  96. codeLines[i] = '';
  97. }
  98. }
  99. }
  100. var continuationLineIndicies = commandLine.continuationLineIndicies = new Set();
  101. var lineContinuationStr = pre.getAttribute('data-continuation-str');
  102. var continuationFilter = pre.getAttribute('data-filter-continuation');
  103. // Identify code lines where the command has continued onto subsequent
  104. // lines and thus need a different prompt. Need to do this after the output
  105. // lines have been removed to ensure we don't pick up a continuation string
  106. // in an output line.
  107. for (var j = 0; j < codeLines.length; j++) {
  108. var line = codeLines[j];
  109. if (!line) {
  110. continue;
  111. }
  112. // Record the next line as a continuation if this one ends in a continuation str.
  113. if (lineContinuationStr && endsWith(line, lineContinuationStr)) {
  114. continuationLineIndicies.add(j + 1);
  115. }
  116. // Record this line as a continuation if marked with a continuation prefix
  117. // (that we will remove).
  118. if (j > 0 && continuationFilter && startsWith(line, continuationFilter)) {
  119. codeLines[j] = line.slice(continuationFilter.length);
  120. continuationLineIndicies.add(j);
  121. }
  122. }
  123. env.code = codeLines.join('\n');
  124. });
  125. Prism.hooks.add('before-insert', function (env) {
  126. var commandLine = getCommandLineInfo(env);
  127. if (commandLine.complete) {
  128. return;
  129. }
  130. // Reinsert the output lines into the highlighted code. -- cwells
  131. var codeLines = env.highlightedCode.split('\n');
  132. var outputLines = commandLine.outputLines || [];
  133. for (var i = 0, l = codeLines.length; i < l; i++) {
  134. // Add spans to allow distinction of input/output text for styling
  135. if (outputLines.hasOwnProperty(i)) {
  136. // outputLines were removed from codeLines so missed out on escaping
  137. // of markup so do it here.
  138. codeLines[i] = '<span class="token output">'
  139. + Prism.util.encode(outputLines[i]) + '</span>';
  140. } else {
  141. codeLines[i] = '<span class="token command">'
  142. + codeLines[i] + '</span>';
  143. }
  144. }
  145. env.highlightedCode = codeLines.join('\n');
  146. });
  147. Prism.hooks.add('complete', function (env) {
  148. if (!hasCommandLineInfo(env)) {
  149. // the previous hooks never ran
  150. return;
  151. }
  152. var commandLine = getCommandLineInfo(env);
  153. if (commandLine.complete) {
  154. return;
  155. }
  156. var pre = env.element.parentElement;
  157. if (CLASS_PATTERN.test(env.element.className)) { // Remove the class "command-line" from the <code>
  158. env.element.className = env.element.className.replace(CLASS_PATTERN, ' ');
  159. }
  160. if (!CLASS_PATTERN.test(pre.className)) { // Add the class "command-line" to the <pre>
  161. pre.className += ' command-line';
  162. }
  163. function getAttribute(key, defaultValue) {
  164. return (pre.getAttribute(key) || defaultValue).replace(/"/g, '&quot');
  165. }
  166. // Create the "rows" that will become the command-line prompts. -- cwells
  167. var promptLines = '';
  168. var rowCount = commandLine.numberOfLines || 0;
  169. var promptText = getAttribute('data-prompt', '');
  170. var promptLine;
  171. if (promptText !== '') {
  172. promptLine = '<span data-prompt="' + promptText + '"></span>';
  173. } else {
  174. var user = getAttribute('data-user', 'user');
  175. var host = getAttribute('data-host', 'localhost');
  176. promptLine = '<span data-user="' + user + '" data-host="' + host + '"></span>';
  177. }
  178. var continuationLineIndicies = commandLine.continuationLineIndicies || new Set();
  179. var continuationPromptText = getAttribute('data-continuation-prompt', '>');
  180. var continuationPromptLine = '<span data-continuation-prompt="' + continuationPromptText + '"></span>';
  181. // Assemble all the appropriate prompt/continuation lines
  182. for (var j = 0; j < rowCount; j++) {
  183. if (continuationLineIndicies.has(j)) {
  184. promptLines += continuationPromptLine;
  185. } else {
  186. promptLines += promptLine;
  187. }
  188. }
  189. // Create the wrapper element. -- cwells
  190. var prompt = document.createElement('span');
  191. prompt.className = PROMPT_CLASS;
  192. prompt.innerHTML = promptLines;
  193. // Remove the prompt from the output lines. -- cwells
  194. var outputLines = commandLine.outputLines || [];
  195. for (var i = 0, l = outputLines.length; i < l; i++) {
  196. if (outputLines.hasOwnProperty(i)) {
  197. var node = prompt.children[i];
  198. node.removeAttribute('data-user');
  199. node.removeAttribute('data-host');
  200. node.removeAttribute('data-prompt');
  201. }
  202. }
  203. env.element.insertBefore(prompt, env.element.firstChild);
  204. commandLine.complete = true;
  205. });
  206. }());