Added a content type system to be able to extend the conversion mechanism

This commit is contained in:
Stéphane Goetz 2015-07-28 17:25:03 +02:00
parent 45328d5f4e
commit b5ce4f1d79
20 changed files with 232 additions and 37 deletions

View File

@ -1,6 +1,5 @@
{ {
"docs_directory": "docs", "docs_directory": "docs",
"valid_content_extensions": ["md", "markdown"],
"title": "My Project", "title": "My Project",
"tagline": "My Stylish Documentation", "tagline": "My Stylish Documentation",

View File

@ -23,9 +23,15 @@ class Generate extends SymfonyCommand
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
// Initialize the system
$daux = new Daux(Daux::STATIC_MODE); $daux = new Daux(Daux::STATIC_MODE);
$daux->initialize($input->getOption('configuration')); $daux->initialize($input->getOption('configuration'));
// Set the format if requested
if ($input->getOption('format')) {
$daux->getParams()['format'] = $input->getOption('format');
}
$width = $this->getApplication()->getTerminalDimensions()[0]; $width = $this->getApplication()->getTerminalDimensions()[0];
// Instiantiate the processor if one is defined // Instiantiate the processor if one is defined
@ -35,11 +41,6 @@ class Generate extends SymfonyCommand
$daux->generateTree(); $daux->generateTree();
$daux->getProcessor()->manipulateTree($daux->tree); $daux->getProcessor()->manipulateTree($daux->tree);
// Set the format if requested
if ($input->getOption('format')) {
$daux->getParams()['format'] = $input->getOption('format');
}
// Generate the documentation // Generate the documentation
$daux->getGenerator()->generateAll($input, $output, $width); $daux->getGenerator()->generateAll($input, $output, $width);
} }

View File

@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux; <?php namespace Todaymade\Daux;
use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\NullOutput;
use Todaymade\Daux\Format\Base\ContentTypes\ContentTypeHandler;
use Todaymade\Daux\Tree\Builder; use Todaymade\Daux\Tree\Builder;
use Todaymade\Daux\Tree\Root; use Todaymade\Daux\Tree\Root;
@ -15,6 +16,17 @@ class Daux
/** @var string */ /** @var string */
public $internal_base; public $internal_base;
/** @var \Todaymade\Daux\Format\Base\Generator */
protected $generator;
/** @var \Todaymade\Daux\Format\Base\ContentTypes\ContentTypeHandler */
protected $typeHandler;
/**
* @var string[]
*/
protected $validExtensions;
/** @var string */ /** @var string */
private $docs_path; private $docs_path;
@ -146,6 +158,8 @@ class Daux
*/ */
public function generateTree() public function generateTree()
{ {
$this->options['valid_content_extensions'] = $this->getContentExtensions();
$this->tree = new Root($this->getParams(), $this->docs_path); $this->tree = new Root($this->getParams(), $this->docs_path);
Builder::build($this->tree, $this->options['ignore']); Builder::build($this->tree, $this->options['ignore']);
@ -257,6 +271,10 @@ class Daux
*/ */
public function getGenerator() public function getGenerator()
{ {
if ($this->generator) {
return $this->generator;
}
$generators = $this->getGenerators(); $generators = $this->getGenerators();
$format = $this->getParams()['format']; $format = $this->getParams()['format'];
@ -275,6 +293,35 @@ class Daux
throw new \RuntimeException("The class '$class' does not implement the '$interface' interface"); throw new \RuntimeException("The class '$class' does not implement the '$interface' interface");
} }
return new $class($this); return $this->generator = new $class($this);
}
public function getContentTypeHandler()
{
if ($this->typeHandler) {
return $this->typeHandler;
}
$base_types = $this->getGenerator()->getContentTypes();
$extended = $this->getProcessor()->addContentType();
$types = array_merge($base_types, $extended);
return $this->typeHandler = new ContentTypeHandler($types);
}
/**
* Get all content file extensions
*
* @return string[]
*/
public function getContentExtensions()
{
if ($this->validExtensions) {
return $this->validExtensions;
}
return $this->validExtensions = $this->getContentTypeHandler()->getContentExtensions();
} }
} }

View File

@ -1,10 +1,10 @@
<?php namespace Todaymade\Daux\Format\Base; <?php namespace Todaymade\Daux\Format\Base;
use League\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
use Todaymade\Daux\Format\Base\ContentTypes\ContentType;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
abstract class MarkdownPage extends SimplePage abstract class ContentPage extends SimplePage
{ {
/** /**
* @var Content * @var Content
@ -17,9 +17,9 @@ abstract class MarkdownPage extends SimplePage
protected $params; protected $params;
/** /**
* @var CommonMarkConverter * @var ContentType
*/ */
protected $converter; protected $contentType;
public function __construct($title, $content) public function __construct($title, $content)
{ {
@ -41,14 +41,17 @@ abstract class MarkdownPage extends SimplePage
$this->params = $params; $this->params = $params;
} }
protected function getMarkdownConverter() /**
* @param ContentType $contentType
*/
public function setContentType($contentType)
{ {
return $this->converter; $this->contentType = $contentType;
} }
protected function convertPage($content) protected function convertPage($content)
{ {
return $this->getMarkdownConverter()->convertToHtml($content); return $this->contentType->convert($content);
} }
protected function generatePage() protected function generatePage()
@ -56,12 +59,12 @@ abstract class MarkdownPage extends SimplePage
return $this->convertPage($this->content); return $this->convertPage($this->content);
} }
public static function fromFile(Content $file, $params, CommonMarkConverter $converter) public static function fromFile(Content $file, $params, ContentType $contentType)
{ {
$page = new static($file->getTitle(), $file->getContent()); $page = new static($file->getTitle(), $file->getContent());
$page->setFile($file); $page->setFile($file);
$page->setParams($params); $page->setParams($params);
$page->converter = $converter; $page->setContentType($contentType);
return $page; return $page;
} }

