troff.js 2.8 KB

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