Hi all,
I'm working on a plugin where I need to create different types of headers. These headers use a different header syntax but otherwise practically function as headers most of the time (with a few exceptions) insofar as the Dokuwiki renderer/handler is concerned (i.e. these headers open/close sections).
I was surprised at how hard it was to tie in a custom CSS class to the header() function for the default XHTML renderer in any given Syntax plugin. It seems rewriting this renderer function would be overkill just to add in a class to it can be styled with CSS or addressed easily with javscript.
The approach I settled on was to rewrite the $renderer->doc and just add in the text, I'll post my custom render function below but you can see the logic under the '//Splice in custom class' comment.
This works, but I feel like the XHTML renderer heading() function would benefit from an argument that allows for an array of custom classes to be added for output. I thought it might be worth mentioning. I have seen a few threads about modifying the headers and this would be an easy way to allow this to happen just with CSS.
public function render($mode, Doku_Renderer $renderer, $data)
{
// Split out the variables from the data array
list($title, $state, $lvl, $status, $pos, $meta1, $meta2) = $data;
// Check if there's an open section, if so, close it.
if ($status == true) $renderer->section_close();
// Make a header
$renderer->header(hsc($title),$lvl,$pos);
// If it's xhtml renderer, put in a custom section open, otherwise just use a normal section open.
if ($mode == 'xhtml') {
// Splice in custom class to allow for styling in XHTML output
$lst_header_pos = strrpos($renderer->doc, "<h");
$lst_header = substr($renderer->doc, $lst_header_pos);
if (strstr($lst_header, 'class="') !== false) {
$h_explode = explode('class="', $lst_header);
$rewrite = $h_explode[0] . 'class="my_custom_class ' . $h_explode[1];
} else {
$rewrite = preg_replace("/<h\d\s/", '$0 class="my_custom_class" ', $lst_header);
}
$renderer->doc = substr_replace($renderer->doc, $rewrite, $lst_header_pos);
$renderer->doc .= "<div class='another_custom_class level" . $lvl . "'>";
} else {
$renderer->section_open($lvl);
}
return true;
}`