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

90 Zeilen
2.6 KiB
PHP

<?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\HTMLUtils;
2020-04-22 22:24:52 +02:00
use Todaymade\Daux\Format\HTML\Template;
use Todaymade\Daux\Format\HTMLFile\ContentTypes\Markdown\ContentType;
class Generator implements \Todaymade\Daux\Format\Base\Generator
{
2020-04-22 22:24:52 +02:00
use RunAction;
use HTMLUtils;
/** @var Daux */
protected $daux;
/** @var Template */
protected $templateRenderer;
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,
2020-04-22 21:55:53 +02:00
function () use ($destination) {
$this->ensureEmptyDestination($destination);
}
);
$data = [
'author' => $config->getAuthor(),
'title' => $config->getTitle(),
2020-04-22 22:24:52 +02:00
'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,
2020-04-22 21:55:53 +02: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);
}
}