Convert to PSR2

This commit is contained in:
Stéphane Goetz 2015-04-23 00:32:30 +02:00
parent ecd5efe758
commit de1214cbab
21 changed files with 664 additions and 493 deletions

View File

@ -65,6 +65,8 @@ software, even if advised of the possibility of such damage.
require_once("vendor/autoload.php"); require_once("vendor/autoload.php");
\Todaymade\Daux\Daux::initConstants();
$global_config = (isset($argv[1]))? $argv[1] : null; $global_config = (isset($argv[1]))? $argv[1] : null;
$destination = (isset($argv[2]))? $argv[2] : null; $destination = (isset($argv[2]))? $argv[2] : null;

View File

@ -65,4 +65,6 @@ software, even if advised of the possibility of such damage.
require_once("vendor/autoload.php"); require_once("vendor/autoload.php");
\Todaymade\Daux\Daux::initConstants();
\Todaymade\Daux\Server\Server::serve($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); \Todaymade\Daux\Server\Server::serve($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST);

View File

@ -1,173 +1,183 @@
<?php namespace Todaymade\Daux; <?php namespace Todaymade\Daux;
use Todaymade\Daux\Server\Helper as ServerHelper;
use Todaymade\Daux\Generator\Helper as GeneratorHelper;
use Todaymade\Daux\Tree\Builder; use Todaymade\Daux\Tree\Builder;
class Daux class Daux
{
const STATIC_MODE = 'DAUX_STATIC';
const LIVE_MODE = 'DAUX_LIVE';
public static $VALID_MARKDOWN_EXTENSIONS;
public $local_base;
public $base_url = '';
public $host;
private $docs_path;
public $tree;
public $options;
private $mode;
public function __construct($mode)
{ {
const STATIC_MODE = 'DAUX_STATIC'; $this->mode = $mode;
const LIVE_MODE = 'DAUX_LIVE';
public static $VALID_MARKDOWN_EXTENSIONS; $this->local_base = dirname(__DIR__);
public $local_base; $this->base_url = '';
public $base_url = '';
public $host;
private $docs_path;
public $tree;
public $options;
private $mode;
public function __construct($mode) { if ($this->mode == Daux::LIVE_MODE) {
$this->mode = $mode; $this->host = $_SERVER['HTTP_HOST'];
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$this->local_base = dirname(dirname(__FILE__)); $t = strrpos($this->base_url, '/index.php');
$this->base_url = ''; if ($t != false) {
$this->base_url = substr($this->base_url, 0, $t);
if ($this->mode == Daux::LIVE_MODE) { }
$this->host = $_SERVER['HTTP_HOST']; if (substr($this->base_url, -1) !== '/') {
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); $this->base_url .= '/';
$t = strrpos($this->base_url, '/index.php');
if ($t != FALSE) $this->base_url = substr($this->base_url, 0, $t);
if (substr($this->base_url, -1) !== '/') $this->base_url .= '/';
} }
} }
}
public function initialize($global_config_file = null, $config_file = 'config.json') { public static function initConstants()
$this->load_global_config($global_config_file); {
$this->load_docs_config($config_file); define("DS", DIRECTORY_SEPARATOR);
$this->generate_directory_tree(); }
public function initialize($global_config_file = null, $config_file = 'config.json')
{
$this->loadConfig($global_config_file);
$this->loadConfigOverrides($config_file);
$this->generateTree();
}
private function loadConfig($global_config_file)
{
if (is_null($global_config_file)) {
$global_config_file = $this->local_base . DS . 'global.json';
}
if (!file_exists($global_config_file)) {
throw new Exception('The Global Config file is missing. Requested File : ' . $global_config_file);
} }
private function load_global_config($global_config_file) { $global_config = json_decode(file_get_contents($global_config_file), true);
if (is_null($global_config_file)) $global_config_file = $this->local_base . DIRECTORY_SEPARATOR . 'global.json'; if (!isset($global_config)) {
if (!file_exists($global_config_file)) { throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct');
throw new Exception('The Global Config file is missing. Requested File : ' . $global_config_file);
}
$global_config = json_decode(file_get_contents($global_config_file), true);
if (!isset($global_config)) {
throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct');
}
if (!isset($global_config['docs_directory'])) {
throw new Exception('The Global Config file does not have the docs directory set.');
}
$this->docs_path = $global_config['docs_directory'];
if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DIRECTORY_SEPARATOR . $this->docs_path)) {
throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path);
}
if (!isset($global_config['valid_markdown_extensions'])) static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown');
else static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions'];
} }
private function load_docs_config($config_file) { if (!isset($global_config['docs_directory'])) {
$config_file = $this->docs_path . DIRECTORY_SEPARATOR . $config_file; throw new Exception('The Global Config file does not have the docs directory set.');
if (!file_exists($config_file)) {
throw new Exception('The local config file is missing. Check path : ' . $config_file);
}
$this->options = json_decode(file_get_contents($this->local_base . DIRECTORY_SEPARATOR . 'default.json'), true);
if (is_file($config_file)) {
$config = json_decode(file_get_contents($config_file), true);
if (!isset($config)) {
throw new Exception('There was an error parsing the Config file. Please review');
}
$this->options = array_merge($this->options, $config);
}
if (isset($this->options['timezone'])) date_default_timezone_set($this->options['timezone']);
else if (!ini_get('date.timezone')) date_default_timezone_set('GMT');
} }
private function generate_directory_tree() { $this->docs_path = $global_config['docs_directory'];
$this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->mode); if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DS . $this->docs_path)) {
if (!empty($this->options['languages'])) { throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path);
foreach ($this->options['languages'] as $key => $node) {
$this->tree->value[$key]->title = $node;
}
}
} }
public function get_base_params() { if (!isset($global_config['valid_markdown_extensions'])) {
$params = array( static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown');
//Informations } else {
'tagline' => $this->options['tagline'], static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions'];
'title' => $this->options['title'], }
'author' => $this->options['author'], }
'image' => $this->options['image'],
'repo' => $this->options['repo'],
'links' => $this->options['links'],
'twitter' => $this->options['twitter'],
//Features private function loadConfigOverrides($config_file)
'google_analytics' => ($g = $this->options['google_analytics']) ? DauxHelper::google_analytics($g, $this->host) : '', {
'piwik_analytics' => ($p = $this->options['piwik_analytics']) ? DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : '', $config_file = $this->docs_path . DS . $config_file;
'toggle_code' => $this->options['toggle_code'], if (!file_exists($config_file)) {
'float' => $this->options['float'], throw new Exception('The local config file is missing. Check path : ' . $config_file);
'date_modified' => $this->options['date_modified'], }
'file_editor' => false, $this->options = json_decode(file_get_contents($this->local_base . DS . 'default.json'), true);
'breadcrumbs' => $this->options['breadcrumbs'], if (is_file($config_file)) {
'breadcrumb_separator' => $this->options['breadcrumb_separator'], $config = json_decode(file_get_contents($config_file), true);
'multilanguage' => !empty($this->options['languages']), if (!isset($config)) {
'languages' => $this->options['languages'], throw new Exception('There was an error parsing the Config file. Please review');
}
$this->options = array_merge($this->options, $config);
}
if (isset($this->options['timezone'])) {
date_default_timezone_set($this->options['timezone']);
} elseif (!ini_get('date.timezone')) {
date_default_timezone_set('GMT');
}
}
private function generateTree()
{
$this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->getParams());
if (!empty($this->options['languages'])) {
foreach ($this->options['languages'] as $key => $node) {
$this->tree->value[$key]->title = $node;
}
}
}
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'],
//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 //Paths and tree
'mode' => $this->mode, 'mode' => $this->mode,
'local_base' => $this->local_base, 'local_base' => $this->local_base,
'docs_path' => $this->docs_path, 'docs_path' => $this->docs_path,
'tree' => $this->tree, 'template' => $this->options['template'],
'index' => ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page, );
'template' => $this->options['template'],
);
if (!$params['multilanguage']) { if ($this->tree) {
$params['tree'] = $this->tree;
$params['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage();
if ($params['multilanguage']) {
foreach ($this->options['languages'] as $key => $name) { foreach ($this->options['languages'] as $key => $name) {
$params['entry_page'][$key] = $this->tree->value[$key]->first_page; $params['entry_page'][$key] = $this->tree->value[$key]->getFirstPage();
} }
} else { } else {
$params['entry_page'] = $this->tree->first_page; $params['entry_page'] = $this->tree->getFirstPage();
} }
return $params;
} }
//TODO :: move to generator if ($this->mode == self::LIVE_MODE) {
public function get_page_params() {
$params = $this->get_base_params();
$params['index_key'] = 'index.html';
$params['base_page'] = $params['base_url'] = '';
$params['theme'] = DauxHelper::get_theme(
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'],
$params['base_url'],
$this->local_base,
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'
);
return $params;
}
//TODO :: move to server
public function get_live_page_params() {
$params = $this->get_base_params();
$params['index_key'] = 'index'; $params['index_key'] = 'index';
$params['host'] = $this->host; $params['host'] = $this->host;
$params['base_page'] = $params['base_url'] = '//' . $this->base_url; $params['base_page'] = $params['base_url'] = '//' . $this->base_url;
if (!$this->options['clean_urls']) $params['base_page'] .= 'index.php/'; if (!$this->options['clean_urls']) {
$params['base_page'] .= 'index.php/';
}
if ($params['image'] !== '') $params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']); if ($params['image'] !== '') {
$params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']);
$params['theme'] = DauxHelper::get_theme( }
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], } else {
$params['base_url'], $params['index_key'] = 'index.html';
$this->local_base, $params['base_page'] = $params['base_url'] = '';
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'
);
return $params;
} }
$params['theme'] = DauxHelper::getTheme(
$this->local_base . DS . 'templates' . DS . $this->options['template'] . DS . 'themes' . DS . $this->options['theme'],
$params['base_url'],
$this->local_base,
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'
);
return $params;
} }
}

