/
redcloth_scan.java.rl
577 lines (478 loc) · 18 KB
/
redcloth_scan.java.rl
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
* redcloth_scan.java.rl
*
* Copyright (C) 2008 Jason Garber
*/
import java.io.IOException;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyClass;
import org.jruby.RubyHash;
import org.jruby.RubyModule;
import org.jruby.RubyNumeric;
import org.jruby.RubyObject;
import org.jruby.RubyString;
import org.jruby.RubySymbol;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.Arity;
import org.jruby.runtime.Block;
import org.jruby.runtime.CallbackFactory;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.exceptions.RaiseException;
import org.jruby.runtime.load.BasicLibraryService;
import org.jruby.util.ByteList;
public class RedclothScanService implements BasicLibraryService {
public static class Base {
public void CLEAR_LIST() {
list_layout = runtime.newArray();
}
public void LIST_ITEM() {
int aint = 0;
IRubyObject aval = ((RubyArray)list_index).entry(nest-1);
if(!aval.isNil()) { aint = RubyNumeric.fix2int(aval); }
if(list_type.equals("ol")) {
((RubyArray)list_index).store(nest-1, runtime.newFixnum(aint + 1));
}
if(nest > ((RubyArray)list_layout).getLength()) {
listm = list_type + "_open";
if(list_continue == 1) {
list_continue = 0;
((RubyHash)regs).aset(runtime.newSymbol("start"), ((RubyArray)list_index).entry(nest-1));
} else {
IRubyObject start = ((RubyHash)regs).aref(runtime.newSymbol("start"));
if(start.isNil()) {
((RubyArray)list_index).store(nest-1, runtime.newFixnum(1));
} else {
IRubyObject start_num = start.callMethod(runtime.getCurrentContext(), "to_i");
((RubyArray)list_index).store(nest-1, start_num);
}
}
((RubyHash)regs).aset(runtime.newSymbol("nest"), runtime.newFixnum(nest));
((RubyString)html).append(self.callMethod(runtime.getCurrentContext(), listm, regs));
((RubyArray)list_layout).store(nest-1, runtime.newString(list_type));
regs = RubyHash.newHash(runtime);
ASET("first", "true");
}
LIST_CLOSE();
((RubyHash)regs).aset(runtime.newSymbol("nest"), ((RubyArray)list_layout).length());
ASET("type", "li_open");
}
public void LIST_CLOSE() {
while(nest < ((RubyArray)list_layout).getLength()) {
((RubyHash)regs).aset(runtime.newSymbol("nest"), ((RubyArray)list_layout).length());
IRubyObject end_list = ((RubyArray)list_layout).pop(runtime.getCurrentContext());
if(!end_list.isNil()) {
String s = end_list.convertToString().toString();
listm = s + "_close";
((RubyString)html).append(self.callMethod(runtime.getCurrentContext(), listm, regs));
}
}
}
public void TRANSFORM(String T) {
if(p > reg && reg >= ts) {
IRubyObject str = RedclothScanService.transform(self, data, reg, p-reg, refs);
((RubyHash)regs).aset(runtime.newSymbol(T), str);
} else {
((RubyHash)regs).aset(runtime.newSymbol(T), runtime.getNil());
}
}
public IRubyObject red_pass(IRubyObject self, IRubyObject regs, IRubyObject ref, String meth, IRubyObject refs) {
IRubyObject txt = ((RubyHash)regs).aref(ref);
if(!txt.isNil()) {
((RubyHash)regs).aset(ref, inline2(self, txt, refs));
}
return self.callMethod(self.getRuntime().getCurrentContext(), meth, regs);
}
public void PASS(IRubyObject H, String A, String T) {
((RubyString)H).append(red_pass(self, regs, runtime.newSymbol(A), T, refs));
}
public void STORE_LINK_ALIAS() {
((RubyHash)refs_found).aset(((RubyHash)regs).aref(runtime.newSymbol("text")), ((RubyHash)regs).aref(runtime.newSymbol("href")));
}
public void STORE_URL(String T) {
if(p > reg && reg >= ts) {
boolean punct = true;
while(p > reg && punct) {
switch(data[p - 1]) {
case ')':
int tempP = p - 1;
int level = -1;
while(tempP > reg) {
switch(data[tempP - 1]) {
case '(': ++level; break;
case ')': --level; break;
}
--tempP;
}
if (level == 0) { punct = false; } else { --p; }
break;
case '!': case '"': case '#': case '$': case '%': case ']': case '[': case '&': case '\'':
case '*': case '+': case ',': case '-': case '.': case '(': case ':':
case ';': case '=': case '?': case '@': case '\\': case '^': case '_':
case '`': case '|': case '~': p--; break;
default: punct = false;
}
}
te = p;
}
STORE(T);
if(!refs.isNil() && refs.callMethod(runtime.getCurrentContext(), "has_key?", ((RubyHash)regs).aref(runtime.newSymbol(T))).isTrue()) {
((RubyHash)regs).aset(runtime.newSymbol(T), ((RubyHash)refs).aref(((RubyHash)regs).aref(runtime.newSymbol(T))));
}
}
public void red_inc(IRubyObject regs, IRubyObject ref) {
int aint = 0;
IRubyObject aval = ((RubyHash)regs).aref(ref);
if(!aval.isNil()) {
aint = RubyNumeric.fix2int(aval);
}
((RubyHash)regs).aset(ref, regs.getRuntime().newFixnum(aint+1));
}
public IRubyObject red_blockcode(IRubyObject self, IRubyObject regs, IRubyObject block) {
Ruby runtime = self.getRuntime();
IRubyObject btype = ((RubyHash)regs).aref(runtime.newSymbol("type"));
if(((RubyString)block).getByteList().realSize > 0) {
((RubyHash)regs).aset(runtime.newSymbol("text"), block);
block = self.callMethod(runtime.getCurrentContext(), btype.asJavaString(), regs);
}
return block;
}
public IRubyObject red_block(IRubyObject self, IRubyObject regs, IRubyObject block, IRubyObject refs) {
Ruby runtime = self.getRuntime();
RubySymbol method;
IRubyObject sym_text = runtime.newSymbol("text");
IRubyObject btype = ((RubyHash)regs).aref(runtime.newSymbol("type"));
block = block.callMethod(runtime.getCurrentContext(), "strip");
if(!block.isNil() && !btype.isNil()) {
method = btype.convertToString().intern();
if(method == runtime.newSymbol("notextile")) {
((RubyHash)regs).aset(sym_text, block);
} else {
((RubyHash)regs).aset(sym_text, inline2(self, block, refs));
}
IRubyObject customTags = ((RubyObject)self).getInstanceVariable("@custom_tags");
if( ((RubyArray)customTags).includes(runtime.getCurrentContext(), btype) ) {
block = self.callMethod(runtime.getCurrentContext(), method.asJavaString(), regs);
} else {
IRubyObject fallback = ((RubyHash)regs).aref(runtime.newSymbol("fallback"));
if(!fallback.isNil()) {
((RubyString)fallback).append(((RubyHash)regs).aref(sym_text));
regs = RubyHash.newHash(runtime);
((RubyHash)regs).aset(sym_text, fallback);
}
block = self.callMethod(runtime.getCurrentContext(), "p", regs);
}
}
return block;
}
public void strCatEscaped(IRubyObject self, IRubyObject str, byte[] data, int ts, int te) {
IRubyObject sourceStr = RubyString.newString(self.getRuntime(), data, ts, te-ts);
IRubyObject escapedStr = self.callMethod(self.getRuntime().getCurrentContext(), "escape", sourceStr);
((RubyString)str).concat(escapedStr);
}
public void strCatEscapedForPreformatted(IRubyObject self, IRubyObject str, byte[] data, int ts, int te) {
IRubyObject sourceStr = RubyString.newString(self.getRuntime(), data, ts, te-ts);
IRubyObject escapedStr = self.callMethod(self.getRuntime().getCurrentContext(), "escape_pre", sourceStr);
((RubyString)str).concat(escapedStr);
}
public void CLEAR(IRubyObject obj) {
if(block == obj) {
block = RubyString.newEmptyString(runtime);
} else if(html == obj) {
html = RubyString.newEmptyString(runtime);
} else if(table == obj) {
table = RubyString.newEmptyString(runtime);
}
}
public void ADD_BLOCK() {
((RubyString)html).append(red_block(self, regs, block, refs));
extend = runtime.getNil();
CLEAR(block);
CLEAR_REGS();
}
public void CLEAR_REGS() {
regs = RubyHash.newHash(runtime);
}
public void RESET_REG() {
reg = -1;
}
public void CAT(IRubyObject H) {
((RubyString)H).cat(data, ts, te-ts);
}
public void SET_PLAIN_BLOCK(String T) {
plain_block = runtime.newString(T);
}
public void RESET_TYPE() {
((RubyHash)regs).aset(runtime.newSymbol("type"), plain_block);
}
public void INLINE(IRubyObject H, String T) {
((RubyString)H).append(self.callMethod(runtime.getCurrentContext(), T, regs));
}
public void RSTRIP_BANG(IRubyObject H) {
((RubyString)H).callMethod(runtime.getCurrentContext(), "rstrip!");
}
public void DONE(IRubyObject H) {
((RubyString)html).append(H);
CLEAR(H);
CLEAR_REGS();
}
public void ADD_EXTENDED_BLOCK() {
((RubyString)html).append(red_block(self, regs, block, refs));
CLEAR(block);
}
public void ADD_BLOCKCODE() {
((RubyString)html).append(red_blockcode(self, regs, block));
CLEAR(block);
CLEAR_REGS();
}
public void ADD_EXTENDED_BLOCKCODE() {
((RubyString)html).append(red_blockcode(self, regs, block));
CLEAR(block);
}
public void AINC(String T) {
red_inc(regs, runtime.newSymbol(T));
}
public void END_EXTENDED() {
extend = runtime.getNil();
CLEAR_REGS();
}
public boolean IS_NOT_EXTENDED() {
return extend.isNil();
}
public void ASET(String T, String V) {
((RubyHash)regs).aset(runtime.newSymbol(T), runtime.newString(V));
}
public void STORE(String T) {
if(p > reg && reg >= ts) {
IRubyObject str = RubyString.newString(runtime, data, reg, p-reg);
((RubyHash)regs).aset(runtime.newSymbol(T), str);
} else {
((RubyHash)regs).aset(runtime.newSymbol(T), runtime.getNil());
}
}
public void STORE_B(String T) {
if(p > bck && bck >= ts) {
IRubyObject str = RubyString.newString(runtime, data, bck, p-bck);
((RubyHash)regs).aset(runtime.newSymbol(T), str);
} else {
((RubyHash)regs).aset(runtime.newSymbol(T), runtime.getNil());
}
}
public IRubyObject self;
public byte[] data;
public int p, pe;
public IRubyObject refs;
public Ruby runtime;
public int orig_p, orig_pe;
public int cs, act, nest;
public int ts = -1;
public int te = -1;
public int reg = -1;
public int bck = -1;
public int eof = -1;
public IRubyObject html;
public IRubyObject table;
public IRubyObject block;
public IRubyObject regs;
public IRubyObject list_layout;
public String list_type = null;
public IRubyObject list_index;
public int list_continue = 0;
public IRubyObject plain_block;
public IRubyObject extend;
public String listm = "";
public IRubyObject refs_found;
}
private static class Transformer extends Base {
%%{
machine redcloth_scan;
include redcloth_common "redcloth_common.java.rl";
action extend { extend = ((RubyHash)regs).aref(runtime.newSymbol("type")); }
include redcloth_scan "redcloth_scan.rl";
}%%
%% write data nofinal;
public Transformer(IRubyObject self, byte[] data, int p, int pe, IRubyObject refs) {
if(p+pe > data.length) {
throw new RuntimeException("BLAHAHA");
}
this.self = self;
// This is GROSS but necessary for EOF matching
this.data = new byte[pe+1];
System.arraycopy(data, p, this.data, 0, pe);
this.data[pe] = 0;
this.p = 0;
this.pe = pe+1;
this.eof = this.pe;
this.orig_p = 0;
this.orig_pe = this.pe;
this.refs = refs;
runtime = self.getRuntime();
html = RubyString.newEmptyString(runtime);
table = RubyString.newEmptyString(runtime);
block = RubyString.newEmptyString(runtime);
CLEAR_REGS();
list_layout = runtime.getNil();
list_index = runtime.newArray();
SET_PLAIN_BLOCK("p");
extend = runtime.getNil();
refs_found = RubyHash.newHash(runtime);
}
public IRubyObject transform() {
%% write init;
%% write exec;
if(((RubyString)block).getByteList().realSize > 0) {
ADD_BLOCK();
}
if(refs.isNil() && !refs_found.callMethod(runtime.getCurrentContext(), "empty?").isTrue()) {
return RedclothScanService.transform(self, data, orig_p, orig_pe, refs_found);
} else {
self.callMethod(self.getRuntime().getCurrentContext(), "after_transform", html);
return html;
}
}
}
public static IRubyObject transform(IRubyObject self, byte[] data, int p, int pe, IRubyObject refs) {
return new Transformer(self, data, p, pe, refs).transform();
}
public static IRubyObject inline2(IRubyObject workingCopy, IRubyObject self, IRubyObject refs) {
return RedclothInline.inline2(workingCopy, self, refs);
}
public static IRubyObject transform2(IRubyObject self, IRubyObject str) {
RubyString ss = str.convertToString();
self.callMethod(self.getRuntime().getCurrentContext(), "before_transform", ss);
return transform(self, ss.getByteList().bytes(), ss.getByteList().begin, ss.getByteList().realSize, self.getRuntime().getNil());
}
@JRubyMethod
public static IRubyObject to(IRubyObject self, IRubyObject formatter) {
Ruby runtime = self.getRuntime();
self.callMethod(runtime.getCurrentContext(), "delete!", runtime.newString("\r"));
IRubyObject workingCopy = self.rbClone();
((RubyObject)workingCopy).extend(new IRubyObject[]{formatter});
IRubyObject workingCopyMethods = workingCopy.callMethod(runtime.getCurrentContext(), "methods");
IRubyObject classInstanceMethods = workingCopy.getType().callMethod(runtime.getCurrentContext(), "instance_methods");
IRubyObject customTags = workingCopyMethods.callMethod(runtime.getCurrentContext(), "-", classInstanceMethods);
((RubyObject)workingCopy).setInstanceVariable("@custom_tags", customTags);
if(workingCopy.callMethod(runtime.getCurrentContext(), "lite_mode").isTrue()) {
return inline2(workingCopy, self, RubyHash.newHash(runtime));
} else {
return transform2(workingCopy, self);
}
}
@JRubyMethod(rest=true)
public static IRubyObject html_esc(IRubyObject self, IRubyObject[] args) {
Ruby runtime = self.getRuntime();
IRubyObject str = runtime.getNil(),
level = runtime.getNil();
if(Arity.checkArgumentCount(runtime, args, 1, 2) == 2) {
level = args[1];
}
str = args[0];
IRubyObject new_str = RubyString.newEmptyString(runtime);
if(str.isNil()) {
return new_str;
}
ByteList bl = str.convertToString().getByteList();
if(bl.realSize == 0) {
return new_str;
}
byte[] bytes = bl.bytes;
int ts = bl.begin;
int te = ts + bl.realSize;
int t = ts, t2 = ts;
String ch = null;
if(te <= ts) {
return new_str;
}
while(t2 < te) {
ch = null;
// normal + pre
switch(bytes[t2]) {
case '&': ch = "amp"; break;
case '>': ch = "gt"; break;
case '<': ch = "lt"; break;
}
// normal (non-pre)
if(level != runtime.newSymbol("html_escape_preformatted")) {
switch(bytes[t2]) {
case '\n': ch = "br"; break;
case '"' : ch = "quot"; break;
case '\'':
ch = (level == runtime.newSymbol("html_escape_attributes")) ? "apos" : "squot";
break;
}
}
if(ch != null) {
if(t2 > t) {
((RubyString)new_str).cat(bytes, t, t2-t);
}
((RubyString)new_str).concat(self.callMethod(runtime.getCurrentContext(), ch, RubyHash.newHash(runtime)));
t = t2 + 1;
}
t2++;
}
if(t2 > t) {
((RubyString)new_str).cat(bytes, t, t2-t);
}
return new_str;
}
@JRubyMethod
public static IRubyObject latex_esc(IRubyObject self, IRubyObject str) {
Ruby runtime = self.getRuntime();
IRubyObject new_str = RubyString.newEmptyString(runtime);
if(str.isNil()) {
return new_str;
}
ByteList bl = str.convertToString().getByteList();
if(bl.realSize == 0) {
return new_str;
}
byte[] bytes = bl.bytes;
int ts = bl.begin;
int te = ts + bl.realSize;
int t = ts;
int t2 = ts;
String ch = null;
while(t2 < te) {
ch = null;
switch(bytes[t2]) {
case '{': ch = "#123"; break;
case '}': ch = "#125"; break;
case '\\': ch = "#92"; break;
case '#': ch = "#35"; break;
case '$': ch = "#36"; break;
case '%': ch = "#37"; break;
case '&': ch = "amp"; break;
case '_': ch = "#95"; break;
case '^': ch = "circ"; break;
case '~': ch = "tilde"; break;
case '<': ch = "lt"; break;
case '>': ch = "gt"; break;
case '\n': ch = "#10"; break;
}
if(ch != null) {
if(t2 > t) {
((RubyString)new_str).cat(bytes, t, t2-t);
}
IRubyObject opts = RubyHash.newHash(runtime);
((RubyHash)opts).aset(runtime.newSymbol("text"), runtime.newString(ch));
((RubyString)new_str).concat(self.callMethod(runtime.getCurrentContext(), "entity", opts));
t = t2 + 1;
}
t2++;
}
if(t2 > t) {
((RubyString)new_str).cat(bytes, t, t2-t);
}
return new_str;
}
public boolean basicLoad(final Ruby runtime) throws IOException {
Init_redcloth_scan(runtime);
return true;
}
public static void Init_redcloth_scan(Ruby runtime) {
RubyModule mRedCloth = runtime.defineModule("RedCloth");
RubyClass super_RedCloth = mRedCloth.defineClassUnder("TextileDoc", runtime.getString(), runtime.getString().getAllocator());
super_RedCloth.defineAnnotatedMethods(RedclothScanService.class);
super_RedCloth.defineClassUnder("ParseError",runtime.getClass("Exception"),runtime.getClass("Exception").getAllocator());
}
}