Transform the server to be able to use the Generator

This commit is contained in:
Stéphane Goetz 2015-07-19 12:21:09 +02:00
parent d531de86cf
commit 932fab5a84
5 changed files with 76 additions and 58 deletions

View File

@ -6,5 +6,16 @@ use Todaymade\Daux\Daux;
interface Generator interface Generator
{ {
public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width); /**
* @param Daux $daux
*/
public function __construct(Daux $daux);
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param integer $width
* @return mixed
*/
public function generateAll(InputInterface $input, OutputInterface $output, $width);
} }

View File

@ -13,32 +13,38 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
{ {
use RunAction; use RunAction;
/** /** @var string */
* @var string
*/
protected $prefix; protected $prefix;
/** /** @var CommonMarkConverter */
* @var CommonMarkConverter
*/
protected $converter; protected $converter;
public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width) /** @var Daux */
protected $daux;
/**
* @param Daux $daux
*/
public function __construct(Daux $daux)
{ {
$params = $daux->getParams(); $this->daux = $daux;
$this->converter = new CommonMarkConverter(['daux' => $this->daux->getParams()]);
}
public function generateAll(InputInterface $input, OutputInterface $output, $width)
{
$params = $this->daux->getParams();
$confluence = $params['confluence']; $confluence = $params['confluence'];
$this->prefix = trim($confluence['prefix']) . " "; $this->prefix = trim($confluence['prefix']) . " ";
$this->converter = new CommonMarkConverter(['daux' => $params]);
$tree = $this->runAction( $tree = $this->runAction(
"Generating Tree ...", "Generating Tree ...",
$output, $output,
$width, $width,
function() use ($daux, $params) { function() use ($params) {
$tree = $this->generateRecursive($daux->tree, $params); $tree = $this->generateRecursive($this->daux->tree, $params);
$tree['title'] = $this->prefix . $daux->getParams()['title']; $tree['title'] = $this->prefix . $params['title'];
return $tree; return $tree;
} }
@ -70,7 +76,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
); );
} elseif ($node instanceof Content) { } elseif ($node instanceof Content) {
$params['request'] = $node->getUrl(); $params['request'] = $node->getUrl();
$params['file_uri'] = $node->getName();
$data = [ $data = [
'title' => $this->prefix . $node->getTitle(), 'title' => $this->prefix . $node->getTitle(),

View File

@ -8,40 +8,50 @@ use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter; use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Format\Base\RunAction; use Todaymade\Daux\Format\Base\RunAction;
use Todaymade\Daux\Generator\Helper; use Todaymade\Daux\Generator\Helper;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Content; 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 class Generator implements \Todaymade\Daux\Format\Base\Generator
{ {
use RunAction; use RunAction;
/** /** @var CommonMarkConverter */
* @var CommonMarkConverter
*/
protected $converter; protected $converter;
public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width) /** @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'); $destination = $input->getOption('destination');
$params = $daux->getParams(); $params = $this->daux->getParams();
if (is_null($destination)) { if (is_null($destination)) {
$destination = $daux->local_base . DS . 'static'; $destination = $this->daux->local_base . DS . 'static';
} }
$this->converter = new CommonMarkConverter(['daux' => $params]);
$this->runAction( $this->runAction(
"Copying Static assets ...", "Copying Static assets ...",
$output, $output,
$width, $width,
function() use ($destination, $daux) { function () use ($destination) {
Helper::copyAssets($destination, $daux->local_base); Helper::copyAssets($destination, $this->daux->local_base);
} }
); );
$output->writeLn("Generating ..."); $output->writeLn("Generating ...");
$this->generateRecursive($daux->tree, $destination, $params, $output, $width); $this->generateRecursive($this->daux->tree, $destination, $params, $output, $width);
} }
private function rebaseConfig(Config $config, $base_url) private function rebaseConfig(Config $config, $base_url)
@ -84,29 +94,32 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
// Rebase configuration again as $params is a shared object // Rebase configuration again as $params is a shared object
$this->rebaseConfig($params, $base_url); $this->rebaseConfig($params, $base_url);
} elseif ($node instanceof Content) {
$this->runAction(
"- " . $node->getUrl(),
$output,
$width,
function() use ($node, $output_dir, $key, $params) {
$params['request'] = $node->getUrl();
$params['file_uri'] = $node->getName();
$page = MarkdownPage::fromFile($node, $params, $this->converter);
file_put_contents($output_dir . DS . $key, $page->getContent());
}
);
} else { } else {
$this->runAction( $this->runAction(
"- " . $node->getUrl(), "- " . $node->getUrl(),
$output, $output,
$width, $width,
function() use ($node, $output_dir, $key) { function () use ($node, $output_dir, $key, $params) {
if (!$node instanceof Content) {
copy($node->getPath(), $output_dir . DS . $key); copy($node->getPath(), $output_dir . DS . $key);
return;
}
$generated = $this->generateOne($node, $params);
file_put_contents($output_dir . DS . $key, $generated->getContent());
} }
); );
} }
} }
} }
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);
}
} }

View File

@ -74,6 +74,6 @@ class Command extends SymfonyCommand
throw new \RuntimeException("the class '$class' does not implement the '$interface' interface"); throw new \RuntimeException("the class '$class' does not implement the '$interface' interface");
} }
(new $class())->generate($daux, $input, $output, $width); (new $class($daux))->generateAll($input, $output, $width);
} }
} }

View File

@ -3,10 +3,8 @@
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception; use Todaymade\Daux\Exception;
use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter; use Todaymade\Daux\Format\HTML\Generator;
use Todaymade\Daux\Format\HTML\MarkdownPage;
use Todaymade\Daux\Format\HTML\RawPage; use Todaymade\Daux\Format\HTML\RawPage;
use Todaymade\Daux\Tree\Raw;
class Server class Server
{ {
@ -123,18 +121,9 @@ class Server
throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
} }
if ($file instanceof Raw) { // TODO :: make it possible to replace the generator in live code
return new RawPage($file->getPath()); $generator = new Generator($this->daux);
} return $generator->generateOne($file, $this->params);
$params = $this->params;
$params['request'] = $request;
$params['file_uri'] = $file->getUri();
if ($request !== 'index') {
$params['entry_page'] = $file->getFirstPage();
}
return MarkdownPage::fromFile($file, $params, new CommonMarkConverter(['daux' => $params]));
} }
public function getRequest() public function getRequest()