Skip to content

Commit ea6382a

Browse files
committed
[tiddlywiki mode] Try to sanitize code a little
1 parent 420bdf1 commit ea6382a

File tree

1 file changed

+78
-123
lines changed

1 file changed

+78
-123
lines changed

mode/tiddlywiki/tiddlywiki.js

Lines changed: 78 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
! Info
1717
CoreVersion parameter is needed for TiddlyWiki only!
1818
***/
19-
//{{{
2019

2120
(function(mod) {
2221
if (typeof exports == "object" && typeof module == "object") // CommonJS
@@ -32,73 +31,60 @@ CodeMirror.defineMode("tiddlywiki", function () {
3231
// Tokenizer
3332
var textwords = {};
3433

35-
var keywords = function () {
36-
function kw(type) {
37-
return { type: type, style: "macro"};
38-
}
39-
return {
40-
"allTags": kw('allTags'), "closeAll": kw('closeAll'), "list": kw('list'),
41-
"newJournal": kw('newJournal'), "newTiddler": kw('newTiddler'),
42-
"permaview": kw('permaview'), "saveChanges": kw('saveChanges'),
43-
"search": kw('search'), "slider": kw('slider'), "tabs": kw('tabs'),
44-
"tag": kw('tag'), "tagging": kw('tagging'), "tags": kw('tags'),
45-
"tiddler": kw('tiddler'), "timeline": kw('timeline'),
46-
"today": kw('today'), "version": kw('version'), "option": kw('option'),
47-
48-
"with": kw('with'),
49-
"filter": kw('filter')
50-
};
51-
}();
34+
var keywords = {
35+
"allTags": true, "closeAll": true, "list": true,
36+
"newJournal": true, "newTiddler": true,
37+
"permaview": true, "saveChanges": true,
38+
"search": true, "slider": true, "tabs": true,
39+
"tag": true, "tagging": true, "tags": true,
40+
"tiddler": true, "timeline": true,
41+
"today": true, "version": true, "option": true,
42+
"with": true, "filter": true
43+
};
5244

5345
var isSpaceName = /[\w_\-]/i,
54-
reHR = /^\-\-\-\-+$/, // <hr>
55-
reWikiCommentStart = /^\/\*\*\*$/, // /***
56-
reWikiCommentStop = /^\*\*\*\/$/, // ***/
57-
reBlockQuote = /^<<<$/,
46+
reHR = /^\-\-\-\-+$/, // <hr>
47+
reWikiCommentStart = /^\/\*\*\*$/, // /***
48+
reWikiCommentStop = /^\*\*\*\/$/, // ***/
49+
reBlockQuote = /^<<<$/,
5850

59-
reJsCodeStart = /^\/\/\{\{\{$/, // //{{{ js block start
60-
reJsCodeStop = /^\/\/\}\}\}$/, // //}}} js stop
61-
reXmlCodeStart = /^<!--\{\{\{-->$/, // xml block start
62-
reXmlCodeStop = /^<!--\}\}\}-->$/, // xml stop
51+
reJsCodeStart = /^\/\/\{\{\{$/, // //{{{ js block start
52+
reJsCodeStop = /^\/\/\}\}\}$/, // //}}} js stop
53+
reXmlCodeStart = /^<!--\{\{\{-->$/, // xml block start
54+
reXmlCodeStop = /^<!--\}\}\}-->$/, // xml stop
6355

64-
reCodeBlockStart = /^\{\{\{$/, // {{{ TW text div block start
65-
reCodeBlockStop = /^\}\}\}$/, // }}} TW text stop
56+
reCodeBlockStart = /^\{\{\{$/, // {{{ TW text div block start
57+
reCodeBlockStop = /^\}\}\}$/, // }}} TW text stop
6658

67-
reUntilCodeStop = /.*?\}\}\}/;
59+
reUntilCodeStop = /.*?\}\}\}/;
6860

6961
function chain(stream, state, f) {
7062
state.tokenize = f;
7163
return f(stream, state);
7264
}
7365