View File

@ -0,0 +1,17 @@
<?php namespace Todaymade\Daux\Format\Base\ContentTypes;
use Todaymade\Daux\Config;
interface ContentType
{
public function __construct(Config $config);
/**
* Get the file extensions supported by this Content Type
*
* @return string[]
*/
public function getExtensions();
public function convert($html);
}

View File

@ -0,0 +1,53 @@
<?php namespace Todaymade\Daux\Format\Base\ContentTypes;
use Todaymade\Daux\Tree\Content;
class ContentTypeHandler
{
/**
* @var ContentType[] $types
*/
protected $types;
/**
* @param ContentType[] $types
*/
public function __construct($types)
{
$this->types = array_reverse($types);
}
/**
* Get all valid content file extensions
*
* @return string[]
*/
public function getContentExtensions()
{
$extensions = [];
foreach ($this->types as $type) {
$extensions = array_merge($extensions, $type->getExtensions());
}
return array_unique($extensions);
}
/**
* Get the ContentType able to handle this node
*
* @param Content $node
* @return ContentType
*/
public function getType(Content $node)
{
$extension = pathinfo($node->getPath(), PATHINFO_EXTENSION);
foreach ($this->types as $type) {
if (in_array($extension, $type->getExtensions())) {
return $type;
}
}
throw new \RuntimeException("no contentType found for {$node->getPath()}");
}
}

View File

@ -1,4 +1,4 @@
<?php namespace Todaymade\Daux\Format\Base\CommonMark; <?php namespace Todaymade\Daux\Format\Base\ContentTypes\Markdown;
use League\CommonMark\DocParser; use League\CommonMark\DocParser;
use League\CommonMark\Environment; use League\CommonMark\Environment;

View File

@ -0,0 +1,31 @@
<?php namespace Todaymade\Daux\Format\Base\ContentTypes\Markdown;
use Todaymade\Daux\Config;
class ContentType implements \Todaymade\Daux\Format\Base\ContentTypes\ContentType
{
/** @var Config */
protected $config;
/** @var CommonMarkConverter */
protected $converter;
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
}
/**
* @return array
*/
public function getExtensions()
{
return ['md', 'markdown'];
}
public function convert($html)
{
return $this->converter->convertToHtml($html);
}
}

View File

@ -1,4 +1,4 @@
<?php namespace Todaymade\Daux\Format\Base\CommonMark; <?php namespace Todaymade\Daux\Format\Base\ContentTypes\Markdown;
use League\CommonMark\HtmlElement; use League\CommonMark\HtmlElement;
use League\CommonMark\HtmlRendererInterface; use League\CommonMark\HtmlRendererInterface;

View File

@ -3,6 +3,7 @@
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\Format\Base\ContentTypes\ContentTypeHandler;
interface Generator interface Generator
{ {
@ -18,4 +19,9 @@ interface Generator
* @return mixed * @return mixed
*/ */
public function generateAll(InputInterface $input, OutputInterface $output, $width); public function generateAll(InputInterface $input, OutputInterface $output, $width);
/**
* @return array
*/
public function getContentTypes();
} }

View File

