tiddlywiki.js 9.5 KB

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