list($attr, $content) = preg_split('/>/u', $match, 2);
if ($this->isSyntaxOk()) {
$attr = trim($attr);
if ($attr == null) {
// No brush and no options, use "text" with default options.
$attr = 'text';
} elseif (substr($attr, 0, 1) == ';') {
// Options but no brush, add "text" brush.
$attr = 'text'.$attr;
}
} else {
$attr = null;
}
return array($this->syntax, $attr, $content);
}
return false;
}
/**
* Render xhtml output or metadata.
*
* @param string $mode Renderer mode (supported modes: xhtml)
* @param Doku_Renderer $renderer The renderer
* @param array $data The data from the handler() function
*
* @return bool If rendering was successful.
*/
public function render($mode, Doku_Renderer $renderer, $data) {
if ($mode != 'xhtml') {
return false;
}
if (count($data) != 3) {
return true;
}
list($this->syntax, $attr, $content) = $data;
if ($this->isSyntaxOk()) {
$title = $this->procTitle($attr);
$highlight = $this->procHighlight($attr);
$renderer->doc .= ''.$renderer->_xmlEntities($content).'
';
} else {
$renderer->file($content);
}
return true;
}
private function procTitle($attr) {
if ($this->syntax == 'file') {
$title = trim(substr($attr, strpos($attr, ' ') + 1));
if (!empty($title)) {
return ' title="' . $title . '"';
}
} elseif (preg_match('/title:/i', $attr)) {
// Extract title(s) from $attr string.
$attr_array = explode(';', $attr);
$title_array = preg_grep('/title:/i', $attr_array);
// Extract everything BUT title(s) from $attr string.
$not_title_array = preg_grep('/title:/i', $attr_array, PREG_GREP_INVERT);
$attr = implode(';', $not_title_array);
// If there are multiple titles, use the last one.
$title = array_pop($title_array);
return ' title="'.preg_replace("/.*title:\s{0,}(.*)/i", '$1', $title).'"';
}
return '';
}
private function procHighlight($attr) {
$highlight = '';
// Check highlight attr for lines ranges
if (preg_match('/highlight:/i', $attr, $matches)) {
// Extract highlight from $attr string.
$attr_array = explode(';', $attr);
$highlight_array = preg_grep('/highlight:/i', $attr_array);
// Extract everything BUT highlight from $attr string.
$not_highlight_array = preg_grep('/highlight:/i', $attr_array, PREG_GREP_INVERT);
$attr = implode(';', $not_highlight_array);
// If there are multiple hightlights, use the last one.
$highlight_str = array_pop($highlight_array);
$highlight_str = preg_replace("/.*highlight:\s{0,}(.*)/i", '$1', $highlight_str);
// Remove [ ]
$highlight_str = str_replace(array('[', ']'), '', $highlight_str);
// Process ranges if exists
$highlight_exp = explode(',', $highlight_str);
foreach ($highlight_exp as $highlight_elt) {
if (!empty($highlight)) {
$highlight .= ',';
}
$highlight_elt = trim($highlight_elt);
$highlight_elt_exp = explode('-', $highlight_elt);
if (count($highlight_elt_exp) == 2) {
foreach (range($highlight_elt_exp[0], $highlight_elt_exp[1]) as $key => $lineNumber) {
if ($key > 0) {
$highlight .= ',';
}
$highlight .= $lineNumber;
}
} else {
$highlight .= $highlight_elt;
}
}
$highlight = ' highlight: ['.$highlight.']';
}
return $highlight;
}
private function isSyntaxOk() {
if ($this->syntax == 'sxh') {
return true;
}
$tags = explode(',', $this->getConf('override'));
foreach ($tags as $tag) {
if ($this->syntax == $tag) {
return true;
}
}
return false;
}
}