nora_mark 0.2beta3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,840 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.dirname(__FILE__) + '/spec_helper.rb'
3
+ require File.dirname(__FILE__) + '/../lib/nora_mark'
4
+ require 'nokogiri'
5
+ require File.dirname(__FILE__) + '/nokogiri_test_helper.rb'
6
+
7
+ describe NoraMark do
8
+ describe 'convert' do
9
+ it 'should generate valid xhtml' do
10
+ text = 'some text'
11
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
12
+ xhtml = Nokogiri::XML::Document.parse(noramark.html[0])
13
+ expect(xhtml.root.name).to eq('html')
14
+ expect(xhtml.root.namespaces['xmlns']).to eq('http://www.w3.org/1999/xhtml')
15
+ expect(xhtml.root['xml:lang']).to eq('ja')
16
+ expect(xhtml.root.element_children[0].name).to eq 'head'
17
+ expect(xhtml.root.at_xpath('xmlns:head/xmlns:title').text).to eq('the title')
18
+ expect(xhtml.root.element_children[1].name).to eq 'body'
19
+ end
20
+ it 'should convert simple paragraph' do
21
+ text = "ここから、パラグラフがはじまります。\n「二行目です。」\n三行目です。\n\n\n ここから、次のパラグラフです。"
22
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
23
+ converted = noramark.html
24
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
25
+ expect(body.element_children.size).to eq 2
26
+ expect(body.element_children[0].selector_and_children).to eq(
27
+ ['div.pgroup',
28
+ ['p', 'ここから、パラグラフがはじまります。'],
29
+ ['p.noindent', '「二行目です。」'],
30
+ ['p', '三行目です。']
31
+ ]
32
+ )
33
+
34
+ expect(body.element_children[1].selector_and_children).to eq(
35
+ ['div.pgroup',
36
+ ['p', 'ここから、次のパラグラフです。']]
37
+ )
38
+ end
39
+ it 'should convert simple paragraph in english mode' do
40
+ text = "paragraph begins.\n2nd line.\n 3rd line.\n\n\n next paragraph."
41
+ noramark = NoraMark::Document.parse(text, :lang => 'en', :title => 'the title')
42
+ converted = noramark.html
43
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
44
+ expect(body.element_children.size).to eq 2
45
+ expect(body.element_children[0].selector_and_children).to eq(
46
+ ['p',
47
+ 'paragraph begins.', ['br', ''],
48
+ '2nd line.', ['br', ''],
49
+ '3rd line.'
50
+ ]
51
+ )
52
+
53
+ expect(body.element_children[1].selector_and_children).to eq(
54
+ ['p', 'next paragraph.']
55
+ )
56
+ end
57
+
58
+ it 'should convert simple paragraph in japanese mode, but paragraph mode is default' do
59
+ text = "paragraph begins.\n2nd line.\n 3rd line.\n\n\n next paragraph."
60
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title', :paragraph_style => :default)
61
+ converted = noramark.html
62
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
63
+ expect(body.element_children.size).to eq 2
64
+ expect(body.element_children[0].selector_and_children).to eq(
65
+ ['p',
66
+ 'paragraph begins.', ['br', ''],
67
+ '2nd line.', ['br', ''],
68
+ '3rd line.'
69
+ ]
70
+ )
71
+
72
+ expect(body.element_children[1].selector_and_children).to eq(
73
+ ['p', 'next paragraph.']
74
+ )
75
+ end
76
+
77
+ it 'should convert paragraph with header' do
78
+ text = "h1: タイトルです。\r\nここから、パラグラフがはじまります。\n\nh2.column:ふたつめの見出しです。\n ここから、次のパラグラフです。\nh3.third.foo: クラスが複数ある見出しです"
79
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
80
+ converted = noramark.html
81
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
82
+ expect(body.element_children.size).to eq 5
83
+ expect(body.element_children[0].a).to eq ['h1', 'タイトルです。']
84
+ expect(body.element_children[1].selector_and_children).to eq(
85
+ ['div.pgroup',
86
+ ['p', 'ここから、パラグラフがはじまります。']
87
+ ]
88
+ )
89
+ expect(body.element_children[2].a).to eq ['h2.column', 'ふたつめの見出しです。']
90
+ expect(body.element_children[3].selector_and_children).to eq(
91
+ ['div.pgroup',
92
+ ['p', 'ここから、次のパラグラフです。']
93
+ ]
94
+ )
95
+ expect(body.element_children[4].a).to eq ['h3.third.foo', 'クラスが複数ある見出しです']
96
+ end
97
+
98
+ it 'should convert div and paragraph' do
99
+ text = "d {\n1st line. \n}"
100
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the document title')
101
+ converted = noramark.html
102
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
103
+ expect(body.element_children[0].selector_and_children).to eq(
104
+ ['div',
105
+ ['div.pgroup',
106
+ ['p', '1st line.']
107
+ ]
108
+ ]
109
+ )
110
+ end
111
+
112
+ it 'should convert div without pgroup' do
113
+ text = "d(wo-pgroup) {\n1st line. \n}"
114
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
115
+ converted = noramark.html
116
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
117
+ expect(body.element_children[0].selector_and_children).to eq(
118
+ ['div',
119
+ ['p', '1st line.']
120
+ ]
121
+ )
122
+ end
123
+
124
+ it 'should nest div without pgroup' do
125
+ text = "d(wo-pgroup) {\nd {\nnested.\n} \n}"
126
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
127
+ converted = noramark.html
128
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
129
+ expect(body.element_children[0].selector_and_children).to eq(
130
+ ['div',
131
+ ['div',
132
+ ['p', 'nested.']
133
+ ]
134
+ ]
135
+ )
136
+ end
137
+
138
+ it 'should nest div without pgroup and with pgroup' do
139
+ text = "d(wo-pgroup) {\nd {\nnested.\n} \n}\nd {\nin pgroup\n}"
140
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
141
+ converted = noramark.html
142
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
143
+ expect(body.element_children[0].selector_and_children).to eq(
144
+ ['div',
145
+ ['div',
146
+ ['p', 'nested.']
147
+ ]
148
+ ])
149
+ expect(body.element_children[1].selector_and_children).to eq(
150
+ ['div',
151
+ ['div.pgroup',
152
+ ['p', 'in pgroup']
153
+ ]
154
+ ])
155
+ end
156
+
157
+
158
+ it 'should convert div with class' do
159
+ text = "d.preface-one {\n h1: title.\n}"
160
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
161
+ converted = noramark.html
162
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
163
+ expect(body.element_children[0].selector_and_children).to eq(
164
+ ['div.preface-one',
165
+ ['h1', 'title.']
166
+ ]
167
+ )
168
+ end
169
+
170
+ it 'should convert div with id and class' do
171
+ text = "d#thecontents.preface-one {\nh1: title.\n}"
172
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
173
+ converted = noramark.html
174
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
175
+ expect(body.element_children[0].selector_and_children).to eq(
176
+ ['div#thecontents.preface-one',
177
+ ['h1', 'title.']
178
+ ]
179
+ )
180
+ end
181
+
182
+ it 'should convert nested div' do
183
+ text = "d.preface {\n outer div. \n d.nested {\n nested!\n}\nouter div again.\n}"
184
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
185
+ converted = noramark.html
186
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
187
+ expect(body.element_children[0].selector_and_children).to eq(
188
+ ['div.preface',
189
+ ['div.pgroup',
190
+ ['p', 'outer div.']
191
+ ],
192
+ ['div.nested',
193
+ ['div.pgroup',
194
+ ['p', 'nested!']
195
+ ]
196
+ ],
197
+ ['div.pgroup',
198
+ ['p', 'outer div again.']
199
+ ],
200
+ ]
201
+ )
202
+ end
203
+
204
+ it 'should convert article' do
205
+ text = "art {\n in the article.\n}"
206
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
207
+ converted = noramark.html
208
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
209
+ expect(body.element_children[0].selector_and_children).to eq(
210
+ ['article',
211
+ ['div.pgroup',
212
+ ['p', 'in the article.']
213
+ ]
214
+ ]
215
+ )
216
+ end
217
+
218
+ it 'should convert article with other notation' do
219
+ text = "arti {\n in the article.\n}"
220
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
221
+ converted = noramark.html
222
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
223
+ expect(body.element_children[0].selector_and_children).to eq(
224
+ ['article',
225
+ ['div.pgroup',
226
+ ['p', 'in the article.']
227
+ ]
228
+ ]
229
+ )
230
+ end
231
+
232
+ it 'should convert article with yet anther notation' do
233
+ text = "article {\n in the article.\n}"
234
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
235
+ converted = noramark.html
236
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
237
+ expect(body.element_children[0].selector_and_children).to eq(
238
+ ['article',
239
+ ['div.pgroup',
240
+ ['p', 'in the article.']
241
+ ]
242
+ ]
243
+ )
244
+ end
245
+
246
+ it 'should convert section ' do
247
+ text = "art {\nsec {\n section in the article. \n}\n}"
248
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
249
+ converted = noramark.html
250
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
251
+ expect(body.element_children[0].selector_and_children).to eq(
252
+ ['article',
253
+ ['section',
254
+ ['div.pgroup',
255
+ ['p', 'section in the article.']
256
+ ]
257
+ ]
258
+ ]
259
+ )
260
+ end
261
+
262
+ it 'should convert section with other notation' do
263
+ text = "art {\nsect {\n section in the article. \n}\n}"
264
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
265
+ converted = noramark.html
266
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
267
+ expect(body.element_children[0].selector_and_children).to eq(
268
+ ['article',
269
+ ['section',
270
+ ['div.pgroup',
271
+ ['p', 'section in the article.']
272
+ ]
273
+ ]
274
+ ]
275
+ )
276
+ end
277
+
278
+ it 'should convert section with yet other notation' do
279
+ text = "art {\nsection {\n section in the article. \n}\n}"
280
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
281
+ converted = noramark.html
282
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
283
+ expect(body.element_children[0].selector_and_children).to eq(
284
+ ['article',
285
+ ['section',
286
+ ['div.pgroup',
287
+ ['p', 'section in the article.']
288
+ ]
289
+ ]
290
+ ]
291
+ )
292
+ end
293
+
294
+
295
+
296
+ it 'should handle block image' do
297
+ text = "this is normal line.\nimage(./image1.jpg, alt text): caption text"
298
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
299
+ converted = noramark.html
300
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
301
+ expect(body.element_children[0].selector_and_children).to eq(
302
+ ['div.pgroup',
303
+ ['p', 'this is normal line.']
304
+ ]
305
+ )
306
+ expect(body.element_children[1].selector_and_children).to eq(
307
+ ['div.img-wrap',
308
+ ["img[src='./image1.jpg'][alt='alt text']", ''],
309
+ ['p', 'caption text']
310
+ ]
311
+ )
312
+ end
313
+
314
+ it 'should handle block image with before caption' do
315
+ text = "this is normal line.\nimage(./image1.jpg, alt text, caption_before: true): caption text"
316
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
317
+ converted = noramark.html
318
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
319
+ expect(body.element_children[0].selector_and_children).to eq(
320
+ ['div.pgroup',
321
+ ['p', 'this is normal line.']
322
+ ]
323
+ )
324
+ expect(body.element_children[1].selector_and_children).to eq(
325
+ ['div.img-wrap',
326
+ ['p', 'caption text'],
327
+ ["img[src='./image1.jpg'][alt='alt text']", '']
328
+ ]
329
+ )
330
+ end
331
+
332
+ it 'should handle block image without caption' do
333
+ text = "this is normal line.\nimage(./image1.jpg, alt text):"
334
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
335
+ converted = noramark.html
336
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
337
+ expect(body.element_children[0].selector_and_children).to eq(
338
+ ['div.pgroup',
339
+ ['p', 'this is normal line.']
340
+ ]
341
+ )
342
+ expect(body.element_children[1].selector_and_children).to eq(
343
+ ['div.img-wrap',
344
+ ["img[src='./image1.jpg'][alt='alt text']", '']
345
+ ]
346
+ )
347
+ end
348
+
349
+ it 'should handle page change article' do
350
+ text = "this is start.\nnewpage(page changed):\nthis is second page.\nnewpage:\nand the third."
351
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
352
+ converted = noramark.html
353
+ expect(converted.size).to eq 3
354
+ body1 = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
355
+ expect(body1.element_children[0].selector_and_children).to eq(
356
+ ['div.pgroup',
357
+ ['p', 'this is start.']
358
+ ]
359
+ )
360
+
361
+ head2 = Nokogiri::XML::Document.parse(converted[1]).root.at_xpath('xmlns:head')
362
+ expect(head2.element_children[0].a).to eq ['title', 'page changed']
363
+ body2 = Nokogiri::XML::Document.parse(converted[1]).root.at_xpath('xmlns:body')
364
+ expect(body2.element_children[0].selector_and_children).to eq(
365
+ ['div.pgroup',
366
+ ['p', 'this is second page.']
367
+ ]
368
+ )
369
+
370
+ head3 = Nokogiri::XML::Document.parse(converted[2]).root.at_xpath('xmlns:head')
371
+ expect(head3.element_children[0].a).to eq ['title', 'page changed']
372
+ body3 = Nokogiri::XML::Document.parse(converted[2]).root.at_xpath('xmlns:body')
373
+ expect(body3.element_children[0].selector_and_children).to eq(
374
+ ['div.pgroup',
375
+ ['p', 'and the third.']
376
+ ]
377
+ )
378
+ end
379
+
380
+ it 'should handle stylesheets' do
381
+ text = "d.styled {\n this is styled document.\n}"
382
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the document title', :stylesheets => ['reset.css', 'mystyle.css'])
383
+ converted = noramark.html
384
+ head = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:head')
385
+ expect(head.element_children[0].a).to eq ['title', 'the document title']
386
+ expect(head.element_children[1].a).to eq ["link[rel='stylesheet'][type='text/css'][href='reset.css']", '']
387
+ expect(head.element_children[2].a).to eq ["link[rel='stylesheet'][type='text/css'][href='mystyle.css']", '']
388
+ end
389
+
390
+ it 'should handle link' do
391
+ text = " link to [link(http://github.com/skoji/noramark){noramark repository}]. \ncan you see this?"
392
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
393
+ converted = noramark.html
394
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
395
+ expect(body.element_children[0].selector_and_children).to eq(
396
+ ['div.pgroup',
397
+ ['p',
398
+ 'link to ',
399
+ ["a[href='http://github.com/skoji/noramark']", 'noramark repository'],
400
+ '.'
401
+ ],
402
+ ['p', 'can you see this?']
403
+ ]
404
+ )
405
+ end
406
+
407
+ it 'should handle link with l' do
408
+ text = "link to [l(http://github.com/skoji/noramark){noramark repository}]. \ncan you see this?"
409
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
410
+ converted = noramark.html
411
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
412
+ expect(body.element_children[0].selector_and_children).to eq(
413
+ ['div.pgroup',
414
+ ['p',
415
+ 'link to ',
416
+ ["a[href='http://github.com/skoji/noramark']", 'noramark repository'],
417
+ '.'
418
+ ],
419
+ ['p', 'can you see this?']
420
+ ]
421
+ )
422
+ end
423
+
424
+ it 'should handle custom paragraph' do
425
+ text = "this is normal line.\np.custom: this text is in custom class."
426
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
427
+ converted = noramark.html
428
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
429
+ expect(body.element_children[0].selector_and_children).to eq(
430
+ ['div.pgroup',
431
+ ['p', 'this is normal line.'],
432
+ ['p.custom', 'this text is in custom class.']
433
+ ]
434
+ )
435
+ end
436
+
437
+ it 'should handle span' do
438
+ text = "p.custom: this text is in [s.keyword{custom}] class."
439
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
440
+ converted = noramark.html
441
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
442
+ expect(body.element_children[0].selector_and_children).to eq(
443
+ ['div.pgroup',
444
+ ['p.custom', 'this text is in ', ['span.keyword', 'custom'], ' class.'
445
+ ]]
446
+ )
447
+ end
448
+
449
+ it 'should handle any block' do
450
+ text = "this is normal line.\ncite {\n this block should be in cite. \n}"
451
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
452
+ converted = noramark.html
453
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
454
+ expect(body.element_children[0].selector_and_children).to eq(
455
+ ['div.pgroup',
456
+ ['p', 'this is normal line.']
457
+ ]
458
+ )
459
+ expect(body.element_children[1].selector_and_children).to eq(
460
+ ['cite',
461
+ ['div.pgroup',
462
+ ['p', 'this block should be in cite.']
463
+ ]
464
+ ]
465
+ )
466
+ end
467
+
468
+ it 'should handle inline image' do
469
+ text = "simple image [img(./image1.jpg, alt)]."
470
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
471
+ converted = noramark.html
472
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
473
+ expect(body.element_children[0].selector_and_children).to eq(
474
+ ['div.pgroup',
475
+ ['p',
476
+ 'simple image ', ["img[src='./image1.jpg'][alt='alt']", ''], '.']]
477
+ )
478
+ end
479
+
480
+ it 'should handle any inline' do
481
+ text = "should be [strong{marked as strong}]."
482
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
483
+ converted = noramark.html
484
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
485
+ expect(body.element_children[0].selector_and_children).to eq(
486
+ ['div.pgroup',
487
+ ['p', 'should be ', ['strong', 'marked as strong'],'.']]
488
+ )
489
+ end
490
+
491
+ it 'should convert inline command within line block' do
492
+ text = "h1: [tcy{20}]縦中横タイトル"
493
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
494
+ converted = noramark.html
495
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
496
+ expect(body.element_children[0].selector_and_children).to eq ['h1', ['span.tcy', '20'], '縦中横タイトル']
497
+ end
498
+
499
+ it 'should handle ruby' do
500
+ text = "[ruby(とんぼ){蜻蛉}]の[ruby(めがね){眼鏡}]はみずいろめがね"
501
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
502
+ converted = noramark.html
503
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
504
+ expect(body.element_children[0].selector_and_children).to eq ['div.pgroup', ['p',
505
+ ['ruby', '蜻蛉', ['rp','('],['rt','とんぼ'],['rp', ')']],
506
+ 'の',
507
+ ['ruby', '眼鏡', ['rp','('],['rt','めがね'],['rp', ')']],
508
+ 'はみずいろめがね']]
509
+ end
510
+
511
+ it 'should handle tatechuyoko' do
512
+ text = "[tcy{10}]年前のことだった"
513
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
514
+ converted = noramark.html
515
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
516
+ expect(body.element_children[0].selector_and_children).to eq(
517
+ ['div.pgroup',
518
+ ['p', ['span.tcy', '10'], '年前のことだった']
519
+ ])
520
+ end
521
+
522
+ it 'should handle ordered list ' do
523
+ text = "this is normal line.\n1: for the 1st.\n2: secondly, blah.\n3: and last...\nthe ordered list ends."
524
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
525
+ converted = noramark.html
526
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
527
+ expect(body.element_children.size).to eq 3
528
+ expect(body.element_children[0].selector_and_children).to eq(
529
+ ['div.pgroup',
530
+ ['p', 'this is normal line.']
531
+ ])
532
+ expect(body.element_children[1].selector_and_children).to eq(
533
+ ['ol',
534
+ ['li', 'for the 1st.'],
535
+ ['li', 'secondly, blah.'],
536
+ ['li', 'and last...']
537
+ ])
538
+ expect(body.element_children[2].selector_and_children).to eq(
539
+ ['div.pgroup',
540
+ ['p', 'the ordered list ends.']
541
+ ])
542
+ end
543
+
544
+ it 'should handle unordered list ' do
545
+ text = "this is normal line.\n*: for the 1st.\n*: secondly, blah.\n*: and last...\nthe ordered list ends."
546
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
547
+ converted = noramark.html
548
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
549
+ expect(body.element_children.size).to eq 3
550
+ expect(body.element_children[0].selector_and_children).to eq(
551
+ ['div.pgroup',
552
+ ['p', 'this is normal line.']
553
+ ])
554
+ expect(body.element_children[1].selector_and_children).to eq(
555
+ ['ul',
556
+ ['li', 'for the 1st.'],
557
+ ['li', 'secondly, blah.'],
558
+ ['li', 'and last...']
559
+ ])
560
+ expect(body.element_children[2].selector_and_children).to eq(
561
+ ['div.pgroup',
562
+ ['p', 'the ordered list ends.']
563
+ ])
564
+ end
565
+
566
+ it 'should handle definition list ' do
567
+ text = "this is normal line.\n;: 1st : this is the first definition\n;: 2nd : blah :blah.\n;: 3rd: this term is the last.\nthe list ends."
568
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
569
+ converted = noramark.html
570
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
571
+ expect(body.element_children.size).to eq 3
572
+ expect(body.element_children[0].selector_and_children).to eq(
573
+ ['div.pgroup',
574
+ ['p', 'this is normal line.']
575
+ ])
576
+ expect(body.element_children[1].selector_and_children).to eq(
577
+ ['dl',
578
+ ['dt', '1st'],['dd', 'this is the first definition'],
579
+ ['dt', '2nd'],['dd', 'blah :blah.'],
580
+ ['dt', '3rd'],['dd', 'this term is the last.'],
581
+ ])
582
+ expect(body.element_children[2].selector_and_children).to eq(
583
+ ['div.pgroup',
584
+ ['p', 'the list ends.']
585
+ ])
586
+ end
587
+
588
+ it 'should escape html' do
589
+ text = ";:definition<div>:</div>&"
590
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
591
+ converted = noramark.html
592
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
593
+ expect(body.element_children[0].selector_and_children).to eq(
594
+ ['dl',
595
+ ['dt', 'definition<div>'],['dd', '</div>&']
596
+ ])
597
+ end
598
+
599
+ it 'should specify stylesheets' do
600
+ text = "stylesheets:css/default.css, css/specific.css, css/iphone.css:(only screen and (min-device-width : 320px) and (max-device-width : 480px))\n\ntext."
601
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the document title')
602
+ converted = noramark.html
603
+ head = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:head')
604
+ expect(head.element_children[0].a).to eq ['title', 'the document title']
605
+ expect(head.element_children[1].a).to eq ["link[rel='stylesheet'][type='text/css'][href='css/default.css']", '']
606
+ expect(head.element_children[2].a).to eq ["link[rel='stylesheet'][type='text/css'][href='css/specific.css']", '']
607
+ expect(head.element_children[3].a).to eq ["link[rel='stylesheet'][type='text/css'][media='only screen and (min-device-width : 320px) and (max-device-width : 480px)'][href='css/iphone.css']", '']
608
+
609
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
610
+ expect(body.element_children[0].selector_and_children).to eq(
611
+ ['div.pgroup',
612
+ ['p',
613
+ 'text.']])
614
+ end
615
+
616
+ it 'should specify title' do
617
+ text = "title:the title of the book in the text.\n\ntext."
618
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
619
+ converted = noramark.html
620
+ head = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:head')
621
+ expect(head.element_children[0].a).to eq ['title', 'the title of the book in the text.']
622
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
623
+ expect(body.element_children[0].selector_and_children).to eq(
624
+ ['div.pgroup',
625
+ ['p',
626
+ 'text.']])
627
+
628
+ end
629
+
630
+ it 'should specify title on each page' do
631
+ text = "title:page1\n\n1st page.\nnewpage:\ntitle:page2\nh1:2nd page"
632
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title', :paragraph_style => :use_paragraph_group)
633
+ converted = noramark.html
634
+ # 1st page
635
+ head = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:head')
636
+ expect(head.element_children[0].a).to eq ['title', 'page1']
637
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
638
+ expect(body.element_children[0].selector_and_children).to eq(
639
+ ['div.pgroup',
640
+ ['p',
641
+ '1st page.']])
642
+ # 2nd page
643
+ head = Nokogiri::XML::Document.parse(converted[1]).root.at_xpath('xmlns:head')
644
+ expect(head.element_children[0].a).to eq ['title', 'page2']
645
+ body = Nokogiri::XML::Document.parse(converted[1]).root.at_xpath('xmlns:body')
646
+ expect(body.element_children[0].selector_and_children).to eq(
647
+ ['h1',"2nd page"])
648
+ end
649
+
650
+
651
+ it 'should ignore comments' do
652
+ text = "# この行はコメントです\nここから、パラグラフがはじまります。\n # これもコメント\n「二行目です。」\n三行目です。\n\n# これもコメント\n\n ここから、次のパラグラフです。"
653
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
654
+ converted = noramark.html
655
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
656
+ expect(body.element_children.size).to eq 2
657
+
658
+ expect(body.element_children[0].selector_and_children).to eq(
659
+ ['div.pgroup',
660
+ ['p', 'ここから、パラグラフがはじまります。'],
661
+ ['p.noindent', '「二行目です。」'],
662
+ ['p', '三行目です。']
663
+ ]
664
+ )
665
+
666
+ expect(body.element_children[1].selector_and_children).to eq(
667
+ ['div.pgroup',
668
+ ['p', 'ここから、次のパラグラフです。']]
669
+ )
670
+ end
671
+
672
+ it 'should handle preprocessor' do
673
+ text = "pre-preprocess text"
674
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title') do
675
+ |nora|
676
+ nora.preprocessor do
677
+ |text|
678
+ text.gsub('pre-preprocess', 'post-process')
679
+ end
680
+ end
681
+ converted = noramark.html
682
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
683
+ expect(body.element_children[0].selector_and_children).to eq(
684
+ ['div.pgroup',
685
+ ['p', 'post-process text'],
686
+ ]
687
+ )
688
+ end
689
+
690
+ it 'should convert h1 in article after title' do
691
+ text = "stylesheets: css/default.css\ntitle: foo\narticle.atogaki {\n\nh1: あとがき。\n\natogaki\n}"
692
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
693
+ converted = noramark.html
694
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
695
+ expect(body.element_children[0].selector_and_children).to eq(
696
+ ["article.atogaki",
697
+ ["h1", "あとがき。"],
698
+ ["div.pgroup",
699
+ ["p", "atogaki"]]]
700
+ )
701
+ end
702
+
703
+ it 'should convert preformatted text' do
704
+ text = <<EOF
705
+ normal line.
706
+ pre <<END
707
+ d {
708
+ this will not converted to div or p or pgroup.
709
+ line_command: this will be not converted too.
710
+ }
711
+ END
712
+ EOF
713
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
714
+ converted = noramark.html
715
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
716
+ expect(body.element_children[0].selector_and_children).to eq(["div.pgroup", ["p", "normal line."]])
717
+ expect(body.element_children[1].selector_and_children).to eq(["pre", "d {\n this will not converted to div or p or pgroup.\nline_command: this will be not converted too.\n}"])
718
+ end
719
+ it 'should convert preformatted code' do
720
+ text = <<EOF
721
+ normal line.
722
+ precode <<END
723
+ d {
724
+ this will not converted to div or p or pgroup.
725
+ line_command: this will be not converted too.
726
+ }
727
+ END
728
+ normal line again.
729
+ EOF
730
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
731
+ converted = noramark.html
732
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
733
+ expect(body.element_children[0].selector_and_children).to eq(["div.pgroup", ["p", "normal line."]])
734
+ expect(body.element_children[1].selector_and_children).to eq(["pre", ["code", "d {\n this will not converted to div or p or pgroup.\nline_command: this will be not converted too.\n}"]])
735
+ expect(body.element_children[2].selector_and_children).to eq(["div.pgroup", ["p", "normal line again."]])
736
+ end
737
+
738
+ it 'should raise error' do
739
+ text = "d {\n block is\nd {\n nested but\nd {\n not terminated }"
740
+ expect { NoraMark::Document.parse(text, :lang => 'ja', :title => 'foo') }.to raise_error KPeg::CompiledParser::ParseError
741
+ end
742
+
743
+ describe 'markdown style' do
744
+ it 'should convert markdown style heading' do
745
+ text = "=: タイトルです。\r\nこれは、セクションの中です。"
746
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
747
+ converted = noramark.html
748
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
749
+ expect(body.element_children.size).to eq 1
750
+ expect(body.element_children[0].selector_and_children).to eq(
751
+ ['section',
752
+ ['h1', 'タイトルです。'],
753
+ ['div.pgroup',
754
+ ['p', 'これは、セクションの中です。']]]
755
+ )
756
+ end
757
+ it 'should markdown style heading interrupted by other headed section' do
758
+ text = "=: タイトルです。\r\nこれは、セクションの中です。\n =: また次のセクションです。\n次のセクションの中です。"
759
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
760
+ converted = noramark.html
761
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
762
+ expect(body.element_children.size).to eq 2
763
+ expect(body.element_children[0].selector_and_children).to eq(
764
+ ['section',
765
+ ['h1', 'タイトルです。'],
766
+ ['div.pgroup',
767
+ ['p', 'これは、セクションの中です。']]])
768
+ expect(body.element_children[1].selector_and_children).to eq(
769
+ ['section',
770
+ ['h1', 'また次のセクションです。'],
771
+ ['div.pgroup',
772
+ ['p', '次のセクションの中です。']]]
773
+ )
774
+ end
775
+ it 'should markdown style heading not interrupted by other explicit section' do
776
+ text = "=: タイトルです。\r\nこれは、セクションの中です。\n section {\n h2: また次のセクションです。\n入れ子になります。\n}\nこのように。"
777
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
778
+ converted = noramark.html
779
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
780
+ expect(body.element_children.size).to eq 1
781
+ expect(body.element_children[0].selector_and_children).to eq(
782
+ ['section',
783
+ ['h1', 'タイトルです。'],
784
+ ['div.pgroup',
785
+ ['p', 'これは、セクションの中です。']],
786
+ ['section',
787
+ ['h2', 'また次のセクションです。'],
788
+ ['div.pgroup',
789
+ ['p', '入れ子になります。']]],
790
+ ['div.pgroup',
791
+ ['p', 'このように。']]]
792
+ )
793
+ end
794
+ it 'should markdown style heading not interrupted by smaller section' do
795
+ text = "=: タイトルです。\r\nこれは、セクションの中です。\n ==: また次のセクションです。\n 入れ子になります。\n===: さらに中のセクション \nさらに入れ子になっているはず。\n=:ここで次のセクションです。\n脱出しているはずです。"
796
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
797
+ converted = noramark.html
798
+ body = Nokogiri::XML::Document.parse(converted[0]).root.at_xpath('xmlns:body')
799
+ expect(body.element_children.size).to eq 2
800
+ expect(body.element_children[0].selector_and_children).to eq(
801
+ ['section',
802
+ ['h1', 'タイトルです。'],
803
+ ['div.pgroup',
804
+ ['p', 'これは、セクションの中です。']],
805
+ ['section',
806
+ ['h2', 'また次のセクションです。'],
807
+ ['div.pgroup',
808
+ ['p', '入れ子になります。']],
809
+ ['section',
810
+ ['h3', 'さらに中のセクション'],
811
+ ['div.pgroup',
812
+ ['p', 'さらに入れ子になっているはず。']]]]] )
813
+ expect(body.element_children[1].selector_and_children).to eq(
814
+ ['section',
815
+ ['h1', 'ここで次のセクションです。'],
816
+ ['div.pgroup',
817
+ ['p', '脱出しているはずです。']]])
818
+
819
+ end
820
+ end
821
+ describe 'create file' do
822
+ before { @basedir = File.join(File.dirname(__FILE__), 'created_files') }
823
+ after { Dir.glob(File.join(@basedir, '*.xhtml')) { |file| File.delete file } }
824
+ it 'should create default file' do
825
+ text = "some text"
826
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the title')
827
+ noramark.html.write_as_files(directory: @basedir)
828
+ expect(File.basename(Dir.glob(File.join(@basedir, '*.xhtml'))[0])).to match /noramark_[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}_00001.xhtml/
829
+ end
830
+ it 'should create named file' do
831
+ text = "some text\nnewpage:\nnext page"
832
+ noramark = NoraMark::Document.parse(text, :lang => 'ja', :title => 'the document title', filename_base: 'nora-test-file', sequence_format: '%03d' )
833
+ noramark.html.write_as_files(directory: @basedir)
834
+ files = Dir.glob(File.join(@basedir, '*.xhtml'))
835
+ expect(File.basename(files[0])).to eq 'nora-test-file_001.xhtml'
836
+ expect(File.basename(files[1])).to eq 'nora-test-file_002.xhtml'
837
+ end
838
+ end
839
+ end
840
+ end