public function run(Page $page) { // Parse the HTML $dom = new Dom(); $dom->setOptions(array('removeScripts' => false, 'removeStyles' => false, 'preserveLineBreaks' => true)); $dom->load($page->body()); // Format foreach ($dom->find($this->selector) as $node) { // Format the node $formattedNode = $this->format($node); // Remove all children foreach ($node->find('*') as $child) { $child->delete(); } // Add the new node $node->addChild($formattedNode); } // Set and return return $page->withBody($dom->root->outerHtml()); }
public function page($path, array $context = array()) { $path = array_filter(explode('/', $path), 'strlen'); $pathname = implode('.', $path); $file = sprintf($this->path, $pathname); if (!file_exists($file)) { throw new PageNotFoundException($path); } // Parse the HTML $dom = new Dom(); $dom->setOptions(array('removeScripts' => false, 'removeStyles' => false, 'preserveLineBreaks' => true)); extract($context); ob_start(); include $file; $dom->load(ob_get_clean()); // Create a new page $page = new Page($path); // Title if (($title = $dom->find('title', 0)) !== null) { $page->withTitle($title->text()); } // Header if (($head = $dom->find('head', 0)) !== null) { foreach ($head->getChildren() as $child) { if ($child->getTag()->name() !== 'title' && !($child->getTag()->name() === 'meta' && $child->getAttribute('charset') !== null)) { $page->withHeader($page->header() . $child->outerHtml()); } } } // Body if (($body = $dom->find('body', 0)) !== null) { $page->withBody($body->innerHtml()); } // Return page return $page; }