You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tiki.js 8.3KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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. stream.eatWhile(/[^\s\u00a0=\"\'\/?(}]/);
  48. state.tokenize = inPlugin;
  49. return "tag";
  50. case "_": //bold
  51. if (stream.eat("_"))
  52. return chain(inBlock("strong", "__", inText));
  53. break;
  54. case "'": //italics
  55. if (stream.eat("'"))
  56. return chain(inBlock("em", "''", inText));
  57. break;
  58. case "(":// Wiki Link
  59. if (stream.eat("("))
  60. return chain(inBlock("variable-2", "))", inText));
  61. break;
  62. case "[":// Weblink
  63. return chain(inBlock("variable-3", "]", inText));
  64. break;
  65. case "|": //table
  66. if (stream.eat("|"))
  67. return chain(inBlock("comment", "||"));
  68. break;
  69. case "-":
  70. if (stream.eat("=")) {//titleBar
  71. return chain(inBlock("header string", "=-", inText));
  72. } else if (stream.eat("-")) {//deleted
  73. return chain(inBlock("error tw-deleted", "--", inText));
  74. }
  75. break;
  76. case "=": //underline
  77. if (stream.match("=="))
  78. return chain(inBlock("tw-underline", "===", inText));
  79. break;
  80. case ":":
  81. if (stream.eat(":"))
  82. return chain(inBlock("comment", "::"));
  83. break;
  84. case "^": //box
  85. return chain(inBlock("tw-box", "^"));
  86. break;
  87. case "~": //np
  88. if (stream.match("np~"))
  89. return chain(inBlock("meta", "~/np~"));
  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. 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");
  283. });