daux.io/libs/Format/HTMLFile/Generator.php

92 lines
2.6 KiB
PHP
Raw Normal View History

<?php namespace Todaymade\Daux\Format\HTMLFile;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Console\RunAction;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Format\HTML\Template;
use Todaymade\Daux\Format\HTML\HTMLUtils;
use Todaymade\Daux\Format\HTMLFile\ContentTypes\Markdown\ContentType;
class Generator implements \Todaymade\Daux\Format\Base\Generator
{
use RunAction, HTMLUtils;
/** @var Daux */
protected $daux;
/** @var Template */
protected $templateRenderer;
/**
* @param Daux $daux
*/
public function __construct(Daux $daux)
{
$config = $daux->getConfig();
$this->daux = $daux;
$this->templateRenderer = new Template($config);
$config->templateRenderer = $this->templateRenderer;
}
/**
* @return array
*/
public function getContentTypes()
{
return [
'markdown' => new ContentType($this->daux->getConfig()),
];
}
/**
* {@inheritdoc}
*/
public function generateAll(InputInterface $input, OutputInterface $output, $width)
{
$destination = $input->getOption('destination');
$config = $this->daux->getConfig();
if (is_null($destination)) {
2019-12-07 11:22:40 +01:00
$destination = $config->getLocalBase() . DIRECTORY_SEPARATOR . 'static';
}
$this->runAction(
'Cleaning destination folder ...',
$width,
function() use ($destination) {
$this->ensureEmptyDestination($destination);
}
);
$data = [
'author' => $config->getAuthor(),
'title' => $config->getTitle(),
'subject' => $config->getTagline()
];
$book = new Book($this->daux->tree, $data);
$current = $this->daux->tree->getIndexPage();
while ($current) {
$this->runAction(
2016-07-27 21:32:51 +02:00
'Generating ' . $current->getTitle(),
$width,
2019-12-07 16:32:38 +01:00
function() use ($book, $current, $config) {
$contentType = $this->daux->getContentTypeHandler()->getType($current);
$content = ContentPage::fromFile($current, $config, $contentType);
$content->templateRenderer = $this->templateRenderer;
$content = $content->getContent();
$book->addPage($current, $content);
}
);
$current = $current->getNext();
}
$content = $book->generate();
file_put_contents($input->getOption('destination') . '/file.html', $content);
}
}