rpm.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. mod(require("../../lib/codemirror"));
  4. else if (typeof define == "function" && define.amd) // AMD
  5. define(["../../lib/codemirror"], mod);
  6. else // Plain browser env
  7. mod(CodeMirror);
  8. })(function(CodeMirror) {
  9. "use strict";
  10. CodeMirror.defineMode("rpm-changes", function() {
  11. var headerSeperator = /^-+$/;
  12. 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} - /;
  13. var simpleEmail = /^[\w+.-]+@[\w.-]+/;
  14. return {
  15. token: function(stream) {
  16. if (stream.sol()) {
  17. if (stream.match(headerSeperator)) { return 'tag'; }
  18. if (stream.match(headerLine)) { return 'tag'; }
  19. }
  20. if (stream.match(simpleEmail)) { return 'string'; }
  21. stream.next();
  22. return null;
  23. }
  24. };
  25. });
  26. CodeMirror.defineMIME("text/x-rpm-changes", "rpm-changes");
  27. // Quick and dirty spec file highlighting
  28. CodeMirror.defineMode("rpm-spec", function() {
  29. var arch = /^(i386|i586|i686|x86_64|ppc64|ppc|ia64|s390x|s390|sparc64|sparcv9|sparc|noarch|alphaev6|alpha|hppa|mipsel)/;
  30. var preamble = /^(Name|Version|Release|License|Summary|Url|Group|Source|BuildArch|BuildRequires|BuildRoot|AutoReqProv|Provides|Requires(\(\w+\))?|Obsoletes|Conflicts|Recommends|Source\d*|Patch\d*|ExclusiveArch|NoSource|Supplements):/;
  31. var section = /^%(debug_package|package|description|prep|build|install|files|clean|changelog|preinstall|preun|postinstall|postun|pre|post|triggerin|triggerun|pretrans|posttrans|verifyscript|check|triggerpostun|triggerprein|trigger)/;
  32. var control_flow_complex = /^%(ifnarch|ifarch|if)/; // rpm control flow macros
  33. var control_flow_simple = /^%(else|endif)/; // rpm control flow macros
  34. var operators = /^(\!|\?|\<\=|\<|\>\=|\>|\=\=|\&\&|\|\|)/; // operators in control flow macros
  35. return {
  36. startState: function () {
  37. return {
  38. controlFlow: false,
  39. macroParameters: false,
  40. section: false
  41. };
  42. },
  43. token: function (stream, state) {
  44. var ch = stream.peek();
  45. if (ch == "#") { stream.skipToEnd(); return "comment"; }
  46. if (stream.sol()) {
  47. if (stream.match(preamble)) { return "preamble"; }
  48. if (stream.match(section)) { return "section"; }
  49. }
  50. if (stream.match(/^\$\w+/)) { return "def"; } // Variables like '$RPM_BUILD_ROOT'
  51. if (stream.match(/^\$\{\w+\}/)) { return "def"; } // Variables like '${RPM_BUILD_ROOT}'
  52. if (stream.match(control_flow_simple)) { return "keyword"; }
  53. if (stream.match(control_flow_complex)) {
  54. state.controlFlow = true;
  55. return "keyword";
  56. }
  57. if (state.controlFlow) {
  58. if (stream.match(operators)) { return "operator"; }
  59. if (stream.match(/^(\d+)/)) { return "number"; }
  60. if (stream.eol()) { state.controlFlow = false; }
  61. }
  62. if (stream.match(arch)) { return "number"; }
  63. // Macros like '%make_install' or '%attr(0775,root,root)'
  64. if (stream.match(/^%[\w]+/)) {
  65. if (stream.match(/^\(/)) { state.macroParameters = true; }
  66. return "macro";
  67. }
  68. if (state.macroParameters) {
  69. if (stream.match(/^\d+/)) { return "number";}
  70. if (stream.match(/^\)/)) {
  71. state.macroParameters = false;
  72. return "macro";
  73. }
  74. }
  75. if (stream.match(/^%\{\??[\w \-]+\}/)) { return "macro"; } // Macros like '%{defined fedora}'
  76. //TODO: Include bash script sub-parser (CodeMirror supports that)
  77. stream.next();
  78. return null;
  79. }
  80. };
  81. });
  82. CodeMirror.defineMIME("text/x-rpm-spec", "rpm-spec");
  83. });