rpm.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. ;(function (mod) {
  4. if (typeof exports == 'object' && typeof module == 'object')
  5. // CommonJS
  6. mod(require('../../lib/codemirror'))
  7. else if (typeof define == 'function' && define.amd)
  8. // AMD
  9. define(['../../lib/codemirror'], mod)
  10. // Plain browser env
  11. else mod(CodeMirror)
  12. })(function (CodeMirror) {
  13. 'use strict'
  14. CodeMirror.defineMode('rpm-changes', function () {
  15. var headerSeparator = /^-+$/
  16. 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} - /
  17. var simpleEmail = /^[\w+.-]+@[\w.-]+/
  18. return {
  19. token: function (stream) {
  20. if (stream.sol()) {
  21. if (stream.match(headerSeparator)) {
  22. return 'tag'
  23. }
  24. if (stream.match(headerLine)) {
  25. return 'tag'
  26. }
  27. }
  28. if (stream.match(simpleEmail)) {
  29. return 'string'
  30. }
  31. stream.next()
  32. return null
  33. },
  34. }
  35. })
  36. CodeMirror.defineMIME('text/x-rpm-changes', 'rpm-changes')
  37. // Quick and dirty spec file highlighting
  38. CodeMirror.defineMode('rpm-spec', function () {
  39. var arch = /^(i386|i586|i686|x86_64|ppc64le|ppc64|ppc|ia64|s390x|s390|sparc64|sparcv9|sparc|noarch|alphaev6|alpha|hppa|mipsel)/
  40. var preamble = /^[a-zA-Z0-9()]+:/
  41. var section =
  42. /^%(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)/
  43. var control_flow_complex = /^%(ifnarch|ifarch|if)/ // rpm control flow macros
  44. var control_flow_simple = /^%(else|endif)/ // rpm control flow macros
  45. var operators = /^(\!|\?|\<\=|\<|\>\=|\>|\=\=|\&\&|\|\|)/ // operators in control flow macros
  46. return {
  47. startState: function () {
  48. return {
  49. controlFlow: false,
  50. macroParameters: false,
  51. section: false,
  52. }
  53. },
  54. token: function (stream, state) {
  55. var ch = stream.peek()
  56. if (ch == '#') {
  57. stream.skipToEnd()
  58. return 'comment'
  59. }
  60. if (stream.sol()) {
  61. if (stream.match(preamble)) {
  62. return 'header'
  63. }
  64. if (stream.match(section)) {
  65. return 'atom'
  66. }
  67. }
  68. if (stream.match(/^\$\w+/)) {
  69. return 'def'
  70. } // Variables like '$RPM_BUILD_ROOT'
  71. if (stream.match(/^\$\{\w+\}/)) {
  72. return 'def'
  73. } // Variables like '${RPM_BUILD_ROOT}'
  74. if (stream.match(control_flow_simple)) {
  75. return 'keyword'
  76. }
  77. if (stream.match(control_flow_complex)) {
  78. state.controlFlow = true
  79. return 'keyword'
  80. }
  81. if (state.controlFlow) {
  82. if (stream.match(operators)) {
  83. return 'operator'
  84. }
  85. if (stream.match(/^(\d+)/)) {
  86. return 'number'
  87. }
  88. if (stream.eol()) {
  89. state.controlFlow = false
  90. }
  91. }
  92. if (stream.match(arch)) {
  93. if (stream.eol()) {
  94. state.controlFlow = false
  95. }
  96. return 'number'
  97. }
  98. // Macros like '%make_install' or '%attr(0775,root,root)'
  99. if (stream.match(/^%[\w]+/)) {
  100. if (stream.match('(')) {
  101. state.macroParameters = true
  102. }
  103. return 'keyword'
  104. }
  105. if (state.macroParameters) {
  106. if (stream.match(/^\d+/)) {
  107. return 'number'
  108. }
  109. if (stream.match(')')) {
  110. state.macroParameters = false
  111. return 'keyword'
  112. }
  113. }
  114. // Macros like '%{defined fedora}'
  115. if (stream.match(/^%\{\??[\w \-\:\!]+\}/)) {
  116. if (stream.eol()) {
  117. state.controlFlow = false
  118. }
  119. return 'def'
  120. }
  121. //TODO: Include bash script sub-parser (CodeMirror supports that)
  122. stream.next()
  123. return null
  124. },
  125. }
  126. })
  127. CodeMirror.defineMIME('text/x-rpm-spec', 'rpm-spec')
  128. })