Debian Codegraph @

codemirror-legacy-modes/mode/tiddlywiki.js

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