/
redcloth_scan.rb.rl
412 lines (380 loc) · 10.2 KB
/
redcloth_scan.rb.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
#
# redcloth_scan.rb.rl
#
# Copyright (C) 2009 Jason Garber
#
%%{
machine redcloth_scan;
include redcloth_common "redcloth_common.rb.rl";
action extend { @extend = @regs[:type] }
include redcloth_scan "redcloth_scan.rl";
}%%
module RedCloth
EXTENSION_LANGUAGE = "Ruby"
class TextileDoc < String
def to(formatter)
self.delete!("\r")
working_copy = self.clone
working_copy.extend(formatter)
if (working_copy.lite_mode)
return working_copy.redcloth_inline2(self, {})
else
return working_copy.redcloth_transform2(self)
end
end
class ParseError < Exception; end
def redcloth_transform2(textile_doc)
before_transform(textile_doc)
return RedCloth::RedclothScan.transform(self, textile_doc, nil)
end
def redcloth_inline2(textile_doc, refs)
return RedCloth::RedclothInline.redcloth_inline2(self, textile_doc, refs)
end
def html_esc(input, level=nil)
return "" if input.nil? || input.empty?
str = input.dup
str.gsub!('&') { amp({}) }
str.gsub!('>') { gt({}) }
str.gsub!('<') { lt({}) }
if (level != :html_escape_preformatted)
str.gsub!("\n") { br({}) }
str.gsub!('"') { quot({}) }
str.gsub!("'") { level == :html_escape_attributes ? apos({}) : squot({}) }
end
return str;
end
LATEX_ESCAPE_CHARACTERS = {
'{' => "#123",
'}' => "#125",
'\\' => "#92",
'#' => "#35",
'$' => "#36",
'%' => "#37",
'&' => "amp",
'_' => "#95",
'^' => "circ",
'~' => "tilde",
'<' => "lt",
'>' => "gt",
'\n'=> "#10"
}
def latex_esc(str)
return "" if str.nil? || str.empty?
ch_regex = Regexp.new(LATEX_ESCAPE_CHARACTERS.keys.map {|ch| Regexp.escape(ch) }.join("|"))
str.gsub(ch_regex) {|ch| entity({:text => LATEX_ESCAPE_CHARACTERS[ch]}) }
end
end
class BaseScanner
attr_accessor :p, :pe, :refs
attr_reader :data
attr_accessor :orig_data, :cs, :act, :ts, :te, :reg, :bck, :eof,
:html, :table, :block, :regs
attr_accessor :list_layout, :list_type, :list_index, :list_continue, :listm,
:refs_found, :plain_block
def STR_NEW(p,n)
@data[p, n]
end
def MARK()
@reg = @p
end
def MARK_B()
@bck = @p
end
def CLEAR_REGS()
@regs = {}
end
def RESET_REG()
@reg = nil
end
def CAT(h)
h << @data[@ts, @te - @ts]
end
def CLEAR(h)
h.replace("")
end
def RSTRIP_BANG(h)
h.rstrip!
end
def SET_PLAIN_BLOCK(t)
@plain_block = t
end
def RESET_TYPE()
@regs[:type] = @plain_block
end
def INLINE(h, t)
h << @textile_doc.send(t, @regs)
end
def DONE(h)
@html << h
CLEAR(h)
CLEAR_REGS()
end
def PASS(h, a, t)
h << red_pass(@regs, a.to_sym, t, @refs)
end
def PARSE_ATTR(a)
red_parse_attr(@regs, a)
end
def PARSE_LINK_ATTR(a)
red_parse_link_attr(@regs, a)
end
def PARSE_IMAGE_ATTR(a)
red_parse_image_attr(@regs, a)
end
def PASS_CODE(h, a, t)
h << red_pass_code(@regs, a, t)
end
def ADD_BLOCK()
@html << red_block(@regs, @block, @refs)
@extend = nil
CLEAR(@block)
CLEAR_REGS()
end
def ADD_EXTENDED_BLOCK()
@html << red_block(@regs, @block, @refs)
CLEAR(@block)
end
def END_EXTENDED()
@extend = nil
CLEAR_REGS()
end
def ADD_BLOCKCODE()
@html << red_blockcode(@regs, @block)
CLEAR(@block)
CLEAR_REGS()
end
def ADD_EXTENDED_BLOCKCODE()
@html << red_blockcode(@regs, @block)
CLEAR(@block)
end
def ASET(t, v)
@regs[t.to_sym] = v
end
def AINC(t)
red_inc(@regs, t.to_sym)
end
def SET_ATTRIBUTES()
SET_ATTRIBUTE("class_buf", "class")
SET_ATTRIBUTE("id_buf", "id")
SET_ATTRIBUTE("lang_buf", "lang")
SET_ATTRIBUTE("style_buf", "style")
end
def SET_ATTRIBUTE(b, a)
@regs[a.to_sym] = @regs[b.to_sym] unless @regs[b.to_sym].nil?
end
def TRANSFORM(t)
if (@reg && @p > @reg && @reg >= @ts)
str = self.class.transform(@textile_doc, STR_NEW(reg, p-reg), @refs)
@regs[t.to_sym] = str
# /*printf("TRANSFORM(" T ") '%s' (p:'%s' reg:'%s')\n", RSTRING_PTR(str), p, reg);*/ \
else
@regs[t.to_sym] = nil
end
end
def STORE(t)
if (@reg && @p > @reg && @reg >= @ts)
str = @data[@reg, @p - @reg]
@regs[t.to_sym] = str
# /*printf("STORE(" T ") '%s' (p:'%s' reg:'%s')\n", RSTRING_PTR(str), p, reg);*/ \
else
@regs[t.to_sym] = nil
end
end
def STORE_B(t)
if (@bck && @p > @bck && @bck >= @ts)
str = @data[@bck, @p - @bck]
@regs[t.to_sym] = str
# /*printf("STORE_B(" T ") '%s' (p:'%s' reg:'%s')\n", RSTRING_PTR(str), p, reg);*/ \
else
@regs[t.to_sym] = nil
end
end
def STORE_URL(t)
if (@reg && @p > @reg && @reg >= @ts)
punct = true
while (@p > @reg && punct)
case @data[@p - 1, 1]
when ')'
temp_p = @p - 1
level = -1
while (temp_p > @reg)
case @data[temp_p - 1, 1]
when '('; level += 1
when ')'; level -= 1
end
temp_p -= 1
end
if (level == 0)
punct = false
else
@p -= 1
end
when '!', '"', '#', '$', '%', ']', '[', '&', '\'',
'*', '+', ',', '-', '.', '(', ':', ';', '=',
'?', '@', '\\', '^', '_', '`', '|', '~'
@p -= 1
else
punct = false
end
end
@te = @p
end
STORE(t)
if ( ! @refs.nil? && @refs.has_key?(@regs[t.to_sym]) )
@regs[t.to_sym] = @refs[@regs[t.to_sym]]
end
end
def STORE_LINK_ALIAS()
@refs_found[@regs[:text]] = @regs[:href]
end
def CLEAR_LIST()
@list_layout = []
end
def SET_LIST_TYPE(t)
@list_type = t
end
def NEST()
@nest += 1
end
def RESET_NEST()
@nest = 0
end
def LIST_ITEM()
aint = 0
aval = @list_index[@nest-1]
aint = aval.to_i unless aval.nil?
if (@list_type == "ol")
@list_index[@nest-1] = aint + 1
end
if (@nest > @list_layout.length)
listm = sprintf("%s_open", @list_type)
if (@regs[:list_continue])
@regs[:list_continue] = nil
@regs[:start] = @list_index[@nest-1]
else
start = @regs[:start]
if (start.nil?)
@list_index[@nest-1] = 1
else
start_num = start.to_i
@list_index[@nest-1] = start_num
end
end
@regs[:nest] = @nest
@html << @textile_doc.send(listm, @regs)
@list_layout[@nest-1] = @list_type
CLEAR_REGS()
ASET("first", true)
end
LIST_CLOSE()
@regs[:nest] = @list_layout.length
ASET("type", "li_open")
end
def LIST_CLOSE()
while (@nest < @list_layout.length)
@regs[:nest] = @list_layout.length
end_list = @list_layout.pop
if (!end_list.nil?)
listm = sprintf("%s_close", end_list)
@html << @textile_doc.send(listm, @regs)
end
end
end
def red_pass(regs, ref, meth, refs)
txt = regs[ref]
regs[ref] = RedCloth::RedclothInline.redcloth_inline2(@textile_doc, txt, refs) if (!txt.nil?)
return @textile_doc.send(meth, regs)
end
def red_inc(regs, ref)
aint = 0
aval = regs[ref]
aint = aval.to_i if (!aval.nil?)
regs[ref] = aint + 1
end
def red_block(regs, block, refs)
sym_text = :text
btype = @regs[:type]
block = block.strip
if (!block.nil? && !btype.nil?)
method = btype.intern
if (method == :notextile)
@regs[sym_text] = block
else
@regs[sym_text] = RedCloth::RedclothInline.redcloth_inline2(@textile_doc, block, refs)
end
if (@textile_doc.send(:formatter_methods).include? method) #FIXME: This is a hack to get around private method.
block = @textile_doc.send(method, @regs)
else
fallback = @regs[:fallback]
if (!fallback.nil?)
fallback << @regs[sym_text]
CLEAR_REGS()
@regs[sym_text] = fallback
end
block = @textile_doc.p(@regs);
end
end
return block
end
def red_blockcode(regs, block)
btype = regs[:type]
if (block.length > 0)
regs[:text] = block
block = @textile_doc.send(btype, regs)
end
return block
end
def rb_str_cat_escaped(str, ts, te)
source_str = STR_NEW(ts, te-ts);
escaped_str = @textile_doc.send(:escape, source_str) #FIXME: This is a hack to get around private method.
str << escaped_str
end
def rb_str_cat_escaped_for_preformatted(str, text)
escaped_str = @textile_doc.send(:escape_pre, text) #FIXME: This is a hack to get around private method.
str << escaped_str
end
end
class RedclothScan < BaseScanner
def self.transform(textile_doc, data, refs)
self.new.transform(textile_doc, data, refs)
end
def transform(textile_doc, data, refs)
@textile_doc = textile_doc
@data = data + "\0"
@refs = refs
@p = 0
@pe = @data.length
@orig_data = data.dup
@html = ""
@table = ""
@block = ""
CLEAR_REGS()
@list_layout = nil
@list_index = [];
SET_PLAIN_BLOCK("p")
@extend = nil
@listm = []
@refs_found = {}
%% write init;
%% write exec;
ADD_BLOCK() if (block.length > 0)
if ( refs.nil? && !refs_found.empty? )
return transform(@textile_doc, orig_data, refs_found)
else
@textile_doc.send(:after_transform, html)
return html
end
end
def initialize
%%{
variable data @data;
variable p @p;
variable pe @pe;
variable cs @cs;
variable ts @ts;
variable te @te;
write data nofinal;
}%%
end
end
end