View File

@ -1,111 +1,130 @@
<?php namespace Todaymade\Daux; <?php namespace Todaymade\Daux;
class DauxHelper { class DauxHelper
{
public static function get_breadcrumb_title_from_request($request, $separator = 'Chevrons', $multilanguage = false) { public static function getBreadcrumbFromRequest($request, $separator = 'Chevrons', $multilanguage = false)
if ($multilanguage) $request = substr($request, strpos($request, '/') + 1); {
$request = str_replace('_', ' ', $request); if ($multilanguage) {
switch ($separator) { $request = substr($request, strpos($request, '/') + 1);
case 'Chevrons': }
$request = str_replace('/', ' <i class="glyphicon glyphicon-chevron-right"></i> ', $request); $request = str_replace('_', ' ', $request);
return $request; switch ($separator) {
case 'Colons': case 'Chevrons':
$request = str_replace('/', ': ', $request); $request = str_replace('/', ' <i class="glyphicon glyphicon-chevron-right"></i> ', $request);
return $request; return $request;
case 'Spaces': case 'Colons':
$request = str_replace('/', ' ', $request); $request = str_replace('/', ': ', $request);
return $request; return $request;
default: case 'Spaces':
$request = str_replace('/', $separator, $request); $request = str_replace('/', ' ', $request);
return $request; return $request;
default:
$request = str_replace('/', $separator, $request);
return $request;
}
return $request;
}
public static function getTheme($theme_folder, $base_url, $local_base, $theme_url)
{
$name = static::pathinfo($theme_folder);
$theme = array();
if (is_file($theme_folder . DS . "config.json")) {
$theme = json_decode(file_get_contents($theme_folder . DS . "config.json"), true);
if (!$theme) {
$theme = array();
} }
return $request; }
$theme['name'] = $name['filename'];
//Default parameters for theme
$theme += [
'css' => [],
'js' => [],
'fonts' => [],
'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',
];
$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['favicon'] = utf8_encode(strtr($theme['favicon'], $substitutions));
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(strtr($css, $substitutions));
} }
public static function get_theme($theme_folder, $base_url, $local_base, $theme_url) { foreach ($theme['fonts'] as $key => $font) {
$name = static::pathinfo($theme_folder); $theme['fonts'][$key] = utf8_encode(strtr($font, $substitutions));
$theme = array();
if (is_file($theme_folder . DIRECTORY_SEPARATOR . "config.json")) {
$theme = json_decode(file_get_contents($theme_folder . DIRECTORY_SEPARATOR . "config.json"), true);
if (!$theme) $theme = array();
}
$theme['name'] = $name['filename'];
//Default parameters for theme
$theme += [
'css' => [],
'js' => [],
'fonts' => [],
'require-jquery' => false,
'bootstrap-js' => false,
'favicon' => '<base_url>resources/img/favicon.png',
'template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl',
'error-template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl',
];
$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['favicon'] = utf8_encode(strtr($theme['favicon'], $substitutions));
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(strtr($css, $substitutions));
}
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(strtr($font, $substitutions));
}
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(strtr($js, $substitutions));
}
return $theme;
} }
public static function google_analytics($analytics, $host) { foreach ($theme['js'] as $key => $js) {
$ga = <<<EOT $theme['js'][$key] = utf8_encode(strtr($js, $substitutions));
}
return $theme;
}
public static function googleAnalytics($analytics, $host)
{
$ga = <<<EOT
<script> <script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (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), (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) 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'); })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
EOT; EOT;
$ga .= "ga('create', '" . $analytics . "', '" . $host . "');"; $ga .= "ga('create', '" . $analytics . "', '" . $host . "');";
$ga .= "ga('send', 'pageview');"; $ga .= "ga('send', 'pageview');";
$ga .= '</script>'; $ga .= '</script>';
return $ga; return $ga;
} }
public static function piwik_analytics($analytics_url, $analytics_id) { public static function piwikAnalytics($analytics_url, $analytics_id)
$pa = <<<EOT {
$pa = <<<EOT
<script type="text/javascript"> <script type="text/javascript">
var _paq = _paq || []; var _paq = _paq || [];
_paq.push(["trackPageView"]); _paq.push(["trackPageView"]);
_paq.push(["enableLinkTracking"]); _paq.push(["enableLinkTracking"]);
(function() { (function() {
EOT; EOT;
$pa .= 'var u=(("https:" == document.location.protocol) ? "https" : "http") + "://' . $analytics_url . '/";'; $pa .= 'var u=(("https:" == document.location.protocol) ? "https" : "http") + "://' . $analytics_url . '/";';
$pa .= '_paq.push(["setTrackerUrl", u+"piwik.php"]);'; $pa .= '_paq.push(["setTrackerUrl", u+"piwik.php"]);';
$pa .= '_paq.push(["setSiteId", ' . $analytics_id . ']);'; $pa .= '_paq.push(["setSiteId", ' . $analytics_id . ']);';
$pa .= <<<EOT $pa .= <<<EOT
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript"; 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); g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
})(); })();
</script> </script>
EOT; EOT;
return $pa; return $pa;
}
public static function pathinfo($path) {
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
if (isset($m[1])) $ret['dir']=$m[1];
if (isset($m[2])) $ret['basename']=$m[2];
if (isset($m[5])) $ret['extension']=$m[5];
if (isset($m[3])) $ret['filename']=$m[3];
return $ret;
}
} }
public static function pathinfo($path)
{
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
if (isset($m[1])) {
$ret['dir']=$m[1];
}
if (isset($m[2])) {
$ret['basename']=$m[2];
}
if (isset($m[5])) {
$ret['extension']=$m[5];
}
if (isset($m[3])) {
$ret['filename']=$m[3];
}
return $ret;
}
}

