-
Notifications
You must be signed in to change notification settings - Fork 0
How to set the background color of a table cell
This tip explains how to set the background color of a table cell. It focuses on the DocBook output for processing by XSL-FO, though the same trick can be used for HTML output.
To set the background color of a table cell, you need to get AsciiDoc to emit a <?dbfo bgcolor="color"?> processing instruction inside the cell <entry> tag.
Here’s the example AsciiDoc that we’ll use as input:
[cols="2"]
|===
|plain
|plain
|RED
{set:cellbgcolor:red}
|plain
{set:cellbgcolor!}
|===
This solution is specific to AsciiDoc (not Asciidoctor).
We need to hack on the tabletags-default template in docbook45.conf (or override in a conf file named asciidoc.conf in the same directory as your file). Notice in the bodydata record, we’ll check for the cellbgcolor attribute and, if it’s set, we’ll emit the processing instruction.
[tabletags-default]
# non-relevant lines hidden
bodydata=<entry align="{halign}" valign="{valign}"{colspan@1:: namest="col_{colstart}" nameend="col_{colend}"}{morerows@0:: morerows="{morerows}"}>{cellbgcolor?<?dbfo bgcolor="{cellbgcolor}"?>}|</entry>
Specifically, take note of this addition:
{cellbgcolor?<?dbfo bgcolor="{cellbgcolor}"?>}
When you run AsciiDoc on the source file, you’ll get the following DocBook result:
<informaltable
frame="all"
rowsep="1" colsep="1"
>
<tgroup cols="2">
<colspec colname="col_1" colwidth="50*"/>
<colspec colname="col_2" colwidth="50*"/>
<tbody>
<row>
<entry align="left" valign="top"><simpara>a</simpara></entry>
<entry align="left" valign="top"><simpara>b</simpara></entry>
</row>
<row>
<entry align="left" valign="top"><?dbfo bgcolor="red"?><simpara>d</simpara></entry>
<entry align="left" valign="top"><simpara>e</simpara></entry>
</row>
</tbody>
</tgroup>
</informaltable>
Notice the <?dbfo bgcolor="red"?> in the output.