metanorma-standoc 1.5.2 → 1.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/lib/asciidoctor/standoc/base_structured_text_preprocessor.rb +123 -0
  3. data/lib/asciidoctor/standoc/basicdoc.rng +31 -1
  4. data/lib/asciidoctor/standoc/cleanup.rb +1 -0
  5. data/lib/asciidoctor/standoc/cleanup_amend.rb +54 -0
  6. data/lib/asciidoctor/standoc/cleanup_block.rb +0 -2
  7. data/lib/asciidoctor/standoc/cleanup_footnotes.rb +0 -3
  8. data/lib/asciidoctor/standoc/cleanup_inline.rb +6 -1
  9. data/lib/asciidoctor/standoc/cleanup_section.rb +1 -2
  10. data/lib/asciidoctor/standoc/converter.rb +3 -1
  11. data/lib/asciidoctor/standoc/front_contributor.rb +15 -7
  12. data/lib/asciidoctor/standoc/inline.rb +13 -1
  13. data/lib/asciidoctor/standoc/isodoc.rng +108 -8
  14. data/lib/asciidoctor/standoc/json2_text_preprocessor.rb +43 -0
  15. data/lib/asciidoctor/standoc/macros.rb +45 -33
  16. data/lib/asciidoctor/standoc/section.rb +8 -3
  17. data/lib/asciidoctor/standoc/table.rb +3 -2
  18. data/lib/asciidoctor/standoc/views/datamodel/model_representation.adoc.erb +10 -10
  19. data/lib/asciidoctor/standoc/yaml2_text_preprocessor.rb +43 -0
  20. data/lib/liquid/custom_blocks/key_iterator.rb +21 -0
  21. data/lib/liquid/custom_filters/values.rb +7 -0
  22. data/lib/metanorma/standoc/version.rb +1 -1
  23. data/metanorma-standoc.gemspec +1 -3
  24. data/spec/asciidoctor-standoc/blocks_spec.rb +254 -142
  25. data/spec/asciidoctor-standoc/cleanup_spec.rb +31 -31
  26. data/spec/asciidoctor-standoc/macros_json2text_spec.rb +10 -0
  27. data/spec/asciidoctor-standoc/macros_spec.rb +2 -0
  28. data/spec/asciidoctor-standoc/macros_yaml2text_spec.rb +5 -560
  29. data/spec/asciidoctor-standoc/section_spec.rb +0 -1
  30. data/spec/asciidoctor-standoc/table_spec.rb +112 -112
  31. data/spec/asciidoctor-standoc/validate_spec.rb +5 -1
  32. data/spec/examples/codes_table.html +1365 -1365
  33. data/spec/fixtures/macros_datamodel/address_class_profile.xml +46 -46
  34. data/spec/fixtures/macros_datamodel/address_component_profile.xml +21 -21
  35. data/spec/fixtures/macros_datamodel/blank_definition_profile.xml +21 -21
  36. data/spec/spec_helper.rb +110 -109
  37. data/spec/support/shared_examples/structured_data_2_text_preprocessor.rb +583 -0
  38. metadata +24 -3
  39. data/lib/asciidoctor/standoc/macros_yaml2text.rb +0 -165
