rst.js 2.62 KB
CodeMirror.defineMode("rst",function(t,n){function e(t,n,e){t.fn=n,r(t,e)}function r(t,n){t.ctx=n||{}}function o(t,n){if(n&&"string"!=typeof n){var r=n.current();n=r[r.length-1]}e(t,u,{back:n})}function i(t){if(t){var n=CodeMirror.listModes();for(var e in n)if(n[e]==t)return!0}return!1}function c(n){return i(n)?CodeMirror.getMode(t,n):null}function u(t,n){function r(t){return s||!n.ctx.back||t.test(n.ctx.back)}function i(n){return t.eol()||t.match(n,!1)}function c(n){return t.match(n)&&r(/\W/)&&i(/\W/)}var u,s,l;if(t.eat(/\\/))return u=t.next(),o(n,u),null;if(s=t.sol(),s&&(u=t.eat(h))){for(l=0;t.eat(u);l++);if(l>=3&&t.match(/^\s*$/))return o(n,null),"header";t.backUp(l+1)}if(s&&t.match($))return t.eol()||e(n,f),"meta";if(t.match(g)){if(x){var m=x;e(n,d,{mode:m,local:m.startState()})}else e(n,d);return"meta"}if(s&&t.match(C,!1)){if(v){var m=v;return e(n,d,{mode:m,local:m.startState()}),null}return e(n,d),"meta"}if(c(M))return o(n,t),"footnote";if(c(S))return o(n,t),"citation";if(u=t.next(),r(y)){if((":"===u||"|"===u)&&t.eat(/\S/)){var p;return p=":"===u?"builtin":"atom",e(n,a,{ch:u,wide:!1,prev:null,token:p}),p}if("*"===u||"`"===u){var k=u,w=!1;if(u=t.next(),u==k&&(w=!0,u=t.next()),u&&!/\s/.test(u)){var p;return p="*"===k?w?"strong":"em":w?"string":"string-2",e(n,a,{ch:k,wide:w,prev:null,token:p}),p}}}return o(n,u),null}function a(t,n){function r(t){return n.ctx.prev=t,c}var i=t.next(),c=n.ctx.token;return i!=n.ctx.ch?r(i):/\s/.test(n.ctx.prev)?r(i):n.ctx.wide&&(i=t.next(),i!=n.ctx.ch)?r(i):t.eol()||_.test(t.peek())?(e(n,u),o(n,i),c):(n.ctx.wide&&t.backUp(1),r(i))}function f(t,n){var r=null;if(t.match(p))r="attribute";else if(t.match(k))r="link";else if(t.match(w))r="quote";else{if(!t.match(b))return t.eatSpace(),t.eol()?(o(n,t),null):(t.skipToEnd(),e(n,l),"comment");r="quote"}return e(n,s,{start:!0}),r}function s(t,n){var e="body";return!n.ctx.start||t.sol()?m(t,n,e):(t.skipToEnd(),r(n),e)}function l(t,n){return m(t,n,"comment")}function d(t,n){return x?t.sol()?(t.eatSpace()||o(n,t),null):x.token(t,n.ctx.local):m(t,n,"meta")}function m(t,n,e){return t.eol()||t.eatSpace()?(t.skipToEnd(),e):(o(n,t),null)}var x=c(n.verbatim),v=c("python"),h=/^[!"#$%&'()*+,-.\/:;<=>?@[\\\]^_`{|}~]/,p=/^\s*\w([-:.\w]*\w)?::(\s|$)/,k=/^\s*_[\w-]+:(\s|$)/,w=/^\s*\[(\d+|#)\](\s|$)/,b=/^\s*\[[A-Za-z][\w-]*\](\s|$)/,M=/^\[(\d+|#)\]_/,S=/^\[[A-Za-z][\w-]*\]_/,$=/^\.\.(\s|$)/,g=/^::\s*$/,y=/^[-\s"([{<\/:]/,_=/^[-\s`'")\]}>\/:.,;!?\\_]/,C=/^\s+(>>>|In \[\d+\]:)\s/;return{startState:function(){return{fn:u,ctx:{}}},copyState:function(t){return{fn:t.fn,ctx:t.ctx}},token:function(t,n){var e=n.fn(t,n);return e}}},"python"),CodeMirror.defineMIME("text/x-rst","rst");