prism-systemd.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // https://www.freedesktop.org/software/systemd/man/systemd.syntax.html
  2. (function (Prism) {
  3. var comment = {
  4. pattern: /^[;#].*/m,
  5. greedy: true
  6. };
  7. var quotesSource = /"(?:[^\r\n"\\]|\\(?:[^\r]|\r\n?))*"(?!\S)/.source;
  8. Prism.languages.systemd = {
  9. 'comment': comment,
  10. 'section': {
  11. pattern: /^\[[^\n\r\[\]]*\](?=[ \t]*$)/m,
  12. greedy: true,
  13. inside: {
  14. 'punctuation': /^\[|\]$/,
  15. 'section-name': {
  16. pattern: /[\s\S]+/,
  17. alias: 'selector'
  18. },
  19. }
  20. },
  21. 'key': {
  22. pattern: /^[^\s=]+(?=[ \t]*=)/m,
  23. greedy: true,
  24. alias: 'attr-name'
  25. },
  26. 'value': {
  27. // This pattern is quite complex because of two properties:
  28. // 1) Quotes (strings) must be preceded by a space. Since we can't use lookbehinds, we have to "resolve"
  29. // the lookbehind. You will see this in the main loop where spaces are handled separately.
  30. // 2) Line continuations.
  31. // After line continuations, empty lines and comments are ignored so we have to consume them.
  32. pattern: RegExp(
  33. /(=[ \t]*(?!\s))/.source +
  34. // the value either starts with quotes or not
  35. '(?:' + quotesSource + '|(?=[^"\r\n]))' +
  36. // main loop
  37. '(?:' + (
  38. /[^\s\\]/.source +
  39. // handle spaces separately because of quotes
  40. '|' + '[ \t]+(?:(?![ \t"])|' + quotesSource + ')' +
  41. // line continuation
  42. '|' + /\\[\r\n]+(?:[#;].*[\r\n]+)*(?![#;])/.source
  43. ) +
  44. ')*'
  45. ),
  46. lookbehind: true,
  47. greedy: true,
  48. alias: 'attr-value',
  49. inside: {
  50. 'comment': comment,
  51. 'quoted': {
  52. pattern: RegExp(/(^|\s)/.source + quotesSource),
  53. lookbehind: true,
  54. greedy: true,
  55. },
  56. 'punctuation': /\\$/m,
  57. 'boolean': {
  58. pattern: /^(?:false|no|off|on|true|yes)$/,
  59. greedy: true
  60. }
  61. }
  62. },
  63. 'punctuation': /=/
  64. };
  65. }(Prism));