scripts/CodeMirror/mode/asciiarmor/asciiarmor.js (view raw)
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {
5 if (typeof exports == "object" && typeof module == "object") // CommonJS
6 mod(require("../../lib/codemirror"));
7 else if (typeof define == "function" && define.amd) // AMD
8 define(["../../lib/codemirror"], mod);
9 else // Plain browser env
10 mod(CodeMirror);
11})(function(CodeMirror) {
12 "use strict";
13
14 function errorIfNotEmpty(stream) {
15 var nonWS = stream.match(/^\s*\S/);
16 stream.skipToEnd();
17 return nonWS ? "error" : null;
18 }
19
20 CodeMirror.defineMode("asciiarmor", function() {
21 return {
22 token: function(stream, state) {
23 var m;
24 if (state.state == "top") {
25 if (stream.sol() && (m = stream.match(/^-----BEGIN (.*)?-----\s*$/))) {
26 state.state = "headers";
27 state.type = m[1];
28 return "tag";
29 }
30 return errorIfNotEmpty(stream);
31 } else if (state.state == "headers") {
32 if (stream.sol() && stream.match(/^\w+:/)) {
33 state.state = "header";
34 return "atom";
35 } else {
36 var result = errorIfNotEmpty(stream);
37 if (result) state.state = "body";
38 return result;
39 }
40 } else if (state.state == "header") {
41 stream.skipToEnd();
42 state.state = "headers";
43 return "string";
44 } else if (state.state == "body") {
45 if (stream.sol() && (m = stream.match(/^-----END (.*)?-----\s*$/))) {
46 if (m[1] != state.type) return "error";
47 state.state = "end";
48 return "tag";
49 } else {
50 if (stream.eatWhile(/[A-Za-z0-9+\/=]/)) {
51 return null;
52 } else {
53 stream.next();
54 return "error";
55 }
56 }
57 } else if (state.state == "end") {
58 return errorIfNotEmpty(stream);
59 }
60 },
61 blankLine: function(state) {
62 if (state.state == "headers") state.state = "body";
63 },
64 startState: function() {
65 return {state: "top", type: null};
66 }
67 };
68 });
69
70 CodeMirror.defineMIME("application/pgp", "asciiarmor");
71 CodeMirror.defineMIME("application/pgp-encrypted", "asciiarmor");
72 CodeMirror.defineMIME("application/pgp-keys", "asciiarmor");
73 CodeMirror.defineMIME("application/pgp-signature", "asciiarmor");
74});