Create a notion of Export format, prepare for multiple generation systems

Cette révision appartient à :
Stéphane Goetz
2015-05-18 14:26:29 +02:00
révisé par Stéphane Goetz
Parent cc4ba14dc2
révision 90dbdb4f0b
19 fichiers modifiés avec 246 ajouts et 229 suppressions

Voir le fichier

@ -0,0 +1,45 @@
<?php namespace Todaymade\Daux\Format\Base;
use Todaymade\Daux\Tree\Content;
abstract class MarkdownPage extends SimplePage
{
/**
* @var Content
*/
protected $file;
/**
* @var array
*/
protected $params;
public function __construct($title, $content)
{
$this->initializePage($title, $content);
}
public function setFile(Content $file)
{
$this->file = $file;
}
public function setParams(array $params)
{
$this->params = $params;
}
protected function generatePage()
{
return (new \Parsedown())->text($this->content);
}
public static function fromFile(Content $file, $params)
{
$page = new static($file->title, file_get_contents($file->getPath()));
$page->setFile($file);
$page->setParams($params);
return $page;
}
}

6
libs/Format/Base/Page.php Fichier normal
Voir le fichier

@ -0,0 +1,6 @@
<?php namespace Todaymade\Daux\Format\Base;
interface Page
{
public function getContent();
}

23
libs/Format/Base/RawPage.php Fichier normal
Voir le fichier

@ -0,0 +1,23 @@
<?php namespace Todaymade\Daux\Format\Base;
use Todaymade\Daux\Exception;
abstract class RawPage implements Page
{
protected $file;
public function __construct($filename)
{
$this->file = $filename;
}
public function getFile()
{
return $this->file;
}
public function getContent()
{
throw new Exception("you should not use this method to show a raw content");
}
}

Voir le fichier

@ -0,0 +1,33 @@
<?php namespace Todaymade\Daux\Format\Base;
abstract class SimplePage implements Page
{
protected $title;
protected $content;
protected $generated = null;
public function __construct($title, $content)
{
$this->initializePage($title, $content);
}
public function getContent()
{
if (is_null($this->generated)) {
$this->generated = $this->generatePage();
}
return $this->generated;
}
protected function initializePage($title, $content)
{
$this->title = $title;
$this->content = $content;
}
protected function generatePage()
{
return $this->content;
}
}

Voir le fichier

@ -0,0 +1,84 @@
<?php namespace Todaymade\Daux\Format\HTML;
class MarkdownPage extends \Todaymade\Daux\Format\Base\MarkdownPage
{
private $language;
private $homepage;
private function initialize()
{
$this->homepage = false;
if ($this->title === 'index') {
$minimum_parent_dir_size = ($this->params['multilanguage']) ? 2 : 1;
if (count($this->file->getParents()) >= $minimum_parent_dir_size) {
$parents = $this->file->getParents();
$this->title = end($parents)->getTitle();
} else {
$this->homepage = ($this->file->getName() === '_index');
$this->title = $this->params['title'];
}
}
$this->language = '';
if ($this->params['multilanguage'] && count($this->file->getParents())) {
reset($this->file->getParents());
$language_dir = current($this->file->getParents());
$this->language = $language_dir->name;
}
}
private function getBreadcrumbTrail($parents, $multilanguage)
{
if ($multilanguage && !empty($parents)) {
$parents = array_splice($parents, 1);
}
$breadcrumb_trail = array();
if (!empty($parents)) {
foreach ($parents as $node) {
$breadcrumb_trail[$node->getTitle()] = $node->getUrl();
}
}
return $breadcrumb_trail;
}
protected function generatePage()
{
$this->initialize();
$params = $this->params;
$entry_page = [];
if ($params['request'] === $params['index_key']) {
if ($params['multilanguage']) {
foreach ($params['languages'] as $key => $name) {
$entry_page[$name] = $params['base_page'] . $params['entry_page'][$key]->getUrl();
}
} else {
$entry_page['View Documentation'] = $params['base_page'] . $params['entry_page']->getUrl();
}
} elseif ($params['file_uri'] === 'index') {
$entry_page[$params['entry_page']->title] = $params['base_page'] . $params['entry_page']->getUrl();
}
$page = [
'entry_page' => $entry_page,
'homepage' => $this->homepage,
'title' => $this->file->getTitle(),
'filename' => $this->file->getName(),
'language' => $this->language,
'path' => $this->file->getPath(),
'modified_time' => filemtime($this->file->getPath()),
'markdown' => $this->content,
'request' => $params['request'],
'content' => (new \Parsedown())->text($this->content),
'breadcrumbs' => $params['breadcrumbs']
];
if ($page['breadcrumbs']) {
$page['breadcrumb_trail'] = $this->getBreadcrumbTrail($this->file->getParents(), $params['multilanguage']);
$page['breadcrumb_separator'] = $params['breadcrumb_separator'];
}
$template = new Template($params['templates'], $params['theme']['templates']);
return $template->render($this->homepage? 'home' : 'content', ['page' => $page, 'params' => $params]);
}
}

