asciidoc-parser 0.19.0

Parser for AsciiDoc format
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
use crate::tests::prelude::*;

track_file!("ref/asciidoc-lang/docs/modules/blocks/pages/hard-line-breaks.adoc");

non_normative!(
    r#"
= Hard Line Breaks

Adjacent lines of regular text in AsciiDoc are combined into a single paragraph when converted.
That means you can wrap paragraph text in the source document, either at a specific column or by putting each sentence or phrase on its own line.
The line breaks separating adjacent lines won't appear in the output.
Instead, the line break will be (effectively) converted to a single space.
(In fact, all repeating space characters are reduced to a single space, just like in HTML.)

TIP: Hard line breaks are automatically retained in xref:verbatim:literal-blocks.adoc[literal], xref:verbatim:listing-blocks.adoc[listing], xref:verbatim:source-blocks.adoc[source], and xref:verses.adoc[verse] blocks and paragraphs.

If you want line breaks in a paragraph to be preserved, there are several techniques you can use.
"#
);

#[test]
fn space_plus_syntax() {
    verifies!(
        r#"
For any single line, you can terminate it with a space followed by a plus sign.
This syntax signals to the processor to end the line in the output with a hard line break.

[source]
----
line one +
line two
----

"#
    );

    let doc = Parser::default().parse("line one +\nline two");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "line one +\nline two",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "line one<br>\nline two",
                },
                source: Span {
                    data: "line one +\nline two",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "line one +\nline two",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
fn hardbreaks_option_paragraph() {
    verifies!(
        r#"
To add this behavior to every line in the paragraph, set the `hardbreaks` option on the paragraph instead.

[source]
----
[%hardbreaks]
line one
line two
----

"#
    );

    let doc = Parser::default().parse("[%hardbreaks]\nline one\nline two");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "line one\nline two",
                        line: 2,
                        col: 1,
                        offset: 14,
                    },
                    rendered: "line one<br>\nline two",
                },
                source: Span {
                    data: "[%hardbreaks]\nline one\nline two",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: Some(Attrlist {
                    attributes: &[ElementAttribute {
                        name: None,
                        value: "%hardbreaks",
                        shorthand_items: &["%hardbreaks"],
                    },],
                    anchor: None,
                    source: Span {
                        data: "%hardbreaks",
                        line: 1,
                        col: 2,
                        offset: 1,
                    },
                },),
            },),],
            source: Span {
                data: "[%hardbreaks]\nline one\nline two",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
fn hardbreaks_option_document_attribute() {
    verifies!(
        r#"
Alternately, you can tell the processor to preserve all line breaks in every paragraph in the document by setting the `hardbreaks-option` document attribute, though this option should be used wisely.

[source]
----
:hardbreaks-option:

line one
line two
----

"#
    );

    let doc = Parser::default().parse(":hardbreaks-option:\n\nline one\nline two");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[Attribute {
                    name: Span {
                        data: "hardbreaks-option",
                        line: 1,
                        col: 2,
                        offset: 1,
                    },
                    value_source: None,
                    value: InterpretedValue::Set,
                    source: Span {
                        data: ":hardbreaks-option:",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                },],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: ":hardbreaks-option:",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "line one\nline two",
                        line: 3,
                        col: 1,
                        offset: 21,
                    },
                    rendered: "line one<br>\nline two",
                },
                source: Span {
                    data: "line one\nline two",
                    line: 3,
                    col: 1,
                    offset: 21,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: ":hardbreaks-option:\n\nline one\nline two",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
fn hard_line_break_solo() {
    verifies!(
        r#"
To insert an empty line in the middle of the paragraph, you can use the hard line break syntax on a line by itself.
This allows you to insert space between lines in the output without introducing separate paragraphs.

[source]
----
line one +
 +
line three
----

"#
    );

    let doc = Parser::default().parse("line one +\n +\nline three");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "line one +\n +\nline three",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "line one<br>\n<br>\nline three",
                },
                source: Span {
                    data: "line one +\n +\nline three",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "line one +\n +\nline three",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
fn start_with_hard_line_break() {
    verifies!(
        r#"
If you want the paragraph to start with a hard line break, you need to place an `\{empty}` attribute reference at the start of the line.
That's because a line that starts with a space has a different meaning.
The `\{empty}` attribute reference allows you to insert nothing at the start of the line.

[source]
----
{empty} +
line two
----

"#
    );

    let doc = Parser::default().parse("{empty} +\nline two");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "{empty} +\nline two",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "<br>\nline two",
                },
                source: Span {
                    data: "{empty} +\nline two",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "{empty} +\nline two",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
fn use_empty_consistently() {
    verifies!(
        r#"
To be consistent, you can always start an empty line with `\{empty}`.

[source]
----
{empty} +
line two +
{empty} +
line four
----

Note that `empty` is a built-in document attribute in AsciiDoc.

"#
    );

    let doc = Parser::default().parse("{empty} +\nline two +\n{empty} +\nline four");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "{empty} +\nline two +\n{empty} +\nline four",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "<br>\nline two<br>\n<br>\nline four",
                },
                source: Span {
                    data: "{empty} +\nline two +\n{empty} +\nline four",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "{empty} +\nline two +\n{empty} +\nline four",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
#[ignore]
fn per_line_dialogue_syntax() {
    // TO DO (https://github.com/asciidoc-rs/asciidoc-parser/issues/440): Await spec clarity on correct behavior for this example.
    verifies!(
        r#"
If you're writing a story with dialogue, and you want to prefix the dialogue lines with `--`, be aware that you are going to get a conflict between the hard line break and the automatic endash replacement.
For example:

[source]
----
-- Come here! -- I said. +
-- What is it? -- replied Lance.
----

The second `--` will not only be substituted with an endash, it will also consume the preceding newline.
As a result, both lines in the source will end up appearing on the same line in the output.
To circumvent this problem, you can need to add an extra newline for the endash replacement to consume.

[source]
----
[%hardbreaks]
-- Come here! -- I said.
{empty}
-- What is it? -- replied Lance.
----

Then each dialog will appear on its own line.

"#
    );

    let doc =
        Parser::default().parse("-- Come here! -- I said. +\n-- What is it? -- replied Lance.");

    dbg!(&doc);
    todo!("This appears to be generating wrong results");
}

#[test]
fn inline_line_break_syntax() {
    verifies!(
        r#"
[#per-line]
== Inline line break syntax

To preserve a line break in a paragraph, insert a space followed by a plus sign (`{plus}`) at the end of the line.
This results in a visible line break (e.g., `<br>`) following the line.

.Line breaks preserved using a space followed by the plus sign ({plus})
[#ex-plus]
----
include::example$paragraph.adoc[tag=hb]
----

The result of <<ex-plus>> is displayed below.

====
include::example$paragraph.adoc[tag=hb]
====

"#
    );

    let doc = Parser::default().parse("Rubies are red, +\nTopazes are blue.");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "Rubies are red, +\nTopazes are blue.",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "Rubies are red,<br>\nTopazes are blue.",
                },
                source: Span {
                    data: "Rubies are red, +\nTopazes are blue.",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "Rubies are red, +\nTopazes are blue.",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
fn hardbreaks_option_syntax() {
    verifies!(
        r#"
[#per-block]
== hardbreaks option

To retain all of the line breaks in an entire paragraph, assign the `hardbreaks` option to the paragraph using an attribute list.

.Line breaks preserved using the hardbreaks option
[#ex-option]
----
include::example$paragraph.adoc[tag=hb-p]
----

The result of <<ex-option>> is displayed below.

====
include::example$paragraph.adoc[tag=hb-p]
====

"#
    );

    let doc = Parser::default().parse("[%hardbreaks]\nRuby is red.\nJava is beige.");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "Ruby is red.\nJava is beige.",
                        line: 2,
                        col: 1,
                        offset: 14,
                    },
                    rendered: "Ruby is red.<br>\nJava is beige.",
                },
                source: Span {
                    data: "[%hardbreaks]\nRuby is red.\nJava is beige.",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: Some(Attrlist {
                    attributes: &[ElementAttribute {
                        name: None,
                        value: "%hardbreaks",
                        shorthand_items: &["%hardbreaks"],
                    },],
                    anchor: None,
                    source: Span {
                        data: "%hardbreaks",
                        line: 1,
                        col: 2,
                        offset: 1,
                    },
                },),
            },),],
            source: Span {
                data: "[%hardbreaks]\nRuby is red.\nJava is beige.",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}

#[test]
fn hardbreaks_option_attribute() {
    verifies!(
        r#"
[#per-document]
== hardbreaks-option attribute

To preserve line breaks in all paragraphs throughout your entire document, set the `hardbreaks-option` document attribute in the document header.

.Line breaks preserved throughout the document using the hardbreaks-option attribute
[#ex-attribute]
----
include::example$paragraph.adoc[tag=hb-attr]
----
"#
    );

    let doc = Parser::default()
        .parse("= Line Break Doc Title\n:hardbreaks-option:\n\nRubies are red,\nTopazes are blue.");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: Some(Span {
                    data: "Line Break Doc Title",
                    line: 1,
                    col: 3,
                    offset: 2,
                },),
                title: Some("Line Break Doc Title",),
                attributes: &[Attribute {
                    name: Span {
                        data: "hardbreaks-option",
                        line: 2,
                        col: 2,
                        offset: 24,
                    },
                    value_source: None,
                    value: InterpretedValue::Set,
                    source: Span {
                        data: ":hardbreaks-option:",
                        line: 2,
                        col: 1,
                        offset: 23,
                    },
                },],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "= Line Break Doc Title\n:hardbreaks-option:",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "Rubies are red,\nTopazes are blue.",
                        line: 4,
                        col: 1,
                        offset: 44,
                    },
                    rendered: "Rubies are red,<br>\nTopazes are blue.",
                },
                source: Span {
                    data: "Rubies are red,\nTopazes are blue.",
                    line: 4,
                    col: 1,
                    offset: 44,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "= Line Break Doc Title\n:hardbreaks-option:\n\nRubies are red,\nTopazes are blue.",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog {
                refs: HashMap::from([]),
                reftext_to_id: HashMap::from([]),
            },
        }
    );
}