-
Notifications
You must be signed in to change notification settings - Fork 3
/
macros.js
339 lines (267 loc) · 7.21 KB
/
macros.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// Jonah macros
//
// These provide various facilities to stories.
//
// <<choice>>
version.extensions.choiceMacro = {major: 1, minor: 2, revision: 0};
macros['choice'] =
{
handler: function (place, macroName, params)
{
var link = document.createElement('a');
link.href = 'javascript:void(0)';
link.className = 'internalLink choice';
if (params[1])
link.innerHTML = params[1];
else
link.innerHTML = params[0];
link.onclick = function()
{
macros['choice'].activate(link, params[0]);
};
place.appendChild(link);
},
activate: function (el, destination)
{
// find the enclosing passage
var parentDiv = el.parentNode;
while (parentDiv.className.indexOf('body') == -1)
parentDiv = parentDiv.parentNode;
var title = parentDiv.parentNode.id.substr(7);
var links = parentDiv.getElementsByTagName('a');
var trashed = [];
for (var i = 0; i < links.length; i++)
if ((links[i] != el) && (links[i].className.indexOf('choice') != -1))
{
var span = document.createElement('span');
span.innerHTML = links[i].innerHTML;
span.className = 'disabled';
links[i].parentNode.insertBefore(span, links[i].nextSibling);
trashed.push(links[i]);
};
for (var i = 0; i < trashed.length; i++)
trashed[i].parentNode.removeChild(trashed[i]);
tale.get(title).text = '<html>' + parentDiv.childNodes[0].innerHTML + '</html>';
state.display(destination, el);
}
};
version.extensions.displayMacro = {major: 1, minor: 0, revision: 0};
// <<display>>
macros['display'] =
{
handler: function (place, name, params)
{
console.log('<<display>>ing "' + params[0] + '"');
new Wikifier(place, tale.get(params[0]).text);
console.log('<<display>> of "' + params[0] + '" complete');
}
};
// <<actions>>
version.extensions.actionsMacro = { major: 1, minor: 2, revision: 0 };
macros['actions'] =
{
handler: function (place, macroName, params)
{
var list = insertElement(place, 'ul');
if (! state.history[0].variables['actions clicked'])
state.history[0].variables['actions clicked'] = {};
for (var i = 0; i < params.length; i++)
{
if (state.history[0].variables['actions clicked'][params[i]])
continue;
var item = insertElement(list, 'li');
var link = Wikifier.createInternalLink(item, params[i]);
insertText(link, params[i]);
// rewrite the function in the link
link.onclick = function()
{
state.history[0].variables['actions clicked'][this.id] = true;
state.display(this.id, link);
};
};
}
};
// <<print>>
version.extensions.printMacro = { major: 1, minor: 1, revision: 0 };
macros['print'] =
{
handler: function (place, macroName, params, parser)
{
try
{
var output = eval(parser.fullArgs());
if (output)
new Wikifier(place, output.toString());
}
catch (e)
{
throwError(place, 'bad expression: ' + e.message);
}
}
};
// <<set>>
version.extensions.setMacro = { major: 1, minor: 1, revision: 0 };
macros['set'] =
{
handler: function (place, macroName, params, parser)
{
macros['set'].run(parser.fullArgs());
},
run: function (expression)
{
// you may call this directly from a script passage
try
{
return eval(Wikifier.parse(expression));
}
catch (e)
{
throwError(place, 'bad expression: ' + e.message);
};
}
};
// <<if>>, <<else>>, and <<endif>>
version.extensions['ifMacros'] = { major: 1, minor: 0, revision: 0};
macros['if'] =
{
handler: function (place, macroName, params, parser)
{
var condition = parser.fullArgs();
var srcOffset = parser.source.indexOf('>>', parser.matchStart) + 2;
var src = parser.source.slice(srcOffset);
var endPos = -1;
var trueClause = '';
var falseClause = '';
for (var i = 0, nesting = 1, currentClause = true; i < src.length; i++)
{
if (src.substr(i, 9) == '<<endif>>')
{
nesting--;
if (nesting == 0)
{
endPos = srcOffset + i + 9; // length of <<endif>>
break;
}
}
if ((src.substr(i, 8) == '<<else>>') && (nesting == 1))
{
currentClause = 'false';
i += 8;
}
if (src.substr(i, 5) == '<<if ')
nesting++;
if (currentClause == true)
trueClause += src.charAt(i);
else
falseClause += src.charAt(i);
};
// display the correct clause
try
{
if (eval(condition))
new Wikifier(place, trueClause.trim());
else
new Wikifier(place, falseClause.trim());
// push the parser past the entire expression
if (endPos != -1)
parser.nextMatch = endPos;
else
throwError(place, "can't find matching endif");
}
catch (e)
{
throwError(place, 'bad condition: ' + e.message);
};
}
};
macros['else'] = macros['endif'] = { handler: function() {} };
// <<remember>>
version.extensions.rememberMacro = {major: 1, minor: 1, revision: 0};
macros['remember'] =
{
handler: function (place, macroName, params, parser)
{
var statement = parser.fullArgs();
var expire = new Date();
var variable, value;
// evaluate the statement if any
macros['set'].run(statement);
// find the variable to save
var variableSigil = Wikifier.parse('$');
variableSigil = variableSigil.replace('[', '\\[');
variableSigil = variableSigil.replace(']', '\\]');
variable = statement.match(new RegExp(variableSigil + '(\\w+)', 'i'))[1];
value = eval(Wikifier.parse('$' + variable));
// simple JSON-like encoding
switch (typeof value)
{
case 'string':
value = '"' + value.replace(/"/g, '\\"') + '"';
break;
case 'number':
case 'boolean':
break;
default:
throwError(place, "can't remember $" + variable + ' (' + (typeof value) +
')');
return;
};
// save the variable as a cookie
expire.setYear(expire.getFullYear() + 1);
document.cookie = macros['remember'].prefix + variable +
'=' + value + '; expires=' + expire.toGMTString();
},
init: function()
{
// figure out our cookie prefix
if (tale.has('StoryTitle'))
macros['remember'].prefix = tale.get('StoryTitle').text + '_';
else
macros['remember'].prefix = '__jonah_';
// restore all cookie'd values to local variables
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++)
{
var bits = cookies[i].split('=');
if (bits[0].trim().indexOf(this.prefix) == 0)
{
// replace our cookie prefix with $ and evaluate the statement
var statement = cookies[i].replace(this.prefix, '$');
eval(Wikifier.parse(statement));
};
}
}
};
// <<silently>>
version.extensions['SilentlyMacro'] = { major: 1, minor: 0, revision: 0 };
macros['silently'] =
{
handler: function (place, macroName, params, parser)
{
var buffer = insertElement(null, 'div');
var srcOffset = parser.source.indexOf('>>', parser.matchStart) + 2;
var src = parser.source.slice(srcOffset);
var endPos = -1;
var silentText = '';
for (var i = 0; i < src.length; i++)
{
if (src.substr(i, 15) == '<<endsilently>>')
endPos = srcOffset + i + 15;
else
silentText += src.charAt(i);
};
if (endPos != -1)
{
new Wikifier(buffer, silentText);
parser.nextMatch = endPos;
}
else
throwError(place, "can't find matching endsilently");
delete buffer;
}
};
macros['endsilently'] =
{
handler: function() { }
};