@@ -0,0 +1,583 @@
1
+ RSpec.shared_examples "structured data 2 text preprocessor" do
2
+ describe "#process" do
3
+ let(:example_file) { "example.#{extention}" }
4
+
5
+ before do
6
+ File.open(example_file, "w") do |n|
7
+ n.puts(transform_to_type(example_content))
8
+ end
9
+ end
10
+
11
+ after do
12
+ FileUtils.rm_rf(example_file)
13
+ end
14
+
15
+ context "Array of hashes" do
16
+ let(:example_content) do
17
+ [{ "name" => "spaghetti",
18
+ "desc" => "wheat noodles of 9mm diameter",
19
+ "symbol" => "SPAG",
20
+ "symbol_def" => "the situation is message like spaghetti at a kid's meal" }]
21
+ end
22
+ let(:input) do
23
+ <<~TEXT
24
+ = Document title
25
+ Author
26
+ :docfile: test.adoc
27
+ :nodoc:
28
+ :novalid:
29
+ :no-isobib:
30
+ :imagesdir: spec/assets
31
+
32
+ [#{extention}2text,#{example_file},my_context]
33
+ ----
34
+ {my_context.*,item,EOF}
35
+ {item.name}:: {item.desc}
36
+ {EOF}
37
+ ----
38
+ TEXT
39
+ end
40
+ let(:output) do
41
+ <<~TEXT
42
+ #{BLANK_HDR}
43
+ <sections>
44
+ <dl id='_'>
45
+ <dt>spaghetti</dt>
46
+ <dd>
47
+ <p id='_'>wheat noodles of 9mm diameter</p>
48
+ </dd>
49
+ </dl>
50
+ </sections>
51
+ </standard-document>
52
+ TEXT
53
+ end
54
+
55
+ it "correctly renders input" do
56
+ expect(
57
+ xmlpp(
58
+ strip_guid(
59
+ Asciidoctor.convert(input,
60
+ backend: :standoc,
61
+ header_footer: true)
62
+ )
63
+ )
64
+ ).to(be_equivalent_to(xmlpp(output)))
65
+ end
66
+ end
67
+
68
+ context "An array of strings" do
69
+ let(:example_content) do
70
+ ["lorem", "ipsum", "dolor"]
71
+ end
72
+ let(:input) do
73
+ <<~TEXT
74
+ = Document title
75
+ Author
76
+ :docfile: test.adoc
77
+ :nodoc:
78
+ :novalid:
79
+ :no-isobib:
80
+ :imagesdir: spec/assets
81
+
82
+ [#{extention}2text,#{example_file},ar]
83
+ ----
84
+ {ar.*,s,EOS}
85
+ === {s.#} {s}
86
+
87
+ This section is about {s}.
88
+
89
+ {EOS}
90
+ ----
91
+ TEXT
92
+ end
93
+ let(:output) do
94
+ <<~TEXT
95
+ #{BLANK_HDR}
96
+ <sections>
97
+ <clause id="_" inline-header="false" obligation="normative">
98
+ <title>0 lorem</title>
99
+ <p id='_'>This section is about lorem.</p>
100
+ </clause>
101
+ <clause id='_' inline-header='false' obligation='normative'>
102
+ <title>1 ipsum</title>
103
+ <p id='_'>This section is about ipsum.</p>
104
+ </clause>
105
+ <clause id='_' inline-header='false' obligation='normative'>
106
+ <title>2 dolor</title>
107
+ <p id='_'>This section is about dolor.</p>
108
+ </clause>
109
+ </sections>
110
+ </standard-document>
111
+ TEXT
112
+ end
113
+
114
+ it "correctly renders input" do
115
+ expect(
116
+ xmlpp(
117
+ strip_guid(
118
+ Asciidoctor.convert(input,
119
+ backend: :standoc,
120
+ header_footer: true)
121
+ )
122
+ )
123
+ ).to(be_equivalent_to(xmlpp(output)))
124
+ end
125
+ end
126
+
127
+ context "A simple hash" do
128
+ let(:example_content) do
129
+ { "name" => "Lorem ipsum", "desc" => "dolor sit amet" }
130
+ end
131
+ let(:input) do
132
+ <<~TEXT
133
+ = Document title
134
+ Author
135
+ :docfile: test.adoc
136
+ :nodoc:
137
+ :novalid:
138
+ :no-isobib:
139
+ :imagesdir: spec/assets
140
+
141
+ [#{extention}2text,#{example_file},my_item]
142
+ ----
143
+ === {my_item.name}
144
+
145
+ {my_item.desc}
146
+ ----
147
+ TEXT
148
+ end
149
+ let(:output) do
150
+ <<~TEXT
151
+ #{BLANK_HDR}
152
+ <sections>
153
+ <clause id="_" inline-header="false" obligation="normative">
154
+ <title>Lorem ipsum</title>
155
+ <p id='_'>dolor sit amet</p>
156
+ </clause>
157
+ </sections>
158
+ </standard-document>
159
+ TEXT
160
+ end
161
+
162
+ it "correctly renders input" do
163
+ expect(
164
+ xmlpp(
165
+ strip_guid(
166
+ Asciidoctor.convert(input,
167
+ backend: :standoc,
168
+ header_footer: true)
169
+ )
170
+ )
171
+ ).to(be_equivalent_to(xmlpp(output)))
172
+ end
173
+ end
174
+
175
+ context "A simple hash with free keys" do
176
+ let(:example_content) do
177
+ { "name" => "Lorem ipsum", "desc" => "dolor sit amet" }
178
+ end
179
+ let(:input) do
180
+ <<~TEXT
181
+ = Document title
182
+ Author
183
+ :docfile: test.adoc
184
+ :nodoc:
185
+ :novalid:
186
+ :no-isobib:
187
+ :imagesdir: spec/assets
188
+
189
+ [#{extention}2text,#{example_file},my_item]
190
+ ----
191
+ {my_item.*,key,EOI}
192
+ === {key}
193
+
194
+ {my_item[key]}
195
+
196
+ {EOI}
197
+ ----
198
+ TEXT
199
+ end
200
+ let(:output) do
201
+ <<~TEXT
202
+ #{BLANK_HDR}
203
+ <sections>
204
+ <clause id="_" inline-header="false" obligation="normative">
205
+ <title>name</title>
206
+ <p id='_'>Lorem ipsum</p>
207
+ </clause>
208
+ <clause id='_' inline-header='false' obligation='normative'>
209
+ <title>desc</title>
210
+ <p id='_'>dolor sit amet</p>
211
+ </clause>
212
+ </sections>
213
+ </standard-document>
214
+ TEXT
215
+ end
216
+
217
+ it "correctly renders input" do
218
+ expect(
219
+ xmlpp(
220
+ strip_guid(
221
+ Asciidoctor.convert(input,
222
+ backend: :standoc,
223
+ header_footer: true)
224
+ )
225
+ )
226
+ ).to(be_equivalent_to(xmlpp(output)))
227
+ end
228
+ end
229
+
230
+ context "An array of hashes" do
231
+ let(:example_content) do
232
+ [{ "name" => "Lorem", "desc" => "ipsum", "nums" => [2] },
233
+ { "name" => "dolor", "desc" => "sit", "nums" => [] },
234
+ { "name" => "amet", "desc" => "lorem", "nums" => [2, 4, 6] }]
235
+ end
236
+ let(:input) do
237
+ <<~TEXT
238
+ = Document title
239
+ Author
240
+ :docfile: test.adoc
241
+ :nodoc:
242
+ :novalid:
243
+ :no-isobib:
244
+ :imagesdir: spec/assets
245
+
246
+ [#{extention}2text,#{example_file},ar]
247
+ ----
248
+ {ar.*,item,EOF}
249
+
250
+ {item.name}:: {item.desc}
251
+
252
+ {item.nums.*,num,EON}
253
+ - {item.name}: {num}
254
+ {EON}
255
+
256
+ {EOF}
257
+ ----
258
+ TEXT
259
+ end
260
+ let(:output) do
261
+ <<~TEXT
262
+ #{BLANK_HDR}
263
+ <sections>
264
+ <dl id='_'>
265
+ <dt>Lorem</dt>
266
+ <dd>
267
+ <p id='_'>ipsum</p>
268
+ <ul id='_'>
269
+ <li>
270
+ <p id='_'>Lorem: 2</p>
271
+ </li>
272
+ </ul>
273
+ </dd>
274
+ <dt>dolor</dt>
275
+ <dd>
276
+ <p id='_'>sit</p>
277
+ </dd>
278
+ <dt>amet</dt>
279
+ <dd>
280
+ <p id='_'>lorem</p>
281
+ <ul id='_'>
282
+ <li>
283
+ <p id='_'>amet: 2</p>
284
+ </li>
285
+ <li>
286
+ <p id='_'>amet: 4</p>
287
+ </li>
288
+ <li>
289
+ <p id='_'>amet: 6</p>
290
+ </li>
291
+ </ul>
292
+ </dd>
293
+ </dl>
294
+ </sections>
295
+ </standard-document>
296
+ TEXT
297
+ end
298
+
299
+ it "correctly renders input" do
300
+ expect(
301
+ xmlpp(
302
+ strip_guid(
303
+ Asciidoctor.convert(input,
304
+ backend: :standoc,
305
+ header_footer: true)
306
+ )
307
+ )
308
+ ).to(be_equivalent_to(xmlpp(output)))
309
+ end
310
+ end
311
+
312
+ context "An array with interpolated file names, etc. \
313
+ for Asciidoc's consumption" do
314
+ let(:example_content) do
315
+ { "prefix" => "doc-", "items" => ["lorem", "ipsum", "dolor"] }
316
+ end
317
+ let(:input) do
318
+ <<~TEXT
319
+ = Document title
320
+ Author
321
+ :docfile: test.adoc
322
+ :nodoc:
323
+ :novalid:
324
+ :no-isobib:
325
+ :imagesdir: spec/assets
326
+
327
+ [#{extention}2text,#{example_file},#{extention}]
328
+ ------
329
+ First item is {#{extention}.items[0]}.
330
+ Last item is {#{extention}.items[-1]}.
331
+
332
+ {#{extention}.items.*,s,EOS}
333
+ === {s.#} -> {s.# + 1} {s} == {#{extention}.items[s.#]}
334
+
335
+ [source,ruby]
336
+ ----
337
+ include::{#{extention}.prefix}{s.#}.rb[]
338
+ ----
339
+
340
+ {EOS}
341
+ ------
342
+ TEXT
343
+ end
344
+ let(:output) do
345
+ <<~TEXT
346
+ #{BLANK_HDR}
347
+ <preface>
348
+ <foreword id='_' obligation='informative'>
349
+ <title>Foreword</title>
350
+ <p id='_'>First item is lorem. Last item is dolor.</p>
351
+ </foreword>
352
+ </preface>
353
+ <sections>
354
+ <clause id='_' inline-header='false' obligation='normative'>
355
+ <title>0 → 1 lorem == lorem</title>
356
+ <sourcecode lang='ruby' id='_'>link:doc-0.rb[]</sourcecode>
357
+ </clause>
358
+ <clause id='_' inline-header='false' obligation='normative'>
359
+ <title>1 → 2 ipsum == ipsum</title>
360
+ <sourcecode lang='ruby' id='_'>link:doc-1.rb[]</sourcecode>
361
+ </clause>
362
+ <clause id='_' inline-header='false' obligation='normative'>
363
+ <title>2 → 3 dolor == dolor</title>
364
+ <sourcecode lang='ruby' id='_'>link:doc-2.rb[]</sourcecode>
365
+ </clause>
366
+ </sections>
367
+ </standard-document>
368
+ TEXT
369
+ end
370
+
371
+ it "correctly renders input" do
372
+ expect(
373
+ xmlpp(
374
+ strip_guid(
375
+ Asciidoctor.convert(input,
376
+ backend: :standoc,
377
+ header_footer: true)
378
+ )
379
+ )
380
+ ).to(be_equivalent_to(xmlpp(output)))
381
+ end
382
+ end
383
+
384
+ context "Array of language codes" do
385
+ let(:example_content) do
386
+ YAML.safe_load(
387
+ File.read(File.expand_path("../../assets/codes.yml", __dir__))
388
+ )
389
+ end
390
+ let(:input) do
391
+ <<~TEXT
392
+ = Document title
393
+ Author
394
+ :docfile: test.adoc
395
+ :nodoc:
396
+ :novalid:
397
+ :no-isobib:
398
+ :imagesdir: spec/assets
399
+
400
+ [#{extention}2text,#{example_file},ar]
401
+ ----
402
+ {ar.*,item,EOF}
403
+ .{item.values[1]}
404
+ [%noheader,cols="h,1"]
405
+ |===
406
+ {item.*,key,EOK}
407
+ | {key} | {item[key]}
408
+
409
+ {EOK}
410
+ |===
411
+ {EOF}
412
+ ----
413
+ TEXT
414
+ end
415
+ let(:output) do
416
+ <<~TEXT
417
+ #{BLANK_HDR}
418
+ <sections>
419
+ #{File.read(File.expand_path('../../examples/codes_table.html', __dir__))}
420
+ </sections>
421
+ </standard-document>
422
+ TEXT
423
+ end
424
+
425
+ it "correctly renders input" do
426
+ expect(
427
+ xmlpp(
428
+ strip_guid(
429
+ Asciidoctor.convert(input,
430
+ backend: :standoc,
431
+ header_footer: true)
432
+ )
433
+ )
434
+ ).to(be_equivalent_to(xmlpp(output)))
435
+ end
436
+ end
437
+
438
+ context "Nested hash dot notation" do
439
+ let(:example_content) do
440
+ { "data" =>
441
+ { "acadsin-zho-hani-latn-2002" =>
442
+ { "code" => "acadsin-zho-hani-latn-2002",
443
+ "name" => {
444
+ "en" => "Academica Sinica -- Chinese Tongyong Pinyin (2002)",
445
+ },
446
+ "authority" => "acadsin",
447
+ "lang" => { "system" => "iso-639-2", "code" => "zho" },
448
+ "source_script" => "Hani",
449
+ "target_script" => "Latn",
450
+ "system" =>
451
+ { "id" => "2002",
452
+ "specification" => "Academica Sinica -- Chinese Tongyong Pinyin (2002)" },
453
+ "notes" => "NOTE: OGC 11-122r1 code `zho_Hani2Latn_AcadSin_2002`" } } }
454
+ end
455
+ let(:input) do
456
+ <<~TEXT
457
+ = Document title
458
+ Author
459
+ :docfile: test.adoc
460
+ :nodoc:
461
+ :novalid:
462
+ :no-isobib:
463
+ :imagesdir: spec/assets
464
+
465
+ [#{extention}2text,#{example_file},authorities]
466
+ ----
467
+ [cols="a,a,a,a",options="header"]
468
+ |===
469
+ | Script conversion system authority code | Name in English | Notes | Name en
470
+
471
+ {authorities.data.*,key,EOI}
472
+ | {key} | {authorities.data[key]['code']} | {authorities.data[key]['notes']} | {authorities.data[key].name.en}
473
+ {EOI}
474
+
475
+ |===
476
+ ----
477
+ TEXT
478
+ end
479
+ let(:output) do
480
+ <<~TEXT
481
+ #{BLANK_HDR}
482
+ <sections>
483
+ <table id='_'>
484
+ <thead>
485
+ <tr>
486
+ <th valign="top" align='left'>Script conversion system authority code</th>
487
+ <th valign="top" align='left'>Name in English</th>
488
+ <th valign="top" align='left'>Notes</th>
489
+ <th valign="top" align='left'>Name en</th>
490
+ </tr>
491
+ </thead>
492
+ <tbody>
493
+ <tr>
494
+ <td valign="top" align='left'>
495
+ <p id='_'>acadsin-zho-hani-latn-2002</p>
496
+ </td>
497
+ <td valign="top" align='left'>
498
+ <p id='_'>acadsin-zho-hani-latn-2002</p>
499
+ </td>
500
+ <td valign="top" align='left'>
501
+ <note id='_'>
502
+ <p id='_'>
503
+ OGC 11-122r1 code
504
+ <tt>zho_Hani2Latn_AcadSin_2002</tt>
505
+ </p>
506
+ </note>
507
+ </td>
508
+ <td valign="top" align='left'>
509
+ <p id='_'>Academica Sinica — Chinese Tongyong Pinyin (2002)</p>
510
+ </td>
511
+ </tr>
512
+ </tbody>
513
+ </table>
514
+ </sections>
515
+ </standard-document>
516
+ TEXT
517
+ end
518
+
519
+ it "correctly renders input" do
520
+ expect(
521
+ xmlpp(
522
+ strip_guid(
523
+ Asciidoctor.convert(input,
524
+ backend: :standoc,
525
+ header_footer: true)
526
+ )
527
+ )
528
+ ).to(be_equivalent_to(xmlpp(output)))
529
+ end
530
+ end
531
+
532
+ context "Liquid code snippets" do
533
+ let(:example_content) do
534
+ [{ "name" => "One", "show" => true },
535
+ { "name" => "Two", "show" => true },
536
+ { "name" => "Three", "show" => false }]
537
+ end
538
+ let(:input) do
539
+ <<~TEXT
540
+ = Document title
541
+ Author
542
+ :docfile: test.adoc
543
+ :nodoc:
544
+ :novalid:
545
+ :no-isobib:
546
+ :imagesdir: spec/assets
547
+
548
+ [#{extention}2text,#{example_file},my_context]
549
+ ----
550
+ {% for item in my_context %}
551
+ {% if item.show %}
552
+ {{ item.name | upcase }}
553
+ {{ item.name | size }}
554
+ {% endif %}
555
+ {% endfor %}
556
+ ----
557
+ TEXT
558
+ end
559
+ let(:output) do
560
+ <<~TEXT
561
+ #{BLANK_HDR}
562
+ <sections>
563
+ <p id='_'>ONE 3</p>
564
+ <p id='_'>TWO 3</p>
565
+ </sections>
566
+ </standard-document>
567
+ TEXT
568
+ end
569
+
570
+ it "renders liquid markup" do
571
+ expect(
572
+ xmlpp(
573
+ strip_guid(
574
+ Asciidoctor.convert(input,
575
+ backend: :standoc,
576
+ header_footer: true)
577
+ )
578
+ )
579
+ ).to(be_equivalent_to(xmlpp(output)))
580
+ end
581
+ end
582
+ end
583
+ end