From 932fab5a844b0c60200057c42dcc53cf69a3748b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Sun, 19 Jul 2015 12:21:09 +0200 Subject: [PATCH] Transform the server to be able to use the Generator --- libs/Format/Base/Generator.php | 13 +++++- libs/Format/Confluence/Generator.php | 33 ++++++++------ libs/Format/HTML/Generator.php | 67 +++++++++++++++++----------- libs/Generator/Command.php | 2 +- libs/Server/Server.php | 19 ++------ 5 files changed, 76 insertions(+), 58 deletions(-) diff --git a/libs/Format/Base/Generator.php b/libs/Format/Base/Generator.php index 01b4bb3..a7602df 100644 --- a/libs/Format/Base/Generator.php +++ b/libs/Format/Base/Generator.php @@ -6,5 +6,16 @@ use Todaymade\Daux\Daux; 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); } diff --git a/libs/Format/Confluence/Generator.php b/libs/Format/Confluence/Generator.php index 8a1e6c0..d159e10 100644 --- a/libs/Format/Confluence/Generator.php +++ b/libs/Format/Confluence/Generator.php @@ -13,32 +13,38 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator { use RunAction; - /** - * @var string - */ + /** @var string */ protected $prefix; - /** - * @var CommonMarkConverter - */ + /** @var CommonMarkConverter */ 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']; $this->prefix = trim($confluence['prefix']) . " "; - $this->converter = new CommonMarkConverter(['daux' => $params]); - $tree = $this->runAction( "Generating Tree ...", $output, $width, - function() use ($daux, $params) { - $tree = $this->generateRecursive($daux->tree, $params); - $tree['title'] = $this->prefix . $daux->getParams()['title']; + function() use ($params) { + $tree = $this->generateRecursive($this->daux->tree, $params); + $tree['title'] = $this->prefix . $params['title']; return $tree; } @@ -70,7 +76,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator ); } elseif ($node instanceof Content) { $params['request'] = $node->getUrl(); - $params['file_uri'] = $node->getName(); $data = [ 'title' => $this->prefix . $node->getTitle(), diff --git a/libs/Format/HTML/Generator.php b/libs/Format/HTML/Generator.php index 4bf3610..6b237b0 100644 --- a/libs/Format/HTML/Generator.php +++ b/libs/Format/HTML/Generator.php @@ -8,40 +8,50 @@ use Todaymade\Daux\DauxHelper; use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter; use Todaymade\Daux\Format\Base\RunAction; use Todaymade\Daux\Generator\Helper; -use Todaymade\Daux\Tree\Directory; 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 { use RunAction; - /** - * @var CommonMarkConverter - */ + /** @var CommonMarkConverter */ 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'); - $params = $daux->getParams(); + $params = $this->daux->getParams(); 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( "Copying Static assets ...", $output, $width, - function() use ($destination, $daux) { - Helper::copyAssets($destination, $daux->local_base); + function () use ($destination) { + Helper::copyAssets($destination, $this->daux->local_base); } ); $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) @@ -84,29 +94,32 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator // Rebase configuration again as $params is a shared object $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 { $this->runAction( "- " . $node->getUrl(), $output, $width, - function() use ($node, $output_dir, $key) { - copy($node->getPath(), $output_dir . DS . $key); + 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()); } ); } } } + + 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); + } } diff --git a/libs/Generator/Command.php b/libs/Generator/Command.php index 18d3e5c..ff22c15 100644 --- a/libs/Generator/Command.php +++ b/libs/Generator/Command.php @@ -74,6 +74,6 @@ class Command extends SymfonyCommand 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); } } diff --git a/libs/Server/Server.php b/libs/Server/Server.php index 13be5db..9902cc0 100644 --- a/libs/Server/Server.php +++ b/libs/Server/Server.php @@ -3,10 +3,8 @@ use Todaymade\Daux\Daux; use Todaymade\Daux\DauxHelper; use Todaymade\Daux\Exception; -use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter; -use Todaymade\Daux\Format\HTML\MarkdownPage; +use Todaymade\Daux\Format\HTML\Generator; use Todaymade\Daux\Format\HTML\RawPage; -use Todaymade\Daux\Tree\Raw; class Server { @@ -123,18 +121,9 @@ class Server throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); } - if ($file instanceof Raw) { - return new RawPage($file->getPath()); - } - - $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])); + // TODO :: make it possible to replace the generator in live code + $generator = new Generator($this->daux); + return $generator->generateOne($file, $this->params); } public function getRequest()