troff.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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")
  5. mod(require("../../lib/codemirror"));
  6. else if (typeof define == "function" && define.amd)
  7. define(["../../lib/codemirror"], mod);
  8. else
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineMode('troff', function() {
  13. var words = {};
  14. function tokenBase(stream) {
  15. if (stream.eatSpace()) return null;
  16. var sol = stream.sol();
  17. var ch = stream.next();
  18. if (ch === '\\') {
  19. if (stream.match('fB') || stream.match('fR') || stream.match('fI') ||
  20. stream.match('u') || stream.match('d') ||
  21. stream.match('%') || stream.match('&')) {
  22. return 'string';
  23. }
  24. if (stream.match('m[')) {
  25. stream.skipTo(']');
  26. stream.next();
  27. return 'string';
  28. }
  29. if (stream.match('s+') || stream.match('s-')) {
  30. stream.eatWhile(/[\d-]/);
  31. return 'string';
  32. }
  33. if (stream.match('\(') || stream.match('*\(')) {
  34. stream.eatWhile(/[\w-]/);
  35. return 'string';
  36. }
  37. return 'string';
  38. }
  39. if (sol && (ch === '.' || ch === '\'')) {
  40. if (stream.eat('\\') && stream.eat('\"')) {
  41. stream.skipToEnd();
  42. return 'comment';
  43. }
  44. }
  45. if (sol && ch === '.') {
  46. if (stream.match('B ') || stream.match('I ') || stream.match('R ')) {
  47. return 'attribute';
  48. }
  49. if (stream.match('TH ') || stream.match('SH ') || stream.match('SS ') || stream.match('HP ')) {
  50. stream.skipToEnd();
  51. return 'quote';
  52. }
  53. if ((stream.match(/[A-Z]/) && stream.match(/[A-Z]/)) || (stream.match(/[a-z]/) && stream.match(/[a-z]/))) {
  54. return 'attribute';
  55. }
  56. }
  57. stream.eatWhile(/[\w-]/);
  58. var cur = stream.current();
  59. return words.hasOwnProperty(cur) ? words[cur] : null;
  60. }
  61. function tokenize(stream, state) {
  62. return (state.tokens[0] || tokenBase) (stream, state);
  63. };
  64. return {
  65. startState: function() {return {tokens:[]};},
  66. token: function(stream, state) {
  67. return tokenize(stream, state);
  68. }
  69. };
  70. });
  71. CodeMirror.defineMIME('text/troff', 'troff');
  72. CodeMirror.defineMIME('text/x-troff', 'troff');
  73. CodeMirror.defineMIME('application/x-troff', 'troff');
  74. });