@ -3,7 +3,7 @@
use DOMDocument; use DOMDocument;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
{ {
public $attachments = []; public $attachments = [];

View File

@ -1,8 +1,8 @@
<?php namespace Todaymade\Daux\Format\Confluence\CommonMark; <?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use League\CommonMark\Environment; use League\CommonMark\Environment;
class CommonMarkConverter extends \Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter class CommonMarkConverter extends \Todaymade\Daux\Format\Base\ContentTypes\Markdown\CommonMarkConverter
{ {
protected function getLinkRenderer(Environment $environment) protected function getLinkRenderer(Environment $environment)
{ {

View File

@ -0,0 +1,12 @@
<?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use Todaymade\Daux\Config;
class ContentType extends \Todaymade\Daux\Format\Base\ContentTypes\Markdown\ContentType
{
public function __construct(Config $config)
{
$this->config = $config;
$this->converter = new CommonMarkConverter(['daux' => $config]);
}
}

View File

@ -1,4 +1,4 @@
<?php namespace Todaymade\Daux\Format\Confluence\CommonMark; <?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use League\CommonMark\Block\Element\AbstractBlock; use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Element\FencedCode; use League\CommonMark\Block\Element\FencedCode;

View File

@ -1,4 +1,4 @@
<?php namespace Todaymade\Daux\Format\Confluence\CommonMark; <?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use League\CommonMark\Block\Element\AbstractBlock; use League\CommonMark\Block\Element\AbstractBlock;
use League\CommonMark\Block\Element\IndentedCode; use League\CommonMark\Block\Element\IndentedCode;

View File

@ -1,11 +1,11 @@
<?php namespace Todaymade\Daux\Format\Confluence\CommonMark; <?php namespace Todaymade\Daux\Format\Confluence\ContentTypes\Markdown;
use League\CommonMark\HtmlElement; use League\CommonMark\HtmlElement;
use League\CommonMark\HtmlRendererInterface; use League\CommonMark\HtmlRendererInterface;
use League\CommonMark\Inline\Element\AbstractInline; use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Element\Link; use League\CommonMark\Inline\Element\Link;
class LinkRenderer extends \Todaymade\Daux\Format\Base\CommonMark\LinkRenderer class LinkRenderer extends \Todaymade\Daux\Format\Base\ContentTypes\Markdown\LinkRenderer
{ {
/** /**
* @param Link $inline * @param Link $inline

View File

@ -5,6 +5,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
use Todaymade\Daux\Console\RunAction; use Todaymade\Daux\Console\RunAction;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\Format\Base\ContentTypes\ContentTypeHandler;
use Todaymade\Daux\Format\Confluence\CommonMark\CommonMarkConverter; use Todaymade\Daux\Format\Confluence\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
@ -16,9 +17,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
/** @var string */ /** @var string */
protected $prefix; protected $prefix;
/** @var CommonMarkConverter */
protected $converter;
/** @var Daux */ /** @var Daux */
protected $daux; protected $daux;
@ -28,7 +26,16 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
public function __construct(Daux $daux) public function __construct(Daux $daux)
{ {
$this->daux = $daux; $this->daux = $daux;
$this->converter = new CommonMarkConverter(['daux' => $this->daux->getParams()]); }
/**
* @return array
*/
public function getContentTypes()
{
return [
new ContentTypes\Markdown\ContentType($this->daux->getParams())
];
} }
/** /**
@ -83,7 +90,7 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
$data = [ $data = [
'title' => $this->prefix . $node->getTitle(), 'title' => $this->prefix . $node->getTitle(),
'file' => $node, 'file' => $node,
'page' => MarkdownPage::fromFile($node, $params, $this->converter), 'page' => ContentPage::fromFile($node, $params, $this->daux->getContentTypeHandler()->getType($node)),
]; ];
// As the page is lazily generated // As the page is lazily generated

View File

@ -2,7 +2,7 @@
use Todaymade\Daux\Tree\Root; use Todaymade\Daux\Tree\Root;
class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage class ContentPage extends \Todaymade\Daux\Format\Base\ContentPage
{ {
private $language; private $language;
private $homepage; private $homepage;

View File

@ -7,6 +7,7 @@ use Todaymade\Daux\Console\RunAction;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter; use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Format\Base\ContentTypes\ContentTypeHandler;
use Todaymade\Daux\Format\Base\LiveGenerator; use Todaymade\Daux\Format\Base\LiveGenerator;
use Todaymade\Daux\GeneratorHelper; use Todaymade\Daux\GeneratorHelper;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
@ -18,9 +19,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
{ {
use RunAction; use RunAction;
/** @var CommonMarkConverter */
protected $converter;
/** @var Daux */ /** @var Daux */
protected $daux; protected $daux;
@ -30,7 +28,16 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
public function __construct(Daux $daux) public function __construct(Daux $daux)
{ {
$this->daux = $daux; $this->daux = $daux;
$this->converter = new CommonMarkConverter(['daux' => $this->daux->getParams()]); }
/**
* @return array
*/
public function getContentTypes()
{
return [
new \Todaymade\Daux\Format\Base\ContentTypes\Markdown\ContentType($this->daux->getParams())
];
} }
public function generateAll(InputInterface $input, OutputInterface $output, $width) public function generateAll(InputInterface $input, OutputInterface $output, $width)
@ -113,6 +120,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
} }
$params['request'] = $node->getUrl(); $params['request'] = $node->getUrl();
return MarkdownPage::fromFile($node, $params, $this->converter); return ContentPage::fromFile($node, $params, $this->daux->getContentTypeHandler()->getType($node));
} }
} }

View File

@ -62,10 +62,22 @@ class Processor
* `Todaymade\Daux\Format\Base\Generator` contract. * `Todaymade\Daux\Format\Base\Generator` contract.
* You can also replace base generators. * You can also replace base generators.
* *
* @return array * @return string[]
*/ */
public function addGenerators() public function addGenerators()
{ {
return []; return [];
} }
/**
* Provide new content Types to be used during the generation
* phase, with this you can change the markdown parser or add
* a completely different file type.
*
* @return \Todaymade\Daux\Format\Base\ContentTypes\ContentType[]
*/
public function addContentType()
{
return [];
}
} }