asciiarmor.js 2.3 KB

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