1
0

tiki.js 8.5 KB

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