New templating system

This commit is contained in:
Stéphane Goetz
2015-04-23 10:24:50 +02:00
bovenliggende de1214cbab
commit 52b679b33f
134 gewijzigde bestanden met toevoegingen van 529 en 592 verwijderingen

Bestand weergeven

@ -111,36 +111,15 @@ class Daux
public function getParams()
{
$params = array(
//Informations
'tagline' => $this->options['tagline'],
'title' => $this->options['title'],
'author' => $this->options['author'],
'image' => $this->options['image'],
'repo' => $this->options['repo'],
'links' => $this->options['links'],
'twitter' => $this->options['twitter'],
$params = $this->options += array(
//Features
'google_analytics' => ($g = $this->options['google_analytics']) ?
DauxHelper::googleAnalytics($g, $this->host) : '',
'piwik_analytics' => ($p = $this->options['piwik_analytics']) ?
DauxHelper::piwikAnalytics($p, $this->options['piwik_analytics_id']) : '',
'toggle_code' => $this->options['toggle_code'],
'float' => $this->options['float'],
'date_modified' => $this->options['date_modified'],
'file_editor' => false,
'breadcrumbs' => $this->options['breadcrumbs'],
'breadcrumb_separator' => $this->options['breadcrumb_separator'],
'multilanguage' => !empty($this->options['languages']),
'languages' => $this->options['languages'],
//Paths and tree
'mode' => $this->mode,
'local_base' => $this->local_base,
'docs_path' => $this->docs_path,
'template' => $this->options['template'],
'templates' => $this->local_base . DS . 'templates',
);
if ($this->tree) {
@ -172,10 +151,10 @@ class Daux
}
$params['theme'] = DauxHelper::getTheme(
$this->local_base . DS . 'templates' . DS . $this->options['template'] . DS . 'themes' . DS . $this->options['theme'],
$this->local_base . DS . 'resources' . DS . 'themes' . DS . $this->options['theme'],
$params['base_url'],
$this->local_base,
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'
$params['base_url'] . "resources/themes/" . $this->options['theme'] . '/'
);
return $params;

Bestand weergeven

@ -47,15 +47,13 @@ class DauxHelper
'require-jquery' => false,
'bootstrap-js' => false,
'favicon' => '<base_url>resources/img/favicon.png',
'template' => $local_base . DS . 'templates' . DS . 'default/default.php',
'error-template' => $local_base . DS . 'templates' . DS . 'default/error.php',
'templates' => $theme_folder . DS . 'templates',
];
$substitutions = ['<local_base>' => $local_base, '<base_url>' => $base_url, '<theme_url>' => $theme_url];
// Substitute some placeholders
$theme['template'] = strtr($theme['template'], $substitutions);
$theme['error-template'] = strtr($theme['error-template'], $substitutions);
$theme['templates'] = strtr($theme['templates'], $substitutions);
$theme['favicon'] = utf8_encode(strtr($theme['favicon'], $substitutions));
foreach ($theme['css'] as $key => $css) {
@ -73,43 +71,6 @@ class DauxHelper
return $theme;
}
public static function googleAnalytics($analytics, $host)
{
$ga = <<<EOT
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
EOT;
$ga .= "ga('create', '" . $analytics . "', '" . $host . "');";
$ga .= "ga('send', 'pageview');";
$ga .= '</script>';
return $ga;
}
public static function piwikAnalytics($analytics_url, $analytics_id)
{
$pa = <<<EOT
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]);
(function() {
EOT;
$pa .= 'var u=(("https:" == document.location.protocol) ? "https" : "http") + "://' . $analytics_url . '/";';
$pa .= '_paq.push(["setTrackerUrl", u+"piwik.php"]);';
$pa .= '_paq.push(["setSiteId", ' . $analytics_id . ']);';
$pa .= <<<EOT
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0];
g.type="text/javascript";
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
})();
</script>
EOT;
return $pa;
}
public static function pathinfo($path)
{
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);

Bestand weergeven

@ -10,18 +10,6 @@ class Helper
@mkdir($path . DS . 'resources');
static::copyRecursive($local_base . DS . 'resources', $path . DS . 'resources');
@mkdir($path . DS . 'js');
static::copyRecursive($local_base . DS . 'js', $path . DS . 'js');
//added and changed these in order to fetch the theme files and put them in the right place
@mkdir($path . DS . 'templates');
@mkdir($path . DS . 'templates' . DS . 'default');
@mkdir($path . DS . 'templates' . DS . 'default' . DS . 'themes');
static::copyRecursive(
$local_base . DS . 'templates' . DS . 'default' . DS . 'themes',
$path . DS . 'templates' . DS . 'default' . DS . 'themes'
);
}
private static function rmdir($dir)

Bestand weergeven

