doc = $doc;
}
/**
* @param string $html
* @throws \RuntimeException
*/
public function load($html)
{
$ok = $this->doc->loadHTML($html);
if (!$ok) {
throw new \RuntimeException('Unable to load given HTML content.');
}
$this->nodes = $this->doc->getElementsByTagName('body')->item(0)->childNodes;
$this->cur = 0;
}
public function doStream()
{
$output = $this->output;
$output->write('
');
$this->until('h1', 'say');
$output->write('
');
$this->say();
$output->write('
');
$this->until('p', 'say');
$output->write('
');
$this->say();
$output->write('{% block start %}{% endblock %}');
$output->write('
');
$output->write('
');
$ok = $this->until('h2', 'say');
do {
$output->write('- ');
$this->say();
$ok = $this->until('h2', 'say');
$output->write('
');
} while ($ok);
$output->write('
');
$output->writeln('
');
}
/**
* @param string $tag
* @param string $action
* @todo Find a way to avoid the copy/paste of `$this->node` line.
*/
protected function until($tag, $action = null)
{
$this->node = $this->nodes->item($this->cur++);
while ($tag !== $this->node->nodeName) {
if (null !== $action) {
$this->$action();
}
$this->node = $this->nodes->item($this->cur++);
}
return null !== $this->node;
}
protected function say()
{
$this->output->write($this->doc->saveHTML($this->node));
}
protected function sayText()
{
$this->output->write($this->node->textContent);
}
}