View File

@ -1,5 +1,5 @@
<?php namespace Todaymade\Daux; <?php namespace Todaymade\Daux;
class Exception extends \Exception { class Exception extends \Exception
{
} }

View File

@ -1,45 +1,49 @@
<?php namespace Todaymade\Daux\Generator; <?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\Entry;
use Todaymade\Daux\MarkdownPage; use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\Tree\Directory; use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Content; use Todaymade\Daux\Tree\Content;
class Generator { class Generator
public function generate($global_config, $destination) { {
public function generate($global_config, $destination)
{
$daux = new Daux(Daux::STATIC_MODE); $daux = new Daux(Daux::STATIC_MODE);
$daux->initialize($global_config); $daux->initialize($global_config);
$this->generate_static($daux, $destination); $params = $daux->getParams();
if (is_null($destination)) {
$destination = $daux->local_base . DS . 'static';
}
Helper::copyAssets($destination, $daux->local_base);
$this->generateRecursive($daux->tree, $destination, $params);
} }
public function generate_static(Daux $daux, $output_dir = NULL) { private function generateRecursive($tree, $output_dir, $params, $base_url = '')
$params = $daux->get_page_params(); {
if (is_null($output_dir)) $output_dir = $daux->local_base . DIRECTORY_SEPARATOR . 'static';
Helper::clean_copy_assets($output_dir, $daux->local_base);
$this->recursive_generate_static($daux->tree, $output_dir, $params);
}
private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') {
$params['base_url'] = $params['base_page'] = $base_url; $params['base_url'] = $params['base_page'] = $base_url;
$new_params = $params; $new_params = $params;
// //
$params['image'] = str_replace('<base_url>', $base_url, $params['image']); $params['image'] = str_replace('<base_url>', $base_url, $params['image']);
if ($base_url !== '') $params['entry_page'] = $tree->first_page; if ($base_url !== '') {
$params['entry_page'] = $tree->getFirstPage();
}
foreach ($tree->value as $key => $node) { foreach ($tree->value as $key => $node) {
if ($node instanceof Directory) { if ($node instanceof Directory) {
$new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key; $new_output_dir = $output_dir . DS . $key;
@mkdir($new_output_dir); @mkdir($new_output_dir);
$this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url); $this->generateRecursive($node, $new_output_dir, $new_params, '../' . $base_url);
} else if ($node instanceof Content) { } elseif ($node instanceof Content) {
$params['request'] = $node->get_url(); $params['request'] = $node->getUrl();
$params['file_uri'] = $node->name; $params['file_uri'] = $node->getName();
$page = MarkdownPage::fromFile($node, $params); $page = MarkdownPage::fromFile($node, $params);
file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content()); file_put_contents($output_dir . DS . $key, $page->getContent());
} else { } else {
copy($node->local_path, $output_dir . DIRECTORY_SEPARATOR . $key); copy($node->getPath(), $output_dir . DS . $key);
} }
} }
} }

