Search

Dark theme | Light theme

November 5, 2014

Awesome Asciidoctor: CSV and DSV Tables

With Asciidoctor we can create tables where the header and rows are in CSV (Comma Separated Values) and DSV (Delimiter Separated Values) format. Normally we use a pipe-symbol (|) to separate cell values. This is actually PSV (Prefix Separated Values) :-).

In the following Asciidoctor markup we create a very simple table with a header and two rows using CSV:

 
1
2
3
4
5
6
7
8
9
= Tables
 
== CSV table
 
[format="csv", options="header"]
|===
Writing tools, Awesomeness
Asciidoctor, Oh yeah!
MS Word, No!
|===

We generate this into HTML and we get the following result:

Asciidoctor provides also another way to define the above table:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
= Tables
 
== CSV table
 
// Define table using CSV syntax.
// The start and end of the table is defined
// as ,=== instead of |===.
// Also the header row is followed by new line,
// to indicate it is the header row.
 
,===
Writing tools, Awesomeness
 
Asciidoctor, Oh yeah!
MS Word, No!
,===
 
 
// We can also specify a separator.
 
[format="csv", separator=";", options="header"]
|===
Name;Description
Asciidoctor;Awesome way to write documentation
|===

The previous samples used a comma to separate values, but we can also use colon (:). The next sample contains tables defined with DSV:

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
== DSV table
 
[format="dsv", options="header"]
|===
Writing tools:Awesomeness
Asciidoctor:Oh yeah!
MS Word:No!
|===
 
// Alternative syntax:
 
:===
Writing tools: Awesomeness
 
Asciidoctor: Oh yeah!
MS Word: No!
:===

With the include directive we can also include data from an external CSV of DSV file to create a table (of course also the traditional pipe-symbol separated format can be in an external file):

 
1
2
3
4
5
= Table with external data
 
[format="csv", options="header"]
|===
include::tools.csv[]
|===

The file tools.cv has the following contents:

 
1
2
Writing tools, Awesomeness
Asciidoctor, Oh yeah!
MS Word, No!

Code written with Asciidoctor 1.5.0.