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

113 regels
3.6 KiB
PHP

<?php namespace Todaymade\Daux\Format\HTML;
use Symfony\Component\Console\Input\InputInterface;
2015-07-14 22:06:01 +02:00
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Config;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter;
2015-07-14 22:06:01 +02:00
use Todaymade\Daux\Format\Base\RunAction;
use Todaymade\Daux\Generator\Helper;
use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Entry;
use Todaymade\Daux\Tree\Raw;
class Generator implements \Todaymade\Daux\Format\Base\Generator
{
2015-07-14 22:06:01 +02:00
use RunAction;
/** @var CommonMarkConverter */
protected $converter;
/** @var Daux */
protected $daux;
/**
* @param Daux $daux
*/
public function __construct(Daux $daux)
{
$this->daux = $daux;
$this->converter = new CommonMarkConverter(['daux' => $this->daux->getParams()]);
}
public function generateAll(InputInterface $input, OutputInterface $output, $width)
{
$destination = $input->getOption('destination');
$params = $this->daux->getParams();
if (is_null($destination)) {
$destination = $this->daux->local_base . DS . 'static';
}
2015-07-14 22:06:01 +02:00
$this->runAction(
"Copying Static assets ...",
$output,
$width,
function () use ($destination) {
Helper::copyAssets($destination, $this->daux->local_base);
2015-07-14 22:06:01 +02:00
}
);
2015-07-14 22:06:01 +02:00
$output->writeLn("Generating ...");
$this->generateRecursive($this->daux->tree, $destination, $params, $output, $width);
}
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-07-19 14:05:12 +02:00
DauxHelper::rebaseConfiguration($params, $base_url);
2015-06-30 15:50:42 +02:00
if ($base_url !== '' && empty($params['entry_page'])) {
$params['entry_page'] = $tree->getFirstPage();
}
foreach ($tree->getEntries() as $key => $node) {
if ($node instanceof Directory) {
$new_output_dir = $output_dir . DS . $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);
// Rebase configuration again as $params is a shared object
2015-07-19 14:05:12 +02:00
DauxHelper::rebaseConfiguration($params, $base_url);
} else {
2015-07-14 22:06:01 +02:00
$this->runAction(
"- " . $node->getUrl(),
$output,
$width,
function () use ($node, $output_dir, $key, $params) {
if (!$node instanceof Content) {
copy($node->getPath(), $output_dir . DS . $key);
return;
}
$generated = $this->generateOne($node, $params);
file_put_contents($output_dir . DS . $key, $generated->getContent());
2015-07-14 22:06:01 +02:00
}
);
}
}
}
public function generateOne(Entry $node, $params)
{
if ($node instanceof Raw) {
return new RawPage($node->getPath());
}
$params['request'] = $node->getUrl();
return MarkdownPage::fromFile($node, $params, $this->converter);
}
}