| 1 |
function errorIfNotEmpty(stream) { |
| 2 |
var nonWS = stream.match(/^\s*\S/); |
| 3 |
stream.skipToEnd(); |
| 4 |
return nonWS ? "error" : null; |
| 5 |
} |
| 6 |
|
| 7 |
export const asciiArmor = { |
| 8 |
name: "asciiarmor", |
| 9 |
token: function(stream, state) { |
| 10 |
var m; |
| 11 |
if (state.state == "top") { |
| 12 |
if (stream.sol() && (m = stream.match(/^-----BEGIN (.*)?-----\s*$/))) { |
| 13 |
state.state = "headers"; |
| 14 |
state.type = m[1]; |
| 15 |
return "tag"; |
| 16 |
} |
| 17 |
return errorIfNotEmpty(stream); |
| 18 |
} else if (state.state == "headers") { |
| 19 |
if (stream.sol() && stream.match(/^\w+:/)) { |
| 20 |
state.state = "header"; |
| 21 |
return "atom"; |
| 22 |
} else { |
| 23 |
var result = errorIfNotEmpty(stream); |
| 24 |
if (result) state.state = "body"; |
| 25 |
return result; |
| 26 |
} |
| 27 |
} else if (state.state == "header") { |
| 28 |
stream.skipToEnd(); |
| 29 |
state.state = "headers"; |
| 30 |
return "string"; |
| 31 |
} else if (state.state == "body") { |
| 32 |
if (stream.sol() && (m = stream.match(/^-----END (.*)?-----\s*$/))) { |
| 33 |
if (m[1] != state.type) return "error"; |
| 34 |
state.state = "end"; |
| 35 |
return "tag"; |
| 36 |
} else { |
| 37 |
if (stream.eatWhile(/[A-Za-z0-9+\/=]/)) { |
| 38 |
return null; |
| 39 |
} else { |
| 40 |
stream.next(); |
| 41 |
return "error"; |
| 42 |
} |
| 43 |
} |
| 44 |
} else if (state.state == "end") { |
| 45 |
return errorIfNotEmpty(stream); |
| 46 |
} |
| 47 |
}, |
| 48 |
blankLine: function(state) { |
| 49 |
if (state.state == "headers") state.state = "body"; |
| 50 |
}, |
| 51 |
startState: function() { |
| 52 |
return {state: "top", type: null}; |
| 53 |
} |
| 54 |
}; |