View File

@ -1,48 +1,55 @@
<?php namespace Todaymade\Daux\Generator; <?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\DauxHelper; class Helper
{
class Helper { public static function copyAssets($path, $local_base)
{
public static function clean_copy_assets($path, $local_base){
@mkdir($path); @mkdir($path);
static::clean_directory($path); static::rmdir($path);
@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');
@mkdir($path . DIRECTORY_SEPARATOR . 'resources');
static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'resources', $path . DIRECTORY_SEPARATOR . 'resources');
@mkdir($path . DIRECTORY_SEPARATOR . 'js');
static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'js', $path . DIRECTORY_SEPARATOR . 'js');
//added and changed these in order to fetch the theme files and put them in the right place //added and changed these in order to fetch the theme files and put them in the right place
@mkdir($path . DIRECTORY_SEPARATOR . 'templates'); @mkdir($path . DS . 'templates');
@mkdir($path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default'); @mkdir($path . DS . 'templates' . DS . 'default');
@mkdir($path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes'); @mkdir($path . DS . 'templates' . DS . 'default' . DS . 'themes');
static::copy_recursive(
$local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes', static::copyRecursive(
$path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes' $local_base . DS . 'templates' . DS . 'default' . DS . 'themes',
$path . DS . 'templates' . DS . 'default' . DS . 'themes'
); );
} }
// Rmdir private static function rmdir($dir)
private static function clean_directory($dir) { {
$it = new \RecursiveDirectoryIterator($dir); $it = new \RecursiveDirectoryIterator($dir);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST); $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) { foreach ($files as $file) {
if ($file->getFilename() === '.' || $file->getFilename() === '..') continue; if ($file->getFilename() === '.' || $file->getFilename() === '..') {
if ($file->isDir()) rmdir($file->getRealPath()); continue;
else unlink($file->getRealPath()); }
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
} }
} }
private static function copy_recursive($src,$dst) { private static function copyRecursive($src, $dst)
{
$dir = opendir($src); $dir = opendir($src);
@mkdir($dst); @mkdir($dst);
while(false !== ( $file = readdir($dir)) ) { while (false !== ( $file = readdir($dir))) {
if (( $file != '.' ) && ( $file != '..' )) { if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) { if (is_dir($src . '/' . $file)) {
static::copy_recursive($src . '/' . $file,$dst . '/' . $file); static::copyRecursive($src . '/' . $file, $dst . '/' . $file);
} } else {
else { copy($src . '/' . $file, $dst . '/' . $file);
copy($src . '/' . $file,$dst . '/' . $file);
} }
} }
} }

View File

