_file = $file;
}
public function init()
{}
public function main()
{
$this->_highlightFile($this->_file);
$this->log('PHP in XSL-FO highlighted');
}
private function _highlightFile($file)
{
$dom = new DomDocument;
$dom->load($file);
$xpath = new DomXPath($dom);
$elements = $xpath->query("//fo:block[@phing='phpfohighlightertask']");
foreach ($elements as $block) {
self::_highlightBlock($block, $dom);
$block->removeAttribute('phing');
}
$dom->save($file);
}
private static function _highlightBlock($block, $fo)
{
$toHighlight = str_replace(
array('>', '<', '&','"'),
array('>', '<', '&', '"'),
$block->nodeValue
);
if (substr($toHighlight, 0, 5) !== ']*>/", $toHighlight)) {
return;
}
$code = highlight_string($toHighlight, true);
$code = str_replace(
array('','',' ','
',"\r"),
array('','',' ',"\n","\n"),
$code
);
$code = preg_replace("!\n\n\n+!", "\n\n", $code);
$code = trim($code);
$dom = new DomDocument;
$dom->loadXML($code);
$xpath = new DomXPath($dom);
$parentSpan = $xpath->query('/span')->item(0);
$style = $parentSpan->getAttributeNode('style')->value;
$colour = substr($style, 7, 7);
$content = $parentSpan->nodeValue;
$inlineParent = $fo->createElement('fo:inline');
$inlineParent->setAttribute('color', $colour);
$nodes = $xpath->query('/span/node()');
foreach ($nodes as $node) {
if ($node->nodeType == XML_ELEMENT_NODE) {
self::_appendInlineChild($node, $inlineParent, $fo);
} else {
$child = $fo->importNode($node, true);
$inlineParent->appendChild($child);
}
}
if (preg_match("/^\s+$/", $inlineParent->firstChild->textContent)) {
$inlineParent->removeChild($inlineParent->firstChild);
}
foreach ($block->childNodes as $node) {
$block->removeChild($node);
}
$block->appendChild($inlineParent);
}
private static function _appendInlineChild($span, $inlineParent, $fo)
{
$style = $span->getAttributeNode('style')->value;
$colour = substr($style, 7, 7);
$content = $span->nodeValue;
$inlineChild = $fo->createElement('fo:inline', $content);
$inlineChild->setAttribute('color', $colour);
$inlineParent->appendChild($inlineChild);
}
}