6
libs/Format/HTML/RawPage.php Fichier normal
Voir le fichier

@ -0,0 +1,6 @@
<?php namespace Todaymade\Daux\Format\HTML;
class RawPage extends \Todaymade\Daux\Format\Base\RawPage
{
}

Voir le fichier

@ -0,0 +1,5 @@
<?php namespace Todaymade\Daux\Format\HTML;
class SimplePage extends \Todaymade\Daux\Format\Base\SimplePage
{
}

135
libs/Format/HTML/Template.php Fichier normal
Voir le fichier

@ -0,0 +1,135 @@
<?php namespace Todaymade\Daux\Format\HTML;
use League\Plates\Engine;
use Todaymade\Daux\Daux;
class Template
{
protected $engine;
public function __construct($base, $theme)
{
// Create new Plates instance
$this->engine = new Engine($base);
if (!is_dir($theme)) {
$theme = $base;
}
$this->engine->addFolder('theme', $theme, true);
$this->registerFunctions();
}
public function render($name, array $data = array())
{
$this->engine->addData([
'base_url' => $data['params']['base_url'],
'base_page' => $data['params']['base_page'],
'page' => $data['page'],
'params' => $data['params'],
'tree' => $data['params']['tree'],
]);
return $this->engine->render($name, $data);
}
protected function registerFunctions()
{
$this->engine->registerFunction('get_navigation', function($tree, $path, $current_url, $base_page, $mode) {
$nav = $this->buildNavigation($tree, $path, $current_url, $base_page, $mode);
return $this->renderNavigation($nav);
});
$this->engine->registerFunction('get_breadcrumb_title', function($page, $base_page) {
$title = '';
$breadcrumb_trail = $page['breadcrumb_trail'];
$separator = $this->getSeparator($page['breadcrumb_separator']);
foreach ($breadcrumb_trail as $key => $value) {
$title .= '<a href="' . $base_page . $value . '">' . $key . '</a>' . $separator;
}
if ($page['filename'] === 'index' || $page['filename'] === '_index') {
if ($page['title'] != '') {
$title = substr($title, 0, -1 * strlen($separator));
}
} else {
$title .= '<a href="' . $base_page . $page['request'] . '">' . $page['title'] . '</a>';
}
return $title;
});
}
private function renderNavigation($entries)
{
$nav = "";
foreach ($entries as $entry) {
if (array_key_exists('children', $entry)) {
if (array_key_exists('href', $entry)) {
$link = '<a href="' . $entry['href'] . '" class="folder">' . $entry['title'] . '</a>';
} else {
$link = '<a href="#" class="aj-nav folder">' . $entry['title'] . '</a>';
}
$link .= $this->renderNavigation($entry['children']);
} else {
$link = '<a href="' . $entry['href'] . '">' . $entry['title'] . '</a>';
}
$nav .= "<li class='$entry[class]'>$link</li>";
}
return "<ul class='nav nav-list'>$nav</ul>";
}
private function buildNavigation($tree, $path, $current_url, $base_page, $mode)
{
$nav = [];
foreach ($tree->value as $node) {
$url = $node->getUri();
if ($node instanceof \Todaymade\Daux\Tree\Content) {
if ($node->value === 'index') {
continue;
}
$link = ($path === '') ? $url : $path . '/' . $url;
$nav[] = [
'title' => $node->getTitle(),
'href' => $base_page . $link,
'class' => ($current_url === $link)? 'active' : ''
];
}
if ($node instanceof \Todaymade\Daux\Tree\Directory) {
$link = ($path === '') ? $url : $path . '/' . $url;
$folder = [
'title' => $node->getTitle(),
'class' => (strpos($current_url, $link) === 0)? 'open' : '',
];
if ($mode === Daux::STATIC_MODE) {
$link .= "/index.html";
}
if ($node->getIndexPage()) {
$folder['href'] = $base_page . $link;
}
//Child pages
$new_path = ($path === '') ? $url : $path . '/' . $url;
$folder['children'] = $this->buildNavigation($node, $new_path, $current_url, $base_page, $mode);
$nav[] = $folder;
}
}
return $nav;
}
private function getSeparator($separator)
{
switch ($separator) {
case 'Chevrons':
return ' <i class="glyphicon glyphicon-chevron-right"></i> ';
default:
return $separator;
}
}
}