summaryrefslogtreecommitdiffstats
path: root/public/prism/prism-shell-session.js
blob: 5674752e616814cb088681f9f57909be76159cfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(function (Prism) {
  // CAREFUL!
  // The following patterns are concatenated, so the group referenced by a back reference is non-obvious!

  var strings = [
    // normal string
    /"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/.source,
    /'[^']*'/.source,
    /\$'(?:[^'\\]|\\[\s\S])*'/.source,

    // here doc
    // 2 capturing groups
    /<<-?\s*(["']?)(\w+)\1\s[\s\S]*?[\r\n]\2/.source,
  ].join('|');

  Prism.languages['shell-session'] = {
    command: {
      pattern: RegExp(
        // user info
        /^/.source +
          '(?:' +
          // <user> ":" ( <path> )?
          (/[^\s@:$#%*!/\\]+@[^\r\n@:$#%*!/\\]+(?::[^\0-\x1F$#%*?"<>:;|]+)?/
            .source +
            '|' +
            // <path>
            // Since the path pattern is quite general, we will require it to start with a special character to
            // prevent false positives.
            /[/~.][^\0-\x1F$#%*?"<>@:;|]*/.source) +
          ')?' +
          // shell symbol
          /[$#%](?=\s)/.source +
          // bash command
          /(?:[^\\\r\n \t'"<$]|[ \t](?:(?!#)|#.*$)|\\(?:[^\r]|\r\n?)|\$(?!')|<(?!<)|<<str>>)+/.source.replace(
            /<<str>>/g,
            function () {
              return strings;
            }
          ),
        'm'
      ),
      greedy: true,
      inside: {
        info: {
          // foo@bar:~/files$ exit
          // foo@bar$ exit
          // ~/files$ exit
          pattern: /^[^#$%]+/,
          alias: 'punctuation',
          inside: {
            user: /^[^\s@:$#%*!/\\]+@[^\r\n@:$#%*!/\\]+/,
            punctuation: /:/,
            path: /[\s\S]+/,
          },
        },
        bash: {
          pattern: /(^[$#%]\s*)\S[\s\S]*/,
          lookbehind: true,
          alias: 'language-bash',
          inside: Prism.languages.bash,
        },
        'shell-symbol': {
          pattern: /^[$#%]/,
          alias: 'important',
        },
      },
    },
    output: /.(?:.*(?:[\r\n]|.$))*/,
  };

  Prism.languages['sh-session'] = Prism.languages['shellsession'] =
    Prism.languages['shell-session'];
})(Prism);
[\s\S]+?\)\)/, greedy: true, inside: { // If there is a $ sign at the beginning highlight $(( and )) as variable variable: [ { pattern: /(^\$\(\([\s\S]+)\)\)/, lookbehind: true, }, /^\$\(\(/, ], number: /\b0x[\dA-Fa-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:[Ee]-?\d+)?/, // Operators according to https://www.gnu.org/software/bash/manual/bashref.html#Shell-Arithmetic operator: /--|\+\+|\*\*=?|<<=?|>>=?|&&|\|\||[=!+\-*/%<>^&|]=?|[?~:]/, // If there is no $ sign at the beginning highlight (( and )) as punctuation punctuation: /\(\(?|\)\)?|,|;/, }, }, // [1]: Command Substitution { pattern: /\$\((?:\([^)]+\)|[^()])+\)|`[^`]+`/, greedy: true, inside: { variable: /^\$\(|^`|\)$|`$/, }, }, // [2]: Brace expansion { pattern: /\$\{[^}]+\}/, greedy: true, inside: { operator: /:[-=?+]?|[!\/]|##?|%%?|\^\^?|,,?/, punctuation: /[\[\]]/, environment: { pattern: RegExp('(\\{)' + envVars), lookbehind: true, alias: 'constant', }, }, }, /\$(?:\w+|[#?*!@$])/, ], // Escape sequences from echo and printf's manuals, and escaped quotes. entity: /\\(?:[abceEfnrtv\\"]|O?[0-7]{1,3}|U[0-9a-fA-F]{8}|u[0-9a-fA-F]{4}|x[0-9a-fA-F]{1,2})/, }; Prism.languages.bash = { shebang: { pattern: /^#!\s*\/.*/, alias: 'important', }, comment: { pattern: /(^|[^"{\\$])#.*/, lookbehind: true, }, 'function-name': [ // a) function foo { // b) foo() { // c) function foo() { // but not “foo {” { // a) and c) pattern: /(\bfunction\s+)[\w-]+(?=(?:\s*\(?:\s*\))?\s*\{)/, lookbehind: true, alias: 'function', }, { // b) pattern: /\b[\w-]+(?=\s*\(\s*\)\s*\{)/, alias: 'function', }, ], // Highlight variable names as variables in for and select beginnings. 'for-or-select': { pattern: /(\b(?:for|select)\s+)\w+(?=\s+in\s)/, alias: 'variable', lookbehind: true, }, // Highlight variable names as variables in the left-hand part // of assignments (“=” and “+=”). 'assign-left': { pattern: /(^|[\s;|&]|[<>]\()\w+(?=\+?=)/, inside: { environment: { pattern: RegExp('(^|[\\s;|&]|[<>]\\()' + envVars), lookbehind: true, alias: 'constant', }, }, alias: 'variable', lookbehind: true, }, string: [ // Support for Here-documents https://en.wikipedia.org/wiki/Here_document { pattern: /((?:^|[^<])<<-?\s*)(\w+)\s[\s\S]*?(?:\r?\n|\r)\2/, lookbehind: true, greedy: true, inside: insideString, }, // Here-document with quotes around the tag // → No expansion (so no “inside”). { pattern: /((?:^|[^<])<<-?\s*)(["'])(\w+)\2\s[\s\S]*?(?:\r?\n|\r)\3/, lookbehind: true, greedy: true, inside: { bash: commandAfterHeredoc, }, }, // “Normal” string { // https://www.gnu.org/software/bash/manual/html_node/Double-Quotes.html pattern: /(^|[^\\](?:\\\\)*)"(?:\\[\s\S]|\$\([^)]+\)|\$(?!\()|`[^`]+`|[^"\\`$])*"/, lookbehind: true, greedy: true, inside: insideString, }, { // https://www.gnu.org/software/bash/manual/html_node/Single-Quotes.html pattern: /(^|[^$\\])'[^']*'/, lookbehind: true, greedy: true, }, { // https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html pattern: /\$'(?:[^'\\]|\\[\s\S])*'/, greedy: true, inside: { entity: insideString.entity, }, }, ], environment: { pattern: RegExp('\\$?' + envVars), alias: 'constant', }, variable: insideString.variable, function: { pattern: /(^|[\s;|&]|[<>]\()(?:add|apropos|apt|apt-cache|apt-get|aptitude|aspell|automysqlbackup|awk|basename|bash|bc|bconsole|bg|bzip2|cal|cat|cfdisk|chgrp|chkconfig|chmod|chown|chroot|cksum|clear|cmp|column|comm|composer|cp|cron|crontab|csplit|curl|cut|date|dc|dd|ddrescue|debootstrap|df|diff|diff3|dig|dir|dircolors|dirname|dirs|dmesg|docker|docker-compose|du|egrep|eject|env|ethtool|expand|expect|expr|fdformat|fdisk|fg|fgrep|file|find|fmt|fold|format|free|fsck|ftp|fuser|gawk|git|gparted|grep|groupadd|groupdel|groupmod|groups|grub-mkconfig|gzip|halt|head|hg|history|host|hostname|htop|iconv|id|ifconfig|ifdown|ifup|import|install|ip|jobs|join|kill|killall|less|link|ln|locate|logname|logrotate|look|lpc|lpr|lprint|lprintd|lprintq|lprm|ls|lsof|lynx|make|man|mc|mdadm|mkconfig|mkdir|mke2fs|mkfifo|mkfs|mkisofs|mknod|mkswap|mmv|more|most|mount|mtools|mtr|mutt|mv|nano|nc|netstat|nice|nl|nohup|notify-send|npm|nslookup|op|open|parted|passwd|paste|pathchk|ping|pkill|pnpm|podman|podman-compose|popd|pr|printcap|printenv|ps|pushd|pv|quota|quotacheck|quotactl|ram|rar|rcp|reboot|remsync|rename|renice|rev|rm|rmdir|rpm|rsync|scp|screen|sdiff|sed|sendmail|seq|service|sftp|sh|shellcheck|shuf|shutdown|sleep|slocate|sort|split|ssh|stat|strace|su|sudo|sum|suspend|swapon|sync|tac|tail|tar|tee|time|timeout|top|touch|tr|traceroute|tsort|tty|umount|uname|unexpand|uniq|units|unrar|unshar|unzip|update-grub|uptime|useradd|userdel|usermod|users|uudecode|uuencode|v|vcpkg|vdir|vi|vim|virsh|vmstat|wait|watch|wc|wget|whereis|which|who|whoami|write|xargs|xdg-open|yarn|yes|zenity|zip|zsh|zypper)(?=$|[)\s;|&])/, lookbehind: true, }, keyword: { pattern: /(^|[\s;|&]|[<>]\()(?:case|do|done|elif|else|esac|fi|for|function|if|in|select|then|until|while)(?=$|[)\s;|&])/, lookbehind: true, }, // https://www.gnu.org/software/bash/manual/html_node/Shell-Builtin-Commands.html builtin: { pattern: /(^|[\s;|&]|[<>]\()(?:\.|:|alias|bind|break|builtin|caller|cd|command|continue|declare|echo|enable|eval|exec|exit|export|getopts|hash|help|let|local|logout|mapfile|printf|pwd|read|readarray|readonly|return|set|shift|shopt|source|test|times|trap|type|typeset|ulimit|umask|unalias|unset)(?=$|[)\s;|&])/, lookbehind: true, // Alias added to make those easier to distinguish from strings. alias: 'class-name', }, boolean: { pattern: /(^|[\s;|&]|[<>]\()(?:false|true)(?=$|[)\s;|&])/, lookbehind: true, }, 'file-descriptor': { pattern: /\B&\d\b/, alias: 'important', }, operator: { // Lots of redirections here, but not just that. pattern: /\d?<>|>\||\+=|=[=~]?|!=?|<<[<-]?|[&\d]?>>|\d[<>]&?|[<>][&=]?|&[>&]?|\|[&|]?/, inside: { 'file-descriptor': { pattern: /^\d/, alias: 'important', }, }, }, punctuation: /\$?\(\(?|\)\)?|\.\.|[{}[\];\\]/, number: { pattern: /(^|\s)(?:[1-9]\d*|0)(?:[.,]\d+)?\b/, lookbehind: true, }, }; commandAfterHeredoc.inside = Prism.languages.bash; /* Patterns in command substitution. */ var toBeCopied = [ 'comment', 'function-name', 'for-or-select', 'assign-left', 'string', 'environment', 'function', 'keyword', 'builtin', 'boolean', 'file-descriptor', 'operator', 'punctuation', 'number', ]; var inside = insideString.variable[1].inside; for (var i = 0; i < toBeCopied.length; i++) { inside[toBeCopied[i]] = Prism.languages.bash[toBeCopied[i]]; } Prism.languages.shell = Prism.languages.bash; })(Prism);