From e3a3438ccd8bf3aa937aaf1abe6ec27594fd9ef0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Sun, 19 Jul 2015 00:55:57 +0200 Subject: [PATCH] Improve processors Now you can add generators and extend the markdown parser --- libs/Daux.php | 17 +++++++ .../Base/CommonMark/CommonMarkConverter.php | 5 +- libs/Format/Base/Generator.php | 10 ++++ libs/Format/Confluence/Generator.php | 5 +- libs/Format/HTML/Generator.php | 7 ++- libs/Generator/Command.php | 50 ++++++++++++++----- libs/Processor.php | 14 ++++++ 7 files changed, 89 insertions(+), 19 deletions(-) create mode 100644 libs/Format/Base/Generator.php diff --git a/libs/Daux.php b/libs/Daux.php index cf7ef3a..f113b9a 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -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); } } diff --git a/libs/Format/Base/CommonMark/CommonMarkConverter.php b/libs/Format/Base/CommonMark/CommonMarkConverter.php index b918c4c..afc61bc 100644 --- a/libs/Format/Base/CommonMark/CommonMarkConverter.php +++ b/libs/Format/Base/CommonMark/CommonMarkConverter.php @@ -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); diff --git a/libs/Format/Base/Generator.php b/libs/Format/Base/Generator.php new file mode 100644 index 0000000..01b4bb3 --- /dev/null +++ b/libs/Format/Base/Generator.php @@ -0,0 +1,10 @@ +getParams()['confluence']; diff --git a/libs/Format/HTML/Generator.php b/libs/Format/HTML/Generator.php index dc4370a..7bac65d 100644 --- a/libs/Format/HTML/Generator.php +++ b/libs/Format/HTML/Generator.php @@ -1,5 +1,6 @@ getOption('destination'); + $params = $daux->getParams(); if (is_null($destination)) { $destination = $daux->local_base . DS . 'static'; diff --git a/libs/Generator/Command.php b/libs/Generator/Command.php index a18636e..18d3e5c 100644 --- a/libs/Generator/Command.php +++ b/libs/Generator/Command.php @@ -32,24 +32,48 @@ class Command extends SymfonyCommand $processor = $input->getOption('processor'); if (!empty($processor) && $processor != 'none') { - $class = "\\Todaymade\\Daux\\Extension\\" . $processor; - if (class_exists($class)) { - $daux->setProcessor(new $class($daux, $output, $width)); - } elseif (file_exists($processor)) { - include $processor; - } + $this->prepareProcessor($processor, $daux, $output, $width); } // Improve the tree with a processor $daux->getProcessor()->manipulateTree($daux->tree); - 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); + $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)); + } elseif (file_exists($processor)) { + include $processor; } } + + protected function launchGeneration(Daux $daux, InputInterface $input, OutputInterface $output, $width) + { + $generators = $daux->getGenerators(); + + $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); + } } diff --git a/libs/Processor.php b/libs/Processor.php index b5cd2b0..f94ca65 100644 --- a/libs/Processor.php +++ b/libs/Processor.php @@ -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 []; + } }