@ -1,49 +1,54 @@
<?php namespace Todaymade\Daux; <?php namespace Todaymade\Daux;
use Todaymade\Daux\Tree\Content;
class MarkdownPage extends SimplePage class MarkdownPage extends SimplePage
{ {
private $filename; private $file;
private $params; private $params;
private $language; private $language;
private $mtime;
private $homepage; private $homepage;
private $breadcrumb_trail; private $breadcrumb_trail;
private static $template; private static $template;
public function __construct() { public function __construct()
{
} }
// For Future Expansion // For Future Expansion
public static function fromFile($file, $params) { public static function fromFile($file, $params)
{
$instance = new self(); $instance = new self();
$instance->initialize_from_file($file, $params); $instance->initialize($file, $params);
return $instance; return $instance;
} }
private function initialize_from_file($file, $params) { private function initialize(Content $file, $params)
$this->title = $file->title; {
$this->filename = $file->name; $this->file = $file;
$this->path = $file->local_path;
$this->mtime = $file->last_modified;
$this->params = $params; $this->params = $params;
$this->title = $file->title;
if ($this->title === 'index') { if ($this->title === 'index') {
$this->homepage = ($this->filename === '_index'); $this->homepage = ($this->file->getName() === '_index');
$minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1; $minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1;
if (count($file->parents) >= $minimum_parent_dir_size) { if (count($file->getParents()) >= $minimum_parent_dir_size) {
$parent = end($file->parents); $parents = $file->getParents();
$this->title = $parent->title; $parent = end($parents);
} else $this->title = $params['title']; $this->title = $parent->getTitle();
} else {
$this->title = $params['title'];
}
} else { } else {
$this->homepage = false; $this->homepage = false;
} }
if ($params['breadcrumbs']) if ($params['breadcrumbs']) {
$this->breadcrumb_trail = $this->build_breadcrumb_trail($file->parents, $params['multilanguage']); $this->breadcrumb_trail = $this->buildBreadcrumbTrail($file->getParents(), $params['multilanguage']);
}
$this->language = ''; $this->language = '';
if ($params['multilanguage'] && !empty($file->parents)) { if ($params['multilanguage'] && count($file->getParents())) {
reset($file->parents); reset($file->getParents());
$language_dir = current($file->parents); $language_dir = current($file->getParents());
$this->language = $language_dir->name; $this->language = $language_dir->name;
} }
if (is_null(static::$template)) { if (is_null(static::$template)) {
@ -52,63 +57,70 @@ class MarkdownPage extends SimplePage
} }
} }
private function build_breadcrumb_trail($parents, $multilanguage) { private function buildBreadcrumbTrail($parents, $multilanguage)
if ($multilanguage && !empty($parents)) $parents = array_splice($parents, 1); {
if ($multilanguage && !empty($parents)) {
$parents = array_splice($parents, 1);
}
$breadcrumb_trail = array(); $breadcrumb_trail = array();
if (!empty($parents)) { if (!empty($parents)) {
foreach ($parents as $node) { foreach ($parents as $node) {
$breadcrumb_trail[$node->title] = $node->get_url(); $breadcrumb_trail[$node->getTitle()] = $node->getUrl();
} }
} }
return $breadcrumb_trail; return $breadcrumb_trail;
} }
public function get_page_content() { public function getContent()
{
if (is_null($this->html)) { if (is_null($this->html)) {
$this->content = file_get_contents($this->path); $this->content = file_get_contents($this->file->getPath());
$this->html = $this->generate_page(); $this->html = $this->generatePage();
} }
return $this->html; return $this->html;
} }
private function generate_page() { private function generatePage()
{
$params = $this->params; $params = $this->params;
$Parsedown = new \Parsedown();
$entry_page = []; $entry_page = [];
//TODO :: debug entry pages if ($params['request'] === $params['index_key']) {
//if ($params['request'] === $params['index_key']) { if ($params['multilanguage']) {
// if ($params['multilanguage']) { foreach ($params['languages'] as $key => $name) {
// foreach ($params['languages'] as $key => $name) { $entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->getUrl());
// $entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->get_url()); }
// } } else {
// } else $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->uri); $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->getUri());
//} else if ($params['file_uri'] === 'index') { }
// $entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page'] . $params['entry_page']->get_url()); } elseif ($params['file_uri'] === 'index') {
//} $entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page'] . $params['entry_page']->getUrl());
}
$page['entry_page'] = (isset($entry_page)) ? $entry_page : null;
$page['entry_page'] = $entry_page;
$page['homepage'] = $this->homepage; $page['homepage'] = $this->homepage;
$page['title'] = $this->title; $page['title'] = $this->file->getTitle();
$page['tagline'] = $params['tagline']; $page['tagline'] = $params['tagline'];
$page['author'] = $params['author']; $page['author'] = $params['author'];
$page['filename'] = $this->filename; $page['filename'] = $this->file->getName();
if ($page['breadcrumbs'] = $params['breadcrumbs']) { if ($page['breadcrumbs'] = $params['breadcrumbs']) {
$page['breadcrumb_trail'] = $this->breadcrumb_trail; $page['breadcrumb_trail'] = $this->breadcrumb_trail;
$page['breadcrumb_separator'] = $params['breadcrumb_separator']; $page['breadcrumb_separator'] = $params['breadcrumb_separator'];
} }
$page['language'] = $this->language; $page['language'] = $this->language;
$page['path'] = $this->path; $page['path'] = $this->file->getPath();
$page['request'] = utf8_encode($params['request']); $page['request'] = utf8_encode($params['request']);
$page['theme'] = $params['theme']; $page['theme'] = $params['theme'];
$page['modified_time'] = filemtime($this->path); $page['modified_time'] = filemtime($this->file->getPath());
$page['markdown'] = $this->content; $page['markdown'] = $this->content;
$page['content'] = $Parsedown->text($this->content);
$page['file_editor'] = $params['file_editor']; $page['file_editor'] = $params['file_editor'];
$page['google_analytics'] = $params['google_analytics']; $page['google_analytics'] = $params['google_analytics'];
$page['piwik_analytics'] = $params['piwik_analytics']; $page['piwik_analytics'] = $params['piwik_analytics'];
$Parsedown = new \Parsedown();
$page['content'] = $Parsedown->text($this->content);
return static::$template->get_content($page, $params); return static::$template->get_content($page, $params);
} }
} }

View File

@ -2,6 +2,6 @@
interface Page interface Page
{ {
function get_page_content(); public function getContent();
function display(); public function display();
} }

View File

@ -12,28 +12,32 @@ class ErrorPage extends SimplePage
private $params; private $params;
private static $template; private static $template;
public function __construct($title, $content, $params) { public function __construct($title, $content, $params)
{
parent::__construct($title, $content); parent::__construct($title, $content);
$this->params = $params; $this->params = $params;
} }
public function display() { public function display()
{
http_response_code(404); http_response_code(404);
parent::display(); parent::display();
} }
public function get_page_content() { public function getContent()
{
include_once($this->params['theme']['error-template']); include_once($this->params['theme']['error-template']);
static::$template = new Template(); static::$template = new Template();
if (is_null($this->html)) { if (is_null($this->html)) {
$this->html = $this->generate_page(); $this->html = $this->generatePage();
} }
return $this->html; return $this->html;
} }
public function generate_page() { private function generatePage()
{
$params = $this->params; $params = $this->params;
$page['title'] = $this->title; $page['title'] = $this->title;
$page['theme'] = $params['theme']; $page['theme'] = $params['theme'];

View File

@ -1,17 +1,21 @@
<?php namespace Todaymade\Daux\Server; <?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Daux; class Helper
use Todaymade\Daux\DauxHelper; {
public static function getRequest()
class Helper {
public static function get_request()
{ {
if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO']; if (isset($_SERVER['PATH_INFO'])) {
else if (isset($_SERVER['REQUEST_URI'])) { $uri = $_SERVER['PATH_INFO'];
} elseif (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI']; $uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME'])); if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
else if (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME']))); $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
if (strncmp($uri, '?/', 2) === 0) $uri = substr($uri, 2); } elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) {
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
if (strncmp($uri, '?/', 2) === 0) {
$uri = substr($uri, 2);
}
$parts = preg_split('#\?#i', $uri, 2); $parts = preg_split('#\?#i', $uri, 2);
$uri = $parts[0]; $uri = $parts[0];
if (isset($parts[1])) { if (isset($parts[1])) {
@ -22,10 +26,13 @@ class Helper {
$_GET = array(); $_GET = array();
} }
$uri = parse_url($uri, PHP_URL_PATH); $uri = parse_url($uri, PHP_URL_PATH);
} else {
return false;
} }
else return false;
$uri = str_replace(array('//', '../'), '/', trim($uri, '/')); $uri = str_replace(array('//', '../'), '/', trim($uri, '/'));
if ($uri == "") $uri = "first_page"; if ($uri == "") {
$uri = "first_page";
}
return $uri; return $uri;
} }
} }

