Improve processors
Now you can add generators and extend the markdown parser
This commit is contained in:
parent
bb3a3408fd
commit
e3a3438ccd
@ -210,5 +210,22 @@ class Daux
|
||||
public function setProcessor(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);
|
||||
}
|
||||
}
|
||||
|
@ -18,8 +18,9 @@ class CommonMarkConverter extends \League\CommonMark\CommonMarkConverter
|
||||
|
||||
$this->extendEnvironment($environment);
|
||||
|
||||
//TODO :: finish
|
||||
//$daux->getProcessor()->extendCommonMarkEnvironment($environment);
|
||||
if (array_key_exists('processor_instance', $config['daux'])) {
|
||||
$config['daux']['processor_instance']->extendCommonMarkEnvironment($environment);
|
||||
}
|
||||
|
||||
$this->docParser = new DocParser($environment);
|
||||
$this->htmlRenderer = new HtmlRenderer($environment);
|
||||
|
10
libs/Format/Base/Generator.php
Normal file
10
libs/Format/Base/Generator.php
Normal 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);
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
<?php namespace Todaymade\Daux\Format\Confluence;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Todaymade\Daux\Config;
|
||||
use Todaymade\Daux\Daux;
|
||||
@ -7,7 +8,7 @@ use Todaymade\Daux\Format\Base\RunAction;
|
||||
use Todaymade\Daux\Tree\Content;
|
||||
use Todaymade\Daux\Tree\Directory;
|
||||
|
||||
class Generator
|
||||
class Generator implements \Todaymade\Daux\Format\Base\Generator
|
||||
{
|
||||
use RunAction;
|
||||
|
||||
@ -16,7 +17,7 @@ class Generator
|
||||
*/
|
||||
protected $prefix;
|
||||
|
||||
public function generate(Daux $daux, OutputInterface $output, $width)
|
||||
public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width)
|
||||
{
|
||||
$confluence = $daux->getParams()['confluence'];
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
<?php namespace Todaymade\Daux\Format\HTML;
|
||||
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Todaymade\Daux\Config;
|
||||
use Todaymade\Daux\Daux;
|
||||
@ -9,12 +10,14 @@ use Todaymade\Daux\Generator\Helper;
|
||||
use Todaymade\Daux\Tree\Directory;
|
||||
use Todaymade\Daux\Tree\Content;
|
||||
|
||||
class Generator
|
||||
class Generator implements \Todaymade\Daux\Format\Base\Generator
|
||||
{
|
||||
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();
|
||||
if (is_null($destination)) {
|
||||
$destination = $daux->local_base . DS . 'static';
|
||||
|
@ -32,6 +32,17 @@ class Command extends SymfonyCommand
|
||||
|
||||
$processor = $input->getOption('processor');
|
||||
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;
|
||||
if (class_exists($class)) {
|
||||
$daux->setProcessor(new $class($daux, $output, $width));
|
||||
@ -40,16 +51,29 @@ class Command extends SymfonyCommand
|
||||
}
|
||||
}
|
||||
|
||||
// Improve the tree with a processor
|
||||
$daux->getProcessor()->manipulateTree($daux->tree);
|
||||
protected function launchGeneration(Daux $daux, InputInterface $input, OutputInterface $output, $width)
|
||||
{
|
||||
$generators = $daux->getGenerators();
|
||||
|
||||
switch (strtolower($input->getOption('format'))) {
|
||||
case 'confluence':
|
||||
(new ConfluenceGenerator())->generate($daux, $output, $width);
|
||||
break;
|
||||
case 'html':
|
||||
default:
|
||||
(new HTMLGenerator())->generate($daux, $input->getOption('destination'), $output, $width);
|
||||
}
|
||||
$format = strtolower($input->getOption('format'));
|
||||
if (empty($format)) {
|
||||
$format = 'html';
|
||||
}
|
||||
|
||||
if (!array_key_exists($format, $generators)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -54,4 +54,18 @@ class Processor
|
||||
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 [];
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user