pseudohikiparser 0.0.0.4.develop
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.
- data/bin/pseudohiki2html.rb +319 -0
- data/lib/htmlelement/htmltemplate.rb +169 -0
- data/lib/htmlelement.rb +172 -0
- data/lib/pseudohiki/blockparser.rb +359 -0
- data/lib/pseudohiki/htmlformat.rb +229 -0
- data/lib/pseudohiki/htmlplugin.rb +155 -0
- data/lib/pseudohiki/inlineparser.rb +169 -0
- data/lib/pseudohiki/plaintextformat.rb +235 -0
- data/lib/pseudohiki/treestack.rb +119 -0
- data/lib/pseudohiki/version.rb +3 -0
- data/lib/pseudohikiparser.rb +6 -0
- data/test/test_blockparser.rb +313 -0
- data/test/test_htmlelement.rb +73 -0
- data/test/test_htmlformat.rb +538 -0
- data/test/test_htmlplugin.rb +14 -0
- data/test/test_htmltemplate.rb +190 -0
- data/test/test_inlineparser.rb +94 -0
- data/test/test_plaintextformat.rb +205 -0
- data/test/test_treestack.rb +133 -0
- metadata +107 -0
@@ -0,0 +1,538 @@
|
|
1
|
+
#/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'lib/pseudohiki/blockparser'
|
5
|
+
require 'lib/pseudohiki/htmlformat'
|
6
|
+
|
7
|
+
|
8
|
+
class TC_HtmlFormat < Test::Unit::TestCase
|
9
|
+
include PseudoHiki
|
10
|
+
|
11
|
+
class ::String
|
12
|
+
def accept(visitor)
|
13
|
+
self.to_s
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def convert_text_to_html(text)
|
18
|
+
formatter = HtmlFormat.get_plain
|
19
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
20
|
+
tree.accept(formatter).to_s
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_visit_tree
|
24
|
+
text = <<TEXT
|
25
|
+
!heading1
|
26
|
+
|
27
|
+
paragraph1.
|
28
|
+
paragraph2.
|
29
|
+
paragraph3.
|
30
|
+
""citation1
|
31
|
+
paragraph4.
|
32
|
+
|
33
|
+
*list1
|
34
|
+
*list1-1
|
35
|
+
**list2
|
36
|
+
**list2-2
|
37
|
+
*list3
|
38
|
+
|
39
|
+
paragraph5.
|
40
|
+
|
41
|
+
!!heading2
|
42
|
+
|
43
|
+
paragraph6.
|
44
|
+
paragraph7.
|
45
|
+
|
46
|
+
paragraph8.
|
47
|
+
|
48
|
+
!heading3
|
49
|
+
|
50
|
+
paragraph9.
|
51
|
+
TEXT
|
52
|
+
html = <<HTML
|
53
|
+
<div class="section h1">
|
54
|
+
<h1>heading1</h1>
|
55
|
+
<p>
|
56
|
+
paragraph1.paragraph2.paragraph3.</p>
|
57
|
+
<blockquote>
|
58
|
+
<p>
|
59
|
+
citation1</p>
|
60
|
+
</blockquote>
|
61
|
+
<p>
|
62
|
+
paragraph4.</p>
|
63
|
+
<ul>
|
64
|
+
<li>list1
|
65
|
+
<li>list1-1<ul>
|
66
|
+
<li>list2
|
67
|
+
<li>list2-2
|
68
|
+
</ul>
|
69
|
+
|
70
|
+
<li>list3
|
71
|
+
</ul>
|
72
|
+
<p>
|
73
|
+
paragraph5.</p>
|
74
|
+
<div class="section h2">
|
75
|
+
<h2>heading2</h2>
|
76
|
+
<p>
|
77
|
+
paragraph6.paragraph7.</p>
|
78
|
+
<p>
|
79
|
+
paragraph8.</p>
|
80
|
+
<!-- end of section h2 -->
|
81
|
+
</div>
|
82
|
+
<!-- end of section h1 -->
|
83
|
+
</div>
|
84
|
+
<div class="section h1">
|
85
|
+
<h1>heading3</h1>
|
86
|
+
<p>
|
87
|
+
paragraph9.</p>
|
88
|
+
<!-- end of section h1 -->
|
89
|
+
</div>
|
90
|
+
HTML
|
91
|
+
|
92
|
+
assert_equal(html,convert_text_to_html(text))
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_visit_tree_with_inline_elements
|
96
|
+
text = <<TEXT
|
97
|
+
!!heading2
|
98
|
+
|
99
|
+
a paragraph with an ''emphasised'' word.
|
100
|
+
a paragraph with a [[link|http://www.example.org/]].
|
101
|
+
TEXT
|
102
|
+
|
103
|
+
html = <<HTML
|
104
|
+
<div class="section h2">
|
105
|
+
<h2>heading2</h2>
|
106
|
+
<p>
|
107
|
+
a paragraph with an <em>emphasised</em> word.a paragraph with a <a href="http://www.example.org/">link</a>.</p>
|
108
|
+
<!-- end of section h2 -->
|
109
|
+
</div>
|
110
|
+
HTML
|
111
|
+
|
112
|
+
assert_equal(html,convert_text_to_html(text))
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_table
|
116
|
+
text = <<TEXT
|
117
|
+
||!col||!^[[col|link]]||>col
|
118
|
+
||col||col||col
|
119
|
+
TEXT
|
120
|
+
|
121
|
+
html = <<HTML
|
122
|
+
<table>
|
123
|
+
<tr><th>col</th><th rowspan="2"><a href="link">col</a></th><td colspan="2">col</td></tr>
|
124
|
+
<tr><td>col</td><td>col</td><td>col</td></tr>
|
125
|
+
</table>
|
126
|
+
HTML
|
127
|
+
|
128
|
+
assert_equal(html,convert_text_to_html(text))
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_dl
|
132
|
+
text = <<TEXT
|
133
|
+
:dt1:dd1
|
134
|
+
:dt2:dd2
|
135
|
+
TEXT
|
136
|
+
|
137
|
+
html = <<HTML
|
138
|
+
<dl>
|
139
|
+
<dt>dt1</dt>
|
140
|
+
<dd>dd1</dd>
|
141
|
+
<dt>dt2</dt>
|
142
|
+
<dd>dd2</dd>
|
143
|
+
</dl>
|
144
|
+
HTML
|
145
|
+
|
146
|
+
assert_equal(html,convert_text_to_html(text))
|
147
|
+
end
|
148
|
+
|
149
|
+
def test_hr
|
150
|
+
text = <<TEXT
|
151
|
+
paragraph
|
152
|
+
|
153
|
+
----
|
154
|
+
|
155
|
+
paragraph
|
156
|
+
TEXT
|
157
|
+
|
158
|
+
html = <<HTML
|
159
|
+
<p>
|
160
|
+
paragraph</p>
|
161
|
+
<hr>
|
162
|
+
<p>
|
163
|
+
paragraph</p>
|
164
|
+
HTML
|
165
|
+
|
166
|
+
assert_equal(html,convert_text_to_html(text))
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_commentout
|
170
|
+
text = <<TEXT
|
171
|
+
a paragraph.
|
172
|
+
//a comment
|
173
|
+
another paragraph.
|
174
|
+
TEXT
|
175
|
+
|
176
|
+
html = <<HTML
|
177
|
+
<p>
|
178
|
+
a paragraph.</p>
|
179
|
+
<p>
|
180
|
+
another paragraph.</p>
|
181
|
+
HTML
|
182
|
+
|
183
|
+
assert_equal(html,convert_text_to_html(text))
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_self_format
|
187
|
+
text = <<TEXT
|
188
|
+
a paragraph.
|
189
|
+
|
190
|
+
*list
|
191
|
+
|
192
|
+
another paragraph.
|
193
|
+
TEXT
|
194
|
+
|
195
|
+
html = <<HTML
|
196
|
+
<p>
|
197
|
+
a paragraph.</p>
|
198
|
+
<ul>
|
199
|
+
<li>list
|
200
|
+
</ul>
|
201
|
+
<p>
|
202
|
+
another paragraph.</p>
|
203
|
+
HTML
|
204
|
+
|
205
|
+
xhtml = <<HTML
|
206
|
+
<p>
|
207
|
+
a paragraph.</p>
|
208
|
+
<ul>
|
209
|
+
<li>list</li>
|
210
|
+
</ul>
|
211
|
+
<p>
|
212
|
+
another paragraph.</p>
|
213
|
+
HTML
|
214
|
+
|
215
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
216
|
+
assert_equal(html, HtmlFormat.format(tree).to_s)
|
217
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s)
|
218
|
+
end
|
219
|
+
|
220
|
+
def test_listwrapnode
|
221
|
+
ul_html = <<HTML
|
222
|
+
<ul>
|
223
|
+
<li>ul list
|
224
|
+
</ul>
|
225
|
+
HTML
|
226
|
+
|
227
|
+
ol_html = <<HTML
|
228
|
+
<ol>
|
229
|
+
<li>ol list
|
230
|
+
</ol>
|
231
|
+
HTML
|
232
|
+
|
233
|
+
tree = BlockParser.parse(['*ul list'])
|
234
|
+
assert_equal(ul_html, HtmlFormat.format(tree).to_s)
|
235
|
+
tree = BlockParser.parse(['#ol list'])
|
236
|
+
assert_equal(ol_html, HtmlFormat.format(tree).to_s)
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_xhtml
|
240
|
+
text = <<TEXT
|
241
|
+
!heading1
|
242
|
+
|
243
|
+
paragraph1.
|
244
|
+
paragraph2.
|
245
|
+
""citation1
|
246
|
+
paragraph3.
|
247
|
+
----
|
248
|
+
|
249
|
+
*list1
|
250
|
+
*list2
|
251
|
+
TEXT
|
252
|
+
|
253
|
+
html = <<HTML
|
254
|
+
<div class="section h1">
|
255
|
+
<h1>heading1</h1>
|
256
|
+
<p>
|
257
|
+
paragraph1.paragraph2.</p>
|
258
|
+
<blockquote>
|
259
|
+
<p>
|
260
|
+
citation1</p>
|
261
|
+
</blockquote>
|
262
|
+
<p>
|
263
|
+
paragraph3.</p>
|
264
|
+
<hr />
|
265
|
+
<ul>
|
266
|
+
<li>list1</li>
|
267
|
+
<li>list2</li>
|
268
|
+
</ul>
|
269
|
+
<!-- end of section h1 -->
|
270
|
+
</div>
|
271
|
+
HTML
|
272
|
+
|
273
|
+
formatter = XhtmlFormat.get_plain
|
274
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
275
|
+
assert_equal(html,tree.accept(formatter).to_s)
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_xhtml5
|
279
|
+
text = <<TEXT
|
280
|
+
!heading1
|
281
|
+
|
282
|
+
paragraph1.
|
283
|
+
paragraph2.
|
284
|
+
""citation1
|
285
|
+
paragraph3.
|
286
|
+
----
|
287
|
+
|
288
|
+
*list1
|
289
|
+
*list2
|
290
|
+
TEXT
|
291
|
+
|
292
|
+
xhtml5 = <<HTML
|
293
|
+
<section class="h1">
|
294
|
+
<h1>heading1</h1>
|
295
|
+
<p>
|
296
|
+
paragraph1.paragraph2.</p>
|
297
|
+
<blockquote>
|
298
|
+
<p>
|
299
|
+
citation1</p>
|
300
|
+
</blockquote>
|
301
|
+
<p>
|
302
|
+
paragraph3.</p>
|
303
|
+
<hr />
|
304
|
+
<ul>
|
305
|
+
<li>list1</li>
|
306
|
+
<li>list2</li>
|
307
|
+
</ul>
|
308
|
+
<!-- end of h1 -->
|
309
|
+
</section>
|
310
|
+
HTML
|
311
|
+
|
312
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
313
|
+
assert_equal(xhtml5, Xhtml5Format.format(tree).to_s)
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_xhtml_list
|
317
|
+
text = <<TEXT
|
318
|
+
*list1(1)
|
319
|
+
*list2(1)
|
320
|
+
**list3(2)
|
321
|
+
**list4(2)
|
322
|
+
*list5(1)
|
323
|
+
TEXT
|
324
|
+
|
325
|
+
html = <<HTML
|
326
|
+
<ul>
|
327
|
+
<li>list1(1)</li>
|
328
|
+
<li>list2(1)<ul>
|
329
|
+
<li>list3(2)</li>
|
330
|
+
<li>list4(2)</li>
|
331
|
+
</ul>
|
332
|
+
</li>
|
333
|
+
<li>list5(1)</li>
|
334
|
+
</ul>
|
335
|
+
HTML
|
336
|
+
|
337
|
+
formatter = XhtmlFormat.get_plain
|
338
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
339
|
+
assert_equal(html,tree.accept(formatter).to_s)
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_xhtml_link
|
343
|
+
text = <<TEXT
|
344
|
+
a line with a [[link|http://www.example.org/]] in it.
|
345
|
+
|
346
|
+
*a list item with a [[link|http://www.example.org/]] in it.
|
347
|
+
TEXT
|
348
|
+
|
349
|
+
html = <<HTML
|
350
|
+
<p>
|
351
|
+
a line with a <a href="http://www.example.org/">link</a> in it.</p>
|
352
|
+
<ul>
|
353
|
+
<li>a list item with a <a href="http://www.example.org/">link</a> in it.</li>
|
354
|
+
</ul>
|
355
|
+
HTML
|
356
|
+
formatter = XhtmlFormat.get_plain
|
357
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
358
|
+
assert_equal(html,tree.accept(formatter).to_s)
|
359
|
+
end
|
360
|
+
|
361
|
+
def test_assign_id
|
362
|
+
text = <<TEXT
|
363
|
+
!![h2]heading1
|
364
|
+
|
365
|
+
*[l1]list1
|
366
|
+
TEXT
|
367
|
+
html = <<HTML
|
368
|
+
<div class="section h2">
|
369
|
+
<h2 id="H2">heading1</h2>
|
370
|
+
<ul>
|
371
|
+
<li id="L1">list1
|
372
|
+
</ul>
|
373
|
+
<!-- end of section h2 -->
|
374
|
+
</div>
|
375
|
+
HTML
|
376
|
+
|
377
|
+
xhtml = <<HTML
|
378
|
+
<div class="section h2">
|
379
|
+
<h2 id="H2">heading1</h2>
|
380
|
+
<ul>
|
381
|
+
<li id="L1">list1</li>
|
382
|
+
</ul>
|
383
|
+
<!-- end of section h2 -->
|
384
|
+
</div>
|
385
|
+
HTML
|
386
|
+
|
387
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
388
|
+
assert_equal(html, HtmlFormat.format(tree).to_s)
|
389
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
390
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s)
|
391
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s) #bug: you should not touch the original tree.
|
392
|
+
end
|
393
|
+
|
394
|
+
def test_verbatim
|
395
|
+
text = <<TEXT
|
396
|
+
<<<
|
397
|
+
a verbatim line.
|
398
|
+
a verbatim line with <greater than/less than>.
|
399
|
+
>>>
|
400
|
+
|
401
|
+
a normal paragraph.
|
402
|
+
|
403
|
+
another verbatim line with <greater than/less than>.
|
404
|
+
|
405
|
+
another normal paragraph.
|
406
|
+
|
407
|
+
the last verbatim line.
|
408
|
+
TEXT
|
409
|
+
xhtml = <<HTML
|
410
|
+
<pre>
|
411
|
+
a verbatim line.a verbatim line with <greater than/less than>.</pre>
|
412
|
+
<p>
|
413
|
+
a normal paragraph.</p>
|
414
|
+
<pre>
|
415
|
+
another verbatim line with <greater than/less than>.</pre>
|
416
|
+
<p>
|
417
|
+
another normal paragraph.</p>
|
418
|
+
<pre>
|
419
|
+
the last verbatim line.</pre>
|
420
|
+
HTML
|
421
|
+
|
422
|
+
tree = BlockParser.parse(text.split(/\r?\n/o))
|
423
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s)
|
424
|
+
end
|
425
|
+
|
426
|
+
def test_tableleaf
|
427
|
+
text = "||cell 1-1||!^>>cell 1-2||cell 1-5"
|
428
|
+
tree = BlockParser.parse([text])
|
429
|
+
assert_equal([[[[["cell 1-1"]], [["cell 1-2"]], [["cell 1-5"]]]]], tree)
|
430
|
+
|
431
|
+
text = "||cell 1-1 is ''emphasised'' partly||!^>>cell 1-2||cell 1-5"
|
432
|
+
tree = BlockParser.parse([text])
|
433
|
+
assert_equal([[[[["cell 1-1 is "],[["emphasised"]], [" partly"]], [["cell 1-2"]], [["cell 1-5"]]]]], tree)
|
434
|
+
end
|
435
|
+
|
436
|
+
def test_quote
|
437
|
+
text = <<TEXT
|
438
|
+
""this line should be enclosed in a p element.
|
439
|
+
""
|
440
|
+
""*this line should be a list item.
|
441
|
+
TEXT
|
442
|
+
|
443
|
+
xhtml = <<HTML
|
444
|
+
<blockquote>
|
445
|
+
<p>
|
446
|
+
this line should be enclosed in a p element.
|
447
|
+
</p>
|
448
|
+
<ul>
|
449
|
+
<li>this line should be a list item.
|
450
|
+
</li>
|
451
|
+
</ul>
|
452
|
+
</blockquote>
|
453
|
+
HTML
|
454
|
+
|
455
|
+
tree = BlockParser.parse(text.lines.to_a)
|
456
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s)
|
457
|
+
end
|
458
|
+
|
459
|
+
def test_verbatim_with_blank_lines
|
460
|
+
text = <<TEXT
|
461
|
+
<<<
|
462
|
+
a verbatim line with [[a link]]
|
463
|
+
|
464
|
+
another verbatim line
|
465
|
+
|
466
|
+
the last verbatim line
|
467
|
+
>>>
|
468
|
+
TEXT
|
469
|
+
|
470
|
+
text2 = <<TEXT
|
471
|
+
a verbatim line with [[a link]]
|
472
|
+
|
473
|
+
another verbatim line
|
474
|
+
|
475
|
+
the last verbatim line
|
476
|
+
TEXT
|
477
|
+
|
478
|
+
|
479
|
+
xhtml = <<HTML
|
480
|
+
<pre>
|
481
|
+
a verbatim line with [[a link]]
|
482
|
+
|
483
|
+
another verbatim line
|
484
|
+
|
485
|
+
the last verbatim line
|
486
|
+
</pre>
|
487
|
+
HTML
|
488
|
+
|
489
|
+
input_array = [
|
490
|
+
"<<<\n",
|
491
|
+
"a verbatim line with [[a link]]\n",
|
492
|
+
"\n",
|
493
|
+
"another verbatim line\n",
|
494
|
+
"\n",
|
495
|
+
"the last verbatim line\n",
|
496
|
+
">>>\n"
|
497
|
+
]
|
498
|
+
tree = BlockParser.parse(text.lines.to_a)
|
499
|
+
tree2 = BlockParser.parse(text2.lines.to_a)
|
500
|
+
# assert_equal(input_array, text.lines.to_a)
|
501
|
+
# assert_equal([], tree2)
|
502
|
+
# assert_equal([], tree)
|
503
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s)
|
504
|
+
end
|
505
|
+
|
506
|
+
def test_automatical_link_generation
|
507
|
+
text = <<TEXT
|
508
|
+
a line with a url http://www.example.org/ to test an automatical link generation.
|
509
|
+
TEXT
|
510
|
+
|
511
|
+
xhtml = <<HTML
|
512
|
+
<p>
|
513
|
+
a line with a url <a href="http://www.example.org/">http://www.example.org/</a> to test an automatical link generation.
|
514
|
+
</p>
|
515
|
+
HTML
|
516
|
+
tree = BlockParser.parse(text.lines.to_a)
|
517
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s)
|
518
|
+
end
|
519
|
+
|
520
|
+
def test_automatical_link_generation_in_verbatim_blocks
|
521
|
+
text = <<TEXT
|
522
|
+
a line with a url http://www.example.org/ to test an automatical link generation.
|
523
|
+
|
524
|
+
another line with [[link|sample.html]]
|
525
|
+
TEXT
|
526
|
+
|
527
|
+
xhtml = <<HTML
|
528
|
+
<pre>
|
529
|
+
a line with a url <a href="http://www.example.org/">http://www.example.org/</a> to test an automatical link generation.
|
530
|
+
</pre>
|
531
|
+
<pre>
|
532
|
+
another line with [[link|sample.html]]
|
533
|
+
</pre>
|
534
|
+
HTML
|
535
|
+
tree = BlockParser.parse(text.lines.to_a)
|
536
|
+
assert_equal(xhtml, XhtmlFormat.format(tree).to_s)
|
537
|
+
end
|
538
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
#/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'lib/pseudohiki/htmlplugin'
|
5
|
+
|
6
|
+
class TC_HtmlPlugin < Test::Unit::TestCase
|
7
|
+
include PseudoHiki
|
8
|
+
|
9
|
+
def test_visit_pluginnode
|
10
|
+
formatter = HtmlFormat.get_plain
|
11
|
+
tree = InlineParser.new("{{co2}} represents the carbon dioxide.").parse.tree
|
12
|
+
assert_equal("CO<sub>2</sub> represents the carbon dioxide.",tree.accept(formatter).to_s)
|
13
|
+
end
|
14
|
+
end
|