View File

@ -2,6 +2,6 @@
use Todaymade\Daux\Exception; use Todaymade\Daux\Exception;
class NotFoundException extends Exception { class NotFoundException extends Exception
{
} }

View File

@ -4,41 +4,43 @@ use Todaymade\Daux\Daux;
use Todaymade\Daux\Exception; use Todaymade\Daux\Exception;
use Todaymade\Daux\MarkdownPage; use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\SimplePage; use Todaymade\Daux\SimplePage;
use Todaymade\Daux\Tree\Directory;
class Server { class Server
{
private $daux; private $daux;
private $params; private $params;
public static function serve() { public static function serve()
{
$daux = new Daux(Daux::LIVE_MODE); $daux = new Daux(Daux::LIVE_MODE);
try try {
{
$daux->initialize(); $daux->initialize();
$server = new static($daux); $server = new static($daux);
$page = $server->handle($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); $page = $server->handle($_REQUEST);
} } catch (NotFoundException $e) {
catch( NotFoundException $e ) $page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams());
{
$page = new ErrorPage("An error occured", $e->getMessage(), $daux->get_live_page_params());
} }
$page->display(); $page->display();
} }
public function __construct(Daux $daux) { public function __construct(Daux $daux)
{
$this->daux = $daux; $this->daux = $daux;
} }
public function handle($url, $query = []) { public function handle($query = [])
$this->params = $this->daux->get_live_page_params(); {
$this->params = $this->daux->getParams();
$request = Helper::get_request(); $request = Helper::getRequest();
$request = urldecode($request); $request = urldecode($request);
$request_type = isset($query['method']) ? $query['method'] : ''; $request_type = isset($query['method']) ? $query['method'] : '';
if($request == 'first_page') { if ($request == 'first_page') {
$request = $this->daux->tree->first_page->uri; $request = $this->daux->tree->first_page->uri;
} }
switch ($request_type) { switch ($request_type) {
@ -48,24 +50,30 @@ class Server {
} }
$content = isset($query['markdown']) ? $query['markdown'] : ''; $content = isset($query['markdown']) ? $query['markdown'] : '';
return $this->save_file($request, $content); return $this->saveFile($request, $content);
default: default:
return $this->get_page($request); return $this->getPage($request);
} }
} }
private function save_file($request, $content) { private function saveFile($request, $content)
$file = $this->get_file_from_request($request); {
$file = $this->getFile($request);
if ($file === false) throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); if ($file === false) {
throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
}
if (!$file->write($content)) throw new Exception('The file you wish to write to is not writable.'); if (!$file->write($content)) {
throw new Exception('The file you wish to write to is not writable.');
}
return new SimplePage('Success', 'Successfully Edited'); return new SimplePage('Success', 'Successfully Edited');
} }
private function get_file_from_request($request) { private function getFile($request)
{
$tree = $this->daux->tree; $tree = $this->daux->tree;
$request = explode('/', $request); $request = explode('/', $request);
foreach ($request as $node) { foreach ($request as $node) {
@ -90,7 +98,7 @@ class Server {
return false; return false;
} }
return $tree->index_page; return $tree->getIndexPage();
} }
// If the entry we found is not a directory, we're done // If the entry we found is not a directory, we're done
@ -98,21 +106,26 @@ class Server {
return $tree; return $tree;
} }
if ($tree->index_page){ if ($tree->getIndexPage()) {
return $tree->index_page; return $tree->getIndexPage();
} }
return ($get_first_file) ? $tree->first_page : false; return false;
} }
private function get_page($request) { private function getPage($request)
{
$params = $this->params; $params = $this->params;
$file = $this->get_file_from_request($request); $file = $this->getFile($request);
if ($file === false) throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); if ($file === false) {
throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
}
$params['request'] = $request; $params['request'] = $request;
$params['file_uri'] = $file->value; $params['file_uri'] = $file->value;
if ($request !== 'index') $params['entry_page'] = $file->first_page; if ($request !== 'index') {
$params['entry_page'] = $file->first_page;
}
return MarkdownPage::fromFile($file, $params); return MarkdownPage::fromFile($file, $params);
} }
} }

View File

@ -6,29 +6,34 @@ class SimplePage implements Page
protected $content; protected $content;
protected $html = null; protected $html = null;
public function __construct($title, $content) { public function __construct($title, $content)
$this->initialize_page($title, $content); {
$this->initializePage($title, $content);
} }
public function initialize_page($title, $content) { public function display()
$this->title = $title; {
$this->content = $content;
}
public function display() {
header('Content-type: text/html; charset=utf-8'); header('Content-type: text/html; charset=utf-8');
echo $this->get_page_content(); echo $this->getContent();
} }
public function get_page_content() { public function getContent()
{
if (is_null($this->html)) { if (is_null($this->html)) {
$this->html = $this->generate_page(); $this->html = $this->generatePage();
} }
return $this->html; return $this->html;
} }
private function generate_page() { private function initializePage($title, $content)
{
$this->title = $title;
$this->content = $content;
}
private function generatePage()
{
return $this->content; return $this->content;
} }
} }

View File

