2015-05-21 17:39:11 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\HTML;
|
|
|
|
|
2015-07-19 00:55:57 +02:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2015-07-14 22:06:01 +02:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2015-07-18 23:51:23 +02:00
|
|
|
use Todaymade\Daux\Config;
|
2015-07-23 17:44:24 +02:00
|
|
|
use Todaymade\Daux\Console\RunAction;
|
2015-07-29 22:31:41 +02:00
|
|
|
use Todaymade\Daux\ContentTypes\Markdown\ContentType;
|
2015-05-21 17:39:11 +02:00
|
|
|
use Todaymade\Daux\Daux;
|
|
|
|
use Todaymade\Daux\DauxHelper;
|
2015-07-19 22:38:37 +02:00
|
|
|
use Todaymade\Daux\Format\Base\LiveGenerator;
|
2015-07-23 17:44:24 +02:00
|
|
|
use Todaymade\Daux\GeneratorHelper;
|
2015-05-21 17:39:11 +02:00
|
|
|
use Todaymade\Daux\Tree\Content;
|
2015-07-19 12:21:09 +02:00
|
|
|
use Todaymade\Daux\Tree\Directory;
|
|
|
|
use Todaymade\Daux\Tree\Entry;
|
|
|
|
use Todaymade\Daux\Tree\Raw;
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2015-07-14 22:06:01 +02:00
|
|
|
use RunAction;
|
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
/** @var Daux */
|
|
|
|
protected $daux;
|
|
|
|
|
2015-07-19 01:05:43 +02:00
|
|
|
/**
|
2015-07-19 12:21:09 +02:00
|
|
|
* @param Daux $daux
|
2015-07-19 01:05:43 +02:00
|
|
|
*/
|
2015-07-19 12:21:09 +02:00
|
|
|
public function __construct(Daux $daux)
|
|
|
|
{
|
|
|
|
$this->daux = $daux;
|
2015-07-28 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getContentTypes()
|
|
|
|
{
|
|
|
|
return [
|
2015-07-29 22:31:41 +02:00
|
|
|
'markdown' => new ContentType($this->daux->getParams())
|
2015-07-28 17:25:03 +02:00
|
|
|
];
|
2015-07-19 12:21:09 +02:00
|
|
|
}
|
2015-07-19 01:05:43 +02:00
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
public function generateAll(InputInterface $input, OutputInterface $output, $width)
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2015-07-19 00:55:57 +02:00
|
|
|
$destination = $input->getOption('destination');
|
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
$params = $this->daux->getParams();
|
2015-05-21 17:39:11 +02:00
|
|
|
if (is_null($destination)) {
|
2015-07-20 15:59:52 +02:00
|
|
|
$destination = $this->daux->local_base . DIRECTORY_SEPARATOR . 'static';
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->runAction(
|
|
|
|
"Copying Static assets ...",
|
|
|
|
$output,
|
|
|
|
$width,
|
2015-07-20 15:59:52 +02:00
|
|
|
function() use ($destination) {
|
2015-07-23 17:44:24 +02:00
|
|
|
GeneratorHelper::copyAssets($destination, $this->daux->local_base);
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
);
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$output->writeLn("Generating ...");
|
2015-07-19 12:21:09 +02:00
|
|
|
$this->generateRecursive($this->daux->tree, $destination, $params, $output, $width);
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* Recursively generate the documentation
|
|
|
|
*
|
2015-07-18 01:10:40 +02:00
|
|
|
* @param Directory $tree
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $output_dir
|
|
|
|
* @param \Todaymade\Daux\Config $params
|
|
|
|
* @param OutputInterface $output
|
|
|
|
* @param integer $width
|
|
|
|
* @param string $base_url
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2015-07-18 01:10:40 +02:00
|
|
|
private function generateRecursive(Directory $tree, $output_dir, $params, $output, $width, $base_url = '')
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2015-07-19 14:05:12 +02:00
|
|
|
DauxHelper::rebaseConfiguration($params, $base_url);
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-06-30 15:50:42 +02:00
|
|
|
if ($base_url !== '' && empty($params['entry_page'])) {
|
2015-05-21 17:39:11 +02:00
|
|
|
$params['entry_page'] = $tree->getFirstPage();
|
|
|
|
}
|
2015-07-18 23:51:23 +02:00
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
foreach ($tree->getEntries() as $key => $node) {
|
2015-05-21 17:39:11 +02:00
|
|
|
if ($node instanceof Directory) {
|
2015-07-20 15:59:52 +02:00
|
|
|
$new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
|
2015-07-17 23:38:06 +02:00
|
|
|
mkdir($new_output_dir);
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->generateRecursive($node, $new_output_dir, $params, $output, $width, '../' . $base_url);
|
2015-07-18 23:51:23 +02:00
|
|
|
|
|
|
|
// Rebase configuration again as $params is a shared object
|
2015-07-19 14:05:12 +02:00
|
|
|
DauxHelper::rebaseConfiguration($params, $base_url);
|
2015-05-21 17:39:11 +02:00
|
|
|
} else {
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->runAction(
|
|
|
|
"- " . $node->getUrl(),
|
|
|
|
$output,
|
|
|
|
$width,
|
2015-07-20 15:59:52 +02:00
|
|
|
function() use ($node, $output_dir, $key, $params) {
|
2015-07-19 12:21:09 +02:00
|
|
|
if (!$node instanceof Content) {
|
2015-07-20 15:59:52 +02:00
|
|
|
copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key);
|
2015-07-19 12:21:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$generated = $this->generateOne($node, $params);
|
2015-07-20 15:59:52 +02:00
|
|
|
file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent());
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
);
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 12:21:09 +02:00
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
/**
|
|
|
|
* @param Entry $node
|
|
|
|
* @param Config $params
|
|
|
|
* @return \Todaymade\Daux\Format\Base\Page
|
|
|
|
*/
|
|
|
|
public function generateOne(Entry $node, Config $params)
|
2015-07-19 12:21:09 +02:00
|
|
|
{
|
|
|
|
if ($node instanceof Raw) {
|
|
|
|
return new RawPage($node->getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
$params['request'] = $node->getUrl();
|
2015-07-28 17:25:03 +02:00
|
|
|
return ContentPage::fromFile($node, $params, $this->daux->getContentTypeHandler()->getType($node));
|
2015-07-19 12:21:09 +02:00
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|