raf 0.0.0 → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,451 @@
1
+ class InlineParser
2
+ token EM_OPEN EM_CLOSE ITALIC_OPEN ITALIC_CLOSE STRIKE_OPEN STRIKE_CLOSE LABEL_OPEN LABEL_CLOSE REFERENCE_OPEN REFERENCE_CLOSE VERB_OPEN VERB_CLOSE ERB_OPEN ERB_CLOSE FOOTNOTE_OPEN FOOTNOTE_CLOSE RUBY_OPEN RUBY_CLOSE IMAGE_OPEN IMAGE_CLOSE MANUEDO_OPEN MANUEDO_CLOSE BAR QUOTE SLASH BACK_SLASH OTHER
3
+ options no_result_var
4
+ rule
5
+ content :
6
+ | elements
7
+
8
+ elements : elements element { val[0].push(val[1]) }
9
+ | element { val }
10
+
11
+
12
+ element : emphasis
13
+ | italic
14
+ | strike
15
+ | label
16
+ | reference
17
+ | ruby
18
+ | footnote
19
+ | image
20
+ | verb
21
+ | erb
22
+ | manuedo
23
+ | normal_strings
24
+
25
+ # --- inline
26
+ emphasis : EM_OPEN content EM_CLOSE { Emphasis.new(val[1]) }
27
+ italic : ITALIC_OPEN content ITALIC_CLOSE { Italic.new(val[1]) }
28
+ strike : STRIKE_OPEN content STRIKE_CLOSE { Strike.new(val[1]) }
29
+ footnote : FOOTNOTE_OPEN content FOOTNOTE_CLOSE {
30
+ @index[:footnote] ||= []
31
+ @index[:footnote] << {:content => val[1] }
32
+ Footnote.new([val[1], @index[:footnote].size])
33
+ }
34
+
35
+
36
+ # --- inline end
37
+
38
+ # --- image
39
+ image_string : OTHER
40
+ | QUOTE
41
+ | BAR
42
+ | SLASH
43
+ | BACK_SLASH
44
+ | EM_OPEN
45
+ | EM_CLOSE
46
+ | ITALIC_OPEN
47
+ | ITALIC_CLOSE
48
+ | STRIKE_OPEN
49
+ | STRIKE_CLOSE
50
+ | RUBY_OPEN
51
+ | RUBY_CLOSE
52
+ | MANUEDO_OPEN
53
+ | MANUEDO_CLOSE
54
+ | ERB_OPEN
55
+ | ERB_CLOSE
56
+ | REFERENCE_OPEN
57
+ | REFERENCE_CLOSE
58
+ | LABEL_OPEN
59
+ | LABEL_CLOSE
60
+ | FOOTNOTE_OPEN
61
+ | FOOTNOTE_CLOSE
62
+ | VERB_OPEN
63
+ | VERB_CLOSE
64
+ | IMAGE_OPEN
65
+
66
+ image_strings : image_strings image_string { val.to_s }
67
+ | image_string { val[0] }
68
+ image : IMAGE_OPEN image_strings IMAGE_CLOSE { Image.new(val[1]) }
69
+
70
+ # --- image end
71
+ # --- ruby
72
+ ruby_string : OTHER
73
+ | QUOTE
74
+ | BAR
75
+ | SLASH
76
+ | BACK_SLASH
77
+ | EM_OPEN
78
+ | EM_CLOSE
79
+ | ITALIC_OPEN
80
+ | ITALIC_CLOSE
81
+ | STRIKE_OPEN
82
+ | STRIKE_CLOSE
83
+ | IMAGE_OPEN
84
+ | IMAGE_CLOSE
85
+ | MANUEDO_OPEN
86
+ | MANUEDO_CLOSE
87
+ | ERB_OPEN
88
+ | ERB_CLOSE
89
+ | REFERENCE_OPEN
90
+ | REFERENCE_CLOSE
91
+ | LABEL_OPEN
92
+ | LABEL_CLOSE
93
+ | RUBY_OPEN
94
+ | FOOTNOTE_OPEN
95
+ | FOOTNOTE_CLOSE
96
+ | VERB_OPEN
97
+ | VERB_CLOSE
98
+
99
+ ruby_strings : ruby_strings ruby_string { val.to_s }
100
+ | ruby_string { val[0] }
101
+
102
+ ruby : RUBY_OPEN ruby_strings RUBY_CLOSE {
103
+ base, text = val[1].split("|",2)
104
+ text ||= base
105
+ Ruby.new([base, text])
106
+ }
107
+
108
+ # --- ruby end
109
+ # --- erb
110
+ erb : ERB_OPEN verb_string ERB_CLOSE {
111
+ e = ERB.new("<%= #{val[1]} %>")
112
+ Plain.new(e.result) }
113
+ # --- erb end
114
+ # --- manuedo
115
+ manuedo : MANUEDO_OPEN content MANUEDO_CLOSE { Manuedo.new(val[1]) }
116
+
117
+ # --- manuedo end
118
+
119
+ # --- label
120
+ label_string : OTHER { val[0] }
121
+ | QUOTE { val[0] }
122
+ | BAR { val[0] }
123
+ | SLASH { val[0] }
124
+ | BACK_SLASH { val[0] }
125
+
126
+ label_strings : label_strings label_string { val.to_s }
127
+ | label_string { val[0] }
128
+
129
+ label : LABEL_OPEN label_strings LABEL_CLOSE {
130
+ label, title = val[1].split("|",2)
131
+ title ||= label
132
+ @index[:label] ||= []
133
+ @index[:label] << {:title => title }
134
+ Label.new([label.to_code, title, @index[:label].size])
135
+ }
136
+
137
+ # --- labe end
138
+ # --- reference : start
139
+ reference_string : OTHER { val[0] }
140
+ | QUOTE { val[0] }
141
+ | BAR { val[0] }
142
+ | SLASH { val[0] }
143
+ | BACK_SLASH { val[0] }
144
+
145
+ reference_strings : reference_strings reference_string { val.to_s }
146
+ | reference_string { val[0] }
147
+
148
+ reference : REFERENCE_OPEN reference_strings REFERENCE_CLOSE {
149
+ title, uri = val[1].split("|",2)
150
+ uri ||= title
151
+ uri = "#" + uri.to_code if uri.gsub(/^\s*https*:\/\//,"") == uri
152
+ Reference.new([title, uri])
153
+ }
154
+ # --- reference : end
155
+ # --- verb
156
+ verb_string : OTHER
157
+ | QUOTE
158
+ | BAR
159
+ | SLASH
160
+ | BACK_SLASH
161
+ | EM_OPEN
162
+ | EM_CLOSE
163
+ | ITALIC_OPEN
164
+ | ITALIC_CLOSE
165
+ | STRIKE_OPEN
166
+ | STRIKE_CLOSE
167
+ | IMAGE_OPEN
168
+ | IMAGE_CLOSE
169
+ | MANUEDO_OPEN
170
+ | MANUEDO_CLOSE
171
+ | ERB_OPEN
172
+ | ERB_CLOSE
173
+ | REFERENCE_OPEN
174
+ | REFERENCE_CLOSE
175
+ | LABEL_OPEN
176
+ | LABEL_CLOSE
177
+ | RUBY_OPEN
178
+ | RUBY_CLOSE
179
+ | FOOTNOTE_OPEN
180
+ | FOOTNOTE_CLOSE
181
+ | VERB_OPEN
182
+
183
+ verb_strings : verb_string
184
+ | verb_strings verb_string { val }
185
+
186
+ verb : VERB_OPEN verb_strings VERB_CLOSE { Verb.new(val[1])}
187
+
188
+ # --- verb end
189
+ # --- normal
190
+ normal_strings : normal_string { Plain.new(val[0]) }
191
+ | normal_strings normal_string { Plain.new([val[0].contents, val[1]]) }
192
+
193
+ normal_string : OTHER { val[0] }
194
+ | QUOTE { val[0] }
195
+ | BAR { val[0] }
196
+ | SLASH { val[0] }
197
+ | BACK_SLASH { val[0] }
198
+ # --- normal end
199
+
200
+ ---- inner
201
+ include ParserUtility
202
+
203
+ EM_OPEN = '((*'
204
+ EM_OPEN_RE = /\A#{Regexp.quote(EM_OPEN)}/
205
+ EM_CLOSE = '*))'
206
+ EM_CLOSE_RE = /\A#{Regexp.quote(EM_CLOSE)}/
207
+
208
+ ITALIC_OPEN = '((_'
209
+ ITALIC_OPEN_RE = /\A#{Regexp.quote(ITALIC_OPEN)}/
210
+ ITALIC_CLOSE = '_))'
211
+ ITALIC_CLOSE_RE = /\A#{Regexp.quote(ITALIC_CLOSE)}/
212
+
213
+ STRIKE_OPEN = '((-'
214
+ STRIKE_OPEN_RE = /\A#{Regexp.quote(STRIKE_OPEN)}/
215
+ STRIKE_CLOSE = '-))'
216
+ STRIKE_CLOSE_RE = /\A#{Regexp.quote(STRIKE_CLOSE)}/
217
+
218
+ RUBY_OPEN = '((^'
219
+ RUBY_OPEN_RE = /\A#{Regexp.quote(RUBY_OPEN)}/
220
+ RUBY_CLOSE = '^))'
221
+ RUBY_CLOSE_RE = /\A#{Regexp.quote(RUBY_CLOSE)}/
222
+
223
+ FOOTNOTE_OPEN = '((['
224
+ FOOTNOTE_OPEN_RE = /\A#{Regexp.quote(FOOTNOTE_OPEN)}/
225
+ FOOTNOTE_CLOSE = ']))'
226
+ FOOTNOTE_CLOSE_RE = /\A#{Regexp.quote(FOOTNOTE_CLOSE)}/
227
+
228
+ IMAGE_OPEN = '(($'
229
+ IMAGE_OPEN_RE = /\A#{Regexp.quote(IMAGE_OPEN)}/
230
+ IMAGE_CLOSE = '$))'
231
+ IMAGE_CLOSE_RE = /\A#{Regexp.quote(IMAGE_CLOSE)}/
232
+
233
+
234
+ LABEL_OPEN = '((>'
235
+ LABEL_OPEN_RE = /\A#{Regexp.quote(LABEL_OPEN)}/
236
+ LABEL_CLOSE = '<))'
237
+ LABEL_CLOSE_RE = /\A#{Regexp.quote(LABEL_CLOSE)}/
238
+
239
+ LABEL_HTML_OPEN = '((&gt;'
240
+ LABEL_HTML_OPEN_RE = /\A#{Regexp.quote(LABEL_HTML_OPEN)}/
241
+ LABEL_HTML_CLOSE = '&lt;))'
242
+ LABEL_HTML_CLOSE_RE = /\A#{Regexp.quote(LABEL_HTML_CLOSE)}/
243
+
244
+
245
+ REFERENCE_OPEN = '((<'
246
+ REFERENCE_OPEN_RE = /\A#{Regexp.quote(REFERENCE_OPEN)}/
247
+ REFERENCE_CLOSE = '>))'
248
+ REFERENCE_CLOSE_RE = /\A#{Regexp.quote(REFERENCE_CLOSE)}/
249
+
250
+ REFERENCE_HTML_OPEN = '((&lt;'
251
+ REFERENCE_HTML_OPEN_RE = /\A#{Regexp.quote(REFERENCE_HTML_OPEN)}/
252
+ REFERENCE_HTML_CLOSE = '&gt;))'
253
+ REFERENCE_HTML_CLOSE_RE = /\A#{Regexp.quote(REFERENCE_HTML_CLOSE)}/
254
+
255
+ VERB_OPEN = "(('"
256
+ VERB_OPEN_RE = /\A#{Regexp.quote(VERB_OPEN)}/
257
+ VERB_CLOSE = "'))"
258
+ VERB_CLOSE_RE = /\A#{Regexp.quote(VERB_CLOSE)}/
259
+
260
+ MANUEDO_OPEN = "((/"
261
+ MANUEDO_OPEN_RE = /\A#{Regexp.quote(MANUEDO_OPEN)}/
262
+ MANUEDO_CLOSE = "/))"
263
+ MANUEDO_CLOSE_RE = /\A#{Regexp.quote(MANUEDO_CLOSE)}/
264
+
265
+ ERB_OPEN = "<%="
266
+ ERB_OPEN_RE = /\A#{Regexp.quote(ERB_OPEN)}/
267
+ ERB_CLOSE = "%>"
268
+ ERB_CLOSE_RE = /\A#{Regexp.quote(ERB_CLOSE)}/
269
+
270
+ BAR = "|"
271
+ BAR_RE = /\A#{Regexp.quote(BAR)}/
272
+ QUOTE = '"'
273
+ QUOTE_RE = /\A#{Regexp.quote(QUOTE)}/
274
+ SLASH = "/"
275
+ SLASH_RE = /\A#{Regexp.quote(SLASH)}/
276
+ BACK_SLASH = "\\"
277
+ BACK_SLASH_RE = /\A#{Regexp.quote(BACK_SLASH)}/
278
+ # URL = "URL:"
279
+ # URL_RE = /\A#{Regexp.quote(URL)}/
280
+
281
+ #other_re_mode = Regexp::EXTENDED
282
+ other_re_mode = Regexp::MULTILINE
283
+ OTHER_RE = Regexp.new(
284
+ "\\A.+?(?=#{Regexp.quote(EM_OPEN)}|#{Regexp.quote(EM_CLOSE)}|#{Regexp.quote(ITALIC_OPEN)}|#{Regexp.quote(ITALIC_CLOSE)}|#{Regexp.quote(STRIKE_OPEN)}|#{Regexp.quote(STRIKE_CLOSE)}|#{Regexp.quote(FOOTNOTE_OPEN)}|#{Regexp.quote(FOOTNOTE_CLOSE)}|#{Regexp.quote(RUBY_OPEN)}|#{Regexp.quote(RUBY_CLOSE)}|#{Regexp.quote(IMAGE_OPEN)}|#{Regexp.quote(IMAGE_CLOSE)}|#{Regexp.quote(LABEL_OPEN)}|#{Regexp.quote(LABEL_CLOSE)}|#{Regexp.quote(LABEL_HTML_OPEN)}|#{Regexp.quote(LABEL_HTML_CLOSE)}|#{Regexp.quote(REFERENCE_OPEN)}|#{Regexp.quote(REFERENCE_CLOSE)}|#{Regexp.quote(REFERENCE_HTML_OPEN)}|#{Regexp.quote(REFERENCE_HTML_CLOSE)}|#{Regexp.quote(MANUEDO_OPEN)}|#{Regexp.quote(MANUEDO_CLOSE)}|#{Regexp.quote(VERB_OPEN)}|#{Regexp.quote(VERB_CLOSE)}|#{Regexp.quote(ERB_OPEN)}|#{Regexp.quote(ERB_CLOSE)}|#{Regexp.quote(BAR)}|#{Regexp.quote(QUOTE)}|#{Regexp.quote(SLASH)}|#{Regexp.quote(BACK_SLASH)})", other_re_mode)
285
+
286
+
287
+ def parse(src)
288
+ @src = StringScanner.new(src.to_s)
289
+ @pre = ""
290
+ @@yydebug = true
291
+ @view_token_type = false
292
+ do_parse
293
+ end
294
+ def initialize
295
+ @index = {}
296
+ end
297
+ attr_reader :index
298
+
299
+ def next_token
300
+ return [false, false] if @src.eos?
301
+ if ret = @src.scan(EM_OPEN_RE)
302
+ puts "i: EM_OPEN: #{ret}" if @view_token_type
303
+ @pre << ret
304
+ [:EM_OPEN, ret]
305
+ elsif ret = @src.scan(EM_CLOSE_RE)
306
+ puts "i: EM_CLOSE: #{ret}" if @view_token_type
307
+ @pre << ret
308
+ [:EM_CLOSE, ret]
309
+ elsif ret = @src.scan(ITALIC_OPEN_RE)
310
+ puts "i: ITALIC_OPEN: #{ret}" if @view_token_type
311
+ @pre << ret
312
+ [:ITALIC_OPEN, ret]
313
+ elsif ret = @src.scan(ITALIC_CLOSE_RE)
314
+ puts "i: ITALIC_CLOSE: #{ret}" if @view_token_type
315
+ @pre << ret
316
+ [:ITALIC_CLOSE, ret]
317
+ elsif ret = @src.scan(STRIKE_OPEN_RE)
318
+ @pre << ret
319
+ puts "i: STRIKE_OPEN: #{ret}" if @view_token_type
320
+ [:STRIKE_OPEN, ret]
321
+ elsif ret = @src.scan(STRIKE_CLOSE_RE)
322
+ @pre << ret
323
+ puts "i: STRIKE_CLOSE: #{ret}" if @view_token_type
324
+ [:STRIKE_CLOSE, ret]
325
+ elsif ret = @src.scan(LABEL_OPEN_RE)
326
+ @pre << ret
327
+ puts "i: LABEL_OPEN: #{ret}" if @view_token_type
328
+ [:LABEL_OPEN, ret]
329
+ elsif ret = @src.scan(LABEL_CLOSE_RE)
330
+ puts "i: LABEL_CLOSE: #{ret}" if @view_token_type
331
+ @pre << ret
332
+ [:LABEL_CLOSE, ret]
333
+ elsif ret = @src.scan(LABEL_HTML_OPEN_RE)
334
+ @pre << ret
335
+ puts "i: LABEL_OPEN: #{ret}" if @view_token_type
336
+ [:LABEL_OPEN, ret]
337
+ elsif ret = @src.scan(LABEL_HTML_CLOSE_RE)
338
+ puts "i: LABEL_CLOSE: #{ret}" if @view_token_type
339
+ @pre << ret
340
+ [:LABEL_CLOSE, ret]
341
+ elsif ret = @src.scan(REFERENCE_OPEN_RE)
342
+ puts "i: REFERENCE_OPEN: #{ret}" if @view_token_type
343
+ @pre << ret
344
+ [:REFERENCE_OPEN, ret]
345
+ elsif ret = @src.scan(REFERENCE_CLOSE_RE)
346
+ puts "i: REFERENCE_CLOSE: #{ret}" if @view_token_type
347
+ @pre << ret
348
+ [:REFERENCE_CLOSE, ret]
349
+ elsif ret = @src.scan(REFERENCE_HTML_OPEN_RE)
350
+ puts "i: REFERENCE_HTML_OPEN: #{ret}" if @view_token_type
351
+ @pre << ret
352
+ [:REFERENCE_OPEN, ret]
353
+ elsif ret = @src.scan(REFERENCE_HTML_CLOSE_RE)
354
+ puts "i: REFERENCE_HTML_CLOSE: #{ret}" if @view_token_type
355
+ @pre << ret
356
+ [:REFERENCE_CLOSE, ret]
357
+ elsif ret = @src.scan(VERB_OPEN_RE)
358
+ puts "i: VERB_OPEN: #{ret}" if @view_token_type
359
+ @pre << ret
360
+ [:VERB_OPEN, ret]
361
+ elsif ret = @src.scan(VERB_CLOSE_RE)
362
+ puts "i: VERB_CLOSE: #{ret}" if @view_token_type
363
+ @pre << ret
364
+ [:VERB_CLOSE, ret]
365
+ elsif ret = @src.scan(RUBY_OPEN_RE)
366
+ puts "i: RUBY_OPEN: #{ret}" if @view_token_type
367
+ @pre << ret
368
+ [:RUBY_OPEN, ret]
369
+ elsif ret = @src.scan(RUBY_CLOSE_RE)
370
+ puts "i: RUBY_CLOSE: #{ret}" if @view_token_type
371
+ @pre << ret
372
+ [:RUBY_CLOSE, ret]
373
+ elsif ret = @src.scan(FOOTNOTE_OPEN_RE)
374
+ puts "i: FOOTNOTE_OPEN: #{ret}" if @view_token_type
375
+ @pre << ret
376
+ [:FOOTNOTE_OPEN, ret]
377
+ elsif ret = @src.scan(FOOTNOTE_CLOSE_RE)
378
+ puts "i: FOOTNOTE_CLOSE: #{ret}" if @view_token_type
379
+ @pre << ret
380
+ [:FOOTNOTE_CLOSE, ret]
381
+ elsif ret = @src.scan(IMAGE_OPEN_RE)
382
+ puts "i: IMAGE_OPEN: #{ret}" if @view_token_type
383
+ @pre << ret
384
+ [:IMAGE_OPEN, ret]
385
+ elsif ret = @src.scan(IMAGE_CLOSE_RE)
386
+ puts "i: IMAGE_CLOSE: #{ret}" if @view_token_type
387
+ @pre << ret
388
+ [:IMAGE_CLOSE, ret]
389
+ elsif ret = @src.scan(MANUEDO_OPEN_RE)
390
+ puts "i: MANUEDO_OPEN: #{ret}" if @view_token_type
391
+ @pre << ret
392
+ [:MANUEDO_OPEN, ret]
393
+ elsif ret = @src.scan(MANUEDO_CLOSE_RE)
394
+ puts "i: MANUED_CLOSE: #{ret}" if @view_token_type
395
+ @pre << ret
396
+ [:MANUEDO_CLOSE, ret]
397
+ elsif ret = @src.scan(ERB_OPEN_RE)
398
+ puts "i: ERB_OPEN: #{ret}" if @view_token_type
399
+ @pre << ret
400
+ [:ERB_OPEN, ret]
401
+ elsif ret = @src.scan(ERB_CLOSE_RE)
402
+ puts "i: ERB_CLOSE: #{ret}" if @view_token_type
403
+ @pre << ret
404
+ [:ERB_CLOSE, ret]
405
+ elsif ret = @src.scan(BAR_RE)
406
+ puts "i: BAR: #{ret}" if @view_token_type
407
+ @pre << ret
408
+ [:BAR, ret]
409
+ elsif ret = @src.scan(QUOTE_RE)
410
+ puts "i: QUOTE: #{ret}" if @view_token_type
411
+ @pre << ret
412
+ [:QUOTE, ret]
413
+ elsif ret = @src.scan(SLASH_RE)
414
+ puts "i: SLASH: #{ret}" if @view_token_type
415
+ @pre << ret
416
+ [:SLASH, ret]
417
+ elsif ret = @src.scan(BACK_SLASH_RE)
418
+ puts "i: BACK_SLASH: #{ret}" if @view_token_type
419
+ @pre << ret
420
+ [:BACK_SLASH, ret]
421
+ elsif ret = @src.scan(OTHER_RE)
422
+ puts "i: OTHER_RE: #{ret}" if @view_token_type
423
+ @pre << ret
424
+ [:OTHER, ret]
425
+ else
426
+ puts "i: OTHER_RE(else): #{ret}" if @view_token_type
427
+ ret = @src.rest
428
+ @pre << ret
429
+ @src.terminate
430
+ [:OTHER, ret]
431
+ end
432
+ end
433
+
434
+ ---- header
435
+ require "parserutility"
436
+ require 'strscan'
437
+ require 'erb'
438
+ require 'rafelement'
439
+
440
+ module Raf
441
+ ---- footer
442
+ if __FILE__ == $0
443
+ raf = InlineParser.new
444
+ src = $stdin.readline
445
+ nodes = raf.parse(src)
446
+ puts "----- output -----"
447
+ nodes.each do |n|
448
+ puts n.apply
449
+ end
450
+ end
451
+ end # end of module Raf