@ -3,8 +3,10 @@
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
class Builder { class Builder
public static function build($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) { {
public static function build($dir, $ignore, $params, $parents = null)
{
if (!$dh = opendir($dir)) { if (!$dh = opendir($dir)) {
return; return;
} }
@ -14,7 +16,7 @@ class Builder {
$new_parents = $parents; $new_parents = $parents;
if (is_null($new_parents)) { if (is_null($new_parents)) {
$new_parents = array(); $new_parents = array();
} else{ } else {
$new_parents[] = $node; $new_parents[] = $node;
} }
@ -23,9 +25,12 @@ class Builder {
continue; continue;
} }
$path = $dir . DIRECTORY_SEPARATOR . $file; $path = $dir . DS . $file;
if ((is_dir($path) && in_array($file, $ignore['folders'])) || (!is_dir($path) && in_array($file, $ignore['files']))) { if (is_dir($path) && in_array($file, $ignore['folders'])) {
continue;
}
if (!is_dir($path) && in_array($file, $ignore['files'])) {
continue; continue;
} }
@ -33,30 +38,29 @@ class Builder {
$entry = null; $entry = null;
if (is_dir($path)) { if (is_dir($path)) {
$entry = static::build($path, $ignore, $mode, $new_parents); $entry = static::build($path, $ignore, $params, $new_parents);
} else if(in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS)) { } elseif (in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS)) {
$entry = new Content($path, $new_parents); $entry = new Content($path, $new_parents);
if ($mode === Daux::STATIC_MODE) { if ($params['mode'] === Daux::STATIC_MODE) {
$entry->uri .= '.html'; $entry->setUri($entry->getUri() . '.html');
} }
} else { } else {
$entry = new Raw($path, $new_parents); $entry = new Raw($path, $new_parents);
} }
if ($entry instanceof Entry) { if ($entry instanceof Entry) {
$node->value[$entry->uri] = $entry; $node->value[$entry->getUri()] = $entry;
} }
} }
$node->sort(); $node->sort();
$node->first_page = $node->get_first_page(); if (isset($node->value[$params['index_key']])) {
$index_key = ($mode === Daux::LIVE_MODE) ? 'index' : 'index.html'; $node->value[$params['index_key']]->setFirstPage($node->getFirstPage());
if (isset($node->value[$index_key])) { $node->setIndexPage($node->value[$params['index_key']]);
$node->value[$index_key]->first_page = $node->first_page; } else {
$node->index_page = $node->value[$index_key]; $node->setIndexPage(false);
} else $node->index_page = false; }
return $node; return $node;
} }
} }

View File

@ -2,17 +2,19 @@
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
class Content extends Entry { class Content extends Entry
{
public $title; public $title;
public function __construct($path = '', $parents = array()) { public function __construct($path = '', $parents = array())
{
parent::__construct($path, $parents); parent::__construct($path, $parents);
$this->value = $this->uri; $this->value = $this->uri;
} }
protected function getFilename($file) { protected function getFilename($file)
{
$file = DauxHelper::pathinfo($file); $file = DauxHelper::pathinfo($file);
return $file['filename']; return $file['filename'];
} }

View File

@ -1,14 +1,16 @@
<?php namespace Todaymade\Daux\Tree; <?php namespace Todaymade\Daux\Tree;
class Directory extends Entry { class Directory extends Entry
{
public $value = []; public $value = [];
public function sort() { public function sort()
uasort($this->value, array($this, 'compare_directory_entries')); {
uasort($this->value, array($this, 'compareEntries'));
} }
private function compare_directory_entries($a, $b) { private function compareEntries($a, $b)
{
$name_a = explode('_', $a->name); $name_a = explode('_', $a->name);
$name_b = explode('_', $b->name); $name_b = explode('_', $b->name);
if (is_numeric($name_a[0])) { if (is_numeric($name_a[0])) {
@ -18,26 +20,37 @@ class Directory extends Entry {
if (($a >= 0) == ($b >= 0)) { if (($a >= 0) == ($b >= 0)) {
$a = abs($a); $a = abs($a);
$b = abs($b); $b = abs($b);
if ($a == $b) return (strcasecmp($name_a[1], $name_b[1])); if ($a == $b) {
return (strcasecmp($name_a[1], $name_b[1]));
}
return ($a > $b) ? 1 : -1; return ($a > $b) ? 1 : -1;
} }
return ($a >= 0) ? -1 : 1; return ($a >= 0) ? -1 : 1;
} }
$t = $name_b[0]; $t = $name_b[0];
if ($t && $t[0] === '-') return -1; if ($t && $t[0] === '-') {
return -1;
}
return ($a < 0) ? 1 : -1; return ($a < 0) ? 1 : -1;
} else { } else {
if (is_numeric($name_b[0])) { if (is_numeric($name_b[0])) {
$b = intval($name_b[0]); $b = intval($name_b[0]);
if ($b >= 0) return 1; if ($b >= 0) {
return 1;
}
$t = $name_a[0]; $t = $name_a[0];
if ($t && $t[0] === '-') return 1; if ($t && $t[0] === '-') {
return 1;
}
return ($b >= 0) ? 1 : -1; return ($b >= 0) ? 1 : -1;
} }
$p = $name_a[0]; $p = $name_a[0];
$q = $name_b[0]; $q = $name_b[0];
if (($p && $p[0] === '-') == ($q && $q[0] === '-')) return strcasecmp($p, $q); if (($p && $p[0] === '-') == ($q && $q[0] === '-')) {
else return ($p[0] === '-') ? 1 : -1; return strcasecmp($p, $q);
} else {
return ($p[0] === '-') ? 1 : -1;
}
} }
} }
} }

View File

@ -2,85 +2,149 @@
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
abstract class Entry abstract class Entry
{
protected $title;
protected $name;
protected $index_page;
protected $first_page;
protected $uri;
protected $local_path;
protected $last_modified;
protected $parents;
public function __construct($path = '', $parents = array())
{ {
public $title; if (!isset($path) || $path == '' || !file_exists($path)) {
public $name; return;
public $index_page; }
public $first_page; $this->local_path = $path;
public $uri; $this->parents = $parents;
public $local_path; $this->last_modified = filemtime($path);
public $last_modified; $this->name = DauxHelper::pathinfo($path)['filename'];
public $parents; $this->title = $this->getTitleInternal($this->name);
$this->uri = $this->getUrlInternal($this->getFilename($path));
$this->index_page = false;
}
public function getName()
{
return $this->name;
}
function __construct($path = '', $parents = array()) { public function setUri($uri)
if (!isset($path) || $path == '' || !file_exists($path)) return; {
$this->local_path = $path; $this->uri = $uri;
$this->parents = $parents; }
$this->last_modified = filemtime($path);
$this->name = DauxHelper::pathinfo($path)['filename']; public function getUri()
$this->title = $this->get_title_from_filename($this->name); {
$this->uri = $this->get_url_from_filename($this->getFilename($path)); return $this->uri;
$this->index_page = false; }
public function getUrl()
{
$url = '';
foreach ($this->parents as $node) {
$url .= $node->uri . '/';
}
$url .= $this->uri;
return $url;
}
public function getIndexPage()
{
return $this->index_page;
}
public function setIndexPage($index_page)
{
$this->index_page = $index_page;
}
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
} }
public function get_url() { foreach ($this->value as $node) {
$url = ''; if ($node instanceof Content && $node->title != 'index') {
foreach ($this->parents as $node) { $this->first_page = $node;
$url .= $node->uri . '/'; return $node;
} }
$url .= $this->uri;
return $url;
} }
foreach ($this->value as $node) {
if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->first_page = $page;
return $page;
}
}
return false;
}
public function get_first_page() { public function setFirstPage($first_page)
foreach ($this->value as $node) { {
if ($node instanceof Content && $node->title != 'index') $this->first_page = $first_page;
return $node; }
}
foreach ($this->value as $node) { public function getTitle()
if ($node instanceof Directory) { {
$page = $node->get_first_page(); return $this->title;
if ($page) return $page; }
}
} public function getParents()
{
return $this->parents;
}
public function getPath()
{
return $this->local_path;
}
public function write($content)
{
if (!is_writable($this->local_path)) {
return false; return false;
} }
public function write($content) { file_put_contents($this->local_path, $content);
if (!is_writable($this->local_path)) { return true;
return false;
}
file_put_contents($this->local_path, $content);
return true;
}
protected function getFilename($file) {
$parts = explode('/', $file);
return end($parts);
}
protected function get_title_from_filename($filename) {
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]);
else {
$t = $filename[0];
if ($t[0] == '-') $filename[0] = substr($t, 1);
}
$filename = implode(' ', $filename);
return $filename;
}
protected function get_url_from_filename($filename) {
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]);
else {
$t = $filename[0];
if ($t[0] == '-') $filename[0] = substr($t, 1);
}
$filename = implode('_', $filename);
return $filename;
}
} }
protected function getFilename($file)
{
$parts = explode('/', $file);
return end($parts);
}
protected function getTitleInternal($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode(' ', $filename);
return $filename;
}
protected function getUrlInternal($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode('_', $filename);
return $filename;
}
}

