Add format and processor as configuration options for live mode

This commit is contained in:
Stéphane Goetz 2015-07-19 22:38:37 +02:00
parent 3671214839
commit d921c412b5
6 changed files with 111 additions and 44 deletions

View File

@ -8,6 +8,9 @@
"image": "",
"languages": {},
"format": "html",
"processor": "",
"ignore": {
"files": [],
"folders": []

View File

@ -233,4 +233,49 @@ class Daux
return array_replace($default, $extended);
}
public function getProcessorClass()
{
$processor = $this->getParams()['processor'];
if (empty($processor)) {
return null;
}
$class = "\\Todaymade\\Daux\\Extension\\" . $processor;
if (!class_exists($class)) {
throw new \RuntimeException("Class '$class' not found. We cannot use it as a Processor");
}
//TODO :: check that it implements processor
return $class;
}
/**
* @return \Todaymade\Daux\Format\Base\Generator
*/
public function getGenerator()
{
$generators = $this->getGenerators();
$format = $this->getParams()['format'];
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");
}
return new $class($this);
}
}

View File

@ -0,0 +1,13 @@
<?php namespace Todaymade\Daux\Format\Base;
use Todaymade\Daux\Config;
use Todaymade\Daux\Tree\Entry;
interface LiveGenerator {
/**
* @param Entry $node
* @param Config $params
* @return \Todaymade\Daux\Format\Base\Page
*/
public function generateOne(Entry $node, Config $params);
}

View File

@ -6,6 +6,7 @@ use Todaymade\Daux\Config;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Format\Base\LiveGenerator;
use Todaymade\Daux\Format\Base\RunAction;
use Todaymade\Daux\Generator\Helper;
use Todaymade\Daux\Tree\Content;
@ -13,7 +14,7 @@ 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, LiveGenerator
{
use RunAction;
@ -100,7 +101,12 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
}
}
public function generateOne(Entry $node, $params)
/**
* @param Entry $node
* @param Config $params
* @return \Todaymade\Daux\Format\Base\Page
*/
public function generateOne(Entry $node, Config $params)
{
if ($node instanceof Raw) {
return new RawPage($node->getPath());

View File

@ -19,7 +19,7 @@ class Command extends SymfonyCommand
->setDescription('Generate documentation')
->addOption('configuration', 'c', InputArgument::OPTIONAL, 'Configuration file')
->addOption('format', 'f', InputArgument::OPTIONAL, 'Output format, html or confluence', 'html')
->addOption('processor', 'p', InputArgument::OPTIONAL, 'Manipulations on the tree', 'none')
->addOption('processor', 'p', InputArgument::OPTIONAL, 'Manipulations on the tree')
->addOption('destination', 'd', InputArgument::OPTIONAL, $description, 'static');
}
@ -30,50 +30,30 @@ class Command extends SymfonyCommand
$width = $this->getApplication()->getTerminalDimensions()[0];
$processor = $input->getOption('processor');
if (!empty($processor) && $processor != 'none') {
$this->prepareProcessor($processor, $daux, $output, $width);
}
// Instiantiate the processor if one is defined
$this->prepareProcessor($daux, $input, $output, $width);
// Improve the tree with a processor
$daux->getProcessor()->manipulateTree($daux->tree);
$this->launchGeneration($daux, $input, $output, $width);
// Set the format if requested
if ($input->getOption('format')) {
$daux->getParams()['format'] = $input->getOption('format');
}
// Generate the documentation
$daux->getGenerator()->generateAll($input, $output, $width);
}
protected function prepareProcessor($processor, Daux $daux, OutputInterface $output, $width)
protected function prepareProcessor(Daux $daux, InputInterface $input, OutputInterface $output, $width)
{
$class = "\\Todaymade\\Daux\\Extension\\" . $processor;
if (class_exists($class)) {
if ($input->getOption('processor')) {
$daux->getParams()['processor'] = $input->getOption('processor');
}
$class = $daux->getProcessorClass();
if ($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($daux))->generateAll($input, $output, $width);
}
}

View File

@ -1,8 +1,10 @@
<?php namespace Todaymade\Daux\Server;
use Symfony\Component\Console\Output\NullOutput;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception;
use Todaymade\Daux\Format\Base\LiveGenerator;
use Todaymade\Daux\Format\HTML\Generator;
use Todaymade\Daux\Format\HTML\RawPage;
@ -22,9 +24,20 @@ class Server
{
$daux = new Daux(Daux::LIVE_MODE);
$daux->initialize();
$class = $daux->getProcessorClass();
if ($class) {
$daux->setProcessor(new $class($daux, new NullOutput(), 0));
}
// Improve the tree with a processor
$daux->getProcessor()->manipulateTree($daux->tree);
$server = new static($daux);
try {
$daux->initialize();
$server = new static($daux);
$page = $server->handle($_REQUEST);
} catch (NotFoundException $e) {
@ -117,9 +130,16 @@ class Server
throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
}
// TODO :: make it possible to replace the generator in live code
$generator = new Generator($this->daux);
return $generator->generateOne($file, $this->params);
$generator = $this->daux->getGenerator();
if (!$generator instanceof LiveGenerator) {
throw new \RuntimeException(
"The generator '" . get_class($generator) . "' does not implement the interface " .
"'Todaymade\\Daux\\Format\\Base\\LiveGenerator' and thus doesn't support live rendering."
);
}
return $this->daux->getGenerator()->generateOne($file, $this->params);
}
public function getRequest()