prism-shell-session.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. (function (Prism) {
  2. // CAREFUL!
  3. // The following patterns are concatenated, so the group referenced by a back reference is non-obvious!
  4. var strings = [
  5. // normal string
  6. /"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/.source,
  7. /'[^']*'/.source,
  8. /\$'(?:[^'\\]|\\[\s\S])*'/.source,
  9. // here doc
  10. // 2 capturing groups
  11. /<<-?\s*(["']?)(\w+)\1\s[\s\S]*?[\r\n]\2/.source
  12. ].join('|');
  13. Prism.languages['shell-session'] = {
  14. 'command': {
  15. pattern: RegExp(
  16. // user info
  17. /^/.source +
  18. '(?:' +
  19. (
  20. // <user> ":" ( <path> )?
  21. /[^\s@:$#%*!/\\]+@[^\r\n@:$#%*!/\\]+(?::[^\0-\x1F$#%*?"<>:;|]+)?/.source +
  22. '|' +
  23. // <path>
  24. // Since the path pattern is quite general, we will require it to start with a special character to
  25. // prevent false positives.
  26. /[/~.][^\0-\x1F$#%*?"<>@:;|]*/.source
  27. ) +
  28. ')?' +
  29. // shell symbol
  30. /[$#%](?=\s)/.source +
  31. // bash command
  32. /(?:[^\\\r\n \t'"<$]|[ \t](?:(?!#)|#.*$)|\\(?:[^\r]|\r\n?)|\$(?!')|<(?!<)|<<str>>)+/.source.replace(/<<str>>/g, function () { return strings; }),
  33. 'm'
  34. ),
  35. greedy: true,
  36. inside: {
  37. 'info': {
  38. // foo@bar:~/files$ exit
  39. // foo@bar$ exit
  40. // ~/files$ exit
  41. pattern: /^[^#$%]+/,
  42. alias: 'punctuation',
  43. inside: {
  44. 'user': /^[^\s@:$#%*!/\\]+@[^\r\n@:$#%*!/\\]+/,
  45. 'punctuation': /:/,
  46. 'path': /[\s\S]+/
  47. }
  48. },
  49. 'bash': {
  50. pattern: /(^[$#%]\s*)\S[\s\S]*/,
  51. lookbehind: true,
  52. alias: 'language-bash',
  53. inside: Prism.languages.bash
  54. },
  55. 'shell-symbol': {
  56. pattern: /^[$#%]/,
  57. alias: 'important'
  58. }
  59. }
  60. },
  61. 'output': /.(?:.*(?:[\r\n]|.$))*/
  62. };
  63. Prism.languages['sh-session'] = Prism.languages['shellsession'] = Prism.languages['shell-session'];
  64. }(Prism));