View File

@ -1,7 +1,9 @@
<?php namespace Todaymade\Daux\Tree; <?php namespace Todaymade\Daux\Tree;
class Raw extends Entry { class Raw extends Entry
public function __construct($path = '', $parents = array()) { {
public function __construct($path = '', $parents = array())
{
parent::__construct($path, $parents); parent::__construct($path, $parents);
$this->value = $this->uri; $this->value = $this->uri;

View File

@ -12,13 +12,14 @@
private function build_navigation($tree, $path, $current_url, $base_page, $mode) { private function build_navigation($tree, $path, $current_url, $base_page, $mode) {
$nav = ''; $nav = '';
foreach ($tree->value as $node) { foreach ($tree->value as $node) {
$url = $node->uri; $url = $node->getUri();
if ($node instanceof \Todaymade\Daux\Tree\Content) { if ($node instanceof \Todaymade\Daux\Tree\Content) {
if ($node->value === 'index') continue; if ($node->value === 'index') continue;
$nav .= '<li'; $nav .= '<li';
$link = ($path === '') ? $url : $path . '/' . $url; $link = ($path === '') ? $url : $path . '/' . $url;
if ($current_url === $link) $nav .= ' class="active"'; if ($current_url === $link) $nav .= ' class="active"';
$nav .= '><a href="' . $base_page . $link . '">' . $node->title . '</a></li>'; $nav .= '><a href="' . $base_page . $link . '">' . $node->getTitle() . '</a></li>';
} }
if ($node instanceof \Todaymade\Daux\Tree\Directory) { if ($node instanceof \Todaymade\Daux\Tree\Directory) {
$nav .= '<li'; $nav .= '<li';
@ -26,9 +27,9 @@
if (strpos($current_url, $link) === 0) $nav .= ' class="open"'; if (strpos($current_url, $link) === 0) $nav .= ' class="open"';
$nav .= ">"; $nav .= ">";
if ($mode === \TodayMade\Daux\Daux::STATIC_MODE) $link .= "/index.html"; if ($mode === \TodayMade\Daux\Daux::STATIC_MODE) $link .= "/index.html";
if ($node->index_page) $nav .= '<a href="' . $base_page . $link . '" class="folder">' . if ($node->getIndexPage()) $nav .= '<a href="' . $base_page . $link . '" class="folder">' .
$node->title . '</a>'; $node->getTitle() . '</a>';
else $nav .= '<a href="#" class="aj-nav folder">' . $node->title . '</a>'; else $nav .= '<a href="#" class="aj-nav folder">' . $node->getTitle() . '</a>';
$nav .= '<ul class="nav nav-list">'; $nav .= '<ul class="nav nav-list">';
$new_path = ($path === '') ? $url : $path . '/' . $url; $new_path = ($path === '') ? $url : $path . '/' . $url;
$nav .= $this->build_navigation($node, $new_path, $current_url, $base_page, $mode); $nav .= $this->build_navigation($node, $new_path, $current_url, $base_page, $mode);