tiddlywiki.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /***
  2. |''Name''|tiddlywiki.js|
  3. |''Description''|Enables TiddlyWikiy syntax highlighting using CodeMirror|
  4. |''Author''|PMario|
  5. |''Version''|0.1.7|
  6. |''Status''|''stable''|
  7. |''Source''|[[GitHub|https://github.com/pmario/CodeMirror2/blob/tw-syntax/mode/tiddlywiki]]|
  8. |''Documentation''|http://codemirror.tiddlyspace.com/|
  9. |''License''|[[MIT License|http://www.opensource.org/licenses/mit-license.php]]|
  10. |''CoreVersion''|2.5.0|
  11. |''Requires''|codemirror.js|
  12. |''Keywords''|syntax highlighting color code mirror codemirror|
  13. ! Info
  14. CoreVersion parameter is needed for TiddlyWiki only!
  15. ***/
  16. //{{{
  17. (function(mod) {
  18. if (typeof exports == "object" && typeof module == "object") // CommonJS
  19. mod(require("../../lib/codemirror"));
  20. else if (typeof define == "function" && define.amd) // AMD
  21. define(["../../lib/codemirror"], mod);
  22. else // Plain browser env
  23. mod(CodeMirror);
  24. })(function(CodeMirror) {
  25. "use strict";
  26. CodeMirror.defineMode("tiddlywiki", function () {
  27. // Tokenizer
  28. var textwords = {};
  29. var keywords = function () {
  30. function kw(type) {
  31. return { type: type, style: "macro"};
  32. }
  33. return {
  34. "allTags": kw('allTags'), "closeAll": kw('closeAll'), "list": kw('list'),
  35. "newJournal": kw('newJournal'), "newTiddler": kw('newTiddler'),
  36. "permaview": kw('permaview'), "saveChanges": kw('saveChanges'),
  37. "search": kw('search'), "slider": kw('slider'), "tabs": kw('tabs'),
  38. "tag": kw('tag'), "tagging": kw('tagging'), "tags": kw('tags'),
  39. "tiddler": kw('tiddler'), "timeline": kw('timeline'),
  40. "today": kw('today'), "version": kw('version'), "option": kw('option'),
  41. "with": kw('with'),
  42. "filter": kw('filter')
  43. };
  44. }();
  45. var isSpaceName = /[\w_\-]/i,
  46. reHR = /^\-\-\-\-+$/, // <hr>
  47. reWikiCommentStart = /^\/\*\*\*$/, // /***
  48. reWikiCommentStop = /^\*\*\*\/$/, // ***/
  49. reBlockQuote = /^<<<$/,
  50. reJsCodeStart = /^\/\/\{\{\{$/, // //{{{ js block start
  51. reJsCodeStop = /^\/\/\}\}\}$/, // //}}} js stop
  52. reXmlCodeStart = /^<!--\{\{\{-->$/, // xml block start
  53. reXmlCodeStop = /^<!--\}\}\}-->$/, // xml stop
  54. reCodeBlockStart = /^\{\{\{$/, // {{{ TW text div block start
  55. reCodeBlockStop = /^\}\}\}$/, // }}} TW text stop
  56. reUntilCodeStop = /.*?\}\}\}/;
  57. function chain(stream, state, f) {
  58. state.tokenize = f;
  59. return f(stream, state);
  60. }
  61. // Used as scratch variables to communicate multiple values without
  62. // consing up tons of objects.
  63. var type, content;
  64. function ret(tp, style, cont) {
  65. type = tp;
  66. content = cont;
  67. return style;
  68. }
  69. function jsTokenBase(stream, state) {
  70. var sol = stream.sol(), ch;
  71. state.block = false; // indicates the start of a code block.
  72. ch = stream.peek(); // don't eat, to make matching simpler
  73. // check start of blocks
  74. if (sol && /[<\/\*{}\-]/.test(ch)) {
  75. if (stream.match(reCodeBlockStart)) {
  76. state.block = true;
  77. return chain(stream, state, twTokenCode);
  78. }
  79. if (stream.match(reBlockQuote)) {
  80. return ret('quote', 'quote');
  81. }
  82. if (stream.match(reWikiCommentStart) || stream.match(reWikiCommentStop)) {
  83. return ret('code', 'comment');
  84. }
  85. if (stream.match(reJsCodeStart) || stream.match(reJsCodeStop) || stream.match(reXmlCodeStart) || stream.match(reXmlCodeStop)) {
  86. return ret('code', 'comment');
  87. }
  88. if (stream.match(reHR)) {
  89. return ret('hr', 'hr');
  90. }
  91. } // sol
  92. ch = stream.next();
  93. if (sol && /[\/\*!#;:>|]/.test(ch)) {
  94. if (ch == "!") { // tw header
  95. stream.skipToEnd();
  96. return ret("header", "header");
  97. }
  98. if (ch == "*") { // tw list
  99. stream.eatWhile('*');
  100. return ret("list", "comment");
  101. }
  102. if (ch == "#") { // tw numbered list
  103. stream.eatWhile('#');
  104. return ret("list", "comment");
  105. }
  106. if (ch == ";") { // definition list, term
  107. stream.eatWhile(';');
  108. return ret("list", "comment");
  109. }
  110. if (ch == ":") { // definition list, description
  111. stream.eatWhile(':');
  112. return ret("list", "comment");
  113. }
  114. if (ch == ">") { // single line quote
  115. stream.eatWhile(">");
  116. return ret("quote", "quote");
  117. }
  118. if (ch == '|') {
  119. return ret('table', 'header');
  120. }
  121. }
  122. if (ch == '{' && stream.match(/\{\{/)) {
  123. return chain(stream, state, twTokenCode);
  124. }
  125. // rudimentary html:// file:// link matching. TW knows much more ...
  126. if (/[hf]/i.test(ch)) {
  127. if (/[ti]/i.test(stream.peek()) && stream.match(/\b(ttps?|tp|ile):\/\/[\-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i)) {
  128. return ret("link", "link");
  129. }
  130. }
  131. // just a little string indicator, don't want to have the whole string covered
  132. if (ch == '"') {
  133. return ret('string', 'string');
  134. }
  135. if (ch == '~') { // _no_ CamelCase indicator should be bold
  136. return ret('text', 'brace');
  137. }
  138. if (/[\[\]]/.test(ch)) { // check for [[..]]
  139. if (stream.peek() == ch) {
  140. stream.next();
  141. return ret('brace', 'brace');
  142. }
  143. }
  144. if (ch == "@") { // check for space link. TODO fix @@...@@ highlighting
  145. stream.eatWhile(isSpaceName);
  146. return ret("link", "link");
  147. }
  148. if (/\d/.test(ch)) { // numbers
  149. stream.eatWhile(/\d/);
  150. return ret("number", "number");
  151. }
  152. if (ch == "/") { // tw invisible comment
  153. if (stream.eat("%")) {
  154. return chain(stream, state, twTokenComment);
  155. }
  156. else if (stream.eat("/")) { //
  157. return chain(stream, state, twTokenEm);
  158. }
  159. }
  160. if (ch == "_") { // tw underline
  161. if (stream.eat("_")) {
  162. return chain(stream, state, twTokenUnderline);
  163. }
  164. }
  165. // strikethrough and mdash handling
  166. if (ch == "-") {
  167. if (stream.eat("-")) {
  168. // if strikethrough looks ugly, change CSS.
  169. if (stream.peek() != ' ')
  170. return chain(stream, state, twTokenStrike);
  171. // mdash
  172. if (stream.peek() == ' ')
  173. return ret('text', 'brace');
  174. }
  175. }
  176. if (ch == "'") { // tw bold
  177. if (stream.eat("'")) {
  178. return chain(stream, state, twTokenStrong);
  179. }
  180. }
  181. if (ch == "<") { // tw macro
  182. if (stream.eat("<")) {
  183. return chain(stream, state, twTokenMacro);
  184. }
  185. }
  186. else {
  187. return ret(ch);
  188. }
  189. // core macro handling
  190. stream.eatWhile(/[\w\$_]/);
  191. var word = stream.current(),
  192. known = textwords.propertyIsEnumerable(word) && textwords[word];
  193. return known ? ret(known.type, known.style, word) : ret("text", null, word);
  194. } // jsTokenBase()
  195. // tw invisible comment
  196. function twTokenComment(stream, state) {
  197. var maybeEnd = false,
  198. ch;
  199. while (ch = stream.next()) {
  200. if (ch == "/" && maybeEnd) {
  201. state.tokenize = jsTokenBase;
  202. break;
  203. }
  204. maybeEnd = (ch == "%");
  205. }
  206. return ret("comment", "comment");
  207. }
  208. // tw strong / bold
  209. function twTokenStrong(stream, state) {
  210. var maybeEnd = false,
  211. ch;
  212. while (ch = stream.next()) {
  213. if (ch == "'" && maybeEnd) {
  214. state.tokenize = jsTokenBase;
  215. break;
  216. }
  217. maybeEnd = (ch == "'");
  218. }
  219. return ret("text", "strong");
  220. }
  221. // tw code
  222. function twTokenCode(stream, state) {
  223. var ch, sb = state.block;
  224. if (sb && stream.current()) {
  225. return ret("code", "comment");
  226. }
  227. if (!sb && stream.match(reUntilCodeStop)) {
  228. state.tokenize = jsTokenBase;
  229. return ret("code", "comment");
  230. }
  231. if (sb && stream.sol() && stream.match(reCodeBlockStop)) {
  232. state.tokenize = jsTokenBase;
  233. return ret("code", "comment");
  234. }
  235. ch = stream.next();
  236. return (sb) ? ret("code", "comment") : ret("code", "comment");
  237. }
  238. // tw em / italic
  239. function twTokenEm(stream, state) {
  240. var maybeEnd = false,
  241. ch;
  242. while (ch = stream.next()) {
  243. if (ch == "/" && maybeEnd) {
  244. state.tokenize = jsTokenBase;
  245. break;
  246. }
  247. maybeEnd = (ch == "/");
  248. }
  249. return ret("text", "em");
  250. }
  251. // tw underlined text
  252. function twTokenUnderline(stream, state) {
  253. var maybeEnd = false,
  254. ch;
  255. while (ch = stream.next()) {
  256. if (ch == "_" && maybeEnd) {
  257. state.tokenize = jsTokenBase;
  258. break;
  259. }
  260. maybeEnd = (ch == "_");
  261. }
  262. return ret("text", "underlined");
  263. }
  264. // tw strike through text looks ugly
  265. // change CSS if needed
  266. function twTokenStrike(stream, state) {
  267. var maybeEnd = false, ch;
  268. while (ch = stream.next()) {
  269. if (ch == "-" && maybeEnd) {
  270. state.tokenize = jsTokenBase;
  271. break;
  272. }
  273. maybeEnd = (ch == "-");
  274. }
  275. return ret("text", "strikethrough");
  276. }
  277. // macro
  278. function twTokenMacro(stream, state) {
  279. var ch, word, known;
  280. if (stream.current() == '<<') {
  281. return ret('brace', 'macro');
  282. }
  283. ch = stream.next();
  284. if (!ch) {
  285. state.tokenize = jsTokenBase;
  286. return ret(ch);
  287. }
  288. if (ch == ">") {
  289. if (stream.peek() == '>') {
  290. stream.next();
  291. state.tokenize = jsTokenBase;
  292. return ret("brace", "macro");
  293. }
  294. }
  295. stream.eatWhile(/[\w\$_]/);
  296. word = stream.current();
  297. known = keywords.propertyIsEnumerable(word) && keywords[word];
  298. if (known) {
  299. return ret(known.type, known.style, word);
  300. }
  301. else {
  302. return ret("macro", null, word);
  303. }
  304. }
  305. // Interface
  306. return {
  307. startState: function () {
  308. return {
  309. tokenize: jsTokenBase,
  310. indented: 0,
  311. level: 0
  312. };
  313. },
  314. token: function (stream, state) {
  315. if (stream.eatSpace()) return null;
  316. var style = state.tokenize(stream, state);
  317. return style;
  318. },
  319. electricChars: ""
  320. };
  321. });
  322. CodeMirror.defineMIME("text/x-tiddlywiki", "tiddlywiki");
  323. });
  324. //}}}