113 lines
2.4 KiB
PHP
113 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Questionnaire\Parser;
|
|
|
|
class ReadmeParser extends Parser
|
|
{
|
|
/**
|
|
* @var \DOMDocument
|
|
*/
|
|
protected $doc;
|
|
|
|
/**
|
|
* @var \DOMNodelist
|
|
*/
|
|
protected $nodes;
|
|
|
|
/**
|
|
* @var \DOMNode
|
|
*/
|
|
protected $node;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
protected $cur;
|
|
|
|
/**
|
|
* @param \DOMDocument $doc
|
|
*/
|
|
public function __construct(\DOMDocument $doc)
|
|
{
|
|
$this->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('<div class="panel panel-default">');
|
|
|
|
$this->until('h1', 'say');
|
|
$output->write('<div class="panel-heading">');
|
|
$this->say();
|
|
$output->write('</div>');
|
|
|
|
$this->until('p', 'say');
|
|
$output->write('<div class="panel-body">');
|
|
$this->say();
|
|
$output->write('{% block start %}{% endblock %}');
|
|
$output->write('</div>');
|
|
|
|
$output->write('<ul class="list-group">');
|
|
|
|
$ok = $this->until('h2', 'say');
|
|
|
|
do {
|
|
$output->write('<li class="list-group-item">');
|
|
$this->say();
|
|
$ok = $this->until('h2', 'say');
|
|
$output->write('</li>');
|
|
} while ($ok);
|
|
|
|
$output->write('</ul>');
|
|
$output->writeln('</div>');
|
|
}
|
|
|
|
/**
|
|
* @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);
|
|
}
|
|
}
|