74-
function jsTokenBase(stream, state) {
75-
var sol = stream.sol(), ch;
66+
function tokenBase(stream, state) {
67+
var sol = stream.sol(), ch = stream.peek();
7668

7769
state.block = false; // indicates the start of a code block.
7870

79-
ch = stream.peek(); // don't eat, to make matching simpler
80-
8171
// check start of blocks
8272
if (sol && /[<\/\*{}\-]/.test(ch)) {
8373
if (stream.match(reCodeBlockStart)) {
8474
state.block = true;
8575
return chain(stream, state, twTokenCode);
8676
}
87-
if (stream.match(reBlockQuote)) {
77+
if (stream.match(reBlockQuote))
8878
return 'quote';
89-
}
90-
if (stream.match(reWikiCommentStart) || stream.match(reWikiCommentStop)) {
79+
if (stream.match(reWikiCommentStart) || stream.match(reWikiCommentStop))
9180
return 'comment';
92-
}
93-
if (stream.match(reJsCodeStart) || stream.match(reJsCodeStop) || stream.match(reXmlCodeStart) || stream.match(reXmlCodeStop)) {
81+
if (stream.match(reJsCodeStart) || stream.match(reJsCodeStop) || stream.match(reXmlCodeStart) || stream.match(reXmlCodeStop))
9482
return 'comment';
95-
}
96-
if (stream.match(reHR)) {
83+
if (stream.match(reHR))
9784
return 'hr';
98-
}
99-
} // sol
100-
ch = stream.next();
85+
}
10186

87+
stream.next();
10288
if (sol && /[\/\*!#;:>|]/.test(ch)) {
10389
if (ch == "!") { // tw header
10490
stream.skipToEnd();
@@ -124,95 +110,77 @@ CodeMirror.defineMode("tiddlywiki", function () {
124110
stream.eatWhile(">");
125111
return "quote";
126112
}
127-
if (ch == '|') {
113+
if (ch == '|')
128114
return 'header';
129-
}
130115
}
131116

132-
if (ch == '{' && stream.match(/\{\{/)) {
117+
if (ch == '{' && stream.match(/\{\{/))
133118
return chain(stream, state, twTokenCode);
134-
}
135119

136120
// rudimentary html:// file:// link matching. TW knows much more ...
137-
if (/[hf]/i.test(ch)) {
138-
if (/[ti]/i.test(stream.peek()) && stream.match(/\b(ttps?|tp|ile):\/\/[\-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i)) {
139-
return "link";
140-
}
141-
}
121+
if (/[hf]/i.test(ch) &&
122+
/[ti]/i.test(stream.peek()) &&
123+
stream.match(/\b(ttps?|tp|ile):\/\/[\-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i))
124+
return "link";
125+
142126
// just a little string indicator, don't want to have the whole string covered
143-
if (ch == '"') {
127+
if (ch == '"')
144128
return 'string';
145-
}
146-
if (ch == '~') { // _no_ CamelCase indicator should be bold
129+
130+
if (ch == '~') // _no_ CamelCase indicator should be bold
147131
return 'brace';
148-
}
149-
if (/[\[\]]/.test(ch)) { // check for [[..]]
150-
if (stream.peek() == ch) {
151-
stream.next();
152-
return 'brace';
153-
}
154-
}
132+
133+
if (/[\[\]]/.test(ch) && stream.match(ch)) // check for [[..]]
134+
return 'brace';
135+
155136
if (ch == "@") { // check for space link. TODO fix @@...@@ highlighting
156137
stream.eatWhile(isSpaceName);
157138
return "link";
158139
}
140+
159141
if (/\d/.test(ch)) { // numbers
160142
stream.eatWhile(/\d/);
161143
return "number";
162144
}
145+
163146
if (ch == "/") { // tw invisible comment
164147
if (stream.eat("%")) {
165148
return chain(stream, state, twTokenComment);
166-
}
167-
else if (stream.eat("/")) { //
149+
} else if (stream.eat("/")) { //
168150
return chain(stream, state, twTokenEm);
169151
}
170152
}
171-
if (ch == "_") { // tw underline
172-
if (stream.eat("_")) {
153+
154+
if (ch == "_" && stream.eat("_")) // tw underline
173155
return chain(stream, state, twTokenUnderline);
174-
}
175-
}
156+
176157
// strikethrough and mdash handling
177-
if (ch == "-") {
178-
if (stream.eat("-")) {
179-
// if strikethrough looks ugly, change CSS.
180-
if (stream.peek() != ' ')
181-
return chain(stream, state, twTokenStrike);
182-
// mdash
183-
if (stream.peek() == ' ')
184-
return 'brace';
185-
}
186-
}
187-
if (ch == "'") { // tw bold
188-
if (stream.eat("'")) {
189-
return chain(stream, state, twTokenStrong);
190-
}
191-
}
192-
if (ch == "<") { // tw macro
193-
if (stream.eat("<")) {
194-
return chain(stream, state, twTokenMacro);
195-
}
196-
}
197-
else {
198-
return null;
158+
if (ch == "-" && 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 'brace';
199165
}
200166

167+
if (ch == "'" && stream.eat("'")) // tw bold
168+
return chain(stream, state, twTokenStrong);
169+
170+
if (ch == "<" && stream.eat("<")) // tw macro
171+
return chain(stream, state, twTokenMacro);
172+
201173
// core macro handling
202174
stream.eatWhile(/[\w\$_]/);
203-
var word = stream.current(),
204-
known = textwords.propertyIsEnumerable(word) && textwords[word];
205-
206-
return known ? known.style : null;
207-
} // jsTokenBase()
175+
return textwords.propertyIsEnumerable(stream.current()) ? "keyword" : null
176+
}
208177

209178
// tw invisible comment
210179
function twTokenComment(stream, state) {
211-
var maybeEnd = false,
212-
ch;
180+
var maybeEnd = false, ch;
213181
while (ch = stream.next()) {
214182
if (ch == "/" && maybeEnd) {
215-
state.tokenize = jsTokenBase;
183+
state.tokenize = tokenBase;
216184
break;
217185
}
218186
maybeEnd = (ch == "%");
@@ -226,7 +194,7 @@ CodeMirror.defineMode("tiddlywiki", function () {
226194
ch;
227195
while (ch = stream.next()) {
228196
if (ch == "'" && maybeEnd) {
229-
state.tokenize = jsTokenBase;
197+
state.tokenize = tokenBase;
230198
break;
231199
}
232200
maybeEnd = (ch == "'");
@@ -243,12 +211,12 @@ CodeMirror.defineMode("tiddlywiki", function () {
243211
}
244212

245213
if (!sb && stream.match(reUntilCodeStop)) {
246-
state.tokenize = jsTokenBase;
214+
state.tokenize = tokenBase;
247215
return "comment";
248216
}
249217

250218
if (sb && stream.sol() && stream.match(reCodeBlockStop)) {
251-
state.tokenize = jsTokenBase;
219+
state.tokenize = tokenBase;
252220
return "comment";
253221
}
254222

@@ -262,7 +230,7 @@ CodeMirror.defineMode("tiddlywiki", function () {
262230
ch;
263231
while (ch = stream.next()) {
264232
if (ch == "/" && maybeEnd) {
265-
state.tokenize = jsTokenBase;
233+
state.tokenize = tokenBase;
266234
break;
267235
}
268236
maybeEnd = (ch == "/");
@@ -276,7 +244,7 @@ CodeMirror.defineMode("tiddlywiki", function () {
276244
ch;
277245
while (ch = stream.next()) {
278246
if (ch == "_" && maybeEnd) {
279-
state.tokenize = jsTokenBase;
247+
state.tokenize = tokenBase;
280248
break;
281249
}
282250
maybeEnd = (ch == "_");
@@ -291,7 +259,7 @@ CodeMirror.defineMode("tiddlywiki", function () {
291259

292260
while (ch = stream.next()) {
293261
if (ch == "-" && maybeEnd) {
294-
state.tokenize = jsTokenBase;
262+
state.tokenize = tokenBase;
295263
break;
296264
}
297265
maybeEnd = (ch == "-");
@@ -301,53 +269,40 @@ CodeMirror.defineMode("tiddlywiki", function () {
301269

302270
// macro
303271
function twTokenMacro(stream, state) {
304-
var ch, word, known;
305-
306272
if (stream.current() == '<<') {
307273
return 'macro';
308274
}
309275

310-
ch = stream.next();
276+
var ch = stream.next();
311277
if (!ch) {
312-
state.tokenize = jsTokenBase;
278+
state.tokenize = tokenBase;
313279
return null;
314280
}
315281
if (ch == ">") {
316282
if (stream.peek() == '>') {
317283
stream.next();
318-
state.tokenize = jsTokenBase;
284+
state.tokenize = tokenBase;
319285
return "macro";
320286
}
321287
}
322288

323289
stream.eatWhile(/[\w\$_]/);
324-
word = stream.current();
325-
known = keywords.propertyIsEnumerable(word) && keywords[word];
326-
327-
return word;
290+
return keywords.propertyIsEnumerable(stream.current()) ? "keyword" : null
328291
}
329292

330293
// Interface
331294
return {
332295
startState: function () {
333-
return {
334-
tokenize: jsTokenBase,
335-
indented: 0,
336-
level: 0
337-
};
296+
return {tokenize: tokenBase};
338297
},
339298

340299
token: function (stream, state) {
341300
if (stream.eatSpace()) return null;
342301
var style = state.tokenize(stream, state);
343302
return style;
344-
},
345-
346-
electricChars: ""
303+
}
347304
};
348305
});
349306

350307
CodeMirror.defineMIME("text/x-tiddlywiki", "tiddlywiki");
351308
});
352-
353-
//}}}

0 commit comments

Comments
 (0)