tiddlywiki.js 3.64 KB
CodeMirror.defineMode("tiddlywiki",function(e,t){function r(e,t,r){return t.tokenize=r,r(e,t)}function n(e,t,r){return s=e,d=r,t}function i(e,t){var i,s=e.sol();if(t.block=!1,i=e.peek(),s&&/[<\/\*{}\-]/.test(i)){if(e.match(W))return t.block=!0,r(e,t,u);if(e.match(v))return n("quote","quote");if(e.match(x)||e.match(g))return n("code","comment");if(e.match(w)||e.match($)||e.match(y)||e.match(z))return n("code","comment");if(e.match(p))return n("hr","hr")}if(i=e.next(),s&&/[\/\*!#;:>|]/.test(i)){if("!"==i)return e.skipToEnd(),n("header","header");if("*"==i)return e.eatWhile("*"),n("list","comment");if("#"==i)return e.eatWhile("#"),n("list","comment");if(";"==i)return e.eatWhile(";"),n("list","comment");if(":"==i)return e.eatWhile(":"),n("list","comment");if(">"==i)return e.eatWhile(">"),n("quote","quote");if("|"==i)return n("table","header")}if("{"==i&&e.match(/\{\{/))return r(e,t,u);if(/[hf]/i.test(i)&&/[ti]/i.test(e.peek())&&e.match(/\b(ttps?|tp|ile):\/\/[\-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i))return n("link","link");if('"'==i)return n("string","string");if("~"==i)return n("text","brace");if(/[\[\]]/.test(i)&&e.peek()==i)return e.next(),n("brace","brace");if("@"==i)return e.eatWhile(b),n("link","link");if(/\d/.test(i))return e.eatWhile(/\d/),n("number","number");if("/"==i){if(e.eat("%"))return r(e,t,a);if(e.eat("/"))return r(e,t,c)}if("_"==i&&e.eat("_"))return r(e,t,l);if("-"==i&&e.eat("-")){if(" "!=e.peek())return r(e,t,f);if(" "==e.peek())return n("text","brace")}if("'"==i&&e.eat("'"))return r(e,t,o);if("<"!=i)return n(i);if(e.eat("<"))return r(e,t,m);e.eatWhile(/[\w\$_]/);var d=e.current(),k=h.propertyIsEnumerable(d)&&h[d];return k?n(k.type,k.style,d):n("text",null,d)}function a(e,t){for(var r,a=!1;r=e.next();){if("/"==r&&a){t.tokenize=i;break}a="%"==r}return n("comment","comment")}function o(e,t){for(var r,a=!1;r=e.next();){if("'"==r&&a){t.tokenize=i;break}a="'"==r}return n("text","strong")}function u(e,t){var r,a=t.block;return a&&e.current()?n("code","comment"):!a&&e.match(C)?(t.tokenize=i,n("code","comment")):a&&e.sol()&&e.match(_)?(t.tokenize=i,n("code","comment")):(r=e.next(),a?n("code","comment"):n("code","comment"))}function c(e,t){for(var r,a=!1;r=e.next();){if("/"==r&&a){t.tokenize=i;break}a="/"==r}return n("text","em")}function l(e,t){for(var r,a=!1;r=e.next();){if("_"==r&&a){t.tokenize=i;break}a="_"==r}return n("text","underlined")}function f(e,t){for(var r,a=!1;r=e.next();){if("-"==r&&a){t.tokenize=i;break}a="-"==r}return n("text","strikethrough")}function m(e,t){var r,a,o;return"<<"==e.current()?n("brace","macro"):(r=e.next())?">"==r&&">"==e.peek()?(e.next(),t.tokenize=i,n("brace","macro")):(e.eatWhile(/[\w\$_]/),a=e.current(),o=k.propertyIsEnumerable(a)&&k[a],o?n(o.type,o.style,a):n("macro",null,a)):(t.tokenize=i,n(r))}var s,d,h=(e.indentUnit,function(){return{}}()),k=function(){function e(e){return{type:e,style:"macro"}}return{allTags:e("allTags"),closeAll:e("closeAll"),list:e("list"),newJournal:e("newJournal"),newTiddler:e("newTiddler"),permaview:e("permaview"),saveChanges:e("saveChanges"),search:e("search"),slider:e("slider"),tabs:e("tabs"),tag:e("tag"),tagging:e("tagging"),tags:e("tags"),tiddler:e("tiddler"),timeline:e("timeline"),today:e("today"),version:e("version"),option:e("option"),with:e("with"),filter:e("filter")}}(),b=/[\w_\-]/i,p=/^\-\-\-\-+$/,x=/^\/\*\*\*$/,g=/^\*\*\*\/$/,v=/^<<<$/,w=/^\/\/\{\{\{$/,$=/^\/\/\}\}\}$/,y=/^<!--\{\{\{-->$/,z=/^<!--\}\}\}-->$/,W=/^\{\{\{$/,_=/^\}\}\}$/,C=/.*?\}\}\}/;return{startState:function(e){return{tokenize:i,indented:0,level:0}},token:function(e,t){if(e.eatSpace())return null;var r=t.tokenize(e,t);return r},electricChars:""}}),CodeMirror.defineMIME("text/x-tiddlywiki","tiddlywiki");