Initialize the markdown parser only once.

This commit is contained in:
Stéphane Goetz 2015-07-19 01:05:43 +02:00
parent e3a3438ccd
commit 87fbb6f15d
6 changed files with 30 additions and 15 deletions

View File

@ -1,7 +1,7 @@
<?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\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
abstract class MarkdownPage extends SimplePage abstract class MarkdownPage extends SimplePage
@ -16,6 +16,11 @@ abstract class MarkdownPage extends SimplePage
*/ */
protected $params; protected $params;
/**
* @var CommonMarkConverter
*/
protected $converter;
public function __construct($title, $content) public function __construct($title, $content)
{ {
$this->initializePage($title, $content); $this->initializePage($title, $content);
@ -38,7 +43,7 @@ abstract class MarkdownPage extends SimplePage
protected function getMarkdownConverter() protected function getMarkdownConverter()
{ {
return new CommonMarkConverter(['daux' => $this->params]); return $this->converter;
} }
protected function convertPage($content) protected function convertPage($content)
@ -51,11 +56,12 @@ abstract class MarkdownPage extends SimplePage
return $this->convertPage($this->content); return $this->convertPage($this->content);
} }
public static function fromFile(Content $file, $params) public static function fromFile(Content $file, $params, CommonMarkConverter $converter)
{ {
$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;
return $page; return $page;
} }

View File

@ -4,6 +4,7 @@ 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;
use Todaymade\Daux\Format\Confluence\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Format\Base\RunAction; 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;
@ -17,13 +18,19 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
*/ */
protected $prefix; protected $prefix;
/**
* @var CommonMarkConverter
*/
protected $converter;
public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width) public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width)
{ {
$confluence = $daux->getParams()['confluence']; $params = $daux->getParams();
$confluence = $params['confluence'];
$this->prefix = trim($confluence['prefix']) . " "; $this->prefix = trim($confluence['prefix']) . " ";
$params = $daux->getParams(); $this->converter = new CommonMarkConverter(['daux' => $params]);
$tree = $this->runAction( $tree = $this->runAction(
"Generating Tree ...", "Generating Tree ...",
@ -68,7 +75,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), 'page' => MarkdownPage::fromFile($node, $params, $this->converter),
]; ];
// As the page is lazily generated // As the page is lazily generated

View File

@ -2,17 +2,11 @@
use DOMDocument; use DOMDocument;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Format\Confluence\CommonMark\CommonMarkConverter;
class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage
{ {
public $attachments = []; public $attachments = [];
protected function getMarkdownConverter()
{
return new CommonMarkConverter(['daux' => $this->params]);
}
protected function generatePage() protected function generatePage()
{ {
$page = parent::generatePage(); $page = parent::generatePage();

View File

@ -5,6 +5,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Todaymade\Daux\Config; use Todaymade\Daux\Config;
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\RunAction; use Todaymade\Daux\Format\Base\RunAction;
use Todaymade\Daux\Generator\Helper; use Todaymade\Daux\Generator\Helper;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
@ -14,6 +15,11 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
{ {
use RunAction; use RunAction;
/**
* @var CommonMarkConverter
*/
protected $converter;
public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width) public function generate(Daux $daux, InputInterface $input, OutputInterface $output, $width)
{ {
$destination = $input->getOption('destination'); $destination = $input->getOption('destination');
@ -23,6 +29,8 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
$destination = $daux->local_base . DS . 'static'; $destination = $daux->local_base . DS . 'static';
} }
$this->converter = new CommonMarkConverter(['daux' => $params]);
$this->runAction( $this->runAction(
"Copying Static assets ...", "Copying Static assets ...",
$output, $output,
@ -85,7 +93,7 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator
$params['request'] = $node->getUrl(); $params['request'] = $node->getUrl();
$params['file_uri'] = $node->getName(); $params['file_uri'] = $node->getName();
$page = MarkdownPage::fromFile($node, $params); $page = MarkdownPage::fromFile($node, $params, $this->converter);
file_put_contents($output_dir . DS . $key, $page->getContent()); file_put_contents($output_dir . DS . $key, $page->getContent());
} }
); );

View File

@ -1,6 +1,5 @@
<?php namespace Todaymade\Daux\Format\HTML; <?php namespace Todaymade\Daux\Format\HTML;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Tree\Root; use Todaymade\Daux\Tree\Root;
class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage

View File

@ -3,6 +3,7 @@
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Exception; use Todaymade\Daux\Exception;
use Todaymade\Daux\Format\Base\CommonMark\CommonMarkConverter;
use Todaymade\Daux\Format\HTML\MarkdownPage; use Todaymade\Daux\Format\HTML\MarkdownPage;
use Todaymade\Daux\Format\HTML\RawPage; use Todaymade\Daux\Format\HTML\RawPage;
use Todaymade\Daux\Tree\Raw; use Todaymade\Daux\Tree\Raw;
@ -133,7 +134,7 @@ class Server
if ($request !== 'index') { if ($request !== 'index') {
$params['entry_page'] = $file->getFirstPage(); $params['entry_page'] = $file->getFirstPage();
} }
return MarkdownPage::fromFile($file, $params); return MarkdownPage::fromFile($file, $params, new CommonMarkConverter(['daux' => $params]));
} }
public function getRequest() public function getRequest()