====== DokuWiki: Add caption to table ====== * This is a small piece of Javascript code to add a caption to a table. * Works with DokuWiki's default template. * Needs WRAP plugin. * Just add a line of ''%%Caption text%%'' before the table. ===== Code ===== Add the following code to your ''conf/userscript.js''. jQuery('.page div.table>table').each(function () { let $table = jQuery(this); let $prevDiv = $table.parent().prev(); if ($prevDiv.hasClass('wrap_caption') || $prevDiv.hasClass('caption')) { if ($prevDiv.children('p').length > 0) { let captionText = $prevDiv.children('p').html(); $table.prepend('' + captionText + ''); $prevDiv.remove(); if (location.hash) { location.href = location.hash; } } } }); Probably you like to add the following lines or similar to your ''conf/userall.css''. caption { text-align: center; font-weight: bold; } ===== Test drive ===== ==== A table without caption (default) ==== The default syntax of a table is like this: ^ No. ^ Description ^ Quantity ^ Remark ^ | 1 | Lorem ipsum dolor sit amet | 100 |Euismod posuere mus lobortis | | 2 | Konsectetur adipiscing elit molestie | 50 |Aenean sed ornare| | 3 | Scelerisque vestibulum metus risus |25 |Sociis mollis, morbi proin suspendisse magnis| | 4 | Nunc a commodo nullam | 200 | | The above code is rendered as follows: ^ No. ^ Description ^ Quantity ^ Remark ^ | 1 | Lorem ipsum dolor sit amet | 100 |Euismod posuere mus lobortis | | 2 | Konsectetur adipiscing elit molestie | 50 |Aenean sed ornare| | 3 | Scelerisque vestibulum metus risus |25 |Sociis mollis, morbi proin suspendisse magnis| | 4 | Nunc a commodo nullam | 200 | | ==== A table with a caption ==== Add one line above the table like this: Table 2. Caption text is displayed like this ^ No. ^ Description ^ Quantity ^ Remark ^ | 1 | Lorem ipsum dolor sit amet | 100 |Euismod posuere mus lobortis | | 2 | Konsectetur adipiscing elit molestie | 50 |Aenean sed ornare| | 3 | Scelerisque vestibulum metus risus |25 |Sociis mollis, morbi proin suspendisse magnis| | 4 | Nunc a commodo nullam | 200 | | this is rendered as: Table 2. Caption text is displayed like this ^ No. ^ Description ^ Quantity ^ Remark ^ | 1 | Lorem ipsum dolor sit amet | 100 |Euismod posuere mus lobortis | | 2 | Konsectetur adipiscing elit molestie | 50 |Aenean sed ornare| | 3 | Scelerisque vestibulum metus risus |25 |Sociis mollis, morbi proin suspendisse magnis| | 4 | Nunc a commodo nullam | 200 | | Since the caption is inserted within the table, it aligns with the table, as shown below with a small table. Table 3. Caption text is displayed like this ^ No. ^ Description ^ Quantity ^ | 1 | Lorem ipsum dolor sit amet | 100 | | 2 | Konsectetur adipiscing elit molestie | 50 | | 3 | Scelerisque vestibulum metus risus |25 | | 4 | Nunc a commodo nullam | 200 | ===== Mechanism ===== The original HTML text is:

Caption text

...
This is transformed to:
...
Caption text
Since this transformation is made **after** the rendering by DokuWiki, the normal formatting syntax (such as ''%%//italic//%%'', ''%%__underline__%%'', etc.) can be used in the caption text.