Improve processors

Now you can add generators and extend the markdown parser
This commit is contained in:
Stéphane Goetz 2015-07-19 00:55:57 +02:00
parent bb3a3408fd
commit e3a3438ccd
7 changed files with 89 additions and 19 deletions

View File

@ -210,5 +210,22 @@ class Daux
public function setProcessor(Processor $processor) public function setProcessor(Processor $processor)
{ {
$this->processor = $processor; $this->processor = $processor;
// This is not the cleanest but it's
// the best i've found to use the
// processor in very remote places
$this->options['processor_instance'] = $processor;
}
public function getGenerators()
{
$default = [
'confluence' => '\Todaymade\Daux\Format\Confluence\Generator',
'html' => '\Todaymade\Daux\Format\HTML\Generator',
];
$extended = $this->getProcessor()->addGenerators();
return array_replace($default, $extended);
} }
} }

View File

@ -18,8 +18,9 @@ class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter
$this->extendEnvironment($environment); $this->extendEnvironment($environment);
//TODO :: finish if (array_key_exists('processor_instance', $config['daux'])) {
//$daux->getProcessor()->extendCommonMarkEnvironment($environment); $config['daux']['processor_instance']->extendCommonMarkEnvironment($environment);
}
$this->docParser = new DocParser($environment); $this->docParser = new DocParser($environment);
$this->htmlRenderer = new HtmlRenderer($environment); $this->htmlRenderer = new HtmlRenderer($environment);

View File

@ -0,0 +1,10 @@
<?php namespace Todaymade\Daux\Format\Base;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Daux;
interface Generator
{
public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width);
}

View File

@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\Format\Confluence; <?php namespace Todaymade\Daux\Format\Confluence;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
@ -7,7 +8,7 @@ use Todaymade\Daux\Format\Base\RunAction;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
class Generator class Generator implements \Todaymade\Daux\Format\Base\Generator
{ {
use RunAction; use RunAction;
@ -16,7 +17,7 @@ class Generator
*/ */
protected $prefix; protected $prefix;
public function generate(Daux $daux, OutputInterface $output, $width) public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width)
{ {
$confluence = $daux->getParams()['confluence']; $confluence = $daux->getParams()['confluence'];

View File

@ -1,5 +1,6 @@
<?php namespace Todaymade\Daux\Format\HTML; <?php namespace Todaymade\Daux\Format\HTML;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
@ -9,12 +10,14 @@ use Todaymade\Daux\Generator\Helper;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
class Generator class Generator implements \Todaymade\Daux\Format\Base\Generator
{ {
use RunAction; use RunAction;
public function generate(Daux $daux, $destination, OutputInterface $output, $width) public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width)
{ {
$destination = $input->getOption('destination');
$params = $daux->getParams(); $params = $daux->getParams();
if (is_null($destination)) { if (is_null($destination)) {
$destination = $daux->local_base . DS . 'static'; $destination = $daux->local_base . DS . 'static';

View File

@ -32,6 +32,17 @@ class Command extends SymfonyCommand
$processor = $input->getOption('processor'); $processor = $input->getOption('processor');
if (!empty($processor) && $processor != 'none') { if (!empty($processor) && $processor != 'none') {
$this->prepareProcessor($processor, $daux, $output, $width);
}
// Improve the tree with a processor
$daux->getProcessor()->manipulateTree($daux->tree);
$this->launchGeneration($daux, $input, $output, $width);
}
protected function prepareProcessor($processor, Daux $daux, OutputInterface $output, $width)
{
$class = "\\Todaymade\\Daux\\Extension\\" . $processor; $class = "\\Todaymade\\Daux\\Extension\\" . $processor;
if (class_exists($class)) { if (class_exists($class)) {
$daux->setProcessor(new $class($daux, $output, $width)); $daux->setProcessor(new $class($daux, $output, $width));
@ -40,16 +51,29 @@ class Command extends SymfonyCommand
} }
} }
// Improve the tree with a processor protected function launchGeneration(Daux $daux, InputInterface $input, OutputInterface $output, $width)
$daux->getProcessor()->manipulateTree($daux->tree); {
$generators = $daux->getGenerators();
switch (strtolower($input->getOption('format'))) { $format = strtolower($input->getOption('format'));
case 'confluence': if (empty($format)) {
(new ConfluenceGenerator())->generate($daux, $output, $width); $format = 'html';
break; }
case 'html':
default: if (!array_key_exists($format, $generators)) {
(new HTMLGenerator())->generate($daux, $input->getOption('destination'), $output, $width); throw new \RuntimeException("The format '$format' doesn't exist, did you forget to set your processor ?");
} }
$class = $generators[$format];
if (!class_exists($class)) {
throw new \RuntimeException("Class '$class' not found. We cannot use it as a Generator");
}
$interface = 'Todaymade\Daux\Format\Base\Generator';
if (!in_array('Todaymade\Daux\Format\Base\Generator', class_implements($class))) {
throw new \RuntimeException("the class '$class' does not implement the '$interface' interface");
}
(new $class())->generate($daux, $input, $output, $width);
} }
} }

View File

@ -54,4 +54,18 @@ class Processor
public function extendCommonMarkEnvironment(Environment $environment) public function extendCommonMarkEnvironment(Environment $environment)
{ {
} }
/**
* Provide new generators with this extension point. You
* can simply return an array, the key is the format's
* name, the value is a class name implementing the
* `Todaymade\Daux\Format\Base\Generator` contract.
* You can also replace base generators.
*
* @return array
*/
public function addGenerators()
{
return [];
}
} }