asciiarmor.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. function errorIfNotEmpty(stream) {
  15. var nonWS = stream.match(/^\s*\S/)
  16. stream.skipToEnd()
  17. return nonWS ? 'error' : null
  18. }
  19. CodeMirror.defineMode('asciiarmor', function () {
  20. return {
  21. token: function (stream, state) {
  22. var m
  23. if (state.state == 'top') {
  24. if (stream.sol() && (m = stream.match(/^-----BEGIN (.*)?-----\s*$/))) {
  25. state.state = 'headers'
  26. state.type = m[1]
  27. return 'tag'
  28. }
  29. return errorIfNotEmpty(stream)
  30. } else if (state.state == 'headers') {
  31. if (stream.sol() && stream.match(/^\w+:/)) {
  32. state.state = 'header'
  33. return 'atom'
  34. } else {
  35. var result = errorIfNotEmpty(stream)
  36. if (result) state.state = 'body'
  37. return result
  38. }
  39. } else if (state.state == 'header') {
  40. stream.skipToEnd()
  41. state.state = 'headers'
  42. return 'string'
  43. } else if (state.state == 'body') {
  44. if (stream.sol() && (m = stream.match(/^-----END (.*)?-----\s*$/))) {
  45. if (m[1] != state.type) return 'error'
  46. state.state = 'end'
  47. return 'tag'
  48. } else {
  49. if (stream.eatWhile(/[A-Za-z0-9+\/=]/)) {
  50. return null
  51. } else {
  52. stream.next()
  53. return 'error'
  54. }
  55. }
  56. } else if (state.state == 'end') {
  57. return errorIfNotEmpty(stream)
  58. }
  59. },
  60. blankLine: function (state) {
  61. if (state.state == 'headers') state.state = 'body'
  62. },
  63. startState: function () {
  64. return { state: 'top', type: null }
  65. },
  66. }
  67. })
  68. CodeMirror.defineMIME('application/pgp', 'asciiarmor')
  69. CodeMirror.defineMIME('application/pgp-encrypted', 'asciiarmor')
  70. CodeMirror.defineMIME('application/pgp-keys', 'asciiarmor')
  71. CodeMirror.defineMIME('application/pgp-signature', 'asciiarmor')
  72. })