diff --git a/generate.php b/generate.php index 2b5589e..e7766b5 100644 --- a/generate.php +++ b/generate.php @@ -65,6 +65,8 @@ software, even if advised of the possibility of such damage. require_once("vendor/autoload.php"); +\Todaymade\Daux\Daux::initConstants(); + $global_config = (isset($argv[1]))? $argv[1] : null; $destination = (isset($argv[2]))? $argv[2] : null; diff --git a/index.php b/index.php index 3f52913..6ad4a24 100644 --- a/index.php +++ b/index.php @@ -65,4 +65,6 @@ software, even if advised of the possibility of such damage. require_once("vendor/autoload.php"); +\Todaymade\Daux\Daux::initConstants(); + \Todaymade\Daux\Server\Server::serve($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); diff --git a/libs/Daux.php b/libs/Daux.php index 5e270e4..610c120 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -1,173 +1,183 @@ mode = $mode; - public static $VALID_MARKDOWN_EXTENSIONS; - public $local_base; - public $base_url = ''; - public $host; - private $docs_path; - public $tree; - public $options; - private $mode; + $this->local_base = dirname(__DIR__); + $this->base_url = ''; - public function __construct($mode) { - $this->mode = $mode; - - $this->local_base = dirname(dirname(__FILE__)); - $this->base_url = ''; - - if ($this->mode == Daux::LIVE_MODE) { - $this->host = $_SERVER['HTTP_HOST']; - $this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); - $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 .= '/'; + if ($this->mode == Daux::LIVE_MODE) { + $this->host = $_SERVER['HTTP_HOST']; + $this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); + $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') { - $this->load_global_config($global_config_file); - $this->load_docs_config($config_file); - $this->generate_directory_tree(); + public static function initConstants() + { + define("DS", DIRECTORY_SEPARATOR); + } + + 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) { - if (is_null($global_config_file)) $global_config_file = $this->local_base . DIRECTORY_SEPARATOR . 'global.json'; - if (!file_exists($global_config_file)) { - 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']; + $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'); } - private function load_docs_config($config_file) { - $config_file = $this->docs_path . DIRECTORY_SEPARATOR . $config_file; - 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'); + if (!isset($global_config['docs_directory'])) { + throw new Exception('The Global Config file does not have the docs directory set.'); } - private function generate_directory_tree() { - $this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->mode); - if (!empty($this->options['languages'])) { - foreach ($this->options['languages'] as $key => $node) { - $this->tree->value[$key]->title = $node; - } - } + $this->docs_path = $global_config['docs_directory']; + if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DS . $this->docs_path)) { + throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path); } - public function get_base_params() { - $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'], + if (!isset($global_config['valid_markdown_extensions'])) { + static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown'); + } else { + static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions']; + } + } - //Features - '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']) : '', - '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'], + private function loadConfigOverrides($config_file) + { + $config_file = $this->docs_path . DS . $config_file; + 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 . DS . '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']); + } 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 - 'mode' => $this->mode, - 'local_base' => $this->local_base, - 'docs_path' => $this->docs_path, - 'tree' => $this->tree, - 'index' => ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page, - 'template' => $this->options['template'], - ); + //Paths and tree + 'mode' => $this->mode, + 'local_base' => $this->local_base, + 'docs_path' => $this->docs_path, + '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) { - $params['entry_page'][$key] = $this->tree->value[$key]->first_page; + $params['entry_page'][$key] = $this->tree->value[$key]->getFirstPage(); } } else { - $params['entry_page'] = $this->tree->first_page; + $params['entry_page'] = $this->tree->getFirstPage(); } - - return $params; } - //TODO :: move to generator - 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(); - + if ($this->mode == self::LIVE_MODE) { $params['index_key'] = 'index'; $params['host'] = $this->host; $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('', $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'], - $params['base_url'], - $this->local_base, - $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/' - ); - - return $params; + if ($params['image'] !== '') { + $params['image'] = str_replace('', $params['base_url'], $params['image']); + } + } else { + $params['index_key'] = 'index.html'; + $params['base_page'] = $params['base_url'] = ''; } + + $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; } +} diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 82753c4..27432ec 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -1,111 +1,130 @@ ', $request); - return $request; - case 'Colons': - $request = str_replace('/', ': ', $request); - return $request; - case 'Spaces': - $request = str_replace('/', ' ', $request); - return $request; - default: - $request = str_replace('/', $separator, $request); - return $request; + public static function getBreadcrumbFromRequest($request, $separator = 'Chevrons', $multilanguage = false) + { + if ($multilanguage) { + $request = substr($request, strpos($request, '/') + 1); + } + $request = str_replace('_', ' ', $request); + switch ($separator) { + case 'Chevrons': + $request = str_replace('/', ' ', $request); + return $request; + case 'Colons': + $request = str_replace('/', ': ', $request); + return $request; + case 'Spaces': + $request = str_replace('/', ' ', $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' => '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, '' => $base_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) { - $name = static::pathinfo($theme_folder); - - $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' => '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, '' => $base_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; + foreach ($theme['fonts'] as $key => $font) { + $theme['fonts'][$key] = utf8_encode(strtr($font, $substitutions)); } - public static function google_analytics($analytics, $host) { - $ga = << $js) { + $theme['js'][$key] = utf8_encode(strtr($js, $substitutions)); + } + + return $theme; + } + + public static function googleAnalytics($analytics, $host) + { + $ga = << (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 .= ''; - return $ga; - } + $ga .= "ga('create', '" . $analytics . "', '" . $host . "');"; + $ga .= "ga('send', 'pageview');"; + $ga .= ''; + return $ga; + } - public static function piwik_analytics($analytics_url, $analytics_id) { - $pa = << 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; - 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; - } + 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; + } +} diff --git a/libs/Exception.php b/libs/Exception.php index 9ccaa45..3e8f2d5 100644 --- a/libs/Exception.php +++ b/libs/Exception.php @@ -1,5 +1,5 @@ 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) { - $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 = '') { + private function generateRecursive($tree, $output_dir, $params, $base_url = '') + { $params['base_url'] = $params['base_page'] = $base_url; $new_params = $params; // $params['image'] = str_replace('', $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) { if ($node instanceof Directory) { - $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key; + $new_output_dir = $output_dir . DS . $key; @mkdir($new_output_dir); - $this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url); - } else if ($node instanceof Content) { - $params['request'] = $node->get_url(); - $params['file_uri'] = $node->name; + $this->generateRecursive($node, $new_output_dir, $new_params, '../' . $base_url); + } elseif ($node instanceof Content) { + $params['request'] = $node->getUrl(); + $params['file_uri'] = $node->getName(); $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 { - copy($node->local_path, $output_dir . DIRECTORY_SEPARATOR . $key); + copy($node->getPath(), $output_dir . DS . $key); } } } diff --git a/libs/Generator/Helper.php b/libs/Generator/Helper.php index ef1ae33..e56b941 100644 --- a/libs/Generator/Helper.php +++ b/libs/Generator/Helper.php @@ -1,48 +1,55 @@ getFilename() === '.' || $file->getFilename() === '..') continue; - if ($file->isDir()) rmdir($file->getRealPath()); - else unlink($file->getRealPath()); + foreach ($files as $file) { + if ($file->getFilename() === '.' || $file->getFilename() === '..') { + continue; + } + 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); @mkdir($dst); - while(false !== ( $file = readdir($dir)) ) { + while (false !== ( $file = readdir($dir))) { if (( $file != '.' ) && ( $file != '..' )) { - if ( is_dir($src . '/' . $file) ) { - static::copy_recursive($src . '/' . $file,$dst . '/' . $file); - } - else { - copy($src . '/' . $file,$dst . '/' . $file); + if (is_dir($src . '/' . $file)) { + static::copyRecursive($src . '/' . $file, $dst . '/' . $file); + } else { + copy($src . '/' . $file, $dst . '/' . $file); } } } diff --git a/libs/MarkdownPage.php b/libs/MarkdownPage.php index a1fe68e..45f7c4f 100644 --- a/libs/MarkdownPage.php +++ b/libs/MarkdownPage.php @@ -1,49 +1,54 @@ initialize_from_file($file, $params); + $instance->initialize($file, $params); return $instance; } - private function initialize_from_file($file, $params) { - $this->title = $file->title; - $this->filename = $file->name; - $this->path = $file->local_path; - $this->mtime = $file->last_modified; + private function initialize(Content $file, $params) + { + $this->file = $file; $this->params = $params; + $this->title = $file->title; if ($this->title === 'index') { - $this->homepage = ($this->filename === '_index'); + $this->homepage = ($this->file->getName() === '_index'); $minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1; - if (count($file->parents) >= $minimum_parent_dir_size) { - $parent = end($file->parents); - $this->title = $parent->title; - } else $this->title = $params['title']; + if (count($file->getParents()) >= $minimum_parent_dir_size) { + $parents = $file->getParents(); + $parent = end($parents); + $this->title = $parent->getTitle(); + } else { + $this->title = $params['title']; + } } else { $this->homepage = false; } - if ($params['breadcrumbs']) - $this->breadcrumb_trail = $this->build_breadcrumb_trail($file->parents, $params['multilanguage']); + if ($params['breadcrumbs']) { + $this->breadcrumb_trail = $this->buildBreadcrumbTrail($file->getParents(), $params['multilanguage']); + } $this->language = ''; - if ($params['multilanguage'] && !empty($file->parents)) { - reset($file->parents); - $language_dir = current($file->parents); + if ($params['multilanguage'] && count($file->getParents())) { + reset($file->getParents()); + $language_dir = current($file->getParents()); $this->language = $language_dir->name; } if (is_null(static::$template)) { @@ -52,63 +57,70 @@ class MarkdownPage extends SimplePage } } - private function build_breadcrumb_trail($parents, $multilanguage) { - if ($multilanguage && !empty($parents)) $parents = array_splice($parents, 1); + private function buildBreadcrumbTrail($parents, $multilanguage) + { + if ($multilanguage && !empty($parents)) { + $parents = array_splice($parents, 1); + } $breadcrumb_trail = array(); if (!empty($parents)) { foreach ($parents as $node) { - $breadcrumb_trail[$node->title] = $node->get_url(); + $breadcrumb_trail[$node->getTitle()] = $node->getUrl(); } } return $breadcrumb_trail; } - public function get_page_content() { + public function getContent() + { if (is_null($this->html)) { - $this->content = file_get_contents($this->path); - $this->html = $this->generate_page(); + $this->content = file_get_contents($this->file->getPath()); + $this->html = $this->generatePage(); } return $this->html; } - private function generate_page() { + private function generatePage() + { $params = $this->params; - $Parsedown = new \Parsedown(); + $entry_page = []; - //TODO :: debug entry pages - //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]->get_url()); - // } - // } else $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->uri); - //} else if ($params['file_uri'] === 'index') { - // $entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page'] . $params['entry_page']->get_url()); - //} - - $page['entry_page'] = (isset($entry_page)) ? $entry_page : null; + 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()); + } + } else { + $entry_page['View Documentation'] = utf8_encode($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()); + } + $page['entry_page'] = $entry_page; $page['homepage'] = $this->homepage; - $page['title'] = $this->title; + $page['title'] = $this->file->getTitle(); $page['tagline'] = $params['tagline']; $page['author'] = $params['author']; - $page['filename'] = $this->filename; + $page['filename'] = $this->file->getName(); if ($page['breadcrumbs'] = $params['breadcrumbs']) { $page['breadcrumb_trail'] = $this->breadcrumb_trail; $page['breadcrumb_separator'] = $params['breadcrumb_separator']; } $page['language'] = $this->language; - $page['path'] = $this->path; + $page['path'] = $this->file->getPath(); $page['request'] = utf8_encode($params['request']); $page['theme'] = $params['theme']; - $page['modified_time'] = filemtime($this->path); + $page['modified_time'] = filemtime($this->file->getPath()); $page['markdown'] = $this->content; - $page['content'] = $Parsedown->text($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); } } diff --git a/libs/Page.php b/libs/Page.php index d17b952..1668b34 100644 --- a/libs/Page.php +++ b/libs/Page.php @@ -2,6 +2,6 @@ interface Page { - function get_page_content(); - function display(); + public function getContent(); + public function display(); } diff --git a/libs/Server/ErrorPage.php b/libs/Server/ErrorPage.php index 3918f81..9546b65 100644 --- a/libs/Server/ErrorPage.php +++ b/libs/Server/ErrorPage.php @@ -12,28 +12,32 @@ class ErrorPage extends SimplePage private $params; private static $template; - public function __construct($title, $content, $params) { + public function __construct($title, $content, $params) + { parent::__construct($title, $content); $this->params = $params; } - public function display() { + public function display() + { http_response_code(404); parent::display(); } - public function get_page_content() { + public function getContent() + { include_once($this->params['theme']['error-template']); static::$template = new Template(); if (is_null($this->html)) { - $this->html = $this->generate_page(); + $this->html = $this->generatePage(); } return $this->html; } - public function generate_page() { + private function generatePage() + { $params = $this->params; $page['title'] = $this->title; $page['theme'] = $params['theme']; diff --git a/libs/Server/Helper.php b/libs/Server/Helper.php index 029ec6f..711689d 100644 --- a/libs/Server/Helper.php +++ b/libs/Server/Helper.php @@ -1,17 +1,21 @@ initialize(); $server = new static($daux); - $page = $server->handle($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); - } - catch( NotFoundException $e ) - { - $page = new ErrorPage("An error occured", $e->getMessage(), $daux->get_live_page_params()); + $page = $server->handle($_REQUEST); + } catch (NotFoundException $e) { + $page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams()); } $page->display(); } - public function __construct(Daux $daux) { + public function __construct(Daux $daux) + { $this->daux = $daux; } - public function handle($url, $query = []) { - $this->params = $this->daux->get_live_page_params(); + public function handle($query = []) + { + $this->params = $this->daux->getParams(); - $request = Helper::get_request(); + $request = Helper::getRequest(); $request = urldecode($request); $request_type = isset($query['method']) ? $query['method'] : ''; - if($request == 'first_page') { + if ($request == 'first_page') { $request = $this->daux->tree->first_page->uri; } switch ($request_type) { @@ -48,24 +50,30 @@ class Server { } $content = isset($query['markdown']) ? $query['markdown'] : ''; - return $this->save_file($request, $content); + return $this->saveFile($request, $content); default: - return $this->get_page($request); + return $this->getPage($request); } } - private function save_file($request, $content) { - $file = $this->get_file_from_request($request); + private function saveFile($request, $content) + { + $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'); } - private function get_file_from_request($request) { + private function getFile($request) + { $tree = $this->daux->tree; $request = explode('/', $request); foreach ($request as $node) { @@ -90,7 +98,7 @@ class Server { return false; } - return $tree->index_page; + return $tree->getIndexPage(); } // If the entry we found is not a directory, we're done @@ -98,21 +106,26 @@ class Server { return $tree; } - if ($tree->index_page){ - return $tree->index_page; + if ($tree->getIndexPage()) { + 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; - $file = $this->get_file_from_request($request); - if ($file === false) throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); + $file = $this->getFile($request); + if ($file === false) { + throw new NotFoundException('The Page you requested is yet to be made. Try again later.'); + } $params['request'] = $request; $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); } } diff --git a/libs/SimplePage.php b/libs/SimplePage.php index 2d5f788..6f41488 100644 --- a/libs/SimplePage.php +++ b/libs/SimplePage.php @@ -6,29 +6,34 @@ class SimplePage implements Page protected $content; protected $html = null; - public function __construct($title, $content) { - $this->initialize_page($title, $content); + public function __construct($title, $content) + { + $this->initializePage($title, $content); } - public function initialize_page($title, $content) { - $this->title = $title; - $this->content = $content; - } - - public function display() { + public function display() + { 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)) { - $this->html = $this->generate_page(); + $this->html = $this->generatePage(); } return $this->html; } - private function generate_page() { + private function initializePage($title, $content) + { + $this->title = $title; + $this->content = $content; + } + + private function generatePage() + { return $this->content; } } diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php index 88d69db..c5258c4 100644 --- a/libs/Tree/Builder.php +++ b/libs/Tree/Builder.php @@ -3,8 +3,10 @@ use Todaymade\Daux\Daux; use Todaymade\Daux\DauxHelper; -class Builder { - public static function build($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) { +class Builder +{ + public static function build($dir, $ignore, $params, $parents = null) + { if (!$dh = opendir($dir)) { return; } @@ -14,7 +16,7 @@ class Builder { $new_parents = $parents; if (is_null($new_parents)) { $new_parents = array(); - } else{ + } else { $new_parents[] = $node; } @@ -23,9 +25,12 @@ class Builder { 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; } @@ -33,30 +38,29 @@ class Builder { $entry = null; if (is_dir($path)) { - $entry = static::build($path, $ignore, $mode, $new_parents); - } else if(in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS)) { + $entry = static::build($path, $ignore, $params, $new_parents); + } elseif (in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS)) { $entry = new Content($path, $new_parents); - if ($mode === Daux::STATIC_MODE) { - $entry->uri .= '.html'; + if ($params['mode'] === Daux::STATIC_MODE) { + $entry->setUri($entry->getUri() . '.html'); } } else { $entry = new Raw($path, $new_parents); } if ($entry instanceof Entry) { - $node->value[$entry->uri] = $entry; + $node->value[$entry->getUri()] = $entry; } } $node->sort(); - $node->first_page = $node->get_first_page(); - $index_key = ($mode === Daux::LIVE_MODE) ? 'index' : 'index.html'; - if (isset($node->value[$index_key])) { - $node->value[$index_key]->first_page = $node->first_page; - $node->index_page = $node->value[$index_key]; - } else $node->index_page = false; + if (isset($node->value[$params['index_key']])) { + $node->value[$params['index_key']]->setFirstPage($node->getFirstPage()); + $node->setIndexPage($node->value[$params['index_key']]); + } else { + $node->setIndexPage(false); + } return $node; - } } diff --git a/libs/Tree/Content.php b/libs/Tree/Content.php index 36d8501..7941e3b 100644 --- a/libs/Tree/Content.php +++ b/libs/Tree/Content.php @@ -2,17 +2,19 @@ use Todaymade\Daux\DauxHelper; -class Content extends Entry { - +class Content extends Entry +{ public $title; - public function __construct($path = '', $parents = array()) { + public function __construct($path = '', $parents = array()) + { parent::__construct($path, $parents); $this->value = $this->uri; } - protected function getFilename($file) { + protected function getFilename($file) + { $file = DauxHelper::pathinfo($file); return $file['filename']; } diff --git a/libs/Tree/Directory.php b/libs/Tree/Directory.php index 95873b7..9d128e3 100644 --- a/libs/Tree/Directory.php +++ b/libs/Tree/Directory.php @@ -1,14 +1,16 @@ value, array($this, 'compare_directory_entries')); + public function sort() + { + uasort($this->value, array($this, 'compareEntries')); } - private function compare_directory_entries($a, $b) { + private function compareEntries($a, $b) + { $name_a = explode('_', $a->name); $name_b = explode('_', $b->name); if (is_numeric($name_a[0])) { @@ -18,26 +20,37 @@ class Directory extends Entry { if (($a >= 0) == ($b >= 0)) { $a = abs($a); $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 >= 0) ? -1 : 1; } $t = $name_b[0]; - if ($t && $t[0] === '-') return -1; + if ($t && $t[0] === '-') { + return -1; + } return ($a < 0) ? 1 : -1; } else { if (is_numeric($name_b[0])) { $b = intval($name_b[0]); - if ($b >= 0) return 1; + if ($b >= 0) { + return 1; + } $t = $name_a[0]; - if ($t && $t[0] === '-') return 1; + if ($t && $t[0] === '-') { + return 1; + } return ($b >= 0) ? 1 : -1; } $p = $name_a[0]; $q = $name_b[0]; - if (($p && $p[0] === '-') == ($q && $q[0] === '-')) return strcasecmp($p, $q); - else return ($p[0] === '-') ? 1 : -1; + if (($p && $p[0] === '-') == ($q && $q[0] === '-')) { + return strcasecmp($p, $q); + } else { + return ($p[0] === '-') ? 1 : -1; + } } } } diff --git a/libs/Tree/Entry.php b/libs/Tree/Entry.php index e3208ff..aca1722 100644 --- a/libs/Tree/Entry.php +++ b/libs/Tree/Entry.php @@ -2,85 +2,149 @@ 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; - public $name; - public $index_page; - public $first_page; - public $uri; - public $local_path; - public $last_modified; - public $parents; + if (!isset($path) || $path == '' || !file_exists($path)) { + return; + } + $this->local_path = $path; + $this->parents = $parents; + $this->last_modified = filemtime($path); + $this->name = DauxHelper::pathinfo($path)['filename']; + $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()) { - if (!isset($path) || $path == '' || !file_exists($path)) return; - $this->local_path = $path; - $this->parents = $parents; - $this->last_modified = filemtime($path); - $this->name = DauxHelper::pathinfo($path)['filename']; - $this->title = $this->get_title_from_filename($this->name); - $this->uri = $this->get_url_from_filename($this->getFilename($path)); - $this->index_page = false; + public function setUri($uri) + { + $this->uri = $uri; + } + + public function getUri() + { + return $this->uri; + } + + 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() { - $url = ''; - foreach ($this->parents as $node) { - $url .= $node->uri . '/'; + foreach ($this->value as $node) { + if ($node instanceof Content && $node->title != 'index') { + $this->first_page = $node; + 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() { - foreach ($this->value as $node) { - if ($node instanceof Content && $node->title != 'index') - return $node; - } - foreach ($this->value as $node) { - if ($node instanceof Directory) { - $page = $node->get_first_page(); - if ($page) return $page; - } - } + public function setFirstPage($first_page) + { + $this->first_page = $first_page; + } + + public function getTitle() + { + return $this->title; + } + + 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; } - public function write($content) { - if (!is_writable($this->local_path)) { - 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; - } - + file_put_contents($this->local_path, $content); + return true; } + + 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; + } +} diff --git a/libs/Tree/Raw.php b/libs/Tree/Raw.php index 2a5e471..50e1016 100644 --- a/libs/Tree/Raw.php +++ b/libs/Tree/Raw.php @@ -1,7 +1,9 @@ value = $this->uri; diff --git a/templates/default/default.tpl b/templates/default/default.php similarity index 98% rename from templates/default/default.tpl rename to templates/default/default.php index 51aa28f..b78cc38 100644 --- a/templates/default/default.tpl +++ b/templates/default/default.php @@ -12,13 +12,14 @@ private function build_navigation($tree, $path, $current_url, $base_page, $mode) { $nav = ''; foreach ($tree->value as $node) { - $url = $node->uri; + $url = $node->getUri(); if ($node instanceof \Todaymade\Daux\Tree\Content) { + if ($node->value === 'index') continue; $nav .= '' . $node->title . ''; + $nav .= '>' . $node->getTitle() . ''; } if ($node instanceof \Todaymade\Daux\Tree\Directory) { $nav .= 'index_page) $nav .= '' . - $node->title . ''; - else $nav .= '' . $node->title . ''; + if ($node->getIndexPage()) $nav .= '' . + $node->getTitle() . ''; + else $nav .= '' . $node->getTitle() . ''; $nav .= '