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
use crate::tests::prelude::*;

track_file!("ref/asciidoc-lang/docs/modules/tables/pages/build-a-basic-table.adoc");

non_normative!(
    r#"
= Build a Basic Table
:page-aliases: index.adoc

A table is a delimited block that can have optional customizations, such as an ID and a title, as well as table-specific attributes, options, and roles.
However, at its most basic, a table only needs columns and rows.

On this page, you'll learn:

* [x] How to set up an AsciiDoc table block and its attribute list.
* [x] How to add columns to a table using the `cols` attribute.
* [x] How to add cells to a table and arrange them into rows.
* [x] How to designate a row as the table's header row.

"#
);

#[test]
fn create_a_table_with_two_columns_and_three_rows() {
    non_normative!(
        r#"
== Create a table with two columns and three rows
"#
    );

    verifies!(
        r#"

In <<ex-cols>>, we'll assign the `cols` attribute a list of column specifiers.
A column specifier represents a column.

.Set up a table with two columns
[source#ex-cols]
----
[cols="1,1"] <.> <.>
|=== <.>
----
<.> On a new line, create an attribute list.
Set the `cols` attribute, followed by an equals sign (`=`).
<.> Assign a list of comma-separated column specifiers enclosed in double quotation marks (`"`) to `cols`.
Each column specifier represents a column.
<.> On the line directly after the attribute list, enter the opening table delimiter.
A table delimiter is one vertical bar followed by three equals signs (`|===`).
This delimiter starts the table block.

The table in <<ex-cols>> will contain two columns because there are two comma-separated entries in the list assigned to `cols`.
Each entry in the list is called a column specifier.
A [.term]*column specifier* represents a column and the width, alignment, and style properties assigned to that column.
When each column specifier is the same number, in this case the integer `1`, all of the columns`' widths will be identical.
Each column in <<ex-cols>> will be the same width regardless of how much content they contain.

Next, let's add three rows to the table.
Each row has the same number of cells.
Since the table in <<ex-rows>> has two columns, each row will contain two cells.
A cell starts with a vertical bar (`|`).

.Add three rows to the table
[source#ex-rows]
----
[cols="1,1"]
|===
|Cell in column 1, row 1 <.>
|Cell in column 2, row 1 <.>
<.>
|Cell in column 1, row 2
|Cell in column 2, row 2

|Cell in column 1, row 3
|Cell in column 2, row 3 <.>
|=== <.>
----
<.> To create a new cell, press kbd:[Shift+|].
After the vertical bar (`|`), enter the content you want displayed in that cell.
<.> On a new line, start another cell with a `|`.
Each consecutive cell is placed in a separate, consecutive column in a row.
<.> Rows are separated by one or more empty lines.
<.> When you finish adding cells to your table, press kbd:[Enter] to go to a new line.
<.> Enter the closing delimiter (`|===`) to end the table block.

TIP: The suggestion to start each cell on its own line and to separate rows by empty lines is merely a stylistic choice.
You can enter xref:add-cells-and-rows.adoc[more than one cell or all of the cells in a row on the same line] since the processor creates a new cell each time it encounters a vertical bar (`|`).

The table from <<ex-rows>> is displayed below.
It contains two columns and three rows of text positioned and styled using the default alignment, style, border, and width attribute values.

[cols="1,1"]
|===
|Cell in column 1, row 1
|Cell in column 2, row 1

|Cell in column 1, row 2 |Cell in column 2, row 2
|Cell in column 1, row 3 |Cell in column 2, row 3
|===

In addition to the xref:add-columns.adoc[cols attribute], you can identify the number of columns using a xref:add-columns.adoc#column-multiplier[column multiplier] or xref:add-columns.adoc#implicit-cols[the table's first row].
However, the `cols` attribute is required to customize the xref:adjust-column-widths.adoc[width], xref:align-by-column.adoc[alignment], or xref:format-column-content.adoc[style] of a column.

"#
    );

    let doc = Parser::default().parse(
        "[cols=\"1,1\"]\n|===\n|Cell in column 1, row 1\n|Cell in column 2, row 1\n\n|Cell in column 1, row 2 |Cell in column 2, row 2\n|Cell in column 1, row 3 |Cell in column 2, row 3\n|===",
    );

    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::Table(TableBlock {
                columns: &[
                    TableColumn {
                        width: 1,
                        h_align: HorizontalAlignment::Left,
                        v_align: VerticalAlignment::Top,
                        style: ColumnStyle::Default,
                    },
                    TableColumn {
                        width: 1,
                        h_align: HorizontalAlignment::Left,
                        v_align: VerticalAlignment::Top,
                        style: ColumnStyle::Default,
                    }
                ],
                header_row: None,
                body_rows: &[
                    TableRow {
                        cells: &[
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 1, row 1",
                                        line: 3,
                                        col: 2,
                                        offset: 19,
                                    },
                                    rendered: "Cell in column 1, row 1",
                                }),
                            },
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 2, row 1",
                                        line: 4,
                                        col: 2,
                                        offset: 44,
                                    },
                                    rendered: "Cell in column 2, row 1",
                                }),
                            },
                        ],
                    },
                    TableRow {
                        cells: &[
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 1, row 2",
                                        line: 6,
                                        col: 2,
                                        offset: 70,
                                    },
                                    rendered: "Cell in column 1, row 2",
                                }),
                            },
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 2, row 2",
                                        line: 6,
                                        col: 27,
                                        offset: 95,
                                    },
                                    rendered: "Cell in column 2, row 2",
                                }),
                            },
                        ],
                    },
                    TableRow {
                        cells: &[
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 1, row 3",
                                        line: 7,
                                        col: 2,
                                        offset: 120,
                                    },
                                    rendered: "Cell in column 1, row 3",
                                }),
                            },
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 2, row 3",
                                        line: 7,
                                        col: 27,
                                        offset: 145,
                                    },
                                    rendered: "Cell in column 2, row 3",
                                }),
                            },
                        ],
                    },
                ],
                footer_row: None,
                source: Span {
                    data: "[cols=\"1,1\"]\n|===\n|Cell in column 1, row 1\n|Cell in column 2, row 1\n\n|Cell in column 1, row 2 |Cell in column 2, row 2\n|Cell in column 1, row 3 |Cell in column 2, row 3\n|===",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: Some(Attrlist {
                    attributes: &[ElementAttribute {
                        name: Some("cols"),
                        shorthand_items: &[],
                        value: "1,1",
                    }],
                    anchor: None,
                    source: Span {
                        data: "cols=\"1,1\"",
                        line: 1,
                        col: 2,
                        offset: 1,
                    },
                }),
            })],
            source: Span {
                data: "[cols=\"1,1\"]\n|===\n|Cell in column 1, row 1\n|Cell in column 2, row 1\n\n|Cell in column 1, row 2 |Cell in column 2, row 2\n|Cell in column 1, row 3 |Cell in column 2, row 3\n|===",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog::default(),
        }
    );
}

#[test]
fn add_a_header_row_to_the_table() {
    non_normative!(
        r#"
=== Add a header row to the table
"#
    );

    verifies!(
        r#"

Let's add a header row to the table in <<ex-header>>.
You can implicitly identify the first row of a table as a header row by entering all of the first row's cells on the line directly after the opening table delimiter.

.Add a header row to the table
[source#ex-header]
----
[cols="1,1"]
|===
|Cell in column 1, header row |Cell in column 2, header row <.>
<.>
|Cell in column 1, row 2
|Cell in column 2, row 2

|Cell in column 1, row 3
|Cell in column 2, row 3

|Cell in column 1, row 4
|Cell in column 2, row 4
|===
----
<.> On the line directly after the opening delimiter (`|===`), enter all of the first row's cells on a single line.
<.> Leave the line directly after the header row empty.

The table from <<ex-header>> is displayed below.

[cols="1,1"]
|===
|Cell in column 1, header row |Cell in column 2, header row

|Cell in column 1, row 2
|Cell in column 2, row 2

|Cell in column 1, row 3
|Cell in column 2, row 3

|Cell in column 1, row 4
|Cell in column 2, row 4
|===

A header row can also be identified by assigning xref:add-header-row.adoc[header to the options attribute].
"#
    );

    let doc = Parser::default().parse(
        "[cols=\"1,1\"]\n|===\n|Cell in column 1, header row |Cell in column 2, header row\n\n|Cell in column 1, row 2\n|Cell in column 2, row 2\n\n|Cell in column 1, row 3\n|Cell in column 2, row 3\n\n|Cell in column 1, row 4\n|Cell in column 2, row 4\n|===",
    );

    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::Table(TableBlock {
                columns: &[
                    TableColumn {
                        width: 1,
                        h_align: HorizontalAlignment::Left,
                        v_align: VerticalAlignment::Top,
                        style: ColumnStyle::Default,
                    },
                    TableColumn {
                        width: 1,
                        h_align: HorizontalAlignment::Left,
                        v_align: VerticalAlignment::Top,
                        style: ColumnStyle::Default,
                    }
                ],
                header_row: Some(TableRow {
                    cells: &[
                        TableCell {
                            content: TableCellContent::Simple(Content {
                                original: Span {
                                    data: "Cell in column 1, header row",
                                    line: 3,
                                    col: 2,
                                    offset: 19,
                                },
                                rendered: "Cell in column 1, header row",
                            }),
                        },
                        TableCell {
                            content: TableCellContent::Simple(Content {
                                original: Span {
                                    data: "Cell in column 2, header row",
                                    line: 3,
                                    col: 32,
                                    offset: 49,
                                },
                                rendered: "Cell in column 2, header row",
                            }),
                        },
                    ],
                }),
                body_rows: &[
                    TableRow {
                        cells: &[
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 1, row 2",
                                        line: 5,
                                        col: 2,
                                        offset: 80,
                                    },
                                    rendered: "Cell in column 1, row 2",
                                }),
                            },
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 2, row 2",
                                        line: 6,
                                        col: 2,
                                        offset: 105,
                                    },
                                    rendered: "Cell in column 2, row 2",
                                }),
                            },
                        ],
                    },
                    TableRow {
                        cells: &[
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 1, row 3",
                                        line: 8,
                                        col: 2,
                                        offset: 131,
                                    },
                                    rendered: "Cell in column 1, row 3",
                                }),
                            },
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 2, row 3",
                                        line: 9,
                                        col: 2,
                                        offset: 156,
                                    },
                                    rendered: "Cell in column 2, row 3",
                                }),
                            },
                        ],
                    },
                    TableRow {
                        cells: &[
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 1, row 4",
                                        line: 11,
                                        col: 2,
                                        offset: 182,
                                    },
                                    rendered: "Cell in column 1, row 4",
                                }),
                            },
                            TableCell {
                                content: TableCellContent::Simple(Content {
                                    original: Span {
                                        data: "Cell in column 2, row 4",
                                        line: 12,
                                        col: 2,
                                        offset: 207,
                                    },
                                    rendered: "Cell in column 2, row 4",
                                }),
                            },
                        ],
                    },
                ],
                footer_row: None,
                source: Span {
                    data: "[cols=\"1,1\"]\n|===\n|Cell in column 1, header row |Cell in column 2, header row\n\n|Cell in column 1, row 2\n|Cell in column 2, row 2\n\n|Cell in column 1, row 3\n|Cell in column 2, row 3\n\n|Cell in column 1, row 4\n|Cell in column 2, row 4\n|===",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: Some(Attrlist {
                    attributes: &[ElementAttribute {
                        name: Some("cols"),
                        shorthand_items: &[],
                        value: "1,1",
                    }],
                    anchor: None,
                    source: Span {
                        data: "cols=\"1,1\"",
                        line: 1,
                        col: 2,
                        offset: 1,
                    },
                }),
            })],
            source: Span {
                data: "[cols=\"1,1\"]\n|===\n|Cell in column 1, header row |Cell in column 2, header row\n\n|Cell in column 1, row 2\n|Cell in column 2, row 2\n\n|Cell in column 1, row 3\n|Cell in column 2, row 3\n\n|Cell in column 1, row 4\n|Cell in column 2, row 4\n|===",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog::default(),
        }
    );
}