rpm.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode("rpm-changes", function() {
  13. var headerSeperator = /^-+$/;
  14. var headerLine = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ?\d{1,2} \d{2}:\d{2}(:\d{2})? [A-Z]{3,4} \d{4} - /;
  15. var simpleEmail = /^[\w+.-]+@[\w.-]+/;
  16. return {
  17. token: function(stream) {
  18. if (stream.sol()) {
  19. if (stream.match(headerSeperator)) { return 'tag'; }
  20. if (stream.match(headerLine)) { return 'tag'; }
  21. }
  22. if (stream.match(simpleEmail)) { return 'string'; }
  23. stream.next();
  24. return null;
  25. }
  26. };
  27. });
  28. CodeMirror.defineMIME("text/x-rpm-changes", "rpm-changes");
  29. // Quick and dirty spec file highlighting
  30. CodeMirror.defineMode("rpm-spec", function() {
  31. var arch = /^(i386|i586|i686|x86_64|ppc64le|ppc64|ppc|ia64|s390x|s390|sparc64|sparcv9|sparc|noarch|alphaev6|alpha|hppa|mipsel)/;
  32. var preamble = /^[a-zA-Z0-9()]+:/;
  33. var section = /^%(debug_package|package|description|prep|build|install|files|clean|changelog|preinstall|preun|postinstall|postun|pretrans|posttrans|pre|post|triggerin|triggerun|verifyscript|check|triggerpostun|triggerprein|trigger)/;
  34. var control_flow_complex = /^%(ifnarch|ifarch|if)/; // rpm control flow macros
  35. var control_flow_simple = /^%(else|endif)/; // rpm control flow macros
  36. var operators = /^(\!|\?|\<\=|\<|\>\=|\>|\=\=|\&\&|\|\|)/; // operators in control flow macros
  37. return {
  38. startState: function () {
  39. return {
  40. controlFlow: false,
  41. macroParameters: false,
  42. section: false
  43. };
  44. },
  45. token: function (stream, state) {
  46. var ch = stream.peek();
  47. if (ch == "#") { stream.skipToEnd(); return "comment"; }
  48. if (stream.sol()) {
  49. if (stream.match(preamble)) { return "header"; }
  50. if (stream.match(section)) { return "atom"; }
  51. }
  52. if (stream.match(/^\$\w+/)) { return "def"; } // Variables like '$RPM_BUILD_ROOT'
  53. if (stream.match(/^\$\{\w+\}/)) { return "def"; } // Variables like '${RPM_BUILD_ROOT}'
  54. if (stream.match(control_flow_simple)) { return "keyword"; }
  55. if (stream.match(control_flow_complex)) {
  56. state.controlFlow = true;
  57. return "keyword";
  58. }
  59. if (state.controlFlow) {
  60. if (stream.match(operators)) { return "operator"; }
  61. if (stream.match(/^(\d+)/)) { return "number"; }
  62. if (stream.eol()) { state.controlFlow = false; }
  63. }
  64. if (stream.match(arch)) {
  65. if (stream.eol()) { state.controlFlow = false; }
  66. return "number";
  67. }
  68. // Macros like '%make_install' or '%attr(0775,root,root)'
  69. if (stream.match(/^%[\w]+/)) {
  70. if (stream.match(/^\(/)) { state.macroParameters = true; }
  71. return "keyword";
  72. }
  73. if (state.macroParameters) {
  74. if (stream.match(/^\d+/)) { return "number";}
  75. if (stream.match(/^\)/)) {
  76. state.macroParameters = false;
  77. return "keyword";
  78. }
  79. }
  80. // Macros like '%{defined fedora}'
  81. if (stream.match(/^%\{\??[\w \-\:\!]+\}/)) {
  82. if (stream.eol()) { state.controlFlow = false; }
  83. return "def";
  84. }
  85. //TODO: Include bash script sub-parser (CodeMirror supports that)
  86. stream.next();
  87. return null;
  88. }
  89. };
  90. });
  91. CodeMirror.defineMIME("text/x-rpm-spec", "rpm-spec");
  92. });