tiki.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. CodeMirror.defineMode('tiki', function(config) {
  2. function inBlock(style, terminator, returnTokenizer) {
  3. return function(stream, state) {
  4. while (!stream.eol()) {
  5. if (stream.match(terminator)) {
  6. state.tokenize = inText;
  7. break;
  8. }
  9. stream.next();
  10. }
  11. if (returnTokenizer) state.tokenize = returnTokenizer;
  12. return style;
  13. };
  14. }
  15. function inLine(style) {
  16. return function(stream, state) {
  17. while(!stream.eol()) {
  18. stream.next();
  19. }
  20. state.tokenize = inText;
  21. return style;
  22. };
  23. }
  24. function inText(stream, state) {
  25. function chain(parser) {
  26. state.tokenize = parser;
  27. return parser(stream, state);
  28. }
  29. var sol = stream.sol();
  30. var ch = stream.next();
  31. //non start of line
  32. switch (ch) { //switch is generally much faster than if, so it is used here
  33. case "{": //plugin
  34. stream.eat("/");
  35. stream.eatSpace();
  36. var tagName = "";
  37. var c;
  38. while ((c = stream.eat(/[^\s\u00a0=\"\'\/?(}]/))) tagName += c;
  39. state.tokenize = inPlugin;
  40. return "tag";
  41. break;
  42. case "_": //bold
  43. if (stream.eat("_")) {
  44. return chain(inBlock("strong", "__", inText));
  45. }
  46. break;
  47. case "'": //italics
  48. if (stream.eat("'")) {
  49. // Italic text
  50. return chain(inBlock("em", "''", inText));
  51. }
  52. break;
  53. case "(":// Wiki Link
  54. if (stream.eat("(")) {
  55. return chain(inBlock("variable-2", "))", inText));
  56. }
  57. break;
  58. case "[":// Weblink
  59. return chain(inBlock("variable-3", "]", inText));
  60. break;
  61. case "|": //table
  62. if (stream.eat("|")) {
  63. return chain(inBlock("comment", "||"));
  64. }
  65. break;
  66. case "-":
  67. if (stream.eat("=")) {//titleBar
  68. return chain(inBlock("header string", "=-", inText));
  69. } else if (stream.eat("-")) {//deleted
  70. return chain(inBlock("error tw-deleted", "--", inText));
  71. }
  72. break;
  73. case "=": //underline
  74. if (stream.match("==")) {
  75. return chain(inBlock("tw-underline", "===", inText));
  76. }
  77. break;
  78. case ":":
  79. if (stream.eat(":")) {
  80. return chain(inBlock("comment", "::"));
  81. }
  82. break;
  83. case "^": //box
  84. return chain(inBlock("tw-box", "^"));
  85. break;
  86. case "~": //np
  87. if (stream.match("np~")) {
  88. return chain(inBlock("meta", "~/np~"));
  89. }
  90. break;
  91. }
  92. //start of line types
  93. if (sol) {
  94. switch (ch) {
  95. case "!": //header at start of line
  96. if (stream.match('!!!!!')) {
  97. return chain(inLine("header string"));
  98. } else if (stream.match('!!!!')) {
  99. return chain(inLine("header string"));
  100. } else if (stream.match('!!!')) {
  101. return chain(inLine("header string"));
  102. } else if (stream.match('!!')) {
  103. return chain(inLine("header string"));
  104. } else {
  105. return chain(inLine("header string"));
  106. }
  107. break;
  108. case "*": //unordered list line item, or <li /> at start of line
  109. case "#": //ordered list line item, or <li /> at start of line
  110. case "+": //ordered list line item, or <li /> at start of line
  111. return chain(inLine("tw-listitem bracket"));
  112. break;
  113. }
  114. }
  115. //stream.eatWhile(/[&{]/); was eating up plugins, turned off to act less like html and more like tiki
  116. return null;
  117. }
  118. var indentUnit = config.indentUnit;
  119. // Return variables for tokenizers
  120. var pluginName, type;
  121. function inPlugin(stream, state) {
  122. var ch = stream.next();
  123. var peek = stream.peek();
  124. if (ch == "}") {
  125. state.tokenize = inText;
  126. //type = ch == ")" ? "endPlugin" : "selfclosePlugin"; inPlugin
  127. return "tag";
  128. } else if (ch == "(" || ch == ")") {
  129. return "bracket";
  130. } else if (ch == "=") {
  131. type = "equals";
  132. if (peek == ">") {
  133. ch = stream.next();
  134. peek = stream.peek();
  135. }
  136. //here we detect values directly after equal character with no quotes
  137. if (!/[\'\"]/.test(peek)) {
  138. state.tokenize = inAttributeNoQuote();
  139. }
  140. //end detect values
  141. return "operator";
  142. } else if (/[\'\"]/.test(ch)) {
  143. state.tokenize = inAttribute(ch);
  144. return state.tokenize(stream, state);
  145. } else {
  146. stream.eatWhile(/[^\s\u00a0=\"\'\/?]/);
  147. return "keyword";
  148. }
  149. }
  150. function inAttribute(quote) {
  151. return function(stream, state) {
  152. while (!stream.eol()) {
  153. if (stream.next() == quote) {
  154. state.tokenize = inPlugin;
  155. break;
  156. }
  157. }
  158. return "string";
  159. };
  160. }
  161. function inAttributeNoQuote() {
  162. return function(stream, state) {
  163. while (!stream.eol()) {
  164. var ch = stream.next();
  165. var peek = stream.peek();
  166. if (ch == " " || ch == "," || /[ )}]/.test(peek)) {
  167. state.tokenize = inPlugin;
  168. break;
  169. }
  170. }
  171. return "string";
  172. };
  173. }
  174. var curState, setStyle;
  175. function pass() {
  176. for (var i = arguments.length - 1; i >= 0; i--) curState.cc.push(arguments[i]);
  177. }
  178. function cont() {
  179. pass.apply(null, arguments);
  180. return true;
  181. }
  182. function pushContext(pluginName, startOfLine) {
  183. var noIndent = curState.context && curState.context.noIndent;
  184. curState.context = {
  185. prev: curState.context,
  186. pluginName: pluginName,
  187. indent: curState.indented,
  188. startOfLine: startOfLine,
  189. noIndent: noIndent
  190. };
  191. }
  192. function popContext() {
  193. if (curState.context) curState.context = curState.context.prev;
  194. }
  195. function element(type) {
  196. if (type == "openPlugin") {curState.pluginName = pluginName; return cont(attributes, endplugin(curState.startOfLine));}
  197. else if (type == "closePlugin") {
  198. var err = false;
  199. if (curState.context) {
  200. err = curState.context.pluginName != pluginName;
  201. popContext();
  202. } else {
  203. err = true;
  204. }
  205. if (err) setStyle = "error";
  206. return cont(endcloseplugin(err));
  207. }
  208. else if (type == "string") {
  209. if (!curState.context || curState.context.name != "!cdata") pushContext("!cdata");
  210. if (curState.tokenize == inText) popContext();
  211. return cont();
  212. }
  213. else return cont();
  214. }
  215. function endplugin(startOfLine) {
  216. return function(type) {
  217. if (
  218. type == "selfclosePlugin" ||
  219. type == "endPlugin"
  220. )
  221. return cont();
  222. if (type == "endPlugin") {pushContext(curState.pluginName, startOfLine); return cont();}
  223. return cont();
  224. };
  225. }
  226. function endcloseplugin(err) {
  227. return function(type) {
  228. if (err) setStyle = "error";
  229. if (type == "endPlugin") return cont();
  230. return pass();
  231. };
  232. }
  233. function attributes(type) {
  234. if (type == "keyword") {setStyle = "attribute"; return cont(attributes);}
  235. if (type == "equals") return cont(attvalue, attributes);
  236. return pass();
  237. }
  238. function attvalue(type) {
  239. if (type == "keyword") {setStyle = "string"; return cont();}
  240. if (type == "string") return cont(attvaluemaybe);
  241. return pass();
  242. }
  243. function attvaluemaybe(type) {
  244. if (type == "string") return cont(attvaluemaybe);
  245. else return pass();
  246. }
  247. return {
  248. startState: function() {
  249. return {tokenize: inText, cc: [], indented: 0, startOfLine: true, pluginName: null, context: null};
  250. },
  251. token: function(stream, state) {
  252. if (stream.sol()) {
  253. state.startOfLine = true;
  254. state.indented = stream.indentation();
  255. }
  256. if (stream.eatSpace()) return null;
  257. setStyle = type = pluginName = null;
  258. var style = state.tokenize(stream, state);
  259. if ((style || type) && style != "comment") {
  260. curState = state;
  261. while (true) {
  262. var comb = state.cc.pop() || element;
  263. if (comb(type || style)) break;
  264. }
  265. }
  266. state.startOfLine = false;
  267. return setStyle || style;
  268. },
  269. indent: function(state, textAfter) {
  270. var context = state.context;
  271. if (context && context.noIndent) return 0;
  272. if (context && /^{\//.test(textAfter))
  273. context = context.prev;
  274. while (context && !context.startOfLine)
  275. context = context.prev;
  276. if (context) return context.indent + indentUnit;
  277. else return 0;
  278. },
  279. electricChars: "/"
  280. };
  281. });
  282. CodeMirror.defineMIME("text/tiki", "tiki");