@ -9,7 +9,6 @@ class MarkdownPage extends SimplePage
private $language;
private $homepage;
private $breadcrumb_trail;
private static $template;
public function __construct()
{
@ -51,10 +50,6 @@ class MarkdownPage extends SimplePage
$language_dir = current($file->getParents());
$this->language = $language_dir->name;
}
if (is_null(static::$template)) {
include_once($params['theme']['template']);
static::$template = new Template();
}
}
private function buildBreadcrumbTrail($parents, $multilanguage)
@ -89,20 +84,18 @@ class MarkdownPage extends SimplePage
if ($params['request'] === $params['index_key']) {
if ($params['multilanguage']) {
foreach ($params['languages'] as $key => $name) {
$entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->getUrl());
$entry_page[$name] = $params['base_page'] . $params['entry_page'][$key]->getUrl();
}
} else {
$entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->getUri());
$entry_page['View Documentation'] = $params['base_page'] . $params['entry_page']->getUri();
}
} elseif ($params['file_uri'] === 'index') {
$entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page'] . $params['entry_page']->getUrl());
$entry_page[$params['entry_page']->title] = $params['base_page'] . $params['entry_page']->getUrl();
}
$page['entry_page'] = $entry_page;
$page['homepage'] = $this->homepage;
$page['title'] = $this->file->getTitle();
$page['tagline'] = $params['tagline'];
$page['author'] = $params['author'];
$page['filename'] = $this->file->getName();
if ($page['breadcrumbs'] = $params['breadcrumbs']) {
$page['breadcrumb_trail'] = $this->breadcrumb_trail;
@ -110,17 +103,13 @@ class MarkdownPage extends SimplePage
}
$page['language'] = $this->language;
$page['path'] = $this->file->getPath();
$page['request'] = utf8_encode($params['request']);
$page['theme'] = $params['theme'];
$page['modified_time'] = filemtime($this->file->getPath());
$page['markdown'] = $this->content;
$page['file_editor'] = $params['file_editor'];
$page['google_analytics'] = $params['google_analytics'];
$page['piwik_analytics'] = $params['piwik_analytics'];
$Parsedown = new \Parsedown();
$page['content'] = $Parsedown->text($this->content);
return static::$template->get_content($page, $params);
$template = new Template($params['templates'], $params['theme']['templates']);
return $template->render($this->homepage? 'home' : 'content', ['page' => $page, 'params' => $params]);
}
}

Bestand weergeven

@ -10,7 +10,6 @@ class ErrorPage extends SimplePage
const FATAL_ERROR_TYPE = 'FATAL_ERROR';
private $params;
private static $template;
public function __construct($title, $content, $params)
{
@ -26,9 +25,6 @@ class ErrorPage extends SimplePage
public function getContent()
{
include_once($this->params['theme']['error-template']);
static::$template = new Template();
if (is_null($this->html)) {
$this->html = $this->generatePage();
}
@ -40,11 +36,9 @@ class ErrorPage extends SimplePage
{
$params = $this->params;
$page['title'] = $this->title;
$page['theme'] = $params['theme'];
$page['content'] = $this->content;
$page['google_analytics'] = $params['google_analytics'];
$page['piwik_analytics'] = $params['piwik_analytics'];
return static::$template->get_content($page, $params);
$template = new Template($params['templates'], $params['theme']['templates']);
return $template->render('error', ['page' => $page, 'params' => $params]);
}
}

117
libs/Template.php Normal file
Bestand weergeven

@ -0,0 +1,117 @@
<?php namespace Todaymade\Daux;
use League\Plates\Engine;
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([
'index' => utf8_encode($data['params']['base_page'] . $data['params']['index']->value),
'base_url' => $data['params']['base_url'],
'base_page' => $data['params']['base_page'],
'page' => $data['page'],
'params' => $data['params'],
//'homepage' => $data['params']['homepage'],
//'project_title' => utf8_encode($data['params']['title']),
'tree' => $data['params']['tree'],
//'entry_page' => $data['page']['entry_page'],
]);
return $this->engine->render($name, $data);
}
protected function registerFunctions()
{
$this->engine->registerFunction('get_navigation', function($tree, $path, $current_url, $base_page, $mode) {
$nav = '<ul class="nav nav-list">';
$nav .= $this->buildNavigation($tree, $path, $current_url, $base_page, $mode);
$nav .= '</ul>';
return $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 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;
}
$nav .= '<li';
$link = ($path === '') ? $url : $path . '/' . $url;
if ($current_url === $link) {
$nav .= ' class="active"';
}
$nav .= '><a href="' . $base_page . $link . '">' . $node->getTitle() . '</a></li>';
}
if ($node instanceof \Todaymade\Daux\Tree\Directory) {
$nav .= '<li';
$link = ($path === '') ? $url : $path . '/' . $url;
if (strpos($current_url, $link) === 0) {
$nav .= ' class="open"';
}
$nav .= ">";
if ($mode === \TodayMade\Daux\Daux::STATIC_MODE) {
$link .= "/index.html";
}
if ($node->getIndexPage()) {
$nav .= '<a href="' . $base_page . $link . '" class="folder">' .
$node->getTitle() . '</a>';
} else {
$nav .= '<a href="#" class="aj-nav folder">' . $node->getTitle() . '</a>';
}
$nav .= '<ul class="nav nav-list">';
$new_path = ($path === '') ? $url : $path . '/' . $url;
$nav .= $this->buildNavigation($node, $new_path, $current_url, $base_page, $mode);
$nav .= '</ul></li>';
}
}
return $nav;
}
private function getSeparator($separator)
{
switch ($separator) {
case 'Chevrons':
return ' <i class="glyphicon glyphicon-chevron-right"></i> ';
default:
return $separator;
}
}
}