1
0

rst.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. CodeMirror.defineMode('rst', function(config, options) {
  2. function setState(state, fn, ctx) {
  3. state.fn = fn;
  4. setCtx(state, ctx);
  5. }
  6. function setCtx(state, ctx) {
  7. state.ctx = ctx || {};
  8. }
  9. function setNormal(state, ch) {
  10. if (ch && (typeof ch !== 'string')) {
  11. var str = ch.current();
  12. ch = str[str.length-1];
  13. }
  14. setState(state, normal, {back: ch});
  15. }
  16. function hasMode(mode) {
  17. if (mode) {
  18. var modes = CodeMirror.listModes();
  19. for (var i in modes) {
  20. if (modes[i] == mode) {
  21. return true;
  22. }
  23. }
  24. }
  25. return false;
  26. }
  27. function getMode(mode) {
  28. if (hasMode(mode)) {
  29. return CodeMirror.getMode(config, mode);
  30. } else {
  31. return null;
  32. }
  33. }
  34. var verbatimMode = getMode(options.verbatim);
  35. var pythonMode = getMode('python');
  36. var reSection = /^[!"#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~]/;
  37. var reDirective = /^\s*\w([-:.\w]*\w)?::(\s|$)/;
  38. var reHyperlink = /^\s*_[\w-]+:(\s|$)/;
  39. var reFootnote = /^\s*\[(\d+|#)\](\s|$)/;
  40. var reCitation = /^\s*\[[A-Za-z][\w-]*\](\s|$)/;
  41. var reFootnoteRef = /^\[(\d+|#)\]_/;
  42. var reCitationRef = /^\[[A-Za-z][\w-]*\]_/;
  43. var reDirectiveMarker = /^\.\.(\s|$)/;
  44. var reVerbatimMarker = /^::\s*$/;
  45. var rePreInline = /^[-\s"([{</:]/;
  46. var rePostInline = /^[-\s`'")\]}>/:.,;!?\\_]/;
  47. var reEnumeratedList = /^\s*((\d+|[A-Za-z#])[.)]|\((\d+|[A-Z-a-z#])\))\s/;
  48. var reBulletedList = /^\s*[-\+\*]\s/;
  49. var reExamples = /^\s+(>>>|In \[\d+\]:)\s/;
  50. function normal(stream, state) {
  51. var ch, sol, i;
  52. if (stream.eat(/\\/)) {
  53. ch = stream.next();
  54. setNormal(state, ch);
  55. return null;
  56. }
  57. sol = stream.sol();
  58. if (sol && (ch = stream.eat(reSection))) {
  59. for (i = 0; stream.eat(ch); i++);
  60. if (i >= 3 && stream.match(/^\s*$/)) {
  61. setNormal(state, null);
  62. return 'section';
  63. } else {
  64. stream.backUp(i + 1);
  65. }
  66. }
  67. if (sol && stream.match(reDirectiveMarker)) {
  68. if (!stream.eol()) {
  69. setState(state, directive);
  70. }
  71. return 'directive-marker';
  72. }
  73. if (stream.match(reVerbatimMarker)) {
  74. if (!verbatimMode) {
  75. setState(state, verbatim);
  76. } else {
  77. var mode = verbatimMode;
  78. setState(state, verbatim, {
  79. mode: mode,
  80. local: mode.startState()
  81. });
  82. }
  83. return 'verbatim-marker';
  84. }
  85. if (sol && stream.match(reExamples, false)) {
  86. if (!pythonMode) {
  87. setState(state, verbatim);
  88. return 'verbatim-marker';
  89. } else {
  90. var mode = pythonMode;
  91. setState(state, verbatim, {
  92. mode: mode,
  93. local: mode.startState()
  94. });
  95. return null;
  96. }
  97. }
  98. if (sol && (stream.match(reEnumeratedList) ||
  99. stream.match(reBulletedList))) {
  100. setNormal(state, stream);
  101. return 'list';
  102. }
  103. function testBackward(re) {
  104. return sol || !state.ctx.back || re.test(state.ctx.back);
  105. }
  106. function testForward(re) {
  107. return stream.eol() || stream.match(re, false);
  108. }
  109. function testInline(re) {
  110. return stream.match(re) && testBackward(/\W/) && testForward(/\W/);
  111. }
  112. if (testInline(reFootnoteRef)) {
  113. setNormal(state, stream);
  114. return 'footnote';
  115. }
  116. if (testInline(reCitationRef)) {
  117. setNormal(state, stream);
  118. return 'citation';
  119. }
  120. ch = stream.next();
  121. if (testBackward(rePreInline)) {
  122. if ((ch === ':' || ch === '|') && stream.eat(/\S/)) {
  123. var token;
  124. if (ch === ':') {
  125. token = 'role';
  126. } else {
  127. token = 'replacement';
  128. }
  129. setState(state, inline, {
  130. ch: ch,
  131. wide: false,
  132. prev: null,
  133. token: token
  134. });
  135. return token;
  136. }
  137. if (ch === '*' || ch === '`') {
  138. var orig = ch,
  139. wide = false;
  140. ch = stream.next();
  141. if (ch == orig) {
  142. wide = true;
  143. ch = stream.next();
  144. }
  145. if (ch && !/\s/.test(ch)) {
  146. var token;
  147. if (orig === '*') {
  148. token = wide ? 'strong' : 'emphasis';
  149. } else {
  150. token = wide ? 'inline' : 'interpreted';
  151. }
  152. setState(state, inline, {
  153. ch: orig, // inline() has to know what to search for
  154. wide: wide, // are we looking for `ch` or `chch`
  155. prev: null, // terminator must not be preceeded with whitespace
  156. token: token // I don't want to recompute this all the time
  157. });
  158. return token;
  159. }
  160. }
  161. }
  162. setNormal(state, ch);
  163. return null;
  164. }
  165. function inline(stream, state) {
  166. var ch = stream.next(),
  167. token = state.ctx.token;
  168. function finish(ch) {
  169. state.ctx.prev = ch;
  170. return token;
  171. }
  172. if (ch != state.ctx.ch) {
  173. return finish(ch);
  174. }
  175. if (/\s/.test(state.ctx.prev)) {
  176. return finish(ch);
  177. }
  178. if (state.ctx.wide) {
  179. ch = stream.next();
  180. if (ch != state.ctx.ch) {
  181. return finish(ch);
  182. }
  183. }
  184. if (!stream.eol() && !rePostInline.test(stream.peek())) {
  185. if (state.ctx.wide) {
  186. stream.backUp(1);
  187. }
  188. return finish(ch);
  189. }
  190. setState(state, normal);
  191. setNormal(state, ch);
  192. return token;
  193. }
  194. function directive(stream, state) {
  195. var token = null;
  196. if (stream.match(reDirective)) {
  197. token = 'directive';
  198. } else if (stream.match(reHyperlink)) {
  199. token = 'hyperlink';
  200. } else if (stream.match(reFootnote)) {
  201. token = 'footnote';
  202. } else if (stream.match(reCitation)) {
  203. token = 'citation';
  204. } else {
  205. stream.eatSpace();
  206. if (stream.eol()) {
  207. setNormal(state, stream);
  208. return null;
  209. } else {
  210. stream.skipToEnd();
  211. setState(state, comment);
  212. return 'comment';
  213. }
  214. }
  215. setState(state, body, {start: true});
  216. return token;
  217. }
  218. function body(stream, state) {
  219. var token = 'body';
  220. if (!state.ctx.start || stream.sol()) {
  221. return block(stream, state, token);
  222. }
  223. stream.skipToEnd();
  224. setCtx(state);
  225. return token;
  226. }
  227. function comment(stream, state) {
  228. return block(stream, state, 'comment');
  229. }
  230. function verbatim(stream, state) {
  231. if (!verbatimMode) {
  232. return block(stream, state, 'verbatim');
  233. } else {
  234. if (stream.sol()) {
  235. if (!stream.eatSpace()) {
  236. setNormal(state, stream);
  237. }
  238. return null;
  239. }
  240. return verbatimMode.token(stream, state.ctx.local);
  241. }
  242. }
  243. function block(stream, state, token) {
  244. if (stream.eol() || stream.eatSpace()) {
  245. stream.skipToEnd();
  246. return token;
  247. } else {
  248. setNormal(state, stream);
  249. return null;
  250. }
  251. }
  252. return {
  253. startState: function() {
  254. return {fn: normal, ctx: {}};
  255. },
  256. copyState: function(state) {
  257. return {fn: state.fn, ctx: state.ctx};
  258. },
  259. token: function(stream, state) {
  260. var token = state.fn(stream, state);
  261. return token;
  262. }
  263. };
  264. });
  265. CodeMirror.defineMIME